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.

595 lines
21 KiB

18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
  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) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file class_sch_screen.h
  26. * @brief Definitions for the Eeschema program SCH_SCREEN class.
  27. */
  28. #ifndef CLASS_SCREEN_H
  29. #define CLASS_SCREEN_H
  30. #include <macros.h>
  31. #include <dlist.h>
  32. #include <sch_item_struct.h>
  33. #include <class_base_screen.h>
  34. #include <class_title_block.h>
  35. #include <class_page_info.h>
  36. #include <kiway_player.h>
  37. #include <sch_marker.h>
  38. #include <../eeschema/general.h>
  39. class LIB_PIN;
  40. class SCH_COMPONENT;
  41. class SCH_SHEET_PATH;
  42. class SCH_SHEET_PIN;
  43. class SCH_LINE;
  44. class SCH_TEXT;
  45. class PLOTTER;
  46. enum SCH_LINE_TEST_T
  47. {
  48. ENTIRE_LENGTH_T,
  49. END_POINTS_ONLY_T,
  50. EXCLUDE_END_POINTS_T
  51. };
  52. /// Max number of sheets in a hierarchy project
  53. #define NB_MAX_SHEET 500
  54. class SCH_SCREEN : public BASE_SCREEN, public KIWAY_HOLDER
  55. {
  56. private:
  57. wxString m_fileName; ///< File used to load the screen.
  58. int m_refCount; ///< Number of sheets referencing this screen.
  59. ///< Delete when it goes to zero.
  60. /// The size of the paper to print or plot on
  61. PAGE_INFO m_paper; // keep with the MVC 'model' if this class gets split
  62. TITLE_BLOCK m_titles;
  63. /// Origin of the auxilliary axis, which is used in exports mostly, but not yet in EESCHEMA
  64. wxPoint m_aux_origin;
  65. DLIST< SCH_ITEM > m_drawList; ///< Object list for the screen.
  66. int m_modification_sync; ///< inequality with PART_LIBS::GetModificationHash()
  67. ///< will trigger ResolveAll().
  68. /**
  69. * Function addConnectedItemsToBlock
  70. * add items connected at \a aPosition to the block pick list.
  71. * <p>
  72. * This method tests all connectible unselected items in the screen that are connected to
  73. * \a aPosition and adds them to the block selection pick list. This is used when a block
  74. * drag is being performed to ensure connections to items in the block are not lost.
  75. *</p>
  76. * @param aPosition = The connection point to test.
  77. */
  78. void addConnectedItemsToBlock( const wxPoint& aPosition );
  79. public:
  80. /**
  81. * Constructor
  82. */
  83. SCH_SCREEN( KIWAY* aKiway );
  84. ~SCH_SCREEN();
  85. virtual wxString GetClass() const override
  86. {
  87. return wxT( "SCH_SCREEN" );
  88. }
  89. const PAGE_INFO& GetPageSettings() const { return m_paper; }
  90. void SetPageSettings( const PAGE_INFO& aPageSettings ) { m_paper = aPageSettings; }
  91. void SetFileName( const wxString& aFileName ) { m_fileName = aFileName; }
  92. const wxString& GetFileName() const { return m_fileName; }
  93. const wxPoint& GetAuxOrigin() const { return m_aux_origin; }
  94. void SetAuxOrigin( const wxPoint& aPosition ) { m_aux_origin = aPosition; }
  95. const TITLE_BLOCK& GetTitleBlock() const { return m_titles; }
  96. //TITLE_BLOCK& GetTitleBlock() const { return (TITLE_BLOCK&) m_titles; }
  97. void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) { m_titles = aTitleBlock; }
  98. void DecRefCount();
  99. void IncRefCount();
  100. int GetRefCount() const { return m_refCount; }
  101. /**
  102. * Function GetDrawItems().
  103. * @return - A pointer to the first item in the linked list of draw items.
  104. */
  105. SCH_ITEM* GetDrawItems() const { return m_drawList.begin(); }
  106. void Append( SCH_ITEM* aItem )
  107. {
  108. m_drawList.Append( aItem );
  109. --m_modification_sync;
  110. }
  111. /**
  112. * Function Append
  113. * adds \a aList of SCH_ITEM objects to the list for draw items for the sheet.
  114. *
  115. * @param aList A reference to a #DLIST containing the #SCH_ITEM to add to the sheet.
  116. */
  117. void Append( DLIST< SCH_ITEM >& aList )
  118. {
  119. m_drawList.Append( aList );
  120. --m_modification_sync;
  121. }
  122. /**
  123. * Function GetCurItem
  124. * returns the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
  125. * @return SCH_ITEM* - the one selected, or NULL.
  126. */
  127. SCH_ITEM* GetCurItem() const { return (SCH_ITEM*) BASE_SCREEN::GetCurItem(); }
  128. /**
  129. * Function SetCurItem
  130. * sets the currently selected object, m_CurrentItem.
  131. * @param aItem Any object derived from SCH_ITEM
  132. */
  133. void SetCurItem( SCH_ITEM* aItem ) { BASE_SCREEN::SetCurItem( (EDA_ITEM*) aItem ); }
  134. /**
  135. * Function Clear
  136. * deletes all draw items and clears the project settings.
  137. */
  138. void Clear();
  139. /**
  140. * Free all the items from the schematic associated with the screen.
  141. *
  142. * This does not delete any sub hierarchies.
  143. */
  144. void FreeDrawList();
  145. /**
  146. * Function GetItem
  147. * checks \a aPosition within a distance of \a aAccuracy for items of type \a aFilter.
  148. * @param aPosition Position in drawing units.
  149. * @param aAccuracy The maximum distance within \a Position to check for an item.
  150. * @param aType The type of item to find or #NOT_USED to find any item type.
  151. * @return The item found that meets the search criteria or NULL if none found.
  152. */
  153. SCH_ITEM* GetItem( const wxPoint& aPosition, int aAccuracy = 0,
  154. KICAD_T aType = NOT_USED ) const;
  155. void Place( SCH_EDIT_FRAME* frame, wxDC* DC ) { };
  156. /**
  157. * Function CheckComponentsToPartsLink
  158. * initializes or reinitializes the weak reference
  159. * to the LIB_PART for each SCH_COMPONENT found in m_drawList.
  160. * It must be called from:
  161. * - Draw function
  162. * - when loading a schematic file
  163. * - before creating a netlist (in case a library is modified)
  164. */
  165. void CheckComponentsToPartsLinks();
  166. /**
  167. * Function Draw
  168. * draws all the items in the screen to \a aCanvas.
  169. * note: this function is useful only for schematic.
  170. * library editor and library viewer do not use a draw list, and therefore
  171. * draws nothing
  172. * @param aCanvas The canvas item to draw on.
  173. * @param aDC The device context to draw on.
  174. * @param aDrawMode The drawing mode.
  175. * @param aColor The drawing color.
  176. */
  177. void Draw( EDA_DRAW_PANEL* aCanvas, wxDC* aDC, GR_DRAWMODE aDrawMode,
  178. EDA_COLOR_T aColor = UNSPECIFIED_COLOR );
  179. /**
  180. * Function Plot
  181. * plots all the schematic objects to \a aPlotter.
  182. * note: this function is useful only for schematic.
  183. * library editor and library viewer do not use a draw list, and therefore
  184. * plots nothing
  185. *
  186. * @param aPlotter The plotter object to plot to.
  187. */
  188. void Plot( PLOTTER* aPlotter );
  189. /**
  190. * Function Remove
  191. * removes \a aItem from the schematic associated with this screen.
  192. *
  193. * @note The removed item is not deleted. It is only unlinked from the item list.
  194. * @param aItem Item to be removed from schematic.
  195. */
  196. void Remove( SCH_ITEM* aItem );
  197. /**
  198. * Function DeleteItem
  199. * removes \a aItem from the linked list and deletes the object. If \a aItem is
  200. * is a schematic sheet label, it is removed from the screen associated with the
  201. * sheet that contains the label to be deleted.
  202. * @param aItem The schematic object to be deleted from the screen.
  203. */
  204. void DeleteItem( SCH_ITEM* aItem );
  205. bool CheckIfOnDrawList( SCH_ITEM* st );
  206. /**
  207. * Function SchematicCleanUp
  208. * performs routine schematic cleaning including breaking wire and buses and
  209. * deleting identical objects superimposed on top of each other.
  210. *
  211. * @return True if any schematic clean up was performed.
  212. */
  213. bool SchematicCleanUp();
  214. /**
  215. * Function TestDanglingEnds
  216. * tests all of the connectible objects in the schematic for unused connection points.
  217. * @return True if any connection state changes were made.
  218. */
  219. bool TestDanglingEnds();
  220. /**
  221. * Function ExtractWires
  222. * extracts the old wires, junctions and buses. If \a aCreateCopy is true, replace
  223. * extracted items with a copy of the original. Old items are to be put in undo list,
  224. * and the new ones can be modified by clean up safely. If an abort draw segmat command
  225. * is made, the old wires must be put back into #m_drawList, and the copies must be
  226. * deleted. This is because previously stored undo commands can handle pointers on wires
  227. * or buses, and we do not delete wires or buses, we must put them in undo list.
  228. *
  229. * Because cleanup deletes and/or modify bus and wires, it is easier is to put
  230. * all the existing wires in undo list and use a new copy of wires for cleanup.
  231. */
  232. void ExtractWires( DLIST< SCH_ITEM >& aList, bool aCreateCopy );
  233. /**
  234. * Function ReplaceWires
  235. * replaces all of the wires, buses, and junctions in the screen with \a aWireList.
  236. *
  237. * @param aWireList List of wires to replace the existing wires with.
  238. */
  239. void ReplaceWires( DLIST< SCH_ITEM >& aWireList );
  240. /**
  241. * Function MarkConnections
  242. * add all wires and junctions connected to \a aSegment which are not connected any
  243. * component pin to \a aItemList.
  244. * @param aSegment The segment to test for connections.
  245. */
  246. void MarkConnections( SCH_LINE* aSegment );
  247. /**
  248. * Functions GetConnection
  249. * adds all of the wires and junctions to \a aList that make up a connection to the
  250. * object at \a aPosition.
  251. * @param aPosition The position of the first connection object in drawing units.
  252. * @param aList The pick list to add the connect item to.
  253. * @param aFullConnection If true all the objects that make up this connection are
  254. * add to \a aList. Otherwise, only the objects up to the first
  255. * node are added.
  256. * @return The number of items added to \a aList.
  257. */
  258. int GetConnection( const wxPoint& aPosition, PICKED_ITEMS_LIST& aList, bool aFullConnection );
  259. /**
  260. * Function BreakSegment
  261. * checks every wire and bus for a intersection at \a aPoint and break into two segments
  262. * at \a aPoint if an intersection is found.
  263. * @param aPoint Test this point for an intersection.
  264. * @return True if any wires or buses were broken.
  265. */
  266. bool BreakSegment( const wxPoint& aPoint );
  267. /**
  268. * Function BreakSegmentsOnJunctions
  269. * tests all junctions and bus entries in the schematic for intersections with wires and
  270. * buses and breaks any intersections into multiple segments.
  271. * @return True if any wires or buses were broken.
  272. */
  273. bool BreakSegmentsOnJunctions();
  274. /* full undo redo management : */
  275. // use BASE_SCREEN::PushCommandToUndoList( PICKED_ITEMS_LIST* aItem )
  276. // use BASE_SCREEN::PushCommandToRedoList( PICKED_ITEMS_LIST* aItem )
  277. /**
  278. * Function ClearUndoORRedoList
  279. * free the undo or redo list from List element
  280. * Wrappers are deleted.
  281. * data pointed by wrappers are deleted if not in use in schematic
  282. * i.e. when they are copy of a schematic item or they are no more in use (DELETED)
  283. * @param aList = the UNDO_REDO_CONTAINER to clear
  284. * @param aItemCount = the count of items to remove. < 0 for all items
  285. * items are removed from the beginning of the list.
  286. * So this function can be called to remove old commands
  287. */
  288. virtual void ClearUndoORRedoList( UNDO_REDO_CONTAINER& aList, int aItemCount = -1 ) override;
  289. /**
  290. * Function Save
  291. * writes the data structures for this object out to \a aFile in "*.sch" format.
  292. *
  293. * @param aFile The FILE to write to.
  294. * @return bool - true if success writing else false.
  295. */
  296. bool Save( FILE* aFile ) const;
  297. /**
  298. * Clear the state flags of all the items in the screen.
  299. */
  300. void ClearDrawingState();
  301. int CountConnectedItems( const wxPoint& aPos, bool aTestJunctions ) const;
  302. /**
  303. * Function IsJunctionNeeded
  304. * tests if a junction is required for the items at \a aPosition on the screen.
  305. * <p>
  306. * A junction is required at \a aPosition if the following criteria are satisfied:
  307. * <ul>
  308. * <li>one wire midpoint, one or more wire endpoints and no junction.</li>
  309. * <li>three or more wire endpoints and no junction.</li>
  310. * <li>two wire midpoints and no junction</li>
  311. * <li>one wire midpoint, a component pin, and no junction.</li>
  312. * <li>three wire endpoints, a component pin, and no junction.</li>
  313. * </ul>
  314. * </p>
  315. * @param aPosition The position to test.
  316. * @return True if a junction is required at \a aPosition.
  317. */
  318. bool IsJunctionNeeded( const wxPoint& aPosition );
  319. /**
  320. * Function IsTerminalPoint
  321. * tests if \a aPosition is a connection point on \a aLayer.
  322. *
  323. * @param aPosition Position to test.
  324. * @param aLayer The layer type to test against. Valid layer types are #LAYER_NOTES,
  325. * #LAYER_BUS, and #LAYER_WIRE.
  326. * @return True if \a Position is a connection point on \a aLayer.
  327. */
  328. bool IsTerminalPoint( const wxPoint& aPosition, int aLayer );
  329. /**
  330. * Function GetPin
  331. * test the screen for a component pin item at \a aPosition.
  332. * @param aPosition Position to test.
  333. * @param aComponent The component if a pin was found, otherwise NULL.
  334. * @param aEndPointOnly Set to true to test if \a aPosition is the connection
  335. * point of the pin.
  336. * @return The pin item if found, otherwise NULL.
  337. */
  338. LIB_PIN* GetPin( const wxPoint& aPosition, SCH_COMPONENT** aComponent = NULL,
  339. bool aEndPointOnly = false ) const;
  340. /**
  341. * Function GetSheet
  342. * returns a sheet object pointer that is named \a aName.
  343. *
  344. * @note The screen hierarchy is not descened.
  345. * @param aName is the case insensitive name of the sheet.
  346. * @return A pointer to the SCH_SHEET object found or NULL.
  347. */
  348. SCH_SHEET* GetSheet( const wxString& aName );
  349. /**
  350. * Function GetSheetLabel
  351. * test the screen if \a aPosition is a sheet label object.
  352. * @param aPosition The position to test.
  353. * @return The sheet label object if found otherwise NULL.
  354. */
  355. SCH_SHEET_PIN* GetSheetLabel( const wxPoint& aPosition );
  356. /**
  357. * Function ClearAnnotation
  358. * clears the annotation for the components in \a aSheetPath on the screen.
  359. * @param aSheetPath The sheet path of the component annotation to clear. If NULL then
  360. * the entire hierarchy is cleared.
  361. */
  362. void ClearAnnotation( SCH_SHEET_PATH* aSheetPath );
  363. /**
  364. * Function GetHierarchicalItems
  365. * adds all schematic sheet and component object in the screen to \a aItems.
  366. * @param aItems Hierarchical item list to fill.
  367. */
  368. void GetHierarchicalItems( EDA_ITEMS& aItems );
  369. /**
  370. * Function GetNode
  371. * returns all the items at \a aPosition that form a node.
  372. *
  373. * @param aPosition The wxPoint to test for node items.
  374. * @param aList A #EDA_ITEMS container to place the items found.
  375. * @return The number of node items found at \a aPosition.
  376. */
  377. int GetNode( const wxPoint& aPosition, EDA_ITEMS& aList );
  378. /**
  379. * Function GetWireOrBus
  380. * returns a wire or bus item located at \a aPosition.
  381. *
  382. * @param aPosition The wxPoint to test for node items.
  383. * @return The SCH_LINE* of the wire or bus item found at \a aPosition or NULL if item not
  384. * found.
  385. */
  386. SCH_LINE* GetWireOrBus( const wxPoint& aPosition );
  387. /**
  388. * Function GetLine
  389. * returns a line item located at \a aPosition.
  390. *
  391. * @param aPosition The wxPoint to test for a line item.
  392. * @param aAccuracy Amount to inflate the item hit test bounding box.
  393. * @param aLayer The layer the line is drawn upon.
  394. * @param aSearchType Additional line test criteria.
  395. * @return The SCH_LINE* of the wire item found at \a aPosition or NULL if item not
  396. * found.
  397. */
  398. SCH_LINE* GetLine( const wxPoint& aPosition, int aAccuracy = 0, int aLayer = LAYER_NOTES,
  399. SCH_LINE_TEST_T aSearchType = ENTIRE_LENGTH_T );
  400. SCH_LINE* GetWire( const wxPoint& aPosition, int aAccuracy = 0,
  401. SCH_LINE_TEST_T aSearchType = ENTIRE_LENGTH_T )
  402. {
  403. return GetLine( aPosition, aAccuracy, LAYER_WIRE, aSearchType );
  404. }
  405. SCH_LINE* GetBus( const wxPoint& aPosition, int aAccuracy = 0,
  406. SCH_LINE_TEST_T aSearchType = ENTIRE_LENGTH_T )
  407. {
  408. return GetLine( aPosition, aAccuracy, LAYER_BUS, aSearchType );
  409. }
  410. /**
  411. * Function GetLabel
  412. * returns a label item located at \a aPosition.
  413. *
  414. * @param aPosition The wxPoint to test for label items.
  415. * @param aAccuracy Amount to inflate the item hit test bounding box.
  416. * @return The SCH_TEXT* of the label item found at \a aPosition or NULL if item not
  417. * found.
  418. */
  419. SCH_TEXT* GetLabel( const wxPoint& aPosition, int aAccuracy = 0 );
  420. /**
  421. * Function SetFootprintField
  422. * searches screen for a component with \a aReference and set the footprint field to
  423. * \a aFootPrint if found.
  424. *
  425. * @param aSheetPath The sheet path used to look up the reference designator.
  426. * @param aReference The reference designator of the component.
  427. * @param aFootPrint The value to set the footprint field.
  428. * @param aSetVisible The value to set the field visibility flag.
  429. * @return True if \a aReference was found otherwise false.
  430. */
  431. bool SetComponentFootprint( SCH_SHEET_PATH* aSheetPath, const wxString& aReference,
  432. const wxString& aFootPrint, bool aSetVisible );
  433. /**
  434. * Function SelectBlockItems
  435. * creates a list of items found when a block command is initiated. The items selected
  436. * depend on the block command. If the drag block command is issued, than any items
  437. * connected to items in the block are also selected.
  438. */
  439. void SelectBlockItems();
  440. /**
  441. * Function UpdatePickList
  442. * adds all the items in the screen within the block selection rectangle to the pick list.
  443. * @return The number of items in the pick list.
  444. */
  445. int UpdatePickList();
  446. #if defined(DEBUG)
  447. void Show( int nestLevel, std::ostream& os ) const override;
  448. #endif
  449. };
  450. /**
  451. * Class SCH_SCREENS
  452. * is a container class that holds multiple SCH_SCREENs in a hierarchy.
  453. * Individual SCH_SCREENs are unique, and correspond to .sch files.
  454. */
  455. class SCH_SCREENS
  456. {
  457. private:
  458. std::vector< SCH_SCREEN* > m_screens;
  459. unsigned int m_index;
  460. public:
  461. SCH_SCREENS();
  462. ~SCH_SCREENS();
  463. int GetCount() const { return m_screens.size(); }
  464. SCH_SCREEN* GetFirst();
  465. SCH_SCREEN* GetNext();
  466. SCH_SCREEN* GetScreen( unsigned int aIndex ) const;
  467. /**
  468. * Function ClearAnnotation
  469. * clears the annotation for all components in the hierarchy.
  470. */
  471. void ClearAnnotation();
  472. /**
  473. * Function SchematicCleanUp
  474. * merges and breaks wire segments in the entire schematic hierarchy.
  475. */
  476. void SchematicCleanUp();
  477. /**
  478. * Function ReplaceDuplicateTimeStamps
  479. * test all sheet and component objects in the schematic for duplicate time stamps
  480. * an replaces them as necessary. Time stamps must be unique in order for complex
  481. * hierarchies know which components go to which sheets.
  482. * @return The number of duplicate time stamps replaced.
  483. */
  484. int ReplaceDuplicateTimeStamps();
  485. /**
  486. * Function DeleteAllMarkers
  487. * deletes all electronic rules check markers of \a aMarkerType from all the screens in
  488. * the list.
  489. * @param aMarkerType Type of markers to be deleted.
  490. */
  491. void DeleteAllMarkers( enum MARKER_BASE::TYPEMARKER aMarkerType );
  492. /**
  493. * Function GetMarkerCount
  494. * returns the number of ERC markers of \a aMarkerType from all of the screens in the list.
  495. *
  496. * @param aMarkerType Indicates the type of marker to count. if MARKER_UNSPEC
  497. * all markers are counted.
  498. * @param aSeverity Indicates the error level of marker to count.
  499. * useMARKER_SEVERITY_UNSPEC to count all markersof the specified type
  500. * @return int count of the markers found.
  501. */
  502. int GetMarkerCount( enum MARKER_BASE::TYPEMARKER aMarkerType,
  503. enum MARKER_BASE::MARKER_SEVERITY aSeverity );
  504. private:
  505. void AddScreenToList( SCH_SCREEN* aScreen );
  506. void BuildScreenList( EDA_ITEM* aItem );
  507. };
  508. #endif /* CLASS_SCREEN_H */