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.

495 lines
20 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2011-2016 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file sch_sheet_path.h
  27. * @brief Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
  28. */
  29. #ifndef CLASS_DRAWSHEET_PATH_H
  30. #define CLASS_DRAWSHEET_PATH_H
  31. #include <base_struct.h>
  32. #include <map>
  33. /** Info about complex hierarchies handling:
  34. * A hierarchical schematic uses sheets (hierarchical sheets) included in a
  35. * given sheet. Each sheet corresponds to a schematic drawing handled by a
  36. * SCH_SCREEN structure. A SCH_SCREEN structure contains drawings, and have
  37. * a filename to write it's data. Also a SCH_SCREEN display a sheet number
  38. * and the name of the sheet.
  39. *
  40. * In simple (and flat) hierarchies a sheet is linked to a SCH_SCREEN,
  41. * and a SCH_SCREEN is used by only one hierarchical sheet.
  42. *
  43. * In complex hierarchies the same SCH_SCREEN (and its data) is shared between
  44. * more than one sheet. Therefore subsheets (like subsheets in a SCH_SCREEN
  45. * shared by many sheets) can be also shared. So the same SCH_SCREEN must
  46. * handle different components references and parts selection depending on
  47. * which sheet is currently selected, and how a given subsheet is selected.
  48. * 2 sheets share the same SCH_SCREEN (the same drawings) if they have the
  49. * same filename.
  50. *
  51. * In KiCad each component and sheet receives (when created) an unique
  52. * identification called Time Stamp. So each sheet has 2 ids: its time stamp
  53. * (that cannot change) and its name ( that can be edited and therefore is
  54. * not reliable for strong identification). KiCad uses Time Stamp ( a unique
  55. * 32 bit id), to identify sheets in hierarchies.
  56. * A given sheet in a hierarchy is fully labeled by its path (or sheet path)
  57. * that is the list of time stamp found to access it through the hierarchy
  58. * the root sheet is /. All other sheets have a path like /1234ABCD or
  59. * /4567FEDC/AA2233DD/. This path can be displayed as human readable sheet
  60. * name like: / or /sheet1/include_sheet/ or /sheet2/include_sheet/
  61. *
  62. * So to know for a given SCH_SCREEN (a given schematic drawings) we must:
  63. * 1) Handle all references possibilities.
  64. * 2) When acceded by a given selected sheet, display (update) the
  65. * corresponding references and sheet path
  66. *
  67. * The class SCH_SHEET_PATH handles paths used to access a sheet. The class
  68. * SCH_SHEET_LIST allows to handle the full (or partial) list of sheets and
  69. * their paths in a complex hierarchy. The class EDA_ScreenList allow to
  70. * handle the list of SCH_SCREEN. It is useful to clear or save data,
  71. * but is not suitable to handle the full complex hierarchy possibilities
  72. * (usable in flat and simple hierarchies).
  73. */
  74. #include "sch_sheet.h" // SCH_SHEETS
  75. class wxFindReplaceData;
  76. class SCH_SCREEN;
  77. class SCH_MARKER;
  78. class SCH_ITEM;
  79. class SCH_REFERENCE_LIST;
  80. class PART_LIBS;
  81. #define SHEET_NOT_FOUND -1
  82. /**
  83. * Type SCH_MULTI_UNIT_REFERENCE_MAP
  84. * is used to create a map of reference designators for multi-unit parts.
  85. */
  86. typedef std::map<wxString, SCH_REFERENCE_LIST> SCH_MULTI_UNIT_REFERENCE_MAP;
  87. /**
  88. * Class SCH_SHEET_PATH
  89. *
  90. * handles access to a stack of flattened #SCH_SHEET objects by way of a path for
  91. * creating a flattened schematic hierarchy.
  92. *
  93. * <p>
  94. * The #SCH_SHEET objects are stored in a list from first (usually the root sheet) to a
  95. * given sheet in last position. The _last_ sheet is usually the sheet we want to select
  96. * or reach (which is what the function Last() returns). Others sheets constitute the
  97. * "path" from the first to the last sheet.
  98. * </p>
  99. */
  100. class SCH_SHEET_PATH : public SCH_SHEETS
  101. {
  102. #define MAX_SHEET_DEPTH 32 /// Maximum number of levels for a sheet path.
  103. int m_pageNumber; /// Page numbers are maintained by the sheet load order.
  104. public:
  105. SCH_SHEET_PATH();
  106. void SetPageNumber( int aPageNumber ) { m_pageNumber = aPageNumber; }
  107. int GetPageNumber() const { return m_pageNumber; }
  108. const SCH_SHEET* GetSheet( unsigned aIndex ) const
  109. {
  110. SCH_SHEET* retv = NULL;
  111. if( aIndex < size() )
  112. retv = at( aIndex );
  113. return const_cast< SCH_SHEET* >( retv );
  114. }
  115. SCH_SHEET* GetSheet( unsigned aIndex )
  116. {
  117. return const_cast< SCH_SHEET* >( static_cast< const SCH_SHEET_PATH& >( *this ).GetSheet( aIndex ) );
  118. }
  119. /**
  120. * Function Cmp
  121. * Compare if this is the same sheet path as aSheetPathToTest
  122. * @param aSheetPathToTest = sheet path to compare
  123. * @return 1 if this sheet path has more sheets than aSheetPathToTest,
  124. * -1 if this sheet path has fewer sheets than aSheetPathToTest,
  125. * or 0 if same
  126. */
  127. int Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const;
  128. /**
  129. * Function Last
  130. * returns a pointer to the last sheet of the list
  131. * One can see the others sheet as the "path" to reach this last sheet
  132. */
  133. SCH_SHEET* Last() const;
  134. /**
  135. * Function LastScreen
  136. * @return the SCH_SCREEN relative to the last sheet in list
  137. */
  138. SCH_SCREEN* LastScreen() const;
  139. /**
  140. * Function LastDrawList
  141. * @return a pointer to the first schematic item handled by the
  142. * SCH_SCREEN relative to the last sheet in list
  143. */
  144. SCH_ITEM* LastDrawList() const;
  145. /**
  146. * Get the last schematic item relative to the first sheet in the list.
  147. *
  148. * @return Last schematic item relative to the first sheet in the list if list
  149. * is not empty. Otherwise NULL.
  150. */
  151. SCH_ITEM* FirstDrawList() const;
  152. /**
  153. * Function Path
  154. * the path uses the time stamps which do not changes even when editing
  155. * sheet parameters
  156. * a path is something like / (root) or /34005677 or /34005677/00AE4523
  157. */
  158. wxString Path() const;
  159. /**
  160. * Function PathHumanReadable
  161. * returns the sheet path in a human readable form, i.e. as a path made
  162. * from sheet names. The the "normal" path instead uses the time
  163. * stamps in the path. (Time stamps do not change even when editing
  164. * sheet parameters).
  165. */
  166. wxString PathHumanReadable() const;
  167. /**
  168. * Function UpdateAllScreenReferences
  169. * updates the reference and the m_Multi parameter (part selection) for all
  170. * components on a screen depending on the actual sheet path.
  171. * Mandatory in complex hierarchies because sheets use the same screen
  172. * (basic schematic)
  173. * but with different references and part selections according to the
  174. * displayed sheet
  175. */
  176. void UpdateAllScreenReferences();
  177. /**
  178. * Function AnnotatePowerSymbols
  179. * annotates the power symbols only starting at \a aReference in the sheet path.
  180. * @param aLibs the library list to use
  181. * @param aReference A pointer to the number for the reference designator of the
  182. * first power symbol to be annotated. If the pointer is NULL
  183. * the annotation starts at 1. The number is incremented for
  184. * each power symbol annotated.
  185. */
  186. void AnnotatePowerSymbols( PART_LIBS* aLibs, int* aReference );
  187. /**
  188. * Function GetComponents
  189. * adds a SCH_REFERENCE() object to \a aReferences for each component in the sheet.
  190. * @param aLibs the library list to use
  191. * @param aReferences List of references to populate.
  192. * @param aIncludePowerSymbols : false to only get normal components.
  193. */
  194. void GetComponents( PART_LIBS* aLibs, SCH_REFERENCE_LIST& aReferences,
  195. bool aIncludePowerSymbols = true );
  196. /**
  197. * Function GetMultiUnitComponents
  198. * adds a SCH_REFERENCE_LIST object to \a aRefList for each same-reference set of
  199. * multi-unit parts in the sheet. The map key for each element will be the
  200. * reference designator.
  201. * @param aLibs the library list to use
  202. * @param aRefList Map of reference designators to reference lists
  203. * @param aIncludePowerSymbols : false to only get normal components.
  204. */
  205. void GetMultiUnitComponents( PART_LIBS* aLibs, SCH_MULTI_UNIT_REFERENCE_MAP &aRefList,
  206. bool aIncludePowerSymbols = true );
  207. /**
  208. * Function SetFootprintField
  209. * searches last sheet in the path for a component with \a aReference and set the footprint
  210. * field to \a aFootPrint if found.
  211. *
  212. * @param aReference The reference designator of the component.
  213. * @param aFootPrint The value to set the footprint field.
  214. * @param aSetVisible The value to set the field visibility flag.
  215. * @return True if \a aReference was found otherwise false.
  216. */
  217. bool SetComponentFootprint( const wxString& aReference, const wxString& aFootPrint,
  218. bool aSetVisible );
  219. /**
  220. * Find the next schematic item in this sheet object.
  221. *
  222. * @param aType - The type of schematic item object to search for.
  223. * @param aLastItem - Start search from aLastItem. If no aLastItem, search from
  224. * the beginning of the list.
  225. * @param aWrap - Wrap around the end of the list to find the next item if aLastItem
  226. * is defined.
  227. * @return - The next schematic item if found. Otherwise, NULL is returned.
  228. */
  229. SCH_ITEM* FindNextItem( KICAD_T aType, SCH_ITEM* aLastItem = NULL, bool aWrap = false ) const;
  230. /**
  231. * Find the previous schematic item in this sheet path object.
  232. *
  233. * @param aType - The type of schematic item object to search for.
  234. * @param aLastItem - Start search from aLastItem. If no aLastItem, search from
  235. * the end of the list.
  236. * @param aWrap - Wrap around the beginning of the list to find the next item if aLastItem
  237. * is defined.
  238. * @return - The previous schematic item if found. Otherwise, NULL is returned.
  239. */
  240. SCH_ITEM* FindPreviousItem( KICAD_T aType, SCH_ITEM* aLastItem = NULL, bool aWrap = false ) const;
  241. /**
  242. * Function TestForRecursion
  243. *
  244. * test the SCH_SHEET_PATH file names to check adding the sheet stored in the file
  245. * \a aSrcFileName to the sheet stored in file \a aDestFileName will cause a sheet
  246. * path recursion.
  247. *
  248. * @param aSrcFileName is the source file name of the sheet add to \a aDestFileName.
  249. * @param aDestFileName is the file name of the destination sheet for \a aSrcFileName.
  250. * @return true if \a aFileName will cause recursion in the sheet path. Otherwise false.
  251. */
  252. bool TestForRecursion( const wxString& aSrcFileName, const wxString& aDestFileName ) const;
  253. int FindSheet( const wxString& aFileName ) const;
  254. /**
  255. * Function FindSheetByName
  256. *
  257. * searches the #SCH_SHEET_PATH for a sheet named \a aSheetName.
  258. *
  259. * @param aSheetName is the name of the sheet to find.
  260. * @return a pointer to the sheet named \a aSheetName if found or NULL if not found.
  261. */
  262. SCH_SHEET* FindSheetByName( const wxString& aSheetName );
  263. /**
  264. * Function FindSheetByPageNumber
  265. *
  266. * searches the #SCH_SHEET_LIST for a sheet with \a aPageNumber.
  267. *
  268. * @param aPageNumber is the number of the sheet to find.
  269. * @return a pointer to a #SCH_SHEET object page \a aPageNumber if found or NULL if not found.
  270. */
  271. SCH_SHEET* FindSheetByPageNumber( int aPageNumber );
  272. bool operator==( const SCH_SHEET_PATH& d1 ) const;
  273. bool operator!=( const SCH_SHEET_PATH& d1 ) const { return !( *this == d1 ) ; }
  274. };
  275. typedef std::vector< SCH_SHEET_PATH > SCH_SHEET_PATHS;
  276. typedef SCH_SHEET_PATHS::iterator SCH_SHEET_PATHS_ITER;
  277. typedef SCH_SHEET_PATHS::const_iterator SCH_SHEET_PATHS_CITER;
  278. typedef SCH_SHEET_PATHS::reverse_iterator SCH_SHEET_PATHS_RITER;
  279. typedef SCH_SHEET_PATHS::const_reverse_iterator SCH_SHEET_PATHS_CRITER;
  280. /**
  281. * Class SCH_SHEET_LIST
  282. *
  283. * handles a list of #SCH_SHEET_PATH objects in a flattened hierarchy.
  284. *
  285. * #SCH_SHEET objects are not unique, there can be many sheets with the same filename and
  286. * that share the same #SCH_SCREEN reference. Each The schematic file (#SCH_SCREEN) may
  287. * be shared between these sheets and component references are specific to a sheet path.
  288. * When a sheet is entered, component references and sheet page number are updated.
  289. */
  290. class SCH_SHEET_LIST : public SCH_SHEET_PATHS
  291. {
  292. private:
  293. bool m_isRootSheet;
  294. SCH_SHEET_PATH m_currentSheetPath;
  295. public:
  296. /**
  297. * Constructor
  298. * build a flattened list of SCH_SHEET_PATH objects from \a aSheet.
  299. *
  300. * If aSheet == NULL, then this is an empty hierarchy which the user can populate.
  301. */
  302. SCH_SHEET_LIST( SCH_SHEET* aSheet = NULL );
  303. ~SCH_SHEET_LIST() {}
  304. /**
  305. * Function GetSheetByPath
  306. * returns a sheet matching the path name in \a aPath.
  307. *
  308. * @param aPath A wxString object containing path of the sheet to get.
  309. * @param aHumanReadable True uses the human readable path for comparison.
  310. * False uses the timestamp generated path.
  311. * @return The sheet that matches \a aPath or NULL if no sheet matching
  312. * \a aPath is found.
  313. */
  314. SCH_SHEET_PATH* GetSheetByPath( const wxString aPath, bool aHumanReadable = true );
  315. /**
  316. * Function IsModified
  317. * checks the entire hierarchy for any modifications.
  318. * @returns True if the hierarchy is modified otherwise false.
  319. */
  320. bool IsModified();
  321. /**
  322. * Function IsAutoSaveRequired
  323. * checks the entire hierarchy for any modifications that require auto save.
  324. * @return True if the hierarchy is modified otherwise false.
  325. */
  326. bool IsAutoSaveRequired();
  327. void ClearModifyStatus();
  328. /**
  329. * Function AnnotatePowerSymbols
  330. * clear and annotates the entire hierarchy of the sheet path list.
  331. * @param aLib the library list to use
  332. */
  333. void AnnotatePowerSymbols( PART_LIBS* aLib );
  334. /**
  335. * Function GetComponents
  336. * adds a SCH_REFERENCE() object to \a aReferences for each component in the list
  337. * of sheets.
  338. * @param aLibs the library list to use
  339. * @param aReferences List of references to populate.
  340. * @param aIncludePowerSymbols Set to false to only get normal components.
  341. */
  342. void GetComponents( PART_LIBS* aLibs, SCH_REFERENCE_LIST& aReferences,
  343. bool aIncludePowerSymbols = true );
  344. /**
  345. * Function GetMultiUnitComponents
  346. * adds a SCH_REFERENCE_LIST object to \a aRefList for each same-reference set of
  347. * multi-unit parts in the list of sheets. The map key for each element will be the
  348. * reference designator.
  349. * @param aLibs the library list to use
  350. * @param aRefList Map of reference designators to reference lists
  351. * @param aIncludePowerSymbols Set to false to only get normal components.
  352. */
  353. void GetMultiUnitComponents( PART_LIBS* aLibs, SCH_MULTI_UNIT_REFERENCE_MAP &aRefList,
  354. bool aIncludePowerSymbols = true );
  355. /**
  356. * Function FindNextItem
  357. * searches the entire schematic for the next schematic object.
  358. *
  359. * @param aType - The type of schematic item to find.
  360. * @param aSheetFound - The sheet the item was found in. NULL if the next item
  361. * is not found.
  362. * @param aLastItem - Find next item after aLastItem if not NULL.
  363. * @param aWrap - Wrap past around the end of the list of sheets.
  364. * @return If found, Returns the next schematic item. Otherwise, returns NULL.
  365. */
  366. SCH_ITEM* FindNextItem( KICAD_T aType, SCH_SHEET_PATH** aSheetFound = NULL,
  367. SCH_ITEM* aLastItem = NULL, bool aWrap = true );
  368. /**
  369. * Function FindPreviousItem
  370. * searches the entire schematic for the previous schematic item.
  371. *
  372. * @param aType - The type of schematic item to find.
  373. * @param aSheetFound - The sheet the item was found in. NULL if the previous item
  374. * is not found.
  375. * @param aLastItem - Find the previous item before aLastItem if not NULL.
  376. * @param aWrap - Wrap past around the beginning of the list of sheets.
  377. * @return If found, the previous schematic item. Otherwise, NULL.
  378. */
  379. SCH_ITEM* FindPreviousItem( KICAD_T aType, SCH_SHEET_PATH** aSheetFound = NULL,
  380. SCH_ITEM* aLastItem = NULL, bool aWrap = true );
  381. /**
  382. * Function SetFootprintField
  383. * searches all the sheets for a component with \a aReference and set the footprint
  384. * field to \a aFootPrint if found.
  385. *
  386. * @param aReference The reference designator of the component.
  387. * @param aFootPrint The value to set the footprint field.
  388. * @param aSetVisible The value to set the field visibility flag.
  389. * @return True if \a aReference was found otherwise false.
  390. */
  391. bool SetComponentFootprint( const wxString& aReference, const wxString& aFootPrint,
  392. bool aSetVisible );
  393. /**
  394. * Function IsComplexHierarchy
  395. * searches all of the sheets for duplicate files names which indicates a complex
  396. * hierarchy.
  397. *
  398. * @return true if the #SCH_SHEET_LIST is a complex hierarchy.
  399. */
  400. bool IsComplexHierarchy() const;
  401. /**
  402. * Function TestForRecursion
  403. *
  404. * test every SCH_SHEET_PATH in the SCH_SHEET_LIST to verify if adding the sheets stored
  405. * in \a aSrcSheetHierarchy to the sheet stored in \a aDestFileName will cause recursion.
  406. *
  407. * @param aSrcSheetHierarchy is the SCH_SHEET_LIST of the source sheet add to \a aDestFileName.
  408. * @param aDestFileName is the file name of the destination sheet for \a aSrcFileName.
  409. * @return true if \a aFileName will cause recursion in the sheet path. Otherwise false.
  410. */
  411. bool TestForRecursion( const SCH_SHEET_LIST& aSrcSheetHierarchy,
  412. const wxString& aDestFileName ) const;
  413. /**
  414. * Function FindSheetByName
  415. *
  416. * searches the entire #SCH_SHEET_LIST for a sheet named \a aSheetName.
  417. *
  418. * @param aSheetName is the name of the sheet to find.
  419. * @return a pointer to the sheet named \a aSheetName if found or NULL if not found.
  420. */
  421. SCH_SHEET* FindSheetByName( const wxString& aSheetName );
  422. /**
  423. * Function BuildSheetList
  424. * builds the list of sheets and their sheet path from \a aSheet.
  425. * If \a aSheet is the root sheet, the full sheet path and sheet list are built.
  426. *
  427. * @param aSheet is the starting sheet from which the list is built, or NULL
  428. * indicating that g_RootSheet should be used.
  429. * @throw std::bad_alloc if the memory for the sheet path list could not be allocated.
  430. */
  431. void BuildSheetList( SCH_SHEET* aSheet );
  432. };
  433. #endif // CLASS_DRAWSHEET_PATH_H