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.

996 lines
35 KiB

8 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 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 ZONE_H
  25. #define ZONE_H
  26. #include <mutex>
  27. #include <vector>
  28. #include <map>
  29. #include <gr_basic.h>
  30. #include <board_item.h>
  31. #include <board_connected_item.h>
  32. #include <layer_ids.h>
  33. #include <lset.h>
  34. #include <geometry/shape_poly_set.h>
  35. #include <zone_settings.h>
  36. #include <teardrop/teardrop_types.h>
  37. class LINE_READER;
  38. class PCB_EDIT_FRAME;
  39. class BOARD;
  40. class ZONE;
  41. class MSG_PANEL_ITEM;
  42. /**
  43. * A struct recording the isolated and single-pad islands within a zone. Each array holds
  44. * indexes into the outlines of a SHAPE_POLY_SET for a zone fill on a particular layer.
  45. *
  46. * Isolated outlines are those whose *connectivity cluster* contains no pads. These generate
  47. * DRC violations.
  48. *
  49. * Single-connection outlines are those with a *direct* connection to only a single item. These
  50. * participate in thermal spoke counting as a pad spoke to an *otherwise* unconnected island
  51. * provides no connectivity to the pad.
  52. */
  53. struct ISOLATED_ISLANDS
  54. {
  55. std::vector<int> m_IsolatedOutlines;
  56. std::vector<int> m_SingleConnectionOutlines;
  57. };
  58. /**
  59. * Handle a list of polygons defining a copper zone.
  60. *
  61. * A zone is described by a main polygon, a time stamp, a layer or a layer set, and a net name.
  62. * Other polygons inside the main polygon are holes in the zone.
  63. */
  64. class ZONE : public BOARD_CONNECTED_ITEM
  65. {
  66. public:
  67. ZONE( BOARD_ITEM_CONTAINER* parent );
  68. ZONE( const ZONE& aZone );
  69. ZONE& operator=( const ZONE &aOther );
  70. ~ZONE();
  71. void CopyFrom( const BOARD_ITEM* aOther ) override;
  72. static inline bool ClassOf( const EDA_ITEM* aItem )
  73. {
  74. return aItem && aItem->Type() == PCB_ZONE_T;
  75. }
  76. void Serialize( google::protobuf::Any &aContainer ) const override;
  77. bool Deserialize( const google::protobuf::Any &aContainer ) override;
  78. /**
  79. * Not all ZONEs are *really* BOARD_CONNECTED_ITEMs....
  80. */
  81. bool IsConnected() const override
  82. {
  83. return !GetIsRuleArea();
  84. }
  85. /**
  86. * Copy aZone data to me
  87. */
  88. void InitDataFromSrcInCopyCtor( const ZONE& aZone );
  89. /**
  90. * For rule areas which exclude footprints (and therefore participate in courtyard conflicts
  91. * during move).
  92. */
  93. bool IsConflicting() const;
  94. /**
  95. * @return a VECTOR2I, position of the first point of the outline
  96. */
  97. VECTOR2I GetPosition() const override;
  98. void SetPosition( const VECTOR2I& aPos ) override {}
  99. /**
  100. * @param aPriority is the priority level.
  101. */
  102. void SetAssignedPriority( unsigned aPriority ) { m_priority = aPriority; }
  103. /**
  104. * @return the priority level of this zone.
  105. */
  106. unsigned GetAssignedPriority() const { return m_priority; }
  107. bool HigherPriority( const ZONE* aOther ) const;
  108. bool SameNet( const ZONE* aOther ) const;
  109. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  110. wxString GetFriendlyName() const override;
  111. void SetLayerSet( const LSET& aLayerSet ) override;
  112. virtual LSET GetLayerSet() const override { return m_layerSet; }
  113. /**
  114. * Set the zone to be on the aLayerSet layers and only remove the fill polygons
  115. * from the unused layers, while keeping the fills on the layers in both the old
  116. * and new layer sets.
  117. */
  118. void SetLayerSetAndRemoveUnusedFills( const LSET& aLayerSet );
  119. ZONE_LAYER_PROPERTIES& LayerProperties( PCB_LAYER_ID aLayer )
  120. {
  121. return m_layerProperties[aLayer];
  122. }
  123. const ZONE_LAYER_PROPERTIES& LayerProperties( PCB_LAYER_ID aLayer ) const;
  124. std::map<PCB_LAYER_ID, ZONE_LAYER_PROPERTIES>& LayerProperties() { return m_layerProperties; }
  125. const std::map<PCB_LAYER_ID, ZONE_LAYER_PROPERTIES>& LayerProperties() const
  126. {
  127. return m_layerProperties;
  128. }
  129. void SetLayerProperties( const std::map<PCB_LAYER_ID, ZONE_LAYER_PROPERTIES>& aOther );
  130. const std::optional<VECTOR2I>& HatchingOffset( PCB_LAYER_ID aLayer ) const;
  131. const wxString& GetZoneName() const { return m_zoneName; }
  132. void SetZoneName( const wxString& aName ) { m_zoneName = aName; }
  133. bool Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const override
  134. {
  135. return BOARD_ITEM::Matches( GetZoneName(), aSearchData );
  136. }
  137. /**
  138. * @return the bounding box of the zone outline.
  139. */
  140. const BOX2I GetBoundingBox() const override;
  141. /**
  142. * Used to preload the zone bounding box cache so we don't have to worry about mutex-locking
  143. * it each time.
  144. */
  145. void CacheBoundingBox();
  146. /**
  147. * @return the zone's clearance in internal units.
  148. */
  149. std::optional<int> GetLocalClearance() const override;
  150. void SetLocalClearance( std::optional<int> aClearance ) { m_ZoneClearance = aClearance.value(); }
  151. /**
  152. * Return any local clearances set in the "classic" (ie: pre-rule) system.
  153. *
  154. * @param aSource [out] optionally reports the source as a user-readable string.
  155. * @return the clearance in internal units.
  156. */
  157. std::optional<int> GetLocalClearance( wxString* aSource ) const override
  158. {
  159. if( m_isRuleArea )
  160. return std::optional<int>();
  161. if( aSource )
  162. *aSource = _( "zone" );
  163. return GetLocalClearance();
  164. }
  165. /**
  166. * @return true if this zone is on a copper layer, false if on a technical layer.
  167. */
  168. bool IsOnCopperLayer() const override;
  169. virtual void SetLayer( PCB_LAYER_ID aLayer ) override;
  170. virtual PCB_LAYER_ID GetLayer() const override;
  171. // Return the first layer in GUI sequence.
  172. PCB_LAYER_ID GetFirstLayer() const;
  173. virtual bool IsOnLayer( PCB_LAYER_ID ) const override;
  174. virtual std::vector<int> ViewGetLayers() const override;
  175. double ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const override;
  176. void SetFillMode( ZONE_FILL_MODE aFillMode ) { m_fillMode = aFillMode; }
  177. ZONE_FILL_MODE GetFillMode() const { return m_fillMode; }
  178. void SetThermalReliefGap( int aThermalReliefGap )
  179. {
  180. if( m_thermalReliefGap != aThermalReliefGap )
  181. SetNeedRefill( true );
  182. m_thermalReliefGap = aThermalReliefGap;
  183. }
  184. int GetThermalReliefGap() const { return m_thermalReliefGap; }
  185. int GetThermalReliefGap( PAD* aPad, wxString* aSource = nullptr ) const;
  186. void SetThermalReliefSpokeWidth( int aThermalReliefSpokeWidth )
  187. {
  188. if( m_thermalReliefSpokeWidth != aThermalReliefSpokeWidth )
  189. SetNeedRefill( true );
  190. m_thermalReliefSpokeWidth = aThermalReliefSpokeWidth;
  191. }
  192. int GetThermalReliefSpokeWidth() const { return m_thermalReliefSpokeWidth; }
  193. /**
  194. * Compute the area currently occupied by the zone fill.
  195. *
  196. * @return the currently filled area
  197. */
  198. double CalculateFilledArea();
  199. /**
  200. * Compute the area of the zone outline (not the filled area).
  201. * @return the currently calculated area
  202. */
  203. double CalculateOutlineArea();
  204. /**
  205. * This area is cached from the most recent call to CalculateFilledArea().
  206. *
  207. * @return the filled area
  208. */
  209. double GetFilledArea()
  210. {
  211. return m_area;
  212. }
  213. /**
  214. * This area is cached from the most recent call to CalculateOutlineArea().
  215. *
  216. * @return the outline area
  217. */
  218. double GetOutlineArea()
  219. {
  220. return m_outlinearea;
  221. }
  222. std::mutex& GetLock()
  223. {
  224. return m_lock;
  225. }
  226. int GetFillFlag( PCB_LAYER_ID aLayer )
  227. {
  228. return m_fillFlags.test( aLayer );
  229. }
  230. void SetFillFlag( PCB_LAYER_ID aLayer, bool aFlag ) { m_fillFlags.set( aLayer, aFlag ); }
  231. bool IsFilled() const { return m_isFilled; }
  232. void SetIsFilled( bool isFilled ) { m_isFilled = isFilled; }
  233. bool NeedRefill() const { return m_needRefill; }
  234. void SetNeedRefill( bool aNeedRefill ) { m_needRefill = aNeedRefill; }
  235. ZONE_CONNECTION GetPadConnection() const { return m_PadConnection; }
  236. void SetPadConnection( ZONE_CONNECTION aPadConnection ) { m_PadConnection = aPadConnection; }
  237. int GetMinThickness() const { return m_ZoneMinThickness; }
  238. void SetMinThickness( int aMinThickness )
  239. {
  240. if( m_ZoneMinThickness != aMinThickness
  241. || ( m_fillMode == ZONE_FILL_MODE::HATCH_PATTERN
  242. && ( m_hatchThickness < aMinThickness || m_hatchGap < aMinThickness ) ) )
  243. {
  244. SetNeedRefill( true );
  245. }
  246. m_ZoneMinThickness = aMinThickness;
  247. m_hatchThickness = std::max( m_hatchThickness, aMinThickness );
  248. m_hatchGap = std::max( m_hatchGap, aMinThickness );
  249. }
  250. int GetHatchThickness() const { return m_hatchThickness; }
  251. void SetHatchThickness( int aThickness ) { m_hatchThickness = aThickness; }
  252. int GetHatchGap() const { return m_hatchGap; }
  253. void SetHatchGap( int aStep ) { m_hatchGap = aStep; }
  254. EDA_ANGLE GetHatchOrientation() const { return m_hatchOrientation; }
  255. void SetHatchOrientation( const EDA_ANGLE& aStep ) { m_hatchOrientation = aStep; }
  256. int GetHatchSmoothingLevel() const { return m_hatchSmoothingLevel; }
  257. void SetHatchSmoothingLevel( int aLevel ) { m_hatchSmoothingLevel = aLevel; }
  258. double GetHatchSmoothingValue() const { return m_hatchSmoothingValue; }
  259. void SetHatchSmoothingValue( double aValue ) { m_hatchSmoothingValue = aValue; }
  260. double GetHatchHoleMinArea() const { return m_hatchHoleMinArea; }
  261. void SetHatchHoleMinArea( double aPct ) { m_hatchHoleMinArea = aPct; }
  262. int GetHatchBorderAlgorithm() const { return m_hatchBorderAlgorithm; }
  263. void SetHatchBorderAlgorithm( int aAlgo ) { m_hatchBorderAlgorithm = aAlgo; }
  264. int GetSelectedCorner() const
  265. {
  266. // Transform relative indices to global index
  267. int globalIndex = -1;
  268. if( m_CornerSelection )
  269. m_Poly->GetGlobalIndex( *m_CornerSelection, globalIndex );
  270. return globalIndex;
  271. }
  272. void SetSelectedCorner( int aCorner )
  273. {
  274. SHAPE_POLY_SET::VERTEX_INDEX selectedCorner;
  275. // If the global index of the corner is correct, assign it to m_CornerSelection
  276. if( m_Poly->GetRelativeIndices( aCorner, &selectedCorner ) )
  277. {
  278. if( m_CornerSelection == nullptr )
  279. m_CornerSelection = new SHAPE_POLY_SET::VERTEX_INDEX;
  280. *m_CornerSelection = selectedCorner;
  281. }
  282. else
  283. throw( std::out_of_range( "aCorner-th vertex does not exist" ) );
  284. }
  285. ///
  286. int GetLocalFlags() const { return m_localFlgs; }
  287. void SetLocalFlags( int aFlags ) { m_localFlgs = aFlags; }
  288. SHAPE_POLY_SET* Outline() { return m_Poly; }
  289. const SHAPE_POLY_SET* Outline() const { return m_Poly; }
  290. void SetOutline( SHAPE_POLY_SET* aOutline ) { m_Poly = aOutline; }
  291. // @copydoc BOARD_ITEM::GetEffectiveShape
  292. virtual std::shared_ptr<SHAPE>
  293. GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
  294. FLASHING aFlash = FLASHING::DEFAULT ) const override;
  295. /**
  296. * Test if a point is near an outline edge or a corner of this zone.
  297. *
  298. * @param aPosition the VECTOR2I to test
  299. * @return true if a hit, else false
  300. */
  301. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  302. /**
  303. * Test if the given VECTOR2I is within the bounds of a filled area of this zone.
  304. *
  305. * @param aLayer is the layer to test on
  306. * @param aRefPos A VECTOR2I to test
  307. * @param aAccuracy Expand the distance by which the areas are expanded for the hittest
  308. * @return true if a hit, else false
  309. */
  310. bool HitTestFilledArea( PCB_LAYER_ID aLayer, const VECTOR2I& aRefPos, int aAccuracy = 0 ) const;
  311. /**
  312. * Test if the given point is contained within a cutout of the zone.
  313. *
  314. * @param aRefPos is the point to test
  315. * @param aOutlineIdx is the index of the outline containing the cutout
  316. * @param aHoleIdx is the index of the hole
  317. * @return true if aRefPos is inside a zone cutout
  318. */
  319. bool HitTestCutout( const VECTOR2I& aRefPos, int* aOutlineIdx = nullptr,
  320. int* aHoleIdx = nullptr ) const;
  321. /**
  322. * Some intersecting zones, despite being on the same layer with the same net, cannot be
  323. * merged due to other parameters such as fillet radius. The copper pour will end up
  324. * effectively merged though, so we need to do some calculations with them in mind.
  325. */
  326. void GetInteractingZones( PCB_LAYER_ID aLayer, std::vector<ZONE*>* aSameNetCollidingZones,
  327. std::vector<ZONE*>* aOtherNetIntersectingZones ) const;
  328. /**
  329. * Convert solid areas full shapes to polygon set
  330. * (the full shape is the polygon area with a thick outline)
  331. * Used in 3D view
  332. * Arcs (ends of segments) are approximated by segments
  333. *
  334. * @param aLayer is the layer of the zone to retrieve
  335. * @param aBuffer = a buffer to store the polygons
  336. * @param aError = Maximum error allowed between true arc and polygon approx
  337. */
  338. void TransformSolidAreasShapesToPolygon( PCB_LAYER_ID aLayer, SHAPE_POLY_SET& aBuffer ) const;
  339. /**
  340. * Convert the outlines shape to a polygon with no holes
  341. * inflated (optional) by max( aClearanceValue, the zone clearance)
  342. * (holes are linked to external outline by overlapping segments)
  343. * Used in filling zones calculations
  344. * Circles (vias) and arcs (ends of tracks) are approximated by segments.
  345. *
  346. * @param aBuffer is a buffer to store the polygon
  347. * @param aClearance is the min clearance around outlines
  348. * @param aBoardOutline is the board outline (if a valid one exists; nullptr otherwise)
  349. */
  350. void TransformSmoothedOutlineToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance,
  351. int aError, ERROR_LOC aErrorLoc,
  352. SHAPE_POLY_SET* aBoardOutline ) const;
  353. /**
  354. * Convert the zone shape to a closed polygon
  355. * Used in filling zones calculations
  356. * Circles and arcs are approximated by segments
  357. *
  358. * @param aLayer is the layer of the filled zone to retrieve
  359. * @param aBuffer is a buffer to store the polygon
  360. * @param aClearance is the clearance around the pad
  361. * @param aError is the maximum deviation from true circle
  362. * @param ignoreLineWidth is used for edge cut items where the line width is only for
  363. * visualization
  364. */
  365. void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  366. int aClearance, int aError, ERROR_LOC aErrorLoc,
  367. bool ignoreLineWidth = false ) const override;
  368. /**
  369. * Test if the given VECTOR2I is near a corner.
  370. *
  371. * @param refPos is the VECTOR2I to test.
  372. * @param aAccuracy increase the item bounding box by this amount.
  373. * @param aCornerHit [out, optional] is the index of the closest vertex found when return
  374. * value is true
  375. * @return true if some corner was found to be closer to refPos than aClearance; false
  376. * otherwise.
  377. */
  378. bool HitTestForCorner( const VECTOR2I& refPos, int aAccuracy,
  379. SHAPE_POLY_SET::VERTEX_INDEX* aCornerHit = nullptr ) const;
  380. /**
  381. * Test if the given VECTOR2I is near a segment defined by 2 corners.
  382. *
  383. * @param refPos is the VECTOR2I to test.
  384. * @param aAccuracy increase the item bounding box by this amount.
  385. * @param aCornerHit [out, optional] is the index of the closest vertex found when return
  386. * value is true.
  387. * @return true if some edge was found to be closer to refPos than aClearance.
  388. */
  389. bool HitTestForEdge( const VECTOR2I& refPos, int aAccuracy,
  390. SHAPE_POLY_SET::VERTEX_INDEX* aCornerHit = nullptr ) const;
  391. /**
  392. * @copydoc BOARD_ITEM::HitTest(const BOX2I& aRect, bool aContained, int aAccuracy) const
  393. */
  394. bool HitTest( const BOX2I& aRect, bool aContained = true, int aAccuracy = 0 ) const override;
  395. /**
  396. * Removes the zone filling.
  397. *
  398. * @return true if a previous filling is removed, false if no change (when no filling found).
  399. */
  400. bool UnFill();
  401. /* Geometric transformations: */
  402. /**
  403. * Move the outlines
  404. *
  405. * @param offset is moving vector
  406. */
  407. void Move( const VECTOR2I& offset ) override;
  408. /**
  409. * Move the outline Edge.
  410. *
  411. * @param offset is moving vector
  412. * @param aEdge is start point of the outline edge
  413. */
  414. void MoveEdge( const VECTOR2I& offset, int aEdge );
  415. /**
  416. * Rotate the outlines.
  417. *
  418. * @param aCentre is rot centre
  419. */
  420. void Rotate( const VECTOR2I& aCentre, const EDA_ANGLE& aAngle ) override;
  421. /**
  422. * Flip this object, i.e. change the board side for this object
  423. * (like Mirror() but changes layer).
  424. *
  425. * @param aCentre is the rotation point.
  426. * @param aFlipDirection is the direction of the flip.
  427. */
  428. virtual void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection ) override;
  429. /**
  430. * Mirror the outlines relative to a given horizontal axis the layer is not changed.
  431. *
  432. * @param aMirrorRef is axis position
  433. * @param aFlipDirection is the direction of the flip.
  434. */
  435. void Mirror( const VECTOR2I& aMirrorRef, FLIP_DIRECTION aFlipDirection ) override;
  436. /**
  437. * @return the class name.
  438. */
  439. wxString GetClass() const override
  440. {
  441. return wxT( "ZONE" );
  442. }
  443. /**
  444. * Access to m_Poly parameters
  445. */
  446. int GetNumCorners( void ) const
  447. {
  448. return m_Poly->TotalVertices();
  449. }
  450. /**
  451. * Return an iterator to visit all points of the zone's main outline without holes.
  452. *
  453. * @return an iterator to visit the zone vertices without holes.
  454. */
  455. SHAPE_POLY_SET::ITERATOR Iterate()
  456. {
  457. return m_Poly->Iterate();
  458. }
  459. /**
  460. * Return an iterator to visit all points of the zone's main outline with holes.
  461. *
  462. * @return an iterator to visit the zone vertices with holes.
  463. */
  464. SHAPE_POLY_SET::ITERATOR IterateWithHoles()
  465. {
  466. return m_Poly->IterateWithHoles();
  467. }
  468. /**
  469. * Return an iterator to visit all points of the zone's main outline with holes.
  470. *
  471. * @return an iterator to visit the zone vertices with holes.
  472. */
  473. SHAPE_POLY_SET::CONST_ITERATOR CIterateWithHoles() const
  474. {
  475. return m_Poly->CIterateWithHoles();
  476. }
  477. void RemoveAllContours( void )
  478. {
  479. m_Poly->RemoveAllContours();
  480. }
  481. const VECTOR2I& GetCornerPosition( int aCornerIndex ) const
  482. {
  483. SHAPE_POLY_SET::VERTEX_INDEX index;
  484. // Convert global to relative indices
  485. if( !m_Poly->GetRelativeIndices( aCornerIndex, &index ) )
  486. throw( std::out_of_range( "aCornerIndex-th vertex does not exist" ) );
  487. return m_Poly->CVertex( index );
  488. }
  489. void SetCornerPosition( int aCornerIndex, const VECTOR2I& new_pos )
  490. {
  491. SHAPE_POLY_SET::VERTEX_INDEX relativeIndices;
  492. // Convert global to relative indices
  493. if( m_Poly->GetRelativeIndices( aCornerIndex, &relativeIndices ) )
  494. {
  495. if( m_Poly->CVertex( relativeIndices ).x != new_pos.x
  496. || m_Poly->CVertex( relativeIndices ).y != new_pos.y )
  497. {
  498. SetNeedRefill( true );
  499. m_Poly->SetVertex( relativeIndices, new_pos );
  500. }
  501. }
  502. else
  503. {
  504. throw( std::out_of_range( "aCornerIndex-th vertex does not exist" ) );
  505. }
  506. }
  507. /**
  508. * Create a new hole on the zone; i.e., a new contour on the zone's outline.
  509. */
  510. void NewHole()
  511. {
  512. m_Poly->NewHole();
  513. }
  514. /**
  515. * Add a new corner to the zone outline (to the main outline or a hole)
  516. *
  517. * @param aPosition is the position of the new corner.
  518. * @param aHoleIdx is the index of the hole (-1 for the main outline, >= 0 for hole).
  519. * @param aAllowDuplication is a flag to indicate whether it is allowed to add this corner
  520. * even if it is duplicated.
  521. * @return true if the corner was added, false if error (aHoleIdx > hole count -1)
  522. */
  523. bool AppendCorner( VECTOR2I aPosition, int aHoleIdx, bool aAllowDuplication = false );
  524. ZONE_BORDER_DISPLAY_STYLE GetHatchStyle() const { return m_borderStyle; }
  525. void SetHatchStyle( ZONE_BORDER_DISPLAY_STYLE aStyle ) { m_borderStyle = aStyle; }
  526. bool HasFilledPolysForLayer( PCB_LAYER_ID aLayer ) const
  527. {
  528. return m_FilledPolysList.count( aLayer ) > 0;
  529. }
  530. /**
  531. * @return a reference to the list of filled polygons.
  532. */
  533. const std::shared_ptr<SHAPE_POLY_SET>& GetFilledPolysList( PCB_LAYER_ID aLayer ) const
  534. {
  535. wxASSERT( m_FilledPolysList.count( aLayer ) );
  536. return m_FilledPolysList.at( aLayer );
  537. }
  538. SHAPE_POLY_SET* GetFill( PCB_LAYER_ID aLayer )
  539. {
  540. wxASSERT( m_FilledPolysList.count( aLayer ) );
  541. return m_FilledPolysList.at( aLayer ).get();
  542. }
  543. /**
  544. * Create a list of triangles that "fill" the solid areas used for instance to draw
  545. * these solid areas on OpenGL.
  546. */
  547. void CacheTriangulation( PCB_LAYER_ID aLayer = UNDEFINED_LAYER );
  548. /**
  549. * Set the list of filled polygons.
  550. */
  551. void SetFilledPolysList( PCB_LAYER_ID aLayer, const SHAPE_POLY_SET& aPolysList )
  552. {
  553. m_FilledPolysList[aLayer] = std::make_shared<SHAPE_POLY_SET>( aPolysList );
  554. }
  555. /**
  556. * Check if a given filled polygon is an insulated island.
  557. *
  558. * @param aLayer is the layer to test
  559. * @param aPolyIdx is an index into m_FilledPolysList[aLayer]
  560. * @return true if the given polygon is insulated (i.e. has no net connection)
  561. */
  562. bool IsIsland( PCB_LAYER_ID aLayer, int aPolyIdx ) const;
  563. void SetIsIsland( PCB_LAYER_ID aLayer, int aPolyIdx )
  564. {
  565. m_insulatedIslands[aLayer].insert( aPolyIdx );
  566. }
  567. bool BuildSmoothedPoly( SHAPE_POLY_SET& aSmoothedPoly, PCB_LAYER_ID aLayer,
  568. SHAPE_POLY_SET* aBoardOutline,
  569. SHAPE_POLY_SET* aSmoothedPolyWithApron = nullptr ) const;
  570. void SetCornerSmoothingType( int aType ) { m_cornerSmoothingType = aType; };
  571. int GetCornerSmoothingType() const { return m_cornerSmoothingType; }
  572. void SetCornerRadius( unsigned int aRadius );
  573. unsigned int GetCornerRadius() const { return m_cornerRadius; }
  574. /**
  575. * Remove a cutout from the zone.
  576. *
  577. * @param aOutlineIdx is the zone outline the hole belongs to
  578. * @param aHoleIdx is the hole in the outline to remove
  579. */
  580. void RemoveCutout( int aOutlineIdx, int aHoleIdx );
  581. /**
  582. * Add a polygon to the zone outline.
  583. *
  584. * If the zone outline is empty, this is the main outline. Otherwise it is a hole
  585. * inside the main outline.
  586. */
  587. void AddPolygon( std::vector<VECTOR2I>& aPolygon );
  588. void AddPolygon( const SHAPE_LINE_CHAIN& aPolygon );
  589. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
  590. BITMAPS GetMenuImage() const override;
  591. EDA_ITEM* Clone() const override;
  592. /**
  593. * @return true if the zone is a teardrop area
  594. */
  595. bool IsTeardropArea() const { return m_teardropType != TEARDROP_TYPE::TD_NONE; }
  596. /**
  597. * Set the type of teardrop if the zone is a teardrop area
  598. * for non teardrop area, the type must be TEARDROP_TYPE::TD_NONE
  599. */
  600. void SetTeardropAreaType( TEARDROP_TYPE aType ) { m_teardropType = aType; }
  601. /**
  602. * @return the type of the teardrop ( has meaning only if the zone is a teardrop area)
  603. */
  604. TEARDROP_TYPE GetTeardropAreaType() const { return m_teardropType; }
  605. /**
  606. * Accessor to determine if any keepout parameters are set
  607. */
  608. bool HasKeepoutParametersSet() const
  609. {
  610. return m_doNotAllowTracks || m_doNotAllowVias || m_doNotAllowPads || m_doNotAllowFootprints
  611. || m_doNotAllowZoneFills;
  612. }
  613. /**
  614. * Accessors to parameters used in Rule Area zones:
  615. */
  616. bool GetIsRuleArea() const { return m_isRuleArea; }
  617. void SetIsRuleArea( bool aEnable ) { m_isRuleArea = aEnable; }
  618. bool GetRuleAreaPlacementEnabled() const { return m_ruleAreaPlacementEnabled ; }
  619. void SetRuleAreaPlacementEnabled( bool aEnabled ) { m_ruleAreaPlacementEnabled = aEnabled; }
  620. wxString GetRuleAreaPlacementSource() const { return m_ruleAreaPlacementSource; }
  621. void SetRuleAreaPlacementSource( const wxString& aSource ) { m_ruleAreaPlacementSource = aSource; }
  622. RULE_AREA_PLACEMENT_SOURCE_TYPE GetRuleAreaPlacementSourceType() const
  623. {
  624. return m_ruleAreaPlacementSourceType;
  625. }
  626. void SetRuleAreaPlacementSourceType( RULE_AREA_PLACEMENT_SOURCE_TYPE aType )
  627. {
  628. m_ruleAreaPlacementSourceType = aType;
  629. }
  630. bool GetDoNotAllowZoneFills() const { return m_doNotAllowZoneFills; }
  631. bool GetDoNotAllowVias() const { return m_doNotAllowVias; }
  632. bool GetDoNotAllowTracks() const { return m_doNotAllowTracks; }
  633. bool GetDoNotAllowPads() const { return m_doNotAllowPads; }
  634. bool GetDoNotAllowFootprints() const { return m_doNotAllowFootprints; }
  635. void SetDoNotAllowZoneFills( bool aEnable ) { m_doNotAllowZoneFills = aEnable; }
  636. void SetDoNotAllowVias( bool aEnable ) { m_doNotAllowVias = aEnable; }
  637. void SetDoNotAllowTracks( bool aEnable ) { m_doNotAllowTracks = aEnable; }
  638. void SetDoNotAllowPads( bool aEnable ) { m_doNotAllowPads = aEnable; }
  639. void SetDoNotAllowFootprints( bool aEnable ) { m_doNotAllowFootprints = aEnable; }
  640. ISLAND_REMOVAL_MODE GetIslandRemovalMode() const { return m_islandRemovalMode; }
  641. void SetIslandRemovalMode( ISLAND_REMOVAL_MODE aRemove ) { m_islandRemovalMode = aRemove; }
  642. long long int GetMinIslandArea() const { return m_minIslandArea; }
  643. void SetMinIslandArea( long long int aArea ) { m_minIslandArea = aArea; }
  644. /**
  645. * HatchBorder related methods
  646. */
  647. /**
  648. * @return the zone hatch pitch in iu.
  649. */
  650. int GetBorderHatchPitch() const;
  651. /**
  652. * @return the default hatch pitch in internal units.
  653. */
  654. static int GetDefaultHatchPitch();
  655. /**
  656. * Set all hatch parameters for the zone.
  657. *
  658. * @param aBorderHatchStyle is the style of the hatch, specified as one of HATCH_STYLE
  659. possible values.
  660. * @param aBorderHatchPitch is the hatch pitch in iu.
  661. * @param aRebuildBorderHatch is a flag to indicate whether to re-hatch after having set the
  662. * previous parameters.
  663. */
  664. void SetBorderDisplayStyle( ZONE_BORDER_DISPLAY_STYLE aBorderHatchStyle, int aBorderHatchPitch,
  665. bool aRebuilBorderdHatch );
  666. /**
  667. * Set the hatch pitch parameter for the zone.
  668. *
  669. * @param aPitch is the hatch pitch in iu.
  670. */
  671. void SetBorderHatchPitch( int aPitch );
  672. /**
  673. * Clear the zone's hatch.
  674. */
  675. void UnHatchBorder();
  676. /**
  677. * Compute the hatch lines depending on the hatch parameters and stores it in the zone's
  678. * attribute m_borderHatchLines.
  679. */
  680. void HatchBorder();
  681. const std::vector<SEG>& GetHatchLines() const { return m_borderHatchLines; }
  682. /**
  683. * Build the hash value of m_FilledPolysList, and store it internally in m_filledPolysHash.
  684. * Used in zone filling calculations, to know if m_FilledPolysList is up to date.
  685. */
  686. void BuildHashValue( PCB_LAYER_ID aLayer );
  687. /**
  688. * @return the hash value previously calculated by BuildHashValue().
  689. */
  690. HASH_128 GetHashValue( PCB_LAYER_ID aLayer );
  691. double Similarity( const BOARD_ITEM& aOther ) const override;
  692. bool operator==( const ZONE& aOther ) const;
  693. bool operator==( const BOARD_ITEM& aOther ) const override;
  694. #if defined(DEBUG)
  695. virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  696. void SetFillPoly( PCB_LAYER_ID aLayer, SHAPE_POLY_SET* aPoly )
  697. {
  698. m_FilledPolysList[ aLayer ] = std::make_shared<SHAPE_POLY_SET>( *aPoly );
  699. SetFillFlag( aLayer, true );
  700. }
  701. #endif
  702. protected:
  703. virtual void swapData( BOARD_ITEM* aImage ) override;
  704. protected:
  705. SHAPE_POLY_SET* m_Poly; ///< Outline of the zone.
  706. int m_cornerSmoothingType;
  707. unsigned int m_cornerRadius;
  708. /// An optional unique name for this zone, used for identifying it in DRC checking
  709. wxString m_zoneName;
  710. LSET m_layerSet;
  711. std::map<PCB_LAYER_ID, ZONE_LAYER_PROPERTIES> m_layerProperties;
  712. /* Priority: when a zone outline is inside and other zone, if its priority is higher
  713. * the other zone priority, it will be created inside.
  714. * if priorities are equal, a DRC error is set
  715. */
  716. unsigned m_priority;
  717. /* A zone outline can be a keepout zone.
  718. * It will be never filled, and DRC should test for pads, tracks and vias
  719. */
  720. bool m_isRuleArea;
  721. /**
  722. * Placement rule area data
  723. */
  724. bool m_ruleAreaPlacementEnabled;
  725. RULE_AREA_PLACEMENT_SOURCE_TYPE m_ruleAreaPlacementSourceType;
  726. wxString m_ruleAreaPlacementSource;
  727. /* A zone outline can be a teardrop zone with different rules for priority
  728. * (always bigger priority than copper zones) and never removed from a
  729. * copper zone having the same netcode
  730. */
  731. TEARDROP_TYPE m_teardropType;
  732. /* For keepout zones only:
  733. * what is not allowed inside the keepout ( pads, tracks and vias )
  734. */
  735. bool m_doNotAllowZoneFills;
  736. bool m_doNotAllowVias;
  737. bool m_doNotAllowTracks;
  738. bool m_doNotAllowPads;
  739. bool m_doNotAllowFootprints;
  740. ZONE_CONNECTION m_PadConnection;
  741. int m_ZoneClearance; // Clearance value in internal units.
  742. int m_ZoneMinThickness; // Minimum thickness value in filled areas.
  743. int m_fillVersion; // See BOARD_DESIGN_SETTINGS for version
  744. // differences.
  745. ISLAND_REMOVAL_MODE m_islandRemovalMode;
  746. /**
  747. * When island removal mode is set to AREA, islands below this area will be removed.
  748. * If this value is negative, all islands will be removed.
  749. */
  750. long long int m_minIslandArea;
  751. /** True when a zone was filled, false after deleting the filled areas. */
  752. bool m_isFilled;
  753. /**
  754. * False when a zone was refilled, true after changes in zone params.
  755. * m_needRefill = false does not imply filled areas are up to date, just
  756. * the zone was refilled after edition, and does not need refilling
  757. */
  758. bool m_needRefill;
  759. int m_thermalReliefGap; // Width of the gap in thermal reliefs.
  760. int m_thermalReliefSpokeWidth; // Width of the copper bridge in thermal reliefs.
  761. /**
  762. * How to fill areas:
  763. *
  764. * ZONE_FILL_MODE::POLYGONS => use solid polygons
  765. * ZONE_FILL_MODE::HATCH_PATTERN => use a grid pattern as shape
  766. */
  767. ZONE_FILL_MODE m_fillMode;
  768. int m_hatchThickness; // thickness of lines (if 0 -> solid shape)
  769. int m_hatchGap; // gap between lines (0 -> solid shape
  770. EDA_ANGLE m_hatchOrientation; // orientation of grid lines
  771. int m_hatchSmoothingLevel; // 0 = no smoothing
  772. // 1 = fillet
  773. // 2 = arc low def
  774. // 3 = arc high def
  775. double m_hatchSmoothingValue; // hole chamfer/fillet size (ratio of hole size)
  776. double m_hatchHoleMinArea; // min size before holes are dropped (ratio)
  777. int m_hatchBorderAlgorithm; // 0 = use min zone thickness
  778. // 1 = use hatch thickness
  779. /// The index of the corner being moved or nullptr if no corner is selected.
  780. SHAPE_POLY_SET::VERTEX_INDEX* m_CornerSelection;
  781. int m_localFlgs; // Variable used in polygon calculations.
  782. /* set of filled polygons used to draw a zone as a filled area.
  783. * from outlines (m_Poly) but unlike m_Poly these filled polygons have no hole
  784. * (they are all in one piece) In very simple cases m_FilledPolysList is same
  785. * as m_Poly. In less simple cases (when m_Poly has holes) m_FilledPolysList is
  786. * a polygon equivalent to m_Poly, without holes but with extra outline segment
  787. * connecting "holes" with external main outline. In complex cases an outline
  788. * described by m_Poly can have many filled areas
  789. */
  790. std::map<PCB_LAYER_ID, std::shared_ptr<SHAPE_POLY_SET>> m_FilledPolysList;
  791. /// Temp variables used while filling
  792. LSET m_fillFlags;
  793. /// A hash value used in zone filling calculations to see if the filled areas are up to date
  794. std::map<PCB_LAYER_ID, HASH_128> m_filledPolysHash;
  795. ZONE_BORDER_DISPLAY_STYLE m_borderStyle; // border display style, see enum above
  796. int m_borderHatchPitch; // for DIAGONAL_EDGE, distance between 2 lines
  797. std::vector<SEG> m_borderHatchLines; // hatch lines
  798. /// For each layer, a set of insulated islands that were not removed
  799. std::map<PCB_LAYER_ID, std::set<int>> m_insulatedIslands;
  800. double m_area; // The filled zone area
  801. double m_outlinearea; // The outline zone area
  802. /// Lock used for multi-threaded filling on multi-layer zones
  803. std::mutex m_lock;
  804. };
  805. #ifndef SWIG
  806. DECLARE_ENUM_TO_WXANY( ZONE_CONNECTION )
  807. DECLARE_ENUM_TO_WXANY( ZONE_FILL_MODE )
  808. DECLARE_ENUM_TO_WXANY( ISLAND_REMOVAL_MODE )
  809. DECLARE_ENUM_TO_WXANY( RULE_AREA_PLACEMENT_SOURCE_TYPE )
  810. #endif
  811. #endif // ZONE_H