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.

644 lines
21 KiB

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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2020 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. #ifndef SCH_SHEEET_H
  25. #define SCH_SHEEET_H
  26. #include <boost/ptr_container/ptr_vector.hpp>
  27. #include <sch_text.h>
  28. #include <sch_field.h>
  29. class KIID;
  30. class LINE_READER;
  31. class SCH_SCREEN;
  32. class SCH_SHEET;
  33. class SCH_SHEET_PIN;
  34. class SCH_SHEET_PATH;
  35. class DANGLING_END_ITEM;
  36. class SCH_EDIT_FRAME;
  37. class NETLIST_OBJECT_LIST;
  38. #define MIN_SHEET_WIDTH 500 // Units are mils.
  39. #define MIN_SHEET_HEIGHT 150 // Units are mils.
  40. /**
  41. * Defines the edge of the sheet that the sheet pin is positioned
  42. * SHEET_LEFT_SIDE = 0: pin on left side
  43. * SHEET_RIGHT_SIDE = 1: pin on right side
  44. * SHEET_TOP_SIDE = 2: pin on top side
  45. * SHEET_BOTTOM_SIDE =3: pin on bottom side
  46. *
  47. * For compatibility reasons, this does not follow same values as text orientation.
  48. */
  49. enum SHEET_SIDE
  50. {
  51. SHEET_LEFT_SIDE = 0,
  52. SHEET_RIGHT_SIDE,
  53. SHEET_TOP_SIDE,
  54. SHEET_BOTTOM_SIDE,
  55. SHEET_UNDEFINED_SIDE
  56. };
  57. enum SHEET_FIELD_TYPE {
  58. SHEETNAME = 0,
  59. SHEETFILENAME,
  60. /// The first 2 are mandatory, and must be instantiated in SCH_SHEET
  61. SHEET_MANDATORY_FIELDS
  62. };
  63. /**
  64. * Define a sheet pin (label) used in sheets to create hierarchical schematics.
  65. *
  66. * A SCH_SHEET_PIN is used to create a hierarchical sheet in the same way a
  67. * pin is used in a component. It connects the objects in the sheet object
  68. * to the objects in the schematic page to the objects in the page that is
  69. * represented by the sheet. In a sheet object, a SCH_SHEET_PIN must be
  70. * connected to a wire, bus, or label. In the schematic page represented by
  71. * the sheet, it corresponds to a hierarchical label.
  72. */
  73. class SCH_SHEET_PIN : public SCH_HIERLABEL
  74. {
  75. private:
  76. int m_number; ///< Label number use for saving sheet label to file.
  77. ///< Sheet label numbering begins at 2.
  78. ///< 0 is reserved for the sheet name.
  79. ///< 1 is reserve for the sheet file name.
  80. SHEET_SIDE m_edge;
  81. public:
  82. SCH_SHEET_PIN( SCH_SHEET* parent,
  83. const wxPoint& pos = wxPoint( 0, 0 ),
  84. const wxString& text = wxEmptyString );
  85. // Do not create a copy constructor. The one generated by the compiler is adequate.
  86. ~SCH_SHEET_PIN() { }
  87. static inline bool ClassOf( const EDA_ITEM* aItem )
  88. {
  89. return aItem && SCH_SHEET_PIN_T == aItem->Type();
  90. }
  91. wxString GetClass() const override
  92. {
  93. return wxT( "SCH_SHEET_PIN" );
  94. }
  95. bool operator ==( const SCH_SHEET_PIN* aPin ) const;
  96. /**
  97. * Return true for items which are moved with the anchor point at mouse cursor
  98. * and false for items moved with no reference to anchor (usually large items)
  99. *
  100. * @return true for a hierarchical sheet pin
  101. */
  102. bool IsMovableFromAnchorPoint() override { return true; }
  103. void Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset ) override;
  104. /**
  105. * Calculate the graphic shape (a polygon) associated to the text.
  106. *
  107. * @param aPoints = a buffer to fill with polygon corners coordinates
  108. * @param aPos = Position of the shape
  109. */
  110. void CreateGraphicShape( RENDER_SETTINGS* aSettings,
  111. std::vector <wxPoint>& aPoints, const wxPoint& aPos ) override;
  112. void SwapData( SCH_ITEM* aItem ) override;
  113. int GetPenWidth() const override;
  114. /**
  115. * Get the sheet label number.
  116. *
  117. * @return Number of the sheet label.
  118. */
  119. int GetNumber() const { return m_number; }
  120. /**
  121. * Set the sheet label number.
  122. *
  123. * @param aNumber - New sheet number label.
  124. */
  125. void SetNumber( int aNumber );
  126. void SetEdge( SHEET_SIDE aEdge );
  127. SHEET_SIDE GetEdge() const;
  128. /**
  129. * Adjust label position to edge based on proximity to vertical or horizontal edge
  130. * of the parent sheet.
  131. */
  132. void ConstrainOnEdge( wxPoint Pos );
  133. /**
  134. * Get the parent sheet object of this sheet pin.
  135. *
  136. * @return The sheet that is the parent of this sheet pin or NULL if it does
  137. * not have a parent.
  138. */
  139. SCH_SHEET* GetParent() const { return (SCH_SHEET*) m_parent; }
  140. #if defined(DEBUG)
  141. void Show( int nestLevel, std::ostream& os ) const override;
  142. #endif
  143. // Geometric transforms (used in block operations):
  144. void Move( const wxPoint& aMoveVector ) override
  145. {
  146. Offset( aMoveVector );
  147. }
  148. void MirrorX( int aXaxis_position ) override;
  149. void MirrorY( int aYaxis_position ) override;
  150. void Rotate( wxPoint aPosition ) override;
  151. bool Matches( wxFindReplaceData& aSearchData, void* aAuxData ) override
  152. {
  153. return SCH_ITEM::Matches( GetText(), aSearchData );
  154. }
  155. bool Replace( wxFindReplaceData& aSearchData, void* aAuxData = NULL ) override
  156. {
  157. return EDA_TEXT::Replace( aSearchData );
  158. }
  159. bool IsReplaceable() const override { return true; }
  160. void GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList ) override;
  161. bool IsConnectable() const override { return true; }
  162. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  163. BITMAP_DEF GetMenuImage() const override;
  164. void SetPosition( const wxPoint& aPosition ) override { ConstrainOnEdge( aPosition ); }
  165. bool IsPointClickableAnchor( const wxPoint& aPos ) const override { return m_isDangling && GetPosition() == aPos; }
  166. bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override;
  167. EDA_ITEM* Clone() const override;
  168. };
  169. /**
  170. * Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
  171. */
  172. class SCH_SHEET : public SCH_ITEM
  173. {
  174. friend class SCH_SHEET_PIN;
  175. SCH_SCREEN* m_screen; // Screen that contains the physical data for the sheet. In
  176. // complex hierarchies multiple sheets can share a common screen.
  177. std::vector<SCH_SHEET_PIN*> m_pins; // The list of sheet connection points.
  178. std::vector<SCH_FIELD> m_fields;
  179. wxPoint m_pos; // The position of the sheet.
  180. wxSize m_size; // The size of the sheet.
  181. int m_borderWidth;
  182. KIGFX::COLOR4D m_borderColor;
  183. KIGFX::COLOR4D m_backgroundColor;
  184. std::vector<SCH_SHEET_INSTANCE> m_instances;
  185. public:
  186. SCH_SHEET( EDA_ITEM* aParent = nullptr, const wxPoint& pos = wxPoint( 0, 0 ) );
  187. /**
  188. * Copy \a aSheet into a new object. All sheet pins are copied as is except and
  189. * the SCH_SHEET_PIN's #m_Parent pointers are set to the new copied parent object.
  190. */
  191. SCH_SHEET( const SCH_SHEET& aSheet );
  192. ~SCH_SHEET();
  193. static inline bool ClassOf( const EDA_ITEM* aItem )
  194. {
  195. return aItem && SCH_SHEET_T == aItem->Type();
  196. }
  197. wxString GetClass() const override
  198. {
  199. return wxT( "SCH_SHEET" );
  200. }
  201. /**
  202. * Return true for items which are moved with the anchor point at mouse cursor
  203. * and false for items moved with no reference to anchor.
  204. *
  205. * Usually return true for small items (labels, junctions) and false for
  206. * items which can be large (hierarchical sheets, components)
  207. *
  208. * @return false for a hierarchical sheet
  209. */
  210. bool IsMovableFromAnchorPoint() override { return false; }
  211. std::vector<SCH_FIELD>& GetFields() { return m_fields; }
  212. /**
  213. * Set multiple schematic fields.
  214. *
  215. * @param aFields are the fields to set in this symbol.
  216. */
  217. void SetFields( const std::vector<SCH_FIELD>& aFields )
  218. {
  219. m_fields = aFields; // vector copying, length is changed possibly
  220. }
  221. wxString GetName() const { return m_fields[ SHEETNAME ].GetText(); }
  222. SCH_SCREEN* GetScreen() const { return m_screen; }
  223. wxSize GetSize() { return m_size; }
  224. void SetSize( const wxSize& aSize ) { m_size = aSize; }
  225. int GetBorderWidth() const { return m_borderWidth; }
  226. void SetBorderWidth( int aWidth ) { m_borderWidth = aWidth; }
  227. KIGFX::COLOR4D GetBorderColor() const { return m_borderColor; }
  228. void SetBorderColor( KIGFX::COLOR4D aColor ) { m_borderColor = aColor; }
  229. KIGFX::COLOR4D GetBackgroundColor() const { return m_backgroundColor; }
  230. void SetBackgroundColor( KIGFX::COLOR4D aColor ) { m_backgroundColor = aColor; }
  231. /**
  232. * Test this sheet to see if the default stroke is used to draw the outline.
  233. *
  234. * The default stroke is defined as follows:
  235. * * The outline width is the default line width or 0.
  236. * * The outline style is set to #PLOT_DASH_TYPE::DEFAULT or #PLOT_DASH_TYPE::SOLID.
  237. * * The outline color is set to #COLOR4D::UNSPECIFIED.
  238. *
  239. * @return True if the outline stroke meets the default criteria.
  240. */
  241. bool UsesDefaultStroke() const;
  242. /**
  243. * @return true if this sheet is the root sheet.
  244. */
  245. bool IsRootSheet() const;
  246. /**
  247. * Set the #SCH_SCREEN associated with this sheet to \a aScreen.
  248. *
  249. * The screen reference counting is performed by SetScreen. If \a aScreen is not
  250. * the same as the current screen, the current screen reference count is decremented
  251. * and \a aScreen becomes the screen for the sheet. If the current screen reference
  252. * count reaches zero, the current screen is deleted. NULL is a valid value for
  253. * \a aScreen.
  254. *
  255. * @param aScreen The new screen to associate with the sheet.
  256. */
  257. void SetScreen( SCH_SCREEN* aScreen );
  258. /**
  259. * Return the number of times the associated screen for the sheet is being used.
  260. *
  261. * If no screen is associated with the sheet, then zero is returned.
  262. */
  263. int GetScreenCount() const;
  264. /**
  265. * Return the list of system text vars & fields for this sheet.
  266. */
  267. void GetContextualTextVars( wxArrayString* aVars ) const;
  268. /**
  269. * Resolve any references to system tokens supported by the sheet.
  270. * @param aDepth a counter to limit recursion and circular references.
  271. */
  272. bool ResolveTextVar( wxString* token, int aDepth = 0 ) const;
  273. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  274. /* there is no member for orientation in sch_sheet, to preserve file
  275. * format, we detect orientation based on pin edges
  276. */
  277. bool IsVerticalOrientation() const;
  278. /**
  279. * Add aSheetPin to the sheet.
  280. *
  281. * Note: Once a sheet pin is added to the sheet, it is owned by the sheet.
  282. * Do not delete the sheet pin object or you will likely get a segfault
  283. * when the sheet is destroyed.
  284. *
  285. * @param aSheetPin The sheet pin item to add to the sheet.
  286. */
  287. void AddPin( SCH_SHEET_PIN* aSheetPin );
  288. std::vector<SCH_SHEET_PIN*>& GetPins() { return m_pins; }
  289. std::vector<SCH_SHEET_PIN*>& GetPins() const
  290. {
  291. return const_cast< std::vector<SCH_SHEET_PIN*>& >( m_pins );
  292. }
  293. /**
  294. * Remove \a aSheetPin from the sheet.
  295. *
  296. * @param aSheetPin The sheet pin item to remove from the sheet.
  297. */
  298. void RemovePin( SCH_SHEET_PIN* aSheetPin );
  299. /**
  300. * Delete sheet label which do not have a corresponding hierarchical label.
  301. *
  302. * Note: Make sure you save a copy of the sheet in the undo list before calling
  303. * CleanupSheet() otherwise any unreferenced sheet labels will be lost.
  304. */
  305. void CleanupSheet();
  306. /**
  307. * Return the sheet pin item found at \a aPosition in the sheet.
  308. *
  309. * @param aPosition The position to check for a sheet pin.
  310. *
  311. * @return The sheet pin found at \a aPosition or NULL if no sheet pin is found.
  312. */
  313. SCH_SHEET_PIN* GetPin( const wxPoint& aPosition );
  314. /**
  315. * Checks if the sheet already has a sheet pin named \a aName.
  316. *
  317. * @param aName Name of the sheet pin to search for.
  318. *
  319. * @return True if sheet pin with \a aName is found, otherwise false.
  320. */
  321. bool HasPin( const wxString& aName );
  322. bool HasPins() { return !m_pins.empty(); }
  323. /**
  324. * Check all sheet labels against schematic for undefined hierarchical labels.
  325. *
  326. * @return True if there are any undefined labels.
  327. */
  328. bool HasUndefinedPins();
  329. /**
  330. * Return the minimum width of the sheet based on the widths of the sheet pin text.
  331. *
  332. * The minimum sheet width is determined by the width of the bounding box of each
  333. * hierarchical sheet pin. If two pins are horizontally adjacent ( same Y position )
  334. * to each other, the sum of the bounding box widths is used. If at some point in
  335. * the future sheet objects can be rotated or pins can be placed in the vertical
  336. * orientation, this function will need to be changed.
  337. *
  338. * @return The minimum width the sheet can be resized.
  339. */
  340. int GetMinWidth() const;
  341. /**
  342. * Return the minimum height that the sheet can be resized based on the sheet pin positions.
  343. *
  344. * The minimum width of a sheet is determined by the Y axis location of the bottom
  345. * most sheet pin. If at some point in the future sheet objects can be rotated or
  346. * pins can be placed in the vertical orientation, this function will need to be
  347. * changed.
  348. *
  349. * @return The minimum height the sheet can be resized.
  350. */
  351. int GetMinHeight() const;
  352. int GetPenWidth() const override;
  353. void Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset ) override;
  354. /**
  355. * Return a bounding box for the sheet body but not the fields.
  356. */
  357. const EDA_RECT GetBodyBoundingBox() const;
  358. const EDA_RECT GetBoundingBox() const override;
  359. /**
  360. * Rotating around the boundingBox's center can cause walking when the sheetname or
  361. * filename is longer than the edge it's on. Use this instead, which always returns
  362. * the center of the sheet itself.
  363. */
  364. wxPoint GetRotationCenter() const;
  365. void SwapData( SCH_ITEM* aItem ) override;
  366. /**
  367. * Count our own components, without the power components.
  368. *
  369. * @return the component count.
  370. */
  371. int SymbolCount() const;
  372. /**
  373. * Search the existing hierarchy for an instance of screen loaded from \a aFileName.
  374. *
  375. * @param aFilename = the filename to find (MUST be absolute, and in wxPATH_NATIVE encoding)
  376. * @param aScreen = a location to return a pointer to the screen (if found)
  377. * @return bool if found, and a pointer to the screen
  378. */
  379. bool SearchHierarchy( const wxString& aFilename, SCH_SCREEN** aScreen );
  380. /**
  381. * Search the existing hierarchy for an instance of screen loaded from \a aFileName.
  382. *
  383. * Don't bother looking at the root sheet, it must be unique. No other references to
  384. * its m_screen otherwise there would be loops in the hierarchy.
  385. *
  386. * @param aScreen = the SCH_SCREEN* screen that we search for
  387. * @param aList = the SCH_SHEET_PATH* that must be used
  388. * @return true if found
  389. */
  390. bool LocatePathOfScreen( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aList );
  391. /**
  392. * Count the number of sheets found in "this" sheet including all of the subsheets.
  393. *
  394. * @return the full count of sheets+subsheets contained by "this"
  395. */
  396. int CountSheets() const;
  397. /**
  398. * Return the filename corresponding to this sheet.
  399. *
  400. * @return a wxString containing the filename
  401. */
  402. wxString GetFileName() const
  403. {
  404. return m_fields[ SHEETFILENAME ].GetText();
  405. }
  406. // Set a new filename without changing anything else
  407. void SetFileName( wxString aFilename )
  408. {
  409. // Filenames are stored using unix notation
  410. aFilename.Replace( wxT("\\"), wxT("/") );
  411. m_fields[ SHEETFILENAME ].SetText( aFilename );
  412. }
  413. // Geometric transforms (used in block operations):
  414. void Move( const wxPoint& aMoveVector ) override
  415. {
  416. m_pos += aMoveVector;
  417. for( SCH_SHEET_PIN* pin : m_pins )
  418. pin->Move( aMoveVector );
  419. for( SCH_FIELD& field : m_fields )
  420. field.Move( aMoveVector );
  421. }
  422. void MirrorY( int aYaxis_position ) override;
  423. void MirrorX( int aXaxis_position ) override;
  424. void Rotate( wxPoint aPosition ) override;
  425. bool Matches( wxFindReplaceData& aSearchData, void* aAuxData ) override;
  426. bool IsReplaceable() const override { return true; }
  427. /**
  428. * Resize this sheet to aSize and adjust all of the labels accordingly.
  429. *
  430. * @param aSize - The new size for this sheet.
  431. */
  432. void Resize( const wxSize& aSize );
  433. void AutoplaceFields( SCH_SCREEN* aScreen, bool aManual ) override;
  434. void GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList ) override;
  435. bool UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
  436. const SCH_SHEET_PATH* aPath = nullptr ) override;
  437. bool IsConnectable() const override { return true; }
  438. bool CanConnect( const SCH_ITEM* aItem ) const override
  439. {
  440. return ( aItem->Type() == SCH_LINE_T && aItem->GetLayer() == LAYER_WIRE ) ||
  441. ( aItem->Type() == SCH_LINE_T && aItem->GetLayer() == LAYER_BUS ) ||
  442. ( aItem->Type() == SCH_NO_CONNECT_T );
  443. }
  444. std::vector<wxPoint> GetConnectionPoints() const override;
  445. SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
  446. void RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction ) override;
  447. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  448. BITMAP_DEF GetMenuImage() const override;
  449. SCH_SHEET& operator=( const SCH_ITEM& aSheet );
  450. bool operator <( const SCH_ITEM& aItem ) const override;
  451. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  452. wxPoint GetPosition() const override { return m_pos; }
  453. void SetPosition( const wxPoint& aPosition ) override;
  454. bool HitTest( const wxPoint& aPosition, int aAccuracy ) const override;
  455. bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
  456. void Plot( PLOTTER* aPlotter ) override;
  457. EDA_ITEM* Clone() const override;
  458. /**
  459. * @return the list of #SCH_SHEET_INSTANCE objects for this sheet.
  460. */
  461. const std::vector<SCH_SHEET_INSTANCE> GetInstances() const;
  462. /**
  463. * Add a new instance \a aSheetPath to the instance list.
  464. *
  465. * If \a aSheetPath does not already exist, it is added to the list. If already exists
  466. * in the list, do nothing. Sheet instances allow for the sharing in complex hierarchies
  467. * which allows for per instance data such as page number for sheets to stored.
  468. *
  469. * @param aInstance is the #KIID_PATH of the sheet instanceadd to the instance list.
  470. * @return false if the instance already exists, true if the instance was added.
  471. */
  472. bool AddInstance( const KIID_PATH& aInstance );
  473. /**
  474. * Return the sheet page number for \a aInstance.
  475. *
  476. * @return the page number for the requested sheet instance.
  477. */
  478. wxString GetPageNumber( const SCH_SHEET_PATH& aInstance ) const;
  479. /**
  480. * Set the page number for the sheet instance \a aInstance.
  481. *
  482. * @param aInstance is the hierarchical path of the sheet.
  483. * @param aReference is the new page number for the sheet.
  484. */
  485. void SetPageNumber( const SCH_SHEET_PATH& aInstance, const wxString& aPageNumber );
  486. /**
  487. * @brief Compares page numbers of schematic sheets. Currently a basic
  488. * @param aPageNumberA
  489. * @param aPageNumberB
  490. * @return 0 if the page numbers are equal, -1 if aPageNumberA < aPageNumberB, 1 otherwise
  491. */
  492. static int ComparePageNum( const wxString& aPageNumberA, const wxString aPageNumberB );
  493. #if defined(DEBUG)
  494. void Show( int nestLevel, std::ostream& os ) const override;
  495. #endif
  496. static const wxString GetDefaultFieldName( int aFieldNdx );
  497. protected:
  498. /**
  499. * Renumber the sheet pins in the sheet.
  500. *
  501. * This method is used internally by SCH_SHEET to update the pin numbering
  502. * when the pin list changes. Make sure you call this method any time a
  503. * sheet pin is added or removed.
  504. */
  505. void renumberPins();
  506. private:
  507. bool doIsConnected( const wxPoint& aPosition ) const override;
  508. };
  509. //typedef std::vector< SCH_SHEET* > SCH_SHEETS; // no ownership over contained SCH_SHEETs
  510. #endif // SCH_SHEEET_H