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.

572 lines
20 KiB

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