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.

470 lines
14 KiB

3 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wandadoo.fr
  5. * Copyright (C) 1992-2023 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 BOARD_ITEM_STRUCT_H
  25. #define BOARD_ITEM_STRUCT_H
  26. #include <eda_item.h>
  27. #include <layer_ids.h>
  28. #include <geometry/geometry_utils.h>
  29. #include <stroke_params.h>
  30. #include <geometry/eda_angle.h>
  31. class BOARD;
  32. class BOARD_DESIGN_SETTINGS;
  33. class BOARD_ITEM_CONTAINER;
  34. class SHAPE_POLY_SET;
  35. class SHAPE_SEGMENT;
  36. class PCB_BASE_FRAME;
  37. class SHAPE;
  38. class PCB_GROUP;
  39. class FOOTPRINT;
  40. namespace KIFONT
  41. {
  42. class METRICS;
  43. }
  44. /**
  45. * Conditionally flashed vias and pads that interact with zones of different priority can be
  46. * very squirrelly.
  47. *
  48. * In particular, when filling a higher-priority zone that does -not- connect to a via/pad, we
  49. * don't know whether or not a lower-priority zone will subsequently connect -- so we can't
  50. * determine clearance because we don't know what the final flashing state will be.
  51. *
  52. * We therefore force the flashing state if the highest-priority zone with the same net -can-
  53. * connect (whether or not it does in the end), and otherwise force the zone-connection state
  54. * to no-connection (even though a lower priority zone -might- have otherwise connected to it.
  55. */
  56. enum ZONE_LAYER_OVERRIDE
  57. {
  58. ZLO_NONE,
  59. ZLO_FORCE_FLASHED,
  60. ZLO_FORCE_NO_ZONE_CONNECTION
  61. };
  62. /**
  63. * A base class for any item which can be embedded within the #BOARD container class, and
  64. * therefore instances of derived classes should only be found in Pcbnew or other programs
  65. * that use class #BOARD and its contents.
  66. */
  67. class BOARD_ITEM : public EDA_ITEM
  68. {
  69. public:
  70. BOARD_ITEM( BOARD_ITEM* aParent, KICAD_T idtype, PCB_LAYER_ID aLayer = F_Cu ) :
  71. EDA_ITEM( aParent, idtype ),
  72. m_layer( aLayer ),
  73. m_isKnockout( false ),
  74. m_isLocked( false ),
  75. m_group( nullptr )
  76. {
  77. }
  78. ~BOARD_ITEM();
  79. void SetParentGroup( PCB_GROUP* aGroup ) { m_group = aGroup; }
  80. PCB_GROUP* GetParentGroup() const { return m_group; }
  81. // Do not create a copy constructor & operator=.
  82. // The ones generated by the compiler are adequate.
  83. int GetX() const
  84. {
  85. VECTOR2I p = GetPosition();
  86. return p.x;
  87. }
  88. int GetY() const
  89. {
  90. VECTOR2I p = GetPosition();
  91. return p.y;
  92. }
  93. /**
  94. * This defaults to the center of the bounding box if not overridden.
  95. *
  96. * @return center point of the item
  97. */
  98. virtual VECTOR2I GetCenter() const
  99. {
  100. return GetBoundingBox().GetCenter();
  101. }
  102. void SetX( int aX )
  103. {
  104. VECTOR2I p( aX, GetY() );
  105. SetPosition( p );
  106. }
  107. void SetY( int aY )
  108. {
  109. VECTOR2I p( GetX(), aY );
  110. SetPosition( p );
  111. }
  112. /**
  113. * Returns information if the object is derived from BOARD_CONNECTED_ITEM.
  114. *
  115. * @return True if the object is of BOARD_CONNECTED_ITEM type, false otherwise.
  116. */
  117. virtual bool IsConnected() const
  118. {
  119. return false;
  120. }
  121. /**
  122. * Return a measure of how likely the other object is to represent the same
  123. * object. The scale runs from 0.0 (definitely different objects) to 1.0 (same)
  124. *
  125. * This is a pure virtual function. Derived classes must implement this.
  126. */
  127. virtual double Similarity( const BOARD_ITEM& aItem ) const = 0;
  128. virtual bool operator==( const BOARD_ITEM& aItem ) const = 0;
  129. /**
  130. * @return true if the object is on any copper layer, false otherwise.
  131. */
  132. virtual bool IsOnCopperLayer() const
  133. {
  134. return IsCopperLayer( GetLayer() );
  135. }
  136. virtual bool HasHole() const
  137. {
  138. return false;
  139. }
  140. virtual bool HasDrilledHole() const
  141. {
  142. return false;
  143. }
  144. /**
  145. * Checks if the given object is tented (its copper shape is covered by solder mask) on a given
  146. * side of the board.
  147. * @param aLayer is the layer to check tenting mode for: F_Cu and F_Mask are treated identically
  148. * as are B_Cu and B_Mask
  149. * @return true if the object is tented on the given side
  150. */
  151. virtual bool IsTented( PCB_LAYER_ID aLayer ) const
  152. {
  153. return false;
  154. }
  155. /**
  156. * A value of wxPoint(0,0) which can be passed to the Draw() functions.
  157. */
  158. static VECTOR2I ZeroOffset;
  159. /**
  160. * Some pad shapes can be complex (rounded/chamfered rectangle), even without considering
  161. * custom shapes. This routine returns a COMPOUND shape (set of simple shapes which make
  162. * up the pad for use with routing, collision determination, etc).
  163. *
  164. * @note This list can contain a SHAPE_SIMPLE (a simple single-outline non-intersecting
  165. * polygon), but should never contain a SHAPE_POLY_SET (a complex polygon consisting of
  166. * multiple outlines and/or holes).
  167. *
  168. * @param aLayer in case of items spanning multiple layers, only the shapes belonging to aLayer
  169. * will be returned. Pass UNDEFINED_LAYER to return shapes for all layers.
  170. * @param aFlash optional parameter allowing a caller to force the pad to be flashed (or not
  171. * flashed) on the current layer (default is to honour the pad's setting and
  172. * the current connections for the given layer).
  173. */
  174. virtual std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
  175. FLASHING aFlash = FLASHING::DEFAULT ) const;
  176. virtual std::shared_ptr<SHAPE_SEGMENT> GetEffectiveHoleShape() const;
  177. /**
  178. * Invoke a function on all children.
  179. * @note This function should not add or remove items to the parent.
  180. */
  181. virtual void RunOnChildren( const std::function<void ( BOARD_ITEM* )>& aFunction ) const { }
  182. /**
  183. * Invoke a function on all descendants.
  184. * @note This function should not add or remove items.
  185. */
  186. virtual void RunOnDescendants( const std::function<void ( BOARD_ITEM* )>& aFunction,
  187. int aDepth = 0 ) const { }
  188. BOARD_ITEM_CONTAINER* GetParent() const { return (BOARD_ITEM_CONTAINER*) m_parent; }
  189. FOOTPRINT* GetParentFootprint() const;
  190. VECTOR2I GetFPRelativePosition() const;
  191. void SetFPRelativePosition( const VECTOR2I& aPos );
  192. /**
  193. * Check if this item has line stoke properties.
  194. *
  195. * @see #STROKE_PARAMS
  196. */
  197. virtual bool HasLineStroke() const { return false; }
  198. virtual STROKE_PARAMS GetStroke() const;
  199. virtual void SetStroke( const STROKE_PARAMS& aStroke );
  200. const KIFONT::METRICS& GetFontMetrics() const;
  201. /**
  202. * Return the primary layer this item is on.
  203. */
  204. virtual PCB_LAYER_ID GetLayer() const { return m_layer; }
  205. /**
  206. * Return a std::bitset of all layers on which the item physically resides.
  207. */
  208. virtual LSET GetLayerSet() const
  209. {
  210. if( m_layer == UNDEFINED_LAYER )
  211. return LSET();
  212. else
  213. return LSET( m_layer );
  214. }
  215. virtual void SetLayerSet( LSET aLayers )
  216. {
  217. if( aLayers.count() == 1 )
  218. {
  219. SetLayer( aLayers.Seq()[0] );
  220. return;
  221. }
  222. wxFAIL_MSG( wxT( "Attempted to SetLayerSet() on a single-layer object." ) );
  223. // Derived classes which support multiple layers must implement this
  224. }
  225. /**
  226. * Set the layer this item is on.
  227. *
  228. * This method is virtual because some items (in fact: class DIMENSION)
  229. * have a slightly different initialization.
  230. *
  231. * @param aLayer The layer number.
  232. */
  233. virtual void SetLayer( PCB_LAYER_ID aLayer )
  234. {
  235. m_layer = aLayer;
  236. }
  237. /**
  238. * Create a copy of this #BOARD_ITEM.
  239. */
  240. virtual BOARD_ITEM* Duplicate() const;
  241. /**
  242. * Swap data between \a aItem and \a aImage.
  243. *
  244. * \a aItem and \a aImage should have the same type.
  245. *
  246. * Used in undo and redo commands to swap values between an item and its copy.
  247. * Only values like layer, size .. which are modified by editing are swapped.
  248. *
  249. * @param aImage the item image which contains data to swap.
  250. */
  251. void SwapItemData( BOARD_ITEM* aImage );
  252. /**
  253. * Test to see if this object is on the given layer.
  254. *
  255. * Virtual so objects like #PAD, which reside on multiple layers can do their own form
  256. * of testing.
  257. *
  258. * @param aLayer The layer to test for.
  259. * @return true if on given layer, else false.
  260. */
  261. virtual bool IsOnLayer( PCB_LAYER_ID aLayer ) const
  262. {
  263. return m_layer == aLayer;
  264. }
  265. virtual bool IsKnockout() const { return m_isKnockout; }
  266. virtual void SetIsKnockout( bool aKnockout ) { m_isKnockout = aKnockout; }
  267. virtual bool IsLocked() const;
  268. virtual void SetLocked( bool aLocked ) { m_isLocked = aLocked; }
  269. virtual void StyleFromSettings( const BOARD_DESIGN_SETTINGS& settings ) { }
  270. /**
  271. * Delete this object after removing from its parent if it has one.
  272. */
  273. void DeleteStructure();
  274. /**
  275. * Move this object.
  276. *
  277. * @param aMoveVector the move vector for this object.
  278. */
  279. virtual void Move( const VECTOR2I& aMoveVector )
  280. {
  281. wxFAIL_MSG( wxT( "virtual BOARD_ITEM::Move called for " ) + GetClass() );
  282. }
  283. /**
  284. * Rotate this object.
  285. *
  286. * @param aRotCentre the rotation center point.
  287. */
  288. virtual void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
  289. /**
  290. * Flip this object, i.e. change the board side for this object.
  291. *
  292. * @param aCentre the rotation point.
  293. * @param aFlipLeftRight mirror across Y axis instead of X (the default).
  294. */
  295. virtual void Flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
  296. /**
  297. * Perform any normalization required after a user rotate and/or flip.
  298. */
  299. virtual void Normalize() {}
  300. /**
  301. * Return the #BOARD in which this #BOARD_ITEM resides, or NULL if none.
  302. */
  303. virtual const BOARD* GetBoard() const;
  304. virtual BOARD* GetBoard();
  305. /**
  306. * For "parent" property.
  307. * @return the parent footprint's ref or the parent item's UUID.
  308. */
  309. wxString GetParentAsString() const;
  310. /**
  311. * Return the name of the PCB layer on which the item resides.
  312. *
  313. * @return the layer name associated with this item.
  314. */
  315. wxString GetLayerName() const;
  316. virtual void ViewGetLayers( int aLayers[], int& aCount ) const override;
  317. /**
  318. * Convert the item shape to a closed polygon. Circles and arcs are approximated by segments.
  319. *
  320. * @param aBuffer a buffer to store the polygon.
  321. * @param aClearance the clearance around the pad.
  322. * @param aError the maximum deviation from true circle.
  323. * @param aErrorLoc should the approximation error be placed outside or inside the polygon?
  324. * @param ignoreLineWidth used for edge cut items where the line width is only
  325. * for visualization.
  326. */
  327. virtual void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  328. int aClearance, int aError, ERROR_LOC aErrorLoc,
  329. bool ignoreLineWidth = false ) const;
  330. enum COMPARE_FLAGS : int
  331. {
  332. DRC = 0x01
  333. };
  334. struct ptr_cmp
  335. {
  336. bool operator() ( const BOARD_ITEM* a, const BOARD_ITEM* b ) const;
  337. };
  338. protected:
  339. /**
  340. * Return a string (to be shown to the user) describing a layer mask.
  341. */
  342. virtual wxString layerMaskDescribe() const;
  343. virtual void swapData( BOARD_ITEM* aImage );
  344. protected:
  345. PCB_LAYER_ID m_layer;
  346. bool m_isKnockout;
  347. bool m_isLocked;
  348. PCB_GROUP* m_group;
  349. };
  350. #ifndef SWIG
  351. DECLARE_ENUM_TO_WXANY( PCB_LAYER_ID );
  352. #endif
  353. /**
  354. * A singleton item of this class is returned for a weak reference that no longer exists.
  355. *
  356. * Its sole purpose is to flag the item as having been deleted.
  357. */
  358. class DELETED_BOARD_ITEM : public BOARD_ITEM
  359. {
  360. public:
  361. DELETED_BOARD_ITEM() :
  362. BOARD_ITEM( nullptr, NOT_USED )
  363. {}
  364. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const override
  365. {
  366. return _( "(Deleted Item)" );
  367. }
  368. wxString GetClass() const override
  369. {
  370. return wxT( "DELETED_BOARD_ITEM" );
  371. }
  372. // pure virtuals:
  373. void SetPosition( const VECTOR2I& ) override {}
  374. VECTOR2I GetPosition() const override { return VECTOR2I( 0, 0 ); }
  375. static DELETED_BOARD_ITEM* GetInstance()
  376. {
  377. static DELETED_BOARD_ITEM* item = nullptr;
  378. if( !item )
  379. item = new DELETED_BOARD_ITEM();
  380. return item;
  381. }
  382. double Similarity( const BOARD_ITEM& aItem ) const override
  383. {
  384. return ( this == &aItem ) ? 1.0 : 0.0;
  385. }
  386. bool operator==( const BOARD_ITEM& aItem ) const override
  387. {
  388. return ( this == &aItem );
  389. }
  390. #if defined(DEBUG)
  391. void Show( int , std::ostream& ) const override {}
  392. #endif
  393. };
  394. #endif /* BOARD_ITEM_STRUCT_H */