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.

217 lines
6.6 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 SCHEMATIC_IFACE
  33. {
  34. public:
  35. SCHEMATIC_IFACE() {};
  36. virtual ~SCHEMATIC_IFACE() {};
  37. virtual CONNECTION_GRAPH* ConnectionGraph() const = 0;
  38. virtual SCH_SHEET_LIST GetSheets() const = 0;
  39. virtual void SetCurrentSheet( const SCH_SHEET_PATH& aPath ) = 0;
  40. virtual SCH_SHEET_PATH& CurrentSheet() const = 0;
  41. virtual wxString GetFileName() const = 0;
  42. virtual PROJECT& Prj() const = 0;
  43. };
  44. /**
  45. * Holds all the data relating to one schematic.
  46. *
  47. * A schematic may consist of one or more sheets (and one root sheet)
  48. * Right now, Eeschema can have only one schematic open at a time, but this could change.
  49. * Please keep this possibility in mind when adding to this object.
  50. */
  51. class SCHEMATIC : public SCHEMATIC_IFACE, public EDA_ITEM
  52. {
  53. public:
  54. SCHEMATIC( PROJECT* aPrj );
  55. virtual ~SCHEMATIC();
  56. virtual wxString GetClass() const override
  57. {
  58. return wxT( "SCHEMATIC" );
  59. }
  60. /// Initialize this schematic to a blank one, unloading anything existing.
  61. void Reset();
  62. /// Return a reference to the project this schematic is part of
  63. PROJECT& Prj() const override { return *m_project; }
  64. void SetProject( PROJECT* aPrj );
  65. const std::map<wxString, wxString>* GetProperties() { return &m_properties; }
  66. /**
  67. * Builds and returns an updated schematic hierarchy
  68. * TODO: can this be cached?
  69. * @return a SCH_SHEET_LIST containing the schematic hierarchy
  70. */
  71. SCH_SHEET_LIST GetSheets() const override
  72. {
  73. return SCH_SHEET_LIST( m_rootSheet );
  74. }
  75. SCH_SHEET& Root() const
  76. {
  77. return *m_rootSheet;
  78. }
  79. /**
  80. * Initialize the schematic with a new root sheet.
  81. *
  82. * This is typically done by calling a file loader that returns the new root sheet
  83. * As a side-effect, takes care of some post-load initialization.
  84. *
  85. * @param aRootSheet is the new root sheet for this schematic.
  86. */
  87. void SetRoot( SCH_SHEET* aRootSheet );
  88. /// A simple test if the schematic is loaded, not a complete one
  89. bool IsValid() const
  90. {
  91. return m_rootSheet != nullptr;
  92. }
  93. /// Helper to retrieve the screen of the root sheet
  94. SCH_SCREEN* RootScreen() const;
  95. bool ResolveTextVar( wxString* token, int aDepth ) const;
  96. /// Helper to retrieve the filename from the root sheet screen
  97. wxString GetFileName() const override;
  98. SCH_SHEET_PATH& CurrentSheet() const override
  99. {
  100. return *m_currentSheet;
  101. }
  102. void SetCurrentSheet( const SCH_SHEET_PATH& aPath ) override
  103. {
  104. *m_currentSheet = aPath;
  105. }
  106. CONNECTION_GRAPH* ConnectionGraph() const override
  107. {
  108. return m_connectionGraph;
  109. }
  110. SCHEMATIC_SETTINGS& Settings() const;
  111. ERC_SETTINGS& ErcSettings() const;
  112. std::vector<SCH_MARKER*> ResolveERCExclusions();
  113. /**
  114. * Return a pointer to a bus alias object for the given label, or null if one
  115. * doesn't exist.
  116. */
  117. std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aLabel ) const;
  118. /**
  119. * Return the set of netname candidates for netclass assignment. The list will include both
  120. * composite names (buses) and atomic net names. Names are fetched from available labels,
  121. * power pins, etc.
  122. */
  123. std::set<wxString> GetNetClassAssignmentCandidates();
  124. /**
  125. * Resolves text vars that refer to other items.
  126. * Note that the actual resolve is delegated to the symbol/sheet in question. This routine
  127. * just does the look-up and delegation.
  128. */
  129. bool ResolveCrossReference( wxString* token, int aDepth ) const;
  130. std::map<wxString, std::set<int>>& GetPageRefsMap() { return m_labelToPageRefsMap; }
  131. std::map<int, wxString> GetVirtualPageToSheetNamesMap() const;
  132. std::map<int, wxString> GetVirtualPageToSheetPagesMap() const;
  133. wxString ConvertRefsToKIIDs( const wxString& aSource ) const;
  134. wxString ConvertKIIDsToRefs( const wxString& aSource ) const;
  135. /**
  136. * Return the full schematic flattened hierarchical sheet list.
  137. */
  138. SCH_SHEET_LIST& GetFullHierarchy() const;
  139. /**
  140. * Update the symbol value and footprint instance data for legacy designs.
  141. *
  142. * Prior to schematic file format version 20200828 and legacy file format version, only
  143. * symbol reference field and unit were saved in the instance data. The value and footprint
  144. * fields must be carried forward from the original symbol to prevent data loss.
  145. */
  146. void SetLegacySymbolInstanceData();
  147. #if defined(DEBUG)
  148. void Show( int nestLevel, std::ostream& os ) const override {}
  149. #endif
  150. private:
  151. friend class SCH_EDIT_FRAME;
  152. PROJECT* m_project;
  153. /// The top-level sheet in this schematic hierarchy (or potentially the only one)
  154. SCH_SHEET* m_rootSheet;
  155. /**
  156. * The sheet path of the sheet currently being edited or displayed.
  157. * Note that this was moved here from SCH_EDIT_FRAME because currently many places in the code
  158. * want to know the current sheet. Potentially this can be moved back to the UI code once
  159. * the only places that want to know it are UI-related
  160. */
  161. SCH_SHEET_PATH* m_currentSheet;
  162. /// Holds and calculates connectivity information of this schematic
  163. CONNECTION_GRAPH* m_connectionGraph;
  164. /**
  165. * Holds a map of labels to the page sequence (virtual page number) that they appear on. It is
  166. * used for updating global label intersheet references.
  167. */
  168. std::map<wxString, std::set<int>> m_labelToPageRefsMap;
  169. /**
  170. * Properties for text variable substitution (and perhaps other uses in future).
  171. */
  172. std::map<wxString, wxString> m_properties;
  173. };
  174. #endif