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.

1160 lines
39 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 1992-2021 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 CLASS_BOARD_H_
  25. #define CLASS_BOARD_H_
  26. #include <board_item_container.h>
  27. #include <common.h> // Needed for stl hash extensions
  28. #include <convert_shape_list_to_polygon.h> // for OUTLINE_ERROR_HANDLER
  29. #include <layer_ids.h>
  30. #include <netinfo.h>
  31. #include <pcb_item_containers.h>
  32. #include <pcb_plot_params.h>
  33. #include <title_block.h>
  34. #include <tools/pcb_selection.h>
  35. #include <mutex>
  36. #include <list>
  37. class BOARD_DESIGN_SETTINGS;
  38. class BOARD_CONNECTED_ITEM;
  39. class BOARD_COMMIT;
  40. class DRC_RTREE;
  41. class PCB_BASE_FRAME;
  42. class PCB_EDIT_FRAME;
  43. class PICKED_ITEMS_LIST;
  44. class BOARD;
  45. class FOOTPRINT;
  46. class ZONE;
  47. class PCB_TRACK;
  48. class PAD;
  49. class PCB_GROUP;
  50. class PCB_MARKER;
  51. class MSG_PANEL_ITEM;
  52. class NETLIST;
  53. class REPORTER;
  54. class SHAPE_POLY_SET;
  55. class CONNECTIVITY_DATA;
  56. class COMPONENT;
  57. class PROJECT;
  58. class PROGRESS_REPORTER;
  59. // Forward declare endpoint from class_track.h
  60. enum ENDPOINT_T : int;
  61. /**
  62. * The allowed types of layers, same as Specctra DSN spec.
  63. */
  64. enum LAYER_T
  65. {
  66. LT_UNDEFINED = -1,
  67. LT_SIGNAL,
  68. LT_POWER,
  69. LT_MIXED,
  70. LT_JUMPER
  71. };
  72. /**
  73. * Container to hold information pertinent to a layer of a BOARD.
  74. */
  75. struct LAYER
  76. {
  77. LAYER()
  78. {
  79. clear();
  80. }
  81. void clear()
  82. {
  83. m_type = LT_SIGNAL;
  84. m_visible = true;
  85. m_number = 0;
  86. m_name.clear();
  87. m_userName.clear();
  88. }
  89. /*
  90. LAYER( const wxString& aName = wxEmptyString,
  91. LAYER_T aType = LT_SIGNAL, bool aVisible = true, int aNumber = -1 ) :
  92. m_name( aName ),
  93. m_type( aType ),
  94. m_visible( aVisible ),
  95. m_number( aNumber )
  96. {
  97. }
  98. */
  99. wxString m_name; ///< The canonical name of the layer. @see #LSET::Name
  100. wxString m_userName; ///< The user defined name of the layer.
  101. LAYER_T m_type; ///< The type of the layer. @see #LAYER_T
  102. bool m_visible;
  103. int m_number; ///< The layer ID. @see PCB_LAYER_ID
  104. /**
  105. * Convert a #LAYER_T enum to a string representation of the layer type.
  106. *
  107. * @param aType The #LAYER_T to convert
  108. * @return The string representation of the layer type.
  109. */
  110. static const char* ShowType( LAYER_T aType );
  111. /**
  112. * Convert a string to a #LAYER_T
  113. *
  114. * @param aType The layer name to convert.
  115. * @return The binary representation of the layer type, or
  116. * LAYER_T(-1) if the string is invalid.
  117. */
  118. static LAYER_T ParseType( const char* aType );
  119. };
  120. // Helper class to handle high light nets
  121. class HIGH_LIGHT_INFO
  122. {
  123. protected:
  124. std::set<int> m_netCodes; // net(s) selected for highlight (-1 when no net selected )
  125. bool m_highLightOn; // highlight active
  126. void Clear()
  127. {
  128. m_netCodes.clear();
  129. m_highLightOn = false;
  130. }
  131. HIGH_LIGHT_INFO()
  132. {
  133. Clear();
  134. }
  135. private:
  136. friend class BOARD;
  137. };
  138. /**
  139. * Provide an interface to hook into board modifications and get callbacks
  140. * on certain modifications that are made to the board. This allows updating
  141. * auxiliary views other than the primary board editor view.
  142. */
  143. class BOARD;
  144. class BOARD_LISTENER
  145. {
  146. public:
  147. virtual ~BOARD_LISTENER() { }
  148. virtual void OnBoardItemAdded( BOARD& aBoard, BOARD_ITEM* aBoardItem ) { }
  149. virtual void OnBoardItemsAdded( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItem ) { }
  150. virtual void OnBoardItemRemoved( BOARD& aBoard, BOARD_ITEM* aBoardItem ) { }
  151. virtual void OnBoardItemsRemoved( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItem ) { }
  152. virtual void OnBoardNetSettingsChanged( BOARD& aBoard ) { }
  153. virtual void OnBoardItemChanged( BOARD& aBoard, BOARD_ITEM* aBoardItem ) { }
  154. virtual void OnBoardItemsChanged( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItem ) { }
  155. virtual void OnBoardHighlightNetChanged( BOARD& aBoard ) { }
  156. };
  157. /**
  158. * Flags to specify how the board is being used.
  159. */
  160. enum class BOARD_USE
  161. {
  162. NORMAL, // A normal board
  163. FPHOLDER // A board that holds a single footprint
  164. };
  165. /**
  166. * Information pertinent to a Pcbnew printed circuit board.
  167. */
  168. class BOARD : public BOARD_ITEM_CONTAINER
  169. {
  170. public:
  171. static inline bool ClassOf( const EDA_ITEM* aItem )
  172. {
  173. return aItem && PCB_T == aItem->Type();
  174. }
  175. /**
  176. * Set what the board is going to be used for.
  177. *
  178. * @param aUse is the flag
  179. */
  180. void SetBoardUse( BOARD_USE aUse ) { m_boardUse = aUse; }
  181. /**
  182. * Get what the board use is.
  183. *
  184. * @return what the board is being used for
  185. */
  186. BOARD_USE GetBoardUse() const { return m_boardUse; }
  187. void IncrementTimeStamp();
  188. int GetTimeStamp() const { return m_timeStamp; }
  189. /**
  190. * Find out if the board is being used to hold a single footprint for editing/viewing.
  191. *
  192. * @return if the board is just holding a footprint
  193. */
  194. bool IsFootprintHolder() const
  195. {
  196. return m_boardUse == BOARD_USE::FPHOLDER;
  197. }
  198. void SetFileName( const wxString& aFileName ) { m_fileName = aFileName; }
  199. const wxString &GetFileName() const { return m_fileName; }
  200. TRACKS& Tracks() { return m_tracks; }
  201. const TRACKS& Tracks() const { return m_tracks; }
  202. FOOTPRINTS& Footprints() { return m_footprints; }
  203. const FOOTPRINTS& Footprints() const { return m_footprints; }
  204. DRAWINGS& Drawings() { return m_drawings; }
  205. const DRAWINGS& Drawings() const { return m_drawings; }
  206. ZONES& Zones() { return m_zones; }
  207. const ZONES& Zones() const { return m_zones; }
  208. MARKERS& Markers() { return m_markers; }
  209. const MARKERS& Markers() const { return m_markers; }
  210. /**
  211. * The groups must maintain the following invariants. These are checked by
  212. * GroupsSanityCheck():
  213. * - An item may appear in at most one group
  214. * - Each group must contain at least one item
  215. * - If a group specifies a name, it must be unique
  216. * - The graph of groups containing subgroups must be cyclic.
  217. */
  218. GROUPS& Groups() { return m_groups; }
  219. const GROUPS& Groups() const { return m_groups; }
  220. const std::vector<BOARD_CONNECTED_ITEM*> AllConnectedItems();
  221. const std::map<wxString, wxString>& GetProperties() const { return m_properties; }
  222. void SetProperties( const std::map<wxString, wxString>& aProps ) { m_properties = aProps; }
  223. bool ResolveTextVar( wxString* token, int aDepth ) const;
  224. /// Visibility settings stored in board prior to 6.0, only used for loading legacy files
  225. LSET m_LegacyVisibleLayers;
  226. GAL_SET m_LegacyVisibleItems;
  227. /// True if the legacy board design settings were loaded from a file
  228. bool m_LegacyDesignSettingsLoaded;
  229. bool m_LegacyCopperEdgeClearanceLoaded;
  230. /// True if netclasses were loaded from the file
  231. bool m_LegacyNetclassesLoaded;
  232. BOARD();
  233. ~BOARD();
  234. wxPoint GetPosition() const override;
  235. void SetPosition( const wxPoint& aPos ) override;
  236. const wxPoint GetFocusPosition() const override { return GetBoundingBox().GetCenter(); }
  237. bool IsEmpty() const
  238. {
  239. return m_drawings.empty() && m_footprints.empty() && m_tracks.empty() && m_zones.empty();
  240. }
  241. void Move( const wxPoint& aMoveVector ) override;
  242. void SetFileFormatVersionAtLoad( int aVersion ) { m_fileFormatVersionAtLoad = aVersion; }
  243. int GetFileFormatVersionAtLoad() const { return m_fileFormatVersionAtLoad; }
  244. void SetGenerator( const wxString& aGenerator ) { m_generator = aGenerator; }
  245. const wxString& GetGenerator() const { return m_generator; }
  246. void Add( BOARD_ITEM* aItem, ADD_MODE aMode = ADD_MODE::INSERT ) override;
  247. void Remove( BOARD_ITEM* aBoardItem, REMOVE_MODE aMode = REMOVE_MODE::NORMAL ) override;
  248. /**
  249. * Must be used if Add() is used using a BULK_x ADD_MODE to generate a change event for
  250. * listeners.
  251. */
  252. void FinalizeBulkAdd( std::vector<BOARD_ITEM*>& aNewItems );
  253. /**
  254. * Must be used if Remove() is used using a BULK_x REMOVE_MODE to generate a change event
  255. * for listeners.
  256. */
  257. void FinalizeBulkRemove( std::vector<BOARD_ITEM*>& aRemovedItems );
  258. /**
  259. * Get the first footprint on the board or nullptr.
  260. *
  261. * This is used primarily by the footprint editor which knows there is only one.
  262. *
  263. * @return first footprint or null pointer
  264. */
  265. FOOTPRINT* GetFirstFootprint() const
  266. {
  267. return m_footprints.empty() ? nullptr : m_footprints.front();
  268. }
  269. /**
  270. * Remove all footprints from the deque and free the memory associated with them.
  271. */
  272. void DeleteAllFootprints();
  273. /**
  274. * @return null if aID is null. Returns an object of Type() == NOT_USED if the aID is not found.
  275. */
  276. BOARD_ITEM* GetItem( const KIID& aID ) const;
  277. void FillItemMap( std::map<KIID, EDA_ITEM*>& aMap );
  278. /**
  279. * Convert cross-references back and forth between ${refDes:field} and ${kiid:field}
  280. */
  281. wxString ConvertCrossReferencesToKIIDs( const wxString& aSource ) const;
  282. wxString ConvertKIIDsToCrossReferences( const wxString& aSource ) const;
  283. /**
  284. * Return a list of missing connections between components/tracks.
  285. * @return an object that contains information about missing connections.
  286. */
  287. std::shared_ptr<CONNECTIVITY_DATA> GetConnectivity() const { return m_connectivity; }
  288. /**
  289. * Build or rebuild the board connectivity database for the board,
  290. * especially the list of connected items, list of nets and rastnest data
  291. * Needed after loading a board to have the connectivity database updated.
  292. */
  293. void BuildConnectivity( PROGRESS_REPORTER* aReporter = nullptr );
  294. /**
  295. * Delete all MARKERS from the board.
  296. */
  297. void DeleteMARKERs();
  298. void DeleteMARKERs( bool aWarningsAndErrors, bool aExclusions );
  299. PROJECT* GetProject() const { return m_project; }
  300. /**
  301. * Link a board to a given project.
  302. *
  303. * Should be called immediately after loading board in order for everything to work.
  304. *
  305. * @param aProject is a loaded project to link to.
  306. */
  307. void SetProject( PROJECT* aProject );
  308. void ClearProject();
  309. /**
  310. * Rebuild DRC markers from the serialized data in BOARD_DESIGN_SETTINGS.
  311. */
  312. std::vector<PCB_MARKER*> ResolveDRCExclusions();
  313. /**
  314. * Reset all high light data to the init state
  315. */
  316. void ResetNetHighLight();
  317. /**
  318. * @return the set of net codes that should be highlighted
  319. */
  320. const std::set<int>& GetHighLightNetCodes() const
  321. {
  322. return m_highLight.m_netCodes;
  323. }
  324. /**
  325. * Select the netcode to be highlighted.
  326. *
  327. * @param aNetCode is the net to highlight.
  328. * @param aMulti is true if you want to add a highlighted net without clearing the old one.
  329. */
  330. void SetHighLightNet( int aNetCode, bool aMulti = false );
  331. /**
  332. * @return true if a net is currently highlighted
  333. */
  334. bool IsHighLightNetON() const { return m_highLight.m_highLightOn; }
  335. /**
  336. * Enable or disable net highlighting.
  337. *
  338. * If a netcode >= 0 has been set with SetHighLightNet and aValue is true, the net will be
  339. * highlighted. If aValue is false, net highlighting will be disabled regardless of
  340. * the highlight netcode being set.
  341. */
  342. void HighLightON( bool aValue = true );
  343. /**
  344. * Disable net highlight.
  345. */
  346. void HighLightOFF()
  347. {
  348. HighLightON( false );
  349. }
  350. /**
  351. * @return The number of copper layers in the BOARD.
  352. */
  353. int GetCopperLayerCount() const;
  354. void SetCopperLayerCount( int aCount );
  355. /**
  356. * A proxy function that calls the corresponding function in m_BoardSettings.
  357. *
  358. * @return the enabled layers in bit-mapped form.
  359. */
  360. LSET GetEnabledLayers() const;
  361. /**
  362. * A proxy function that calls the correspondent function in m_BoardSettings.
  363. *
  364. * @param aLayerMask the new bit-mask of enabled layers.
  365. */
  366. void SetEnabledLayers( LSET aLayerMask );
  367. /**
  368. * A proxy function that calls the correspondent function in m_BoardSettings
  369. * tests whether a given layer is enabled
  370. * @param aLayer = The layer to be tested
  371. * @return true if the layer is visible.
  372. */
  373. bool IsLayerEnabled( PCB_LAYER_ID aLayer ) const;
  374. /**
  375. * A proxy function that calls the correspondent function in m_BoardSettings
  376. * tests whether a given layer is visible
  377. *
  378. * @param aLayer is the layer to be tested.
  379. * @return true if the layer is visible otherwise false.
  380. */
  381. bool IsLayerVisible( PCB_LAYER_ID aLayer ) const;
  382. /**
  383. * A proxy function that calls the correspondent function in m_BoardSettings.
  384. *
  385. * @return the visible layers in bit-mapped form.
  386. */
  387. LSET GetVisibleLayers() const;
  388. /**
  389. * A proxy function that calls the correspondent function in m_BoardSettings
  390. * changes the bit-mask of visible layers.
  391. *
  392. * @param aLayerMask is the new bit-mask of visible layers.
  393. */
  394. void SetVisibleLayers( LSET aLayerMask );
  395. // these 2 functions are not tidy at this time, since there are PCB_LAYER_IDs that
  396. // are not stored in the bitmap.
  397. /**
  398. * Return a set of all the element categories that are visible.
  399. *
  400. * @return the set of visible GAL layers.
  401. * @see enum GAL_LAYER_ID
  402. */
  403. GAL_SET GetVisibleElements() const;
  404. /**
  405. * A proxy function that calls the correspondent function in m_BoardSettings.
  406. *
  407. * @param aMask is the new bit-mask of visible element bitmap or-ed from enum GAL_LAYER_ID
  408. * @see enum GAL_LAYER_ID
  409. */
  410. void SetVisibleElements( const GAL_SET& aMask );
  411. /**
  412. * Change the bit-mask of visible element categories and layers.
  413. *
  414. * @see enum GAL_LAYER_ID
  415. */
  416. void SetVisibleAlls();
  417. /**
  418. * Test whether a given element category is visible.
  419. *
  420. * @param aLayer is from the enum by the same name.
  421. * @return true if the element is visible otherwise false.
  422. * @see enum GAL_LAYER_ID
  423. */
  424. bool IsElementVisible( GAL_LAYER_ID aLayer ) const;
  425. /**
  426. * Change the visibility of an element category.
  427. *
  428. * @param aLayer is from the enum by the same name.
  429. * @param aNewState is the new visibility state of the element category.
  430. * @see enum GAL_LAYER_ID
  431. */
  432. void SetElementVisibility( GAL_LAYER_ID aLayer, bool aNewState );
  433. /**
  434. * Expect either of the two layers on which a footprint can reside, and returns
  435. * whether that layer is visible.
  436. *
  437. * @param aLayer is one of the two allowed layers for footprints: F_Cu or B_Cu
  438. * @return true if the layer is visible, otherwise false.
  439. */
  440. bool IsFootprintLayerVisible( PCB_LAYER_ID aLayer ) const;
  441. /**
  442. * @return the BOARD_DESIGN_SETTINGS for this BOARD
  443. */
  444. BOARD_DESIGN_SETTINGS& GetDesignSettings() const;
  445. const ZONE_SETTINGS& GetZoneSettings() const override;
  446. void SetZoneSettings( const ZONE_SETTINGS& aSettings ) override;
  447. const PAGE_INFO& GetPageSettings() const { return m_paper; }
  448. void SetPageSettings( const PAGE_INFO& aPageSettings ) { m_paper = aPageSettings; }
  449. const PCB_PLOT_PARAMS& GetPlotOptions() const { return m_plotOptions; }
  450. void SetPlotOptions( const PCB_PLOT_PARAMS& aOptions ) { m_plotOptions = aOptions; }
  451. TITLE_BLOCK& GetTitleBlock() { return m_titles; }
  452. const TITLE_BLOCK& GetTitleBlock() const { return m_titles; }
  453. void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) { m_titles = aTitleBlock; }
  454. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  455. /**
  456. * Extract the board outlines and build a closed polygon from lines, arcs and circle items
  457. * on edge cut layer.
  458. *
  459. * Any closed outline inside the main outline is a hole. All contours should be closed,
  460. * i.e. have valid vertices to build a closed polygon.
  461. *
  462. * @param aOutlines is the #SHAPE_POLY_SET to fill in with outlines/holes.
  463. * @param aErrorHandler is an optional DRC_ITEM error handler.
  464. * @return true if success, false if a contour is not valid
  465. */
  466. bool GetBoardPolygonOutlines( SHAPE_POLY_SET& aOutlines,
  467. OUTLINE_ERROR_HANDLER* aErrorHandler = nullptr );
  468. /**
  469. * Build a set of polygons which are the outlines of copper items (pads, tracks, vias, texts,
  470. * zones).
  471. *
  472. * Holes in vias or pads are ignored. The polygons are not merged. This is useful to
  473. * export the shape of copper layers to dxf polygons or 3D viewer/
  474. *
  475. * @param aLayer is a copper layer, like B_Cu, etc.
  476. * @param aOutlines is the SHAPE_POLY_SET to fill in with items outline.
  477. */
  478. void ConvertBrdLayerToPolygonalContours( PCB_LAYER_ID aLayer, SHAPE_POLY_SET& aOutlines ) const;
  479. /**
  480. * Return the ID of a layer.
  481. */
  482. const PCB_LAYER_ID GetLayerID( const wxString& aLayerName ) const;
  483. /**
  484. * Return the name of a \a aLayer.
  485. *
  486. * @param aLayer is the #PCB_LAYER_ID of the layer.
  487. * @return a string containing the name of the layer.
  488. */
  489. const wxString GetLayerName( PCB_LAYER_ID aLayer ) const;
  490. /**
  491. * Changes the name of the layer given by aLayer.
  492. * @param aLayer A layer, like B_Cu, etc.
  493. * @param aLayerName The new layer name
  494. * @return true if aLayerName was legal and unique among other layer names at other layer
  495. * indices and aLayer was within range, else false.
  496. */
  497. bool SetLayerName( PCB_LAYER_ID aLayer, const wxString& aLayerName );
  498. /**
  499. * Return an "English Standard" name of a PCB layer when given \a aLayerNumber.
  500. *
  501. * This function is static so it can be called without a BOARD instance. Use
  502. * GetLayerName() if want the layer names of a specific BOARD, which could
  503. * be different than the default if the user has renamed any copper layers.
  504. *
  505. * @param aLayerId is the layer identifier (index) to fetch.
  506. * @return a string containing the layer name or "BAD INDEX" if aLayerId is not legal.
  507. */
  508. static wxString GetStandardLayerName( PCB_LAYER_ID aLayerId )
  509. {
  510. // a BOARD's standard layer name is the PCB_LAYER_ID fixed name
  511. return LayerName( aLayerId );
  512. }
  513. /**
  514. * Return the type of the copper layer given by aLayer.
  515. *
  516. * @param aIndex A layer index in m_Layer
  517. * @param aLayer A reference to a LAYER description.
  518. * @return false if the index was out of range.
  519. */
  520. bool SetLayerDescr( PCB_LAYER_ID aIndex, const LAYER& aLayer );
  521. /**
  522. * Return the type of the copper layer given by aLayer.
  523. *
  524. * @param aLayer A layer index, like B_Cu, etc.
  525. * @return the layer type, or LAYER_T(-1) if the index was out of range.
  526. */
  527. LAYER_T GetLayerType( PCB_LAYER_ID aLayer ) const;
  528. /**
  529. * Change the type of the layer given by aLayer.
  530. *
  531. * @param aLayer A layer index, like B_Cu, etc.
  532. * @param aLayerType The new layer type.
  533. * @return true if aLayerType was legal and aLayer was within range, else false.
  534. */
  535. bool SetLayerType( PCB_LAYER_ID aLayer, LAYER_T aLayerType );
  536. /**
  537. * @param aNet Only count nodes belonging to this net.
  538. * @return the number of pads members of nets (i.e. with netcode > 0).
  539. */
  540. unsigned GetNodesCount( int aNet = -1 ) const;
  541. /**
  542. * @return the number of unconnected nets in the current ratsnest.
  543. */
  544. unsigned GetUnconnectedNetCount() const;
  545. /**
  546. * Return a reference to a list of all the pads.
  547. *
  548. * The returned list is not sorted and contains pointers to PADS, but those pointers do
  549. * not convey ownership of the respective PADs.
  550. *
  551. * @return a full list of pads.
  552. */
  553. const std::vector<PAD*> GetPads() const;
  554. void BuildListOfNets()
  555. {
  556. m_NetInfo.buildListOfNets();
  557. }
  558. /**
  559. * Search for a net with the given netcode.
  560. *
  561. * @param aNetcode A netcode to search for.
  562. * @return the net if found or NULL if not found.
  563. */
  564. NETINFO_ITEM* FindNet( int aNetcode ) const;
  565. /**
  566. * Search for a net with the given name.
  567. *
  568. * @param aNetname A Netname to search for.
  569. * @return the net if found or NULL if not found.
  570. */
  571. NETINFO_ITEM* FindNet( const wxString& aNetname ) const;
  572. const NETINFO_LIST& GetNetInfo() const
  573. {
  574. return m_NetInfo;
  575. }
  576. NETINFO_LIST& GetNetInfo()
  577. {
  578. return m_NetInfo;
  579. }
  580. #ifndef SWIG
  581. /**
  582. * @return iterator to the first element of the NETINFO_ITEMs list.
  583. */
  584. NETINFO_LIST::iterator BeginNets() const
  585. {
  586. return m_NetInfo.begin();
  587. }
  588. /**
  589. * @return iterator to the last element of the NETINFO_ITEMs list.
  590. */
  591. NETINFO_LIST::iterator EndNets() const
  592. {
  593. return m_NetInfo.end();
  594. }
  595. #endif
  596. /**
  597. * @return the number of nets (NETINFO_ITEM).
  598. */
  599. unsigned GetNetCount() const
  600. {
  601. return m_NetInfo.GetNetCount();
  602. }
  603. /**
  604. * Calculate the bounding box containing all board items (or board edge segments).
  605. *
  606. * @param aBoardEdgesOnly is true if we are interested in board edge segments only.
  607. * @return the board's bounding box.
  608. */
  609. EDA_RECT ComputeBoundingBox( bool aBoardEdgesOnly = false ) const;
  610. const EDA_RECT GetBoundingBox() const override
  611. {
  612. return ComputeBoundingBox( false );
  613. }
  614. /**
  615. * Return the board bounding box calculated using exclusively the board edges (graphics
  616. * on Edge.Cuts layer).
  617. *
  618. * If there are items outside of the area limited by Edge.Cuts graphics, the items will
  619. * not be taken into account.
  620. *
  621. * @return bounding box calculated using exclusively the board edges.
  622. */
  623. const EDA_RECT GetBoardEdgesBoundingBox() const
  624. {
  625. return ComputeBoundingBox( true );
  626. }
  627. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  628. /**
  629. * May be re-implemented for each derived class in order to handle
  630. * all the types given by its member data. Implementations should call
  631. * inspector->Inspect() on types in scanTypes[], and may use IterateForward()
  632. * to do so on lists of such data.
  633. * @param inspector An INSPECTOR instance to use in the inspection.
  634. * @param testData Arbitrary data used by the inspector.
  635. * @param scanTypes Which KICAD_T types are of interest and the order
  636. * is significant too, terminated by EOT.
  637. * @return SEARCH_QUIT if the Iterator is to stop the scan, else SCAN_CONTINUE, and
  638. * determined by the inspector.
  639. */
  640. SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
  641. /**
  642. * Search for a FOOTPRINT within this board with the given reference designator.
  643. *
  644. * Finds only the first one, if there is more than one such FOOTPRINT.
  645. *
  646. * @param aReference The reference designator of the FOOTPRINT to find.
  647. * @return If found the FOOTPRINT having the given reference designator, else nullptr.
  648. */
  649. FOOTPRINT* FindFootprintByReference( const wxString& aReference ) const;
  650. /**
  651. * Search for a FOOTPRINT within this board with the given path.
  652. *
  653. * @param aPath The path ([sheetUUID, .., symbolUUID]) to search for.
  654. * @return If found, the FOOTPRINT having the given uuid, else NULL.
  655. */
  656. FOOTPRINT* FindFootprintByPath( const KIID_PATH& aPath ) const;
  657. /**
  658. * Return a list of name candidates for netclass assignment.
  659. *
  660. * Tokens may appear more than once if they were harvested from hierarchical nets
  661. * (ie: /CLK, /sheet1/CLK).
  662. */
  663. std::vector<wxString> GetNetClassAssignmentCandidates() const;
  664. /**
  665. * Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
  666. *
  667. * Must be called after a Design Rules edit, or after reading a netlist (or editing
  668. * the list of nets) Also this function removes the non existing nets in netclasses
  669. * and add net nets in default netclass (this happens after reading a netlist)
  670. */
  671. void SynchronizeNetsAndNetClasses();
  672. /**
  673. * Copy the current project's text variables into the boards property cache.
  674. */
  675. void SynchronizeProperties();
  676. wxString GetClass() const override
  677. {
  678. return wxT( "BOARD" );
  679. }
  680. #if defined(DEBUG)
  681. void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  682. #endif
  683. /*************************/
  684. /* Copper Areas handling */
  685. /*************************/
  686. /**
  687. * Set the .m_NetCode member of all copper areas, according to the area Net Name
  688. * The SetNetCodesFromNetNames is an equivalent to net name, for fast comparisons.
  689. * However the Netcode is an arbitrary equivalence, it must be set after each netlist read
  690. * or net change
  691. * Must be called after pad netcodes are calculated
  692. * @return : error count
  693. * For non copper areas, netcode is set to 0
  694. */
  695. int SetAreasNetCodesFromNetNames();
  696. /**
  697. * Return the Zone at a given index.
  698. *
  699. * @param index The array type index into a collection of ZONE *.
  700. * @return a pointer to the Area or NULL if index out of range.
  701. */
  702. ZONE* GetArea( int index ) const
  703. {
  704. if( (unsigned) index < m_zones.size() )
  705. return m_zones[index];
  706. return nullptr;
  707. }
  708. /**
  709. * @return a std::list of pointers to all board zones (possibly including zones in footprints)
  710. */
  711. std::list<ZONE*> GetZoneList( bool aIncludeZonesInFootprints = false ) const;
  712. /**
  713. * @return The number of copper pour areas or ZONEs.
  714. */
  715. int GetAreaCount() const
  716. {
  717. return static_cast<int>( m_zones.size() );
  718. }
  719. /* Functions used in test, merge and cut outlines */
  720. /**
  721. * Add an empty copper area to board areas list.
  722. *
  723. * @param aNewZonesList is a PICKED_ITEMS_LIST * where to store new areas pickers (useful
  724. * in undo commands) can be NULL.
  725. * @param aNetcode is the netcode of the copper area (0 = no net).
  726. * @param aLayer is the layer of area.
  727. * @param aStartPointPosition is position of the first point of the polygon outline of this
  728. * area.
  729. * @param aHatch is the hatch option.
  730. * @return a reference to the new area.
  731. */
  732. ZONE* AddArea( PICKED_ITEMS_LIST* aNewZonesList, int aNetcode, PCB_LAYER_ID aLayer,
  733. wxPoint aStartPointPosition, ZONE_BORDER_DISPLAY_STYLE aHatch );
  734. /**
  735. * Process an area that has been modified, by normalizing its polygon against itself.
  736. * i.e. convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s)
  737. *
  738. * This may change the number and order of copper areas in the net.
  739. *
  740. * @param aNewZonesList is a PICKED_ITEMS_LIST where to store new created areas pickers.
  741. * @param aCurrArea is the zone to process.
  742. * @return true if changes are made.
  743. */
  744. bool NormalizeAreaPolygon( PICKED_ITEMS_LIST* aNewZonesList, ZONE* aCurrArea );
  745. /**
  746. * Process an area that has been modified, by normalizing its polygon
  747. * and merging the intersecting polygons for any other areas on the same net.
  748. *
  749. * This may change the number and order of copper areas in the net.
  750. *
  751. * @param aModifiedZonesList is a #PICKED_ITEMS_LIST where to store deleted or added areas
  752. * (useful in undo commands can be NULL).
  753. * @param modified_area is the area to test.
  754. * @return true if some areas modified.
  755. */
  756. bool OnAreaPolygonModified( PICKED_ITEMS_LIST* aModifiedZonesList, ZONE* modified_area );
  757. /**
  758. * Test for intersection of 2 copper areas.
  759. *
  760. * @param aZone1 is the area reference.
  761. * @param aZone2 is the area to compare for intersection calculations.
  762. * @return false if no intersection, true if intersection.
  763. */
  764. bool TestZoneIntersection( ZONE* aZone1, ZONE* aZone2 );
  765. /**
  766. * Find a pad \a aPosition on \a aLayer.
  767. *
  768. * @param aPosition A wxPoint object containing the position to hit test.
  769. * @param aLayerMask A layer or layers to mask the hit test.
  770. * @return A pointer to a PAD object if found or NULL if not found.
  771. */
  772. PAD* GetPad( const wxPoint& aPosition, LSET aLayerMask ) const;
  773. PAD* GetPad( const wxPoint& aPosition ) const
  774. {
  775. return GetPad( aPosition, LSET().set() );
  776. }
  777. /**
  778. * Find a pad connected to \a aEndPoint of \a aTrace.
  779. *
  780. * @param aTrace A pointer to a PCB_TRACK object to hit test against.
  781. * @param aEndPoint The end point of \a aTrace the hit test against.
  782. * @return A pointer to a PAD object if found or NULL if not found.
  783. */
  784. PAD* GetPad( const PCB_TRACK* aTrace, ENDPOINT_T aEndPoint ) const;
  785. /**
  786. * Return pad found at \a aPosition on \a aLayerMask using the fast search method.
  787. * <p>
  788. * The fast search method only works if the pad list has already been built.
  789. * </p>
  790. * @param aPosition A wxPoint object containing the position to hit test.
  791. * @param aLayerMask A layer or layers to mask the hit test.
  792. * @return A pointer to a PAD object if found or NULL if not found.
  793. */
  794. PAD* GetPadFast( const wxPoint& aPosition, LSET aLayerMask ) const;
  795. /**
  796. * Locate the pad connected at \a aPosition on \a aLayer starting at list position
  797. * \a aPad
  798. * <p>
  799. * This function uses a fast search in this sorted pad list and it is faster than
  800. * GetPadFast(). This list is a sorted pad list must be built before calling this
  801. * function.
  802. * </p>
  803. * @note The normal pad list is sorted by increasing netcodes.
  804. * @param aPadList is the list of pads candidates (a std::vector<PAD*>).
  805. * @param aPosition A wxPoint object containing the position to test.
  806. * @param aLayerMask A layer or layers to mask the hit test.
  807. * @return a PAD object pointer to the connected pad.
  808. */
  809. PAD* GetPad( std::vector<PAD*>& aPadList, const wxPoint& aPosition, LSET aLayerMask ) const;
  810. /**
  811. * Delete a given pad from the BOARD by removing it from its footprint and from the
  812. * m_NetInfo. Makes no UI calls.
  813. *
  814. * @param aPad is the pad to delete.
  815. */
  816. void PadDelete( PAD* aPad );
  817. /**
  818. * First empties then fills the vector with all pads and sorts them by increasing x
  819. * coordinate, and for increasing y coordinate for same values of x coordinates. The vector
  820. * only holds pointers to the pads and those pointers are only references to pads which are
  821. * owned by the BOARD through other links.
  822. *
  823. * @param aVector Where to put the pad pointers.
  824. * @param aNetCode = the netcode filter:
  825. * = -1 to build the full pad list.
  826. * = a given netcode to build the pad list relative to the given net
  827. */
  828. void GetSortedPadListByXthenYCoord( std::vector<PAD*>& aVector, int aNetCode = -1 ) const;
  829. /**
  830. * Return data on the length and number of track segments connected to a given track.
  831. * This uses the connectivity data for the board to calculate connections
  832. *
  833. * @param aTrack Starting track (can also be a via) to check against for connection.
  834. * @return a tuple containing <number, length, package length>
  835. */
  836. std::tuple<int, double, double> GetTrackLength( const PCB_TRACK& aTrack ) const;
  837. /**
  838. * Collect all the TRACKs and VIAs that are members of a net given by aNetCode.
  839. * Used from python.
  840. *
  841. * @param aNetCode gives the id of the net.
  842. * @return list of track which are in the net identified by @a aNetCode.
  843. */
  844. TRACKS TracksInNet( int aNetCode );
  845. /**
  846. * Get a footprint by its bounding rectangle at \a aPosition on \a aLayer.
  847. *
  848. * If more than one footprint is at \a aPosition, then the closest footprint on the
  849. * active layer is returned. The distance is calculated via manhattan distance from
  850. * the center of the bounding rectangle to \a aPosition.
  851. *
  852. * @param aPosition A wxPoint object containing the position to test.
  853. * @param aActiveLayer Layer to test.
  854. * @param aVisibleOnly Search only the visible layers if true.
  855. * @param aIgnoreLocked Ignore locked footprints when true.
  856. */
  857. FOOTPRINT* GetFootprint( const wxPoint& aPosition, PCB_LAYER_ID aActiveLayer,
  858. bool aVisibleOnly, bool aIgnoreLocked = false ) const;
  859. /**
  860. * Reset all items' netcodes to 0 (no net).
  861. */
  862. void ClearAllNetCodes();
  863. /**
  864. * Map all nets in the given board to nets with the same name (if any) in the destination
  865. * board. This allows us to share layouts which came from the same hierarchical sheet in
  866. * the schematic.
  867. */
  868. void MapNets( const BOARD* aDestBoard );
  869. void SanitizeNetcodes();
  870. /**
  871. * Add a listener to the board to receive calls whenever something on the
  872. * board has been modified. The board does not take ownership of the
  873. * listener object. Make sure to call RemoveListener before deleting the
  874. * listener object. The order of listener invocations is not guaranteed.
  875. * If the specified listener object has been added before, it will not be
  876. * added again.
  877. */
  878. void AddListener( BOARD_LISTENER* aListener );
  879. /**
  880. * Remove the specified listener. If it has not been added before, it
  881. * will do nothing.
  882. */
  883. void RemoveListener( BOARD_LISTENER* aListener );
  884. /**
  885. * Notify the board and its listeners that an item on the board has
  886. * been modified in some way.
  887. */
  888. void OnItemChanged( BOARD_ITEM* aItem );
  889. /**
  890. * Notify the board and its listeners that an item on the board has
  891. * been modified in some way.
  892. */
  893. void OnItemsChanged( std::vector<BOARD_ITEM*>& aItems );
  894. /**
  895. * Consistency check of internal m_groups structure.
  896. *
  897. * @param repair if true, modify groups structure until it passes the sanity check.
  898. * @return empty string on success. Or error description if there's a problem.
  899. */
  900. wxString GroupsSanityCheck( bool repair = false );
  901. /**
  902. * @param repair if true, make one modification to groups structure that brings it
  903. * closer to passing the sanity check.
  904. * @return empty string on success. Or error description if there's a problem.
  905. */
  906. wxString GroupsSanityCheckInternal( bool repair );
  907. struct GroupLegalOpsField
  908. {
  909. bool create : 1;
  910. bool ungroup : 1;
  911. bool removeItems : 1;
  912. bool enter : 1;
  913. };
  914. /**
  915. * Check which selection tool group operations are legal given the selection.
  916. *
  917. * @return bit field of legal ops.
  918. */
  919. GroupLegalOpsField GroupLegalOps( const PCB_SELECTION& selection ) const;
  920. // --------- Item order comparators ---------
  921. struct cmp_items
  922. {
  923. bool operator() ( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
  924. };
  925. struct cmp_drawings
  926. {
  927. bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
  928. };
  929. // ------------ Run-time caches -------------
  930. std::mutex m_CachesMutex;
  931. std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, bool > m_InsideCourtyardCache;
  932. std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, bool > m_InsideFCourtyardCache;
  933. std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, bool > m_InsideBCourtyardCache;
  934. std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, bool > m_InsideAreaCache;
  935. std::map< wxString, LSET > m_LayerExpressionCache;
  936. std::map< ZONE*, std::unique_ptr<DRC_RTREE> > m_CopperZoneRTrees;
  937. private:
  938. // The default copy constructor & operator= are inadequate,
  939. // either write one or do not use it at all
  940. BOARD( const BOARD& aOther ) = delete;
  941. BOARD& operator=( const BOARD& aOther ) = delete;
  942. template <typename Func, typename... Args>
  943. void InvokeListeners( Func&& aFunc, Args&&... args )
  944. {
  945. for( auto&& l : m_listeners )
  946. ( l->*aFunc )( std::forward<Args>( args )... );
  947. }
  948. friend class PCB_EDIT_FRAME;
  949. /// What is this board being used for
  950. BOARD_USE m_boardUse;
  951. int m_timeStamp; // actually a modification counter
  952. wxString m_fileName;
  953. MARKERS m_markers;
  954. DRAWINGS m_drawings;
  955. FOOTPRINTS m_footprints;
  956. TRACKS m_tracks;
  957. GROUPS m_groups;
  958. ZONES m_zones;
  959. LAYER m_layers[PCB_LAYER_ID_COUNT];
  960. HIGH_LIGHT_INFO m_highLight; // current high light data
  961. HIGH_LIGHT_INFO m_highLightPrevious; // a previously stored high light data
  962. int m_fileFormatVersionAtLoad; // the version loaded from the file
  963. wxString m_generator; // the generator tag from the file
  964. std::map<wxString, wxString> m_properties;
  965. std::shared_ptr<CONNECTIVITY_DATA> m_connectivity;
  966. PAGE_INFO m_paper;
  967. TITLE_BLOCK m_titles; // text in lower right of screen and plots
  968. PCB_PLOT_PARAMS m_plotOptions;
  969. PROJECT* m_project; // project this board is a part of
  970. /**
  971. * All of the board design settings are stored as a JSON object inside the project file. The
  972. * object itself is located here because the alternative is to require a valid project be
  973. * passed in when constructing a BOARD, since things in the BOARD constructor rely on access
  974. * to the BOARD_DESIGN_SETTINGS object.
  975. *
  976. * A reference to this object is set up in the PROJECT_FILE for the PROJECT this board is
  977. * part of, so that the JSON load/store operations work. This link is established when
  978. * boards are loaded from disk.
  979. */
  980. std::unique_ptr<BOARD_DESIGN_SETTINGS> m_designSettings;
  981. NETINFO_LIST m_NetInfo; // net info list (name, design constraints...
  982. std::vector<BOARD_LISTENER*> m_listeners;
  983. };
  984. #endif // CLASS_BOARD_H_