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.

1122 lines
41 KiB

5 years ago
11 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
11 years ago
4 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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The 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 FOOTPRINT_H
  25. #define FOOTPRINT_H
  26. #include <deque>
  27. #include <template_fieldnames.h>
  28. #include <board_item_container.h>
  29. #include <board_item.h>
  30. #include <collectors.h>
  31. #include <component_classes/component_class_manager.h>
  32. #include <embedded_files.h>
  33. #include <layer_ids.h> // ALL_LAYERS definition.
  34. #include <lset.h>
  35. #include <lib_id.h>
  36. #include <list>
  37. #include <zones.h>
  38. #include <convert_shape_list_to_polygon.h>
  39. #include <pcb_item_containers.h>
  40. #include <pcb_text.h>
  41. #include <pcb_field.h>
  42. #include <functional>
  43. #include <math/vector3.h>
  44. class LINE_READER;
  45. class EDA_3D_CANVAS;
  46. class PAD;
  47. class BOARD;
  48. class MSG_PANEL_ITEM;
  49. class SHAPE;
  50. class REPORTER;
  51. class COMPONENT_CLASS_CACHE_PROXY;
  52. namespace KIGFX {
  53. class VIEW;
  54. }
  55. namespace KIFONT {
  56. class OUTLINE_FONT;
  57. }
  58. enum INCLUDE_NPTH_T
  59. {
  60. DO_NOT_INCLUDE_NPTH = false,
  61. INCLUDE_NPTH = true
  62. };
  63. /**
  64. * The set of attributes allowed within a FOOTPRINT, using FOOTPRINT::SetAttributes()
  65. * and FOOTPRINT::GetAttributes(). These are to be ORed together when calling
  66. * FOOTPRINT::SetAttributes()
  67. */
  68. enum FOOTPRINT_ATTR_T
  69. {
  70. FP_THROUGH_HOLE = 0x0001,
  71. FP_SMD = 0x0002,
  72. FP_EXCLUDE_FROM_POS_FILES = 0x0004,
  73. FP_EXCLUDE_FROM_BOM = 0x0008,
  74. FP_BOARD_ONLY = 0x0010, // Footprint has no corresponding symbol
  75. FP_JUST_ADDED = 0x0020, // Footprint just added by netlist update
  76. FP_ALLOW_SOLDERMASK_BRIDGES = 0x0040,
  77. FP_ALLOW_MISSING_COURTYARD = 0x0080,
  78. FP_DNP = 0x0100
  79. };
  80. class FP_3DMODEL
  81. {
  82. public:
  83. FP_3DMODEL() :
  84. // Initialize with sensible values
  85. m_Scale { 1, 1, 1 },
  86. m_Rotation { 0, 0, 0 },
  87. m_Offset { 0, 0, 0 },
  88. m_Opacity( 1.0 ),
  89. m_Show( true )
  90. {
  91. }
  92. VECTOR3D m_Scale; ///< 3D model scaling factor (dimensionless)
  93. VECTOR3D m_Rotation; ///< 3D model rotation (degrees)
  94. VECTOR3D m_Offset; ///< 3D model offset (mm)
  95. double m_Opacity;
  96. wxString m_Filename; ///< The 3D shape filename in 3D library
  97. bool m_Show; ///< Include model in rendering
  98. bool operator==( const FP_3DMODEL& aOther ) const
  99. {
  100. return m_Scale == aOther.m_Scale
  101. && m_Rotation == aOther.m_Rotation
  102. && m_Offset == aOther.m_Offset
  103. && m_Opacity == aOther.m_Opacity
  104. && m_Filename == aOther.m_Filename
  105. && m_Show == aOther.m_Show;
  106. }
  107. };
  108. class FOOTPRINT : public BOARD_ITEM_CONTAINER, public EMBEDDED_FILES
  109. {
  110. public:
  111. FOOTPRINT( BOARD* parent );
  112. FOOTPRINT( const FOOTPRINT& aFootprint );
  113. // Move constructor and operator needed due to std containers inside the footprint
  114. FOOTPRINT( FOOTPRINT&& aFootprint );
  115. ~FOOTPRINT();
  116. FOOTPRINT& operator=( const FOOTPRINT& aOther );
  117. FOOTPRINT& operator=( FOOTPRINT&& aOther );
  118. void Serialize( google::protobuf::Any &aContainer ) const override;
  119. bool Deserialize( const google::protobuf::Any &aContainer ) override;
  120. static inline bool ClassOf( const EDA_ITEM* aItem )
  121. {
  122. return aItem && aItem->Type() == PCB_FOOTPRINT_T;
  123. }
  124. LSET GetPrivateLayers() const { return m_privateLayers; }
  125. void SetPrivateLayers( LSET aLayers ) { m_privateLayers = aLayers; }
  126. ///< @copydoc BOARD_ITEM_CONTAINER::Add()
  127. void Add( BOARD_ITEM* aItem, ADD_MODE aMode = ADD_MODE::INSERT,
  128. bool aSkipConnectivity = false ) override;
  129. ///< @copydoc BOARD_ITEM_CONTAINER::Remove()
  130. void Remove( BOARD_ITEM* aItem, REMOVE_MODE aMode = REMOVE_MODE::NORMAL ) override;
  131. /**
  132. * Clear (i.e. force the ORPHANED dummy net info) the net info which
  133. * depends on a given board for all pads of the footprint.
  134. *
  135. * This is needed when a footprint is copied between the fp editor and
  136. * the board editor for instance, because net info become fully broken
  137. */
  138. void ClearAllNets();
  139. /**
  140. * Old footprints do not always have a valid UUID (some can be set to null uuid)
  141. * However null UUIDs, having a special meaning in editor, create issues when
  142. * editing a footprint
  143. * So all null uuids a re replaced by a valid uuid
  144. * @return true if at least one uuid is changed, false if no change
  145. */
  146. bool FixUuids();
  147. /**
  148. * Return the bounding box containing pads when the footprint is on the front side,
  149. * orientation 0, position 0,0.
  150. *
  151. * Mainly used in Gerber place file to draw a footprint outline when the courtyard
  152. * is missing or broken.
  153. *
  154. * @return The rectangle containing the pads for the normalized footprint.
  155. */
  156. BOX2I GetFpPadsLocalBbox() const;
  157. /**
  158. * Return a bounding polygon for the shapes and pads in the footprint.
  159. *
  160. * This operation is slower but more accurate than calculating a bounding box.
  161. */
  162. SHAPE_POLY_SET GetBoundingHull() const;
  163. SHAPE_POLY_SET GetBoundingHull( PCB_LAYER_ID aLayer ) const;
  164. bool TextOnly() const;
  165. // Virtual function
  166. const BOX2I GetBoundingBox() const override;
  167. const BOX2I GetBoundingBox( bool aIncludeText ) const;
  168. /**
  169. * Return the bounding box of the footprint on a given set of layers
  170. */
  171. const BOX2I GetLayerBoundingBox( LSET aLayers ) const;
  172. VECTOR2I GetCenter() const override { return GetBoundingBox( false ).GetCenter(); }
  173. std::deque<PAD*>& Pads() { return m_pads; }
  174. const std::deque<PAD*>& Pads() const { return m_pads; }
  175. DRAWINGS& GraphicalItems() { return m_drawings; }
  176. const DRAWINGS& GraphicalItems() const { return m_drawings; }
  177. ZONES& Zones() { return m_zones; }
  178. const ZONES& Zones() const { return m_zones; }
  179. GROUPS& Groups() { return m_groups; }
  180. const GROUPS& Groups() const { return m_groups; }
  181. bool HasThroughHolePads() const;
  182. std::vector<FP_3DMODEL>& Models() { return m_3D_Drawings; }
  183. const std::vector<FP_3DMODEL>& Models() const { return m_3D_Drawings; }
  184. void SetPosition( const VECTOR2I& aPos ) override;
  185. VECTOR2I GetPosition() const override { return m_pos; }
  186. void SetOrientation( const EDA_ANGLE& aNewAngle );
  187. EDA_ANGLE GetOrientation() const { return m_orient; }
  188. /**
  189. * Used as Layer property setter -- performs a flip if necessary to set the footprint layer
  190. * @param aLayer is the target layer (F_Cu or B_Cu)
  191. */
  192. void SetLayerAndFlip( PCB_LAYER_ID aLayer );
  193. // to make property magic work
  194. PCB_LAYER_ID GetLayer() const override { return BOARD_ITEM::GetLayer(); }
  195. // For property system:
  196. void SetOrientationDegrees( double aOrientation )
  197. {
  198. SetOrientation( EDA_ANGLE( aOrientation, DEGREES_T ) );
  199. }
  200. double GetOrientationDegrees() const
  201. {
  202. return m_orient.AsDegrees();
  203. }
  204. const LIB_ID& GetFPID() const { return m_fpid; }
  205. void SetFPID( const LIB_ID& aFPID )
  206. {
  207. m_fpid = aFPID;
  208. }
  209. wxString GetFPIDAsString() const { return m_fpid.Format(); }
  210. void SetFPIDAsString( const wxString& aFPID ) { m_fpid.Parse( aFPID ); }
  211. wxString GetLibDescription() const { return m_libDescription; }
  212. void SetLibDescription( const wxString& aDesc ) { m_libDescription = aDesc; }
  213. wxString GetKeywords() const { return m_keywords; }
  214. void SetKeywords( const wxString& aKeywords ) { m_keywords = aKeywords; }
  215. const KIID_PATH& GetPath() const { return m_path; }
  216. void SetPath( const KIID_PATH& aPath ) { m_path = aPath; }
  217. wxString GetSheetname() const { return m_sheetname; }
  218. void SetSheetname( const wxString& aSheetname ) { m_sheetname = aSheetname; }
  219. wxString GetSheetfile() const { return m_sheetfile; }
  220. void SetSheetfile( const wxString& aSheetfile ) { m_sheetfile = aSheetfile; }
  221. wxString GetFilters() const { return m_filters; }
  222. void SetFilters( const wxString& aFilters ) { m_filters = aFilters; }
  223. std::optional<int> GetLocalClearance() const { return m_clearance; }
  224. void SetLocalClearance( std::optional<int> aClearance ) { m_clearance = aClearance; }
  225. std::optional<int> GetLocalSolderMaskMargin() const { return m_solderMaskMargin; }
  226. void SetLocalSolderMaskMargin( std::optional<int> aMargin ) { m_solderMaskMargin = aMargin; }
  227. std::optional<int> GetLocalSolderPasteMargin() const { return m_solderPasteMargin; }
  228. void SetLocalSolderPasteMargin( std::optional<int> aMargin ) { m_solderPasteMargin = aMargin; }
  229. std::optional<double> GetLocalSolderPasteMarginRatio() const { return m_solderPasteMarginRatio; }
  230. void SetLocalSolderPasteMarginRatio( std::optional<double> aRatio ) { m_solderPasteMarginRatio = aRatio; }
  231. void SetLocalZoneConnection( ZONE_CONNECTION aType ) { m_zoneConnection = aType; }
  232. ZONE_CONNECTION GetLocalZoneConnection() const { return m_zoneConnection; }
  233. int GetAttributes() const { return m_attributes; }
  234. void SetAttributes( int aAttributes ) { m_attributes = aAttributes; }
  235. void SetFlag( int aFlag ) { m_arflag = aFlag; }
  236. void IncrementFlag() { m_arflag += 1; }
  237. int GetFlag() const { return m_arflag; }
  238. bool IsNetTie() const
  239. {
  240. for( const wxString& group : m_netTiePadGroups )
  241. {
  242. if( !group.IsEmpty() )
  243. return true;
  244. }
  245. return false;
  246. }
  247. std::optional<int> GetLocalClearance( wxString* aSource ) const
  248. {
  249. if( m_clearance.has_value() && aSource )
  250. *aSource = wxString::Format( _( "footprint %s" ), GetReference() );
  251. return m_clearance;
  252. }
  253. /**
  254. * Return any local clearance overrides set in the "classic" (ie: pre-rule) system.
  255. *
  256. * @param aSource [out] optionally reports the source as a user-readable string.
  257. * @return the clearance in internal units.
  258. */
  259. std::optional<int> GetClearanceOverrides( wxString* aSource ) const
  260. {
  261. return GetLocalClearance( aSource );
  262. }
  263. ZONE_CONNECTION GetZoneConnectionOverrides( wxString* aSource ) const
  264. {
  265. if( m_zoneConnection != ZONE_CONNECTION::INHERITED && aSource )
  266. *aSource = wxString::Format( _( "footprint %s" ), GetReference() );
  267. return m_zoneConnection;
  268. }
  269. /**
  270. * @return a list of pad groups, each of which is allowed to short nets within their group.
  271. * A pad group is a comma-separated list of pad numbers.
  272. */
  273. const std::vector<wxString>& GetNetTiePadGroups() const { return m_netTiePadGroups; }
  274. void ClearNetTiePadGroups()
  275. {
  276. m_netTiePadGroups.clear();
  277. }
  278. void AddNetTiePadGroup( const wxString& aGroup )
  279. {
  280. m_netTiePadGroups.emplace_back( aGroup );
  281. }
  282. /**
  283. * @return a map from pad numbers to net-tie group indices. If a pad is not a member of
  284. * a net-tie group its index will be -1.
  285. */
  286. std::map<wxString, int> MapPadNumbersToNetTieGroups() const;
  287. /**
  288. * @return a list of pads that appear in \a aPad's net-tie pad group.
  289. */
  290. std::vector<PAD*> GetNetTiePads( PAD* aPad ) const;
  291. /**
  292. * Returns the most likely attribute based on pads
  293. * Either FP_THROUGH_HOLE/FP_SMD/OTHER(0)
  294. * @return 0/FP_SMD/FP_THROUGH_HOLE
  295. */
  296. int GetLikelyAttribute() const;
  297. void Move( const VECTOR2I& aMoveVector ) override;
  298. void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override;
  299. void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection ) override;
  300. /**
  301. * Move the reference point of the footprint.
  302. *
  303. * It looks like a move footprint:
  304. * the footprints elements (pads, outlines, edges .. ) are moved
  305. * However:
  306. * - the footprint position is not modified.
  307. * - the relative (local) coordinates of these items are modified
  308. * (a move footprint does not change these local coordinates,
  309. * but changes the footprint position)
  310. */
  311. void MoveAnchorPosition( const VECTOR2I& aMoveVector );
  312. /**
  313. * @return true if the footprint is flipped, i.e. on the back side of the board
  314. */
  315. bool IsFlipped() const { return GetLayer() == B_Cu; }
  316. /**
  317. * Use instead of IsFlipped() when you also need to account for unsided footprints (those
  318. * purely on user-layers, etc.).
  319. */
  320. PCB_LAYER_ID GetSide() const;
  321. /**
  322. * @copydoc BOARD_ITEM::IsOnLayer
  323. */
  324. bool IsOnLayer( PCB_LAYER_ID aLayer ) const override;
  325. // m_footprintStatus bits:
  326. #define FP_is_LOCKED 0x01 ///< footprint LOCKED: no autoplace allowed
  327. #define FP_is_PLACED 0x02 ///< In autoplace: footprint automatically placed
  328. #define FP_to_PLACE 0x04 ///< In autoplace: footprint waiting for autoplace
  329. #define FP_PADS_are_LOCKED 0x08
  330. bool IsLocked() const override
  331. {
  332. return ( m_fpStatus & FP_is_LOCKED ) != 0;
  333. }
  334. /**
  335. * Set the #MODULE_is_LOCKED bit in the m_ModuleStatus.
  336. *
  337. * @param isLocked true means turn on locked status, else unlock
  338. */
  339. void SetLocked( bool isLocked ) override
  340. {
  341. if( isLocked )
  342. m_fpStatus |= FP_is_LOCKED;
  343. else
  344. m_fpStatus &= ~FP_is_LOCKED;
  345. }
  346. /**
  347. * @return true if the footprint is flagged with conflicting with some item
  348. */
  349. bool IsConflicting() const;
  350. bool IsPlaced() const { return m_fpStatus & FP_is_PLACED; }
  351. void SetIsPlaced( bool isPlaced )
  352. {
  353. if( isPlaced )
  354. m_fpStatus |= FP_is_PLACED;
  355. else
  356. m_fpStatus &= ~FP_is_PLACED;
  357. }
  358. bool NeedsPlaced() const { return m_fpStatus & FP_to_PLACE; }
  359. void SetNeedsPlaced( bool needsPlaced )
  360. {
  361. if( needsPlaced )
  362. m_fpStatus |= FP_to_PLACE;
  363. else
  364. m_fpStatus &= ~FP_to_PLACE;
  365. }
  366. bool LegacyPadsLocked() const { return m_fpStatus & FP_PADS_are_LOCKED; }
  367. /**
  368. * Test if footprint attributes for type (SMD/Through hole/Other) match the expected
  369. * type based on the pads in the footprint.
  370. * Footprints with plated through-hole pads should usually be marked through hole even if they
  371. * also have SMD because they might not be auto-placed. Exceptions to this might be shielded
  372. * connectors. Otherwise, footprints with SMD pads should be marked SMD.
  373. * Footprints with no connecting pads should be marked "Other"
  374. *
  375. * @param aErrorHandler callback to handle the error messages generated
  376. */
  377. void CheckFootprintAttributes( const std::function<void( const wxString& )>& aErrorHandler );
  378. /**
  379. * Run non-board-specific DRC checks on footprint's pads. These are the checks supported by
  380. * both the PCB DRC and the Footprint Editor Footprint Checker.
  381. *
  382. * @param aErrorHandler callback to handle the error messages generated
  383. */
  384. void CheckPads( UNITS_PROVIDER* aUnitsProvider,
  385. const std::function<void( const PAD*, int, const wxString& )>& aErrorHandler );
  386. /**
  387. * Check for overlapping, different-numbered, non-net-tie pads.
  388. *
  389. * @param aErrorHandler callback to handle the error messages generated
  390. */
  391. void CheckShortingPads( const std::function<void( const PAD*, const PAD*, int aErrorCode,
  392. const VECTOR2I& )>& aErrorHandler );
  393. /**
  394. * Check for un-allowed shorting of pads in net-tie footprints. If two pads are shorted,
  395. * they must both appear in one of the allowed-shorting lists.
  396. *
  397. * @param aErrorHandler callback to handle the error messages generated
  398. */
  399. void CheckNetTies( const std::function<void( const BOARD_ITEM* aItem,
  400. const BOARD_ITEM* bItem,
  401. const BOARD_ITEM* cItem,
  402. const VECTOR2I& )>& aErrorHandler );
  403. /**
  404. * Sanity check net-tie pad groups. Pads cannot be listed more than once, and pad numbers
  405. * must correspond to a pad.
  406. *
  407. * @param aErrorHandler callback to handle the error messages generated
  408. */
  409. void CheckNetTiePadGroups( const std::function<void( const wxString& )>& aErrorHandler );
  410. void CheckClippedSilk( const std::function<void( BOARD_ITEM* aItemA,
  411. BOARD_ITEM* aItemB,
  412. const VECTOR2I& aPt )>& aErrorHandler );
  413. /**
  414. * Cache the pads that are allowed to connect to each other in the footprint.
  415. */
  416. void BuildNetTieCache();
  417. /**
  418. * Get the set of net codes that are allowed to connect to a footprint item
  419. */
  420. const std::set<int>& GetNetTieCache( const BOARD_ITEM* aItem ) const
  421. {
  422. static const std::set<int> emptySet;
  423. auto it = m_netTieCache.find( aItem );
  424. if( it == m_netTieCache.end() )
  425. return emptySet;
  426. return it->second;
  427. }
  428. /**
  429. * Generate pads shapes on layer \a aLayer as polygons and adds these polygons to
  430. * \a aBuffer.
  431. *
  432. * Useful to generate a polygonal representation of a footprint in 3D view and plot functions,
  433. * when a full polygonal approach is needed.
  434. *
  435. * @param aLayer is the layer to consider, or #UNDEFINED_LAYER to consider all layers.
  436. * @param aBuffer i the buffer to store polygons.
  437. * @param aClearance is an additional size to add to pad shapes.
  438. * @param aMaxError is the maximum deviation from true for arcs.
  439. */
  440. void TransformPadsToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer, int aClearance,
  441. int aMaxError, ERROR_LOC aErrorLoc ) const;
  442. /**
  443. * Generate shapes of graphic items (outlines) on layer \a aLayer as polygons and adds these
  444. * polygons to \a aBuffer.
  445. *
  446. * Useful to generate a polygonal representation of a footprint in 3D view and plot functions,
  447. * when a full polygonal approach is needed.
  448. *
  449. * @param aLayer is the layer to consider, or #UNDEFINED_LAYER to consider all.
  450. * @param aBuffer is the buffer to store polygons.
  451. * @param aClearance is a value to inflate shapes.
  452. * @param aError is the maximum error between true arc and polygon approximation.
  453. * @param aIncludeText set to true to transform text shapes.
  454. * @param aIncludeShapes set to true to transform footprint shapes.
  455. */
  456. void TransformFPShapesToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer, int aClearance,
  457. int aError, ERROR_LOC aErrorLoc,
  458. bool aIncludeText = true,
  459. bool aIncludeShapes = true,
  460. bool aIncludePrivateItems = false ) const;
  461. /**
  462. * This function is the same as TransformFPShapesToPolySet but only generates text.
  463. */
  464. void TransformFPTextToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer, int aClearance,
  465. int aError, ERROR_LOC aErrorLoc ) const
  466. {
  467. TransformFPShapesToPolySet( aBuffer, aLayer, aClearance, aError, aErrorLoc, true, false );
  468. }
  469. /**
  470. * Return the list of system text vars for this footprint.
  471. */
  472. void GetContextualTextVars( wxArrayString* aVars ) const;
  473. /**
  474. * Resolve any references to system tokens supported by the component.
  475. *
  476. * @param aDepth a counter to limit recursion and circular references.
  477. */
  478. bool ResolveTextVar( wxString* token, int aDepth = 0 ) const;
  479. /// @copydoc EDA_ITEM::GetMsgPanelInfo
  480. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  481. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  482. /**
  483. * Test if a point is inside the bounding polygon of the footprint.
  484. *
  485. * The other hit test methods are just checking the bounding box, which can be quite
  486. * inaccurate for rotated or oddly-shaped footprints.
  487. *
  488. * @param aPosition is the point to test
  489. * @return true if aPosition is inside the bounding polygon
  490. */
  491. bool HitTestAccurate( const VECTOR2I& aPosition, int aAccuracy = 0 ) const;
  492. bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
  493. /**
  494. * Test if the point hits one or more of the footprint elements on a given layer.
  495. *
  496. * @param aPosition is the point to test
  497. * @param aAccuracy is the hit test accuracy
  498. * @param aLayer is the layer to test
  499. * @return true if aPosition hits a footprint element on aLayer
  500. */
  501. bool HitTestOnLayer( const VECTOR2I& aPosition, PCB_LAYER_ID aLayer, int aAccuracy = 0 ) const;
  502. bool HitTestOnLayer( const BOX2I& aRect, bool aContained, PCB_LAYER_ID aLayer, int aAccuracy = 0 ) const;
  503. /**
  504. * @return reference designator text.
  505. */
  506. const wxString& GetReference() const { return Reference().GetText(); }
  507. /**
  508. * @param aReference A reference to a wxString object containing the reference designator
  509. * text.
  510. */
  511. void SetReference( const wxString& aReference ) { Reference().SetText( aReference ); }
  512. // Property system doesn't like const references
  513. wxString GetReferenceAsString() const
  514. {
  515. return GetReference();
  516. }
  517. /**
  518. * Bump the current reference by \a aDelta.
  519. */
  520. void IncrementReference( int aDelta );
  521. /**
  522. * @return the value text.
  523. */
  524. const wxString& GetValue() const { return Value().GetText(); }
  525. /**
  526. * @param aValue A reference to a wxString object containing the value text.
  527. */
  528. void SetValue( const wxString& aValue ) { Value().SetText( aValue ); }
  529. // Property system doesn't like const references
  530. wxString GetValueAsString() const
  531. {
  532. return GetValue();
  533. }
  534. /// read/write accessors:
  535. PCB_FIELD& Value() { return *GetField( FIELD_T::VALUE ); }
  536. PCB_FIELD& Reference() { return *GetField( FIELD_T::REFERENCE ); }
  537. /// The const versions to keep the compiler happy.
  538. const PCB_FIELD& Value() const { return *GetField( FIELD_T::VALUE ); }
  539. const PCB_FIELD& Reference() const { return *GetField( FIELD_T::REFERENCE ); }
  540. //-----<Fields>-----------------------------------------------------------
  541. /**
  542. * Return a mandatory field in this footprint. The const version will return nullptr if
  543. * the field doesn't exist; the non-const version will create the field and return it.
  544. */
  545. PCB_FIELD* GetField( FIELD_T aFieldType );
  546. const PCB_FIELD* GetField( FIELD_T aFieldNdx ) const;
  547. /**
  548. * Return a field in this symbol.
  549. *
  550. * @param aFieldName is the name of the field
  551. *
  552. * @return is the field with \a aFieldName or NULL if the field does not exist.
  553. */
  554. PCB_FIELD* GetField( const wxString& aFieldName ) const;
  555. bool HasField( const wxString& aFieldName ) const;
  556. /**
  557. * Populate a std::vector with PCB_TEXTs.
  558. *
  559. * @param aVector is the vector to populate.
  560. * @param aVisibleOnly is used to add only the fields that are visible and contain text.
  561. */
  562. void GetFields( std::vector<PCB_FIELD*>& aVector, bool aVisibleOnly ) const;
  563. /**
  564. * Return a reference to the deque holding the footprint's fields
  565. */
  566. const std::deque<PCB_FIELD*>& GetFields() const { return m_fields; }
  567. std::deque<PCB_FIELD*>& GetFields() { return m_fields; }
  568. /**
  569. * Return the next ordinal for a user field for this footprint
  570. */
  571. int GetNextFieldOrdinal() const;
  572. /**
  573. * @brief Apply default board settings to the footprint field text properties.
  574. *
  575. * This is needed because the board settings are not available when the footprint is
  576. * being created in the footprint library cache, and we want these fields to have
  577. * the correct default text properties.
  578. */
  579. void ApplyDefaultSettings( const BOARD& board, bool aStyleFields, bool aStyleText,
  580. bool aStyleShapes );
  581. bool IsBoardOnly() const { return m_attributes & FP_BOARD_ONLY; }
  582. void SetBoardOnly( bool aIsBoardOnly = true )
  583. {
  584. if( aIsBoardOnly )
  585. m_attributes |= FP_BOARD_ONLY;
  586. else
  587. m_attributes &= ~FP_BOARD_ONLY;
  588. }
  589. bool IsExcludedFromPosFiles() const { return m_attributes & FP_EXCLUDE_FROM_POS_FILES; }
  590. void SetExcludedFromPosFiles( bool aExclude = true )
  591. {
  592. if( aExclude )
  593. m_attributes |= FP_EXCLUDE_FROM_POS_FILES;
  594. else
  595. m_attributes &= ~FP_EXCLUDE_FROM_POS_FILES;
  596. }
  597. bool IsExcludedFromBOM() const { return m_attributes & FP_EXCLUDE_FROM_BOM; }
  598. void SetExcludedFromBOM( bool aExclude = true )
  599. {
  600. if( aExclude )
  601. m_attributes |= FP_EXCLUDE_FROM_BOM;
  602. else
  603. m_attributes &= ~FP_EXCLUDE_FROM_BOM;
  604. }
  605. bool AllowMissingCourtyard() const { return m_attributes & FP_ALLOW_MISSING_COURTYARD; }
  606. void SetAllowMissingCourtyard( bool aAllow = true )
  607. {
  608. if( aAllow )
  609. m_attributes |= FP_ALLOW_MISSING_COURTYARD;
  610. else
  611. m_attributes &= ~FP_ALLOW_MISSING_COURTYARD;
  612. }
  613. bool IsDNP() const { return m_attributes & FP_DNP; }
  614. void SetDNP( bool aDNP = true )
  615. {
  616. if( aDNP )
  617. m_attributes |= FP_DNP;
  618. else
  619. m_attributes &= ~FP_DNP;
  620. }
  621. void SetFileFormatVersionAtLoad( int aVersion ) { m_fileFormatVersionAtLoad = aVersion; }
  622. int GetFileFormatVersionAtLoad() const { return m_fileFormatVersionAtLoad; }
  623. /**
  624. * Return a #PAD with a matching number.
  625. *
  626. * @note Numbers may not be unique depending on how the footprint was created.
  627. *
  628. * @param aPadNumber the pad number to find.
  629. * @param aSearchAfterMe = not nullptr to find a pad living after aAfterMe
  630. * @return the first matching numbered #PAD is returned or NULL if not found.
  631. */
  632. PAD* FindPadByNumber( const wxString& aPadNumber, PAD* aSearchAfterMe = nullptr ) const;
  633. /**
  634. * Get a pad at \a aPosition on \a aLayerMask in the footprint.
  635. *
  636. * @param aPosition A VECTOR2I object containing the position to hit test.
  637. * @param aLayerMask A layer or layers to mask the hit test.
  638. * @return A pointer to a #PAD object if found otherwise NULL.
  639. */
  640. PAD* GetPad( const VECTOR2I& aPosition, LSET aLayerMask = LSET::AllLayersMask() );
  641. std::vector<const PAD*> GetPads( const wxString& aPadNumber,
  642. const PAD* aIgnore = nullptr ) const;
  643. /**
  644. * Return the number of pads.
  645. *
  646. * @param aIncludeNPTH includes non-plated through holes when true. Does not include
  647. * non-plated through holes when false.
  648. * @return the number of pads according to \a aIncludeNPTH.
  649. */
  650. unsigned GetPadCount( INCLUDE_NPTH_T aIncludeNPTH = INCLUDE_NPTH_T(INCLUDE_NPTH) ) const;
  651. /**
  652. * Return the number of unique non-blank pads.
  653. *
  654. * A complex pad can be built with many pads having the same pad name to create a complex
  655. * shape or fragmented solder paste areas.
  656. *
  657. * @param aIncludeNPTH includes non-plated through holes when true. Does not include
  658. * non-plated through holes when false.
  659. * @return the number of unique pads according to \a aIncludeNPTH.
  660. */
  661. unsigned GetUniquePadCount( INCLUDE_NPTH_T aIncludeNPTH = INCLUDE_NPTH_T(INCLUDE_NPTH) ) const;
  662. /**
  663. * Return the names of the unique, non-blank pads.
  664. */
  665. std::set<wxString>
  666. GetUniquePadNumbers( INCLUDE_NPTH_T aIncludeNPTH = INCLUDE_NPTH_T(INCLUDE_NPTH) ) const;
  667. /**
  668. * Return the next available pad number in the footprint.
  669. *
  670. * @param aFillSequenceGaps true if the numbering should "fill in" gaps in the sequence,
  671. * else return the highest value + 1
  672. * @return the next available pad number
  673. */
  674. wxString GetNextPadNumber( const wxString& aLastPadName ) const;
  675. /**
  676. * Position Reference and Value fields at the top and bottom of footprint's bounding box.
  677. */
  678. void AutoPositionFields();
  679. /**
  680. * Get the type of footprint
  681. * @return "SMD"/"Through hole"/"Other" based on attributes
  682. */
  683. wxString GetTypeName() const;
  684. double GetArea( int aPadding = 0 ) const;
  685. KIID GetLink() const { return m_link; }
  686. void SetLink( const KIID& aLink ) { m_link = aLink; }
  687. BOARD_ITEM* Duplicate() const override;
  688. /**
  689. * Duplicate a given item within the footprint, optionally adding it to the board.
  690. *
  691. * @return the new item, or NULL if the item could not be duplicated.
  692. */
  693. BOARD_ITEM* DuplicateItem( const BOARD_ITEM* aItem, bool aAddToFootprint = false );
  694. /**
  695. * Add \a a3DModel definition to the end of the 3D model list.
  696. *
  697. * @param a3DModel A pointer to a #FP_3DMODEL to add to the list.
  698. */
  699. void Add3DModel( FP_3DMODEL* a3DModel );
  700. INSPECT_RESULT Visit( INSPECTOR inspector, void* testData,
  701. const std::vector<KICAD_T>& aScanTypes ) override;
  702. wxString GetClass() const override
  703. {
  704. return wxT( "FOOTPRINT" );
  705. }
  706. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
  707. BITMAPS GetMenuImage() const override;
  708. EDA_ITEM* Clone() const override;
  709. ///< @copydoc BOARD_ITEM::RunOnChildren
  710. void RunOnChildren( const std::function<void (BOARD_ITEM*)>& aFunction ) const override;
  711. ///< @copydoc BOARD_ITEM::RunOnDescendants
  712. void RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction,
  713. int aDepth = 0 ) const override;
  714. virtual std::vector<int> ViewGetLayers() const override;
  715. double ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const override;
  716. virtual const BOX2I ViewBBox() const override;
  717. /**
  718. * Test for validity of a name of a footprint to be used in a footprint library
  719. * ( no spaces, dir separators ... ).
  720. *
  721. * @param aName is the name in library to validate.
  722. * @return true if the given name is valid
  723. */
  724. static bool IsLibNameValid( const wxString& aName );
  725. /**
  726. * Test for validity of the name in a library of the footprint ( no spaces, dir
  727. * separators ... ).
  728. *
  729. * @param aUserReadable set to false to get the list of invalid characters or true to get
  730. * a readable form (i.e ' ' = 'space' '\\t'= 'tab').
  731. *
  732. * @return the list of invalid chars in the library name.
  733. */
  734. static const wxChar* StringLibNameInvalidChars( bool aUserReadable );
  735. /**
  736. * Return true if a board footprint differs from the library version.
  737. */
  738. bool FootprintNeedsUpdate( const FOOTPRINT* aLibFP, int aCompareFlags = 0,
  739. REPORTER* aReporter = nullptr );
  740. /**
  741. * Take ownership of caller's heap allocated aInitialComments block.
  742. *
  743. * The comments are single line strings already containing the s-expression comments with
  744. * optional leading whitespace and then a '#' character followed by optional single line
  745. * text (text with no line endings, not even one). This block of single line comments
  746. * will be output upfront of any generated s-expression text in the PCBIO::Format() function.
  747. *
  748. * @note A block of single line comments constitutes a multiline block of single line
  749. * comments. That is, the block is made of consecutive single line comments.
  750. *
  751. * @param aInitialComments is a heap allocated wxArrayString or NULL, which the caller
  752. * gives up ownership of over to this FOOTPRINT.
  753. */
  754. void SetInitialComments( wxArrayString* aInitialComments )
  755. {
  756. delete m_initial_comments;
  757. m_initial_comments = aInitialComments;
  758. }
  759. /**
  760. * Calculate the ratio of total area of the footprint pads and graphical items to the
  761. * area of the footprint. Used by selection tool heuristics.
  762. *
  763. * @return the ratio.
  764. */
  765. double CoverageRatio( const GENERAL_COLLECTOR& aCollector ) const;
  766. static double GetCoverageArea( const BOARD_ITEM* aItem, const GENERAL_COLLECTOR& aCollector );
  767. /// Return the initial comments block or NULL if none, without transfer of ownership.
  768. const wxArrayString* GetInitialComments() const { return m_initial_comments; }
  769. /**
  770. * Used in DRC to test the courtyard area (a complex polygon).
  771. *
  772. * @return the courtyard polygon.
  773. */
  774. const SHAPE_POLY_SET& GetCourtyard( PCB_LAYER_ID aLayer ) const;
  775. /**
  776. * Return the cached courtyard area. No checks are performed.
  777. *
  778. * @return the cached courtyard polygon.
  779. */
  780. const SHAPE_POLY_SET& GetCachedCourtyard( PCB_LAYER_ID aLayer ) const;
  781. /**
  782. * Build complex polygons of the courtyard areas from graphic items on the courtyard layers.
  783. *
  784. * @note Set the #MALFORMED_F_COURTYARD and #MALFORMED_B_COURTYARD status flags if the given
  785. * courtyard layer does not contain a (single) closed shape.
  786. */
  787. void BuildCourtyardCaches( OUTLINE_ERROR_HANDLER* aErrorHandler = nullptr );
  788. // @copydoc BOARD_ITEM::GetEffectiveShape
  789. std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
  790. FLASHING aFlash = FLASHING::DEFAULT ) const override;
  791. EMBEDDED_FILES* GetEmbeddedFiles() override
  792. {
  793. return static_cast<EMBEDDED_FILES*>( this );
  794. }
  795. const EMBEDDED_FILES* GetEmbeddedFiles() const
  796. {
  797. return static_cast<const EMBEDDED_FILES*>( this );
  798. }
  799. /**
  800. * Get a list of outline fonts referenced in the footprint
  801. */
  802. std::set<KIFONT::OUTLINE_FONT*> GetFonts() const override;
  803. void EmbedFonts() override;
  804. double Similarity( const BOARD_ITEM& aOther ) const override;
  805. /// Sets the component class object pointer for this footprint
  806. void SetStaticComponentClass( const COMPONENT_CLASS* aClass ) const;
  807. /// Returns the component class for this footprint
  808. const COMPONENT_CLASS* GetStaticComponentClass() const;
  809. /// Returns the component class for this footprint
  810. const COMPONENT_CLASS* GetComponentClass() const;
  811. /// Used for display in the properties panel
  812. wxString GetComponentClassAsString() const;
  813. /// Forces immediate recalculation of the component class for this footprint
  814. void RecomputeComponentClass() const;
  815. /// Forces deferred (on next access) recalculation of the component class for this footprint
  816. void InvalidateComponentClassCache() const;
  817. /**
  818. * @brief Sets the transient component class names
  819. *
  820. * This is used during paste operations as we can't resolve the component classes immediately
  821. * until we have the true board context once the pasted items have been placed
  822. */
  823. void SetTransientComponentClassNames( const std::unordered_set<wxString>& classNames )
  824. {
  825. m_transientComponentClassNames = classNames;
  826. }
  827. /// Gets the transient component class names
  828. const std::unordered_set<wxString>& GetTransientComponentClassNames()
  829. {
  830. return m_transientComponentClassNames;
  831. }
  832. /// Remove the transient component class names
  833. void ClearTransientComponentClassNames() { m_transientComponentClassNames.clear(); };
  834. /// Resolves a set of component class names to this footprint's actual component class
  835. void ResolveComponentClassNames( BOARD* aBoard,
  836. const std::unordered_set<wxString>& aComponentClassNames );
  837. bool operator==( const BOARD_ITEM& aOther ) const override;
  838. bool operator==( const FOOTPRINT& aOther ) const;
  839. #if defined(DEBUG)
  840. virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  841. #endif
  842. struct cmp_drawings
  843. {
  844. bool operator()( const BOARD_ITEM* itemA, const BOARD_ITEM* itemB ) const;
  845. };
  846. struct cmp_pads
  847. {
  848. bool operator()( const PAD* aFirst, const PAD* aSecond ) const;
  849. };
  850. // TODO(JE) padstacks -- this code isn't used anywhere though
  851. #if 0
  852. struct cmp_padstack
  853. {
  854. bool operator()( const PAD* aFirst, const PAD* aSecond ) const;
  855. };
  856. #endif
  857. struct cmp_zones
  858. {
  859. bool operator()( const ZONE* aFirst, const ZONE* aSecond ) const;
  860. };
  861. protected:
  862. virtual void swapData( BOARD_ITEM* aImage ) override;
  863. void addMandatoryFields();
  864. private:
  865. std::deque<PCB_FIELD*> m_fields; // Fields, mapped by name, owned by pointer
  866. std::deque<BOARD_ITEM*> m_drawings; // Drawings in the footprint, owned by pointer
  867. std::deque<PAD*> m_pads; // Pads, owned by pointer
  868. std::vector<ZONE*> m_zones; // Rule area zones, owned by pointer
  869. std::deque<PCB_GROUP*> m_groups; // Groups, owned by pointer
  870. EDA_ANGLE m_orient; // Orientation
  871. VECTOR2I m_pos; // Position of footprint on the board in internal units.
  872. LIB_ID m_fpid; // The #LIB_ID of the FOOTPRINT.
  873. int m_attributes; // Flag bits (see FOOTPRINT_ATTR_T)
  874. int m_fpStatus; // For autoplace: flags (LOCKED, FIELDS_AUTOPLACED)
  875. int m_fileFormatVersionAtLoad;
  876. // Bounding box caching strategy:
  877. // While we attempt to notice the low-hanging fruit operations and update the bounding boxes
  878. // accordingly, we rely mostly on a "if anything changed then the caches are stale" approach.
  879. // We implement this by having PCB_BASE_FRAME's OnModify() method increment an operation
  880. // counter, and storing that as a timestamp for the various caches.
  881. // This means caches will get regenerated often -- but still far less often than if we had no
  882. // caches at all. The principal opitmization would be to change to dirty flag and make sure
  883. // that any edit that could affect the bounding boxes (including edits to the footprint
  884. // children) marked the bounding boxes dirty. It would definitely be faster -- but also more
  885. // fragile.
  886. mutable BOX2I m_cachedBoundingBox;
  887. mutable int m_boundingBoxCacheTimeStamp;
  888. mutable BOX2I m_cachedTextExcludedBBox;
  889. mutable int m_textExcludedBBoxCacheTimeStamp;
  890. mutable SHAPE_POLY_SET m_cachedHull;
  891. mutable int m_hullCacheTimeStamp;
  892. // A list of pad groups, each of which is allowed to short nets within their group.
  893. // A pad group is a comma-separated list of pad numbers.
  894. std::vector<wxString> m_netTiePadGroups;
  895. // A list of 1:N footprint item to allowed net numbers
  896. std::map<const BOARD_ITEM*, std::set<int>> m_netTieCache;
  897. // Optional overrides
  898. ZONE_CONNECTION m_zoneConnection;
  899. std::optional<int> m_clearance;
  900. std::optional<int> m_solderMaskMargin; // Solder mask margin
  901. std::optional<int> m_solderPasteMargin; // Solder paste margin absolute value
  902. std::optional<double> m_solderPasteMarginRatio; // Solder mask margin ratio of pad size
  903. // The final margin is the sum of these 2 values
  904. wxString m_libDescription; // File name and path for documentation file.
  905. wxString m_keywords; // Search keywords to find footprint in library.
  906. KIID_PATH m_path; // Path to associated symbol ([sheetUUID, .., symbolUUID]).
  907. wxString m_sheetname; // Name of the sheet containing the symbol for this footprint
  908. wxString m_sheetfile; // File of the sheet containing the symbol for this footprint
  909. wxString m_filters; // Footprint filters from symbol
  910. timestamp_t m_lastEditTime;
  911. int m_arflag; // Use to trace ratsnest and auto routing.
  912. KIID m_link; // Temporary logical link used during editing
  913. LSET m_privateLayers; // Layers visible only in the footprint editor
  914. std::vector<FP_3DMODEL> m_3D_Drawings; // 3D models.
  915. wxArrayString* m_initial_comments; // s-expression comments in the footprint,
  916. // lazily allocated only if needed for speed
  917. SHAPE_POLY_SET m_courtyard_cache_front; // Note that a footprint can have both front and back
  918. SHAPE_POLY_SET m_courtyard_cache_back; // courtyards populated.
  919. mutable HASH_128 m_courtyard_cache_front_hash;
  920. mutable HASH_128 m_courtyard_cache_back_hash;
  921. mutable std::mutex m_courtyard_cache_mutex;
  922. std::unordered_set<wxString> m_transientComponentClassNames;
  923. std::unique_ptr<COMPONENT_CLASS_CACHE_PROXY> m_componentClassCacheProxy;
  924. };
  925. #endif // FOOTPRINT_H