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.

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