You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef KICAD_SCHEMATIC_H
  20. #define KICAD_SCHEMATIC_H
  21. #include <eda_item.h>
  22. #include <sch_sheet_path.h>
  23. #include <schematic_settings.h>
  24. class BUS_ALIAS;
  25. class CONNECTION_GRAPH;
  26. class EDA_BASE_FRAME;
  27. class ERC_SETTINGS;
  28. class PROJECT;
  29. class SCH_SCREEN;
  30. class SCH_SHEET;
  31. class SCH_SHEET_LIST;
  32. class SCH_GLOBALLABEL;
  33. class SCHEMATIC_IFACE
  34. {
  35. public:
  36. SCHEMATIC_IFACE() {};
  37. virtual ~SCHEMATIC_IFACE() {};
  38. virtual CONNECTION_GRAPH* ConnectionGraph() const = 0;
  39. virtual SCH_SHEET_LIST GetSheets() const = 0;
  40. virtual void SetCurrentSheet( const SCH_SHEET_PATH& aPath ) = 0;
  41. virtual SCH_SHEET_PATH& CurrentSheet() const = 0;
  42. virtual wxString GetFileName() const = 0;
  43. virtual PROJECT& Prj() const = 0;
  44. };
  45. /**
  46. * Holds all the data relating to one schematic.
  47. *
  48. * A schematic may consist of one or more sheets (and one root sheet)
  49. * Right now, Eeschema can have only one schematic open at a time, but this could change.
  50. * Please keep this possibility in mind when adding to this object.
  51. */
  52. class SCHEMATIC : public SCHEMATIC_IFACE, public EDA_ITEM
  53. {
  54. public:
  55. SCHEMATIC( PROJECT* aPrj );
  56. virtual ~SCHEMATIC();
  57. virtual wxString GetClass() const override
  58. {
  59. return wxT( "SCHEMATIC" );
  60. }
  61. /// Initialize this schematic to a blank one, unloading anything existing.
  62. void Reset();
  63. /// Return a reference to the project this schematic is part of
  64. PROJECT& Prj() const override { return *m_project; }
  65. void SetProject( PROJECT* aPrj );
  66. const std::map<wxString, wxString>* GetProperties() { return &m_properties; }
  67. /**
  68. * Builds and returns an updated schematic hierarchy
  69. * TODO: can this be cached?
  70. * @return a SCH_SHEET_LIST containing the schematic hierarchy
  71. */
  72. SCH_SHEET_LIST GetSheets() const override
  73. {
  74. return SCH_SHEET_LIST( m_rootSheet );
  75. }
  76. SCH_SHEET& Root() const
  77. {
  78. return *m_rootSheet;
  79. }
  80. /**
  81. * Initialize the schematic with a new root sheet.
  82. *
  83. * This is typically done by calling a file loader that returns the new root sheet
  84. * As a side-effect, takes care of some post-load initialization.
  85. *
  86. * @param aRootSheet is the new root sheet for this schematic.
  87. */
  88. void SetRoot( SCH_SHEET* aRootSheet );
  89. /// A simple test if the schematic is loaded, not a complete one
  90. bool IsValid() const
  91. {
  92. return m_rootSheet != nullptr;
  93. }
  94. /// Helper to retrieve the screen of the root sheet
  95. SCH_SCREEN* RootScreen() const;
  96. bool ResolveTextVar( wxString* token, int aDepth ) const;
  97. /// Helper to retrieve the filename from the root sheet screen
  98. wxString GetFileName() const override;
  99. SCH_SHEET_PATH& CurrentSheet() const override
  100. {
  101. return *m_currentSheet;
  102. }
  103. void SetCurrentSheet( const SCH_SHEET_PATH& aPath ) override
  104. {
  105. *m_currentSheet = aPath;
  106. }
  107. CONNECTION_GRAPH* ConnectionGraph() const override
  108. {
  109. return m_connectionGraph;
  110. }
  111. SCHEMATIC_SETTINGS& Settings() const;
  112. ERC_SETTINGS& ErcSettings() const;
  113. std::vector<SCH_MARKER*> ResolveERCExclusions();
  114. /**
  115. * Return a pointer to a bus alias object for the given label, or null if one
  116. * doesn't exist.
  117. */
  118. std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aLabel ) const;
  119. /**
  120. * Return the set of netname candidates for netclass assignment. The list will include both
  121. * composite names (buses) and atomic net names. Names are fetched from available labels,
  122. * power pins, etc.
  123. */
  124. std::set<wxString> GetNetClassAssignmentCandidates();
  125. /**
  126. * Resolves text vars that refer to other items.
  127. * Note that the actual resolve is delegated to the symbol/sheet in question. This routine
  128. * just does the look-up and delegation.
  129. */
  130. bool ResolveCrossReference( wxString* token, int aDepth ) const;
  131. std::map<wxString, std::set<int>>& GetPageRefsMap() { return m_labelToPageRefsMap; }
  132. std::map<int, wxString> GetVirtualPageToSheetNamesMap() const;
  133. std::map<int, wxString> GetVirtualPageToSheetPagesMap() const;
  134. wxString ConvertRefsToKIIDs( const wxString& aSource ) const;
  135. wxString ConvertKIIDsToRefs( const wxString& aSource ) const;
  136. /**
  137. * Return the full schematic flattened hierarchical sheet list.
  138. */
  139. SCH_SHEET_LIST& GetFullHierarchy() const;
  140. /**
  141. * Update the symbol value and footprint instance data for legacy designs.
  142. *
  143. * Prior to schematic file format version 20200828 and legacy file format version, only
  144. * symbol reference field and unit were saved in the instance data. The value and footprint
  145. * fields must be carried forward from the original symbol to prevent data loss.
  146. */
  147. void SetLegacySymbolInstanceData();
  148. /**
  149. * @return a filename that can be used in plot and print functions for the current screen
  150. * and sheet path. This filename is unique and must be used instead of the screen filename
  151. * when one must create files for each sheet in the hierarchy.
  152. * Name is &ltroot sheet filename&gt-&ltsheet path&gt and has no extension.
  153. * However if filename is too long name is &ltsheet filename&gt-&ltsheet number&gt
  154. */
  155. wxString GetUniqueFilenameForCurrentSheet();
  156. /**
  157. * Set the m_ScreenNumber and m_NumberOfScreens members for screens.
  158. *
  159. * @note This must be called after deleting or adding a sheet and when entering a sheet.
  160. */
  161. void SetSheetNumberAndCount();
  162. /**
  163. * Update the schematic's page reference map for all global labels, and refresh the labels
  164. * so that they are redrawn with up-to-date references.
  165. */
  166. void RecomputeIntersheetRefs( bool autoplaceUninitialized,
  167. const std::function<void( SCH_GLOBALLABEL* )>& aItemCallback );
  168. #if defined(DEBUG)
  169. void Show( int nestLevel, std::ostream& os ) const override {}
  170. #endif
  171. private:
  172. friend class SCH_EDIT_FRAME;
  173. PROJECT* m_project;
  174. /// The top-level sheet in this schematic hierarchy (or potentially the only one)
  175. SCH_SHEET* m_rootSheet;
  176. /**
  177. * The sheet path of the sheet currently being edited or displayed.
  178. * Note that this was moved here from SCH_EDIT_FRAME because currently many places in the code
  179. * want to know the current sheet. Potentially this can be moved back to the UI code once
  180. * the only places that want to know it are UI-related
  181. */
  182. SCH_SHEET_PATH* m_currentSheet;
  183. /// Holds and calculates connectivity information of this schematic
  184. CONNECTION_GRAPH* m_connectionGraph;
  185. /**
  186. * Holds a map of labels to the page sequence (virtual page number) that they appear on. It is
  187. * used for updating global label intersheet references.
  188. */
  189. std::map<wxString, std::set<int>> m_labelToPageRefsMap;
  190. /**
  191. * Properties for text variable substitution (and perhaps other uses in future).
  192. */
  193. std::map<wxString, wxString> m_properties;
  194. };
  195. #endif