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.

1240 lines
36 KiB

6 years ago
6 years ago
14 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <bitmaps.h>
  26. #include <fctsys.h>
  27. #include <geometry/geometry_utils.h>
  28. #include <kicad_string.h>
  29. #include <macros.h>
  30. #include <msgpanel.h>
  31. #include <pcb_base_frame.h>
  32. #include <pcb_screen.h>
  33. #include <richio.h>
  34. #include <trigo.h>
  35. #include <convert_to_biu.h>
  36. #include <class_board.h>
  37. #include <class_zone.h>
  38. #include <pcbnew.h>
  39. #include <zones.h>
  40. #include <math_for_graphics.h>
  41. #include <geometry/polygon_test_point_inside.h>
  42. #include <math/util.h> // for KiROUND
  43. #include <pgm_base.h>
  44. #include <settings/color_settings.h>
  45. #include <settings/settings_manager.h>
  46. ZONE_CONTAINER::ZONE_CONTAINER( BOARD_ITEM_CONTAINER* aParent, bool aInModule )
  47. : BOARD_CONNECTED_ITEM( aParent, aInModule ? PCB_MODULE_ZONE_AREA_T : PCB_ZONE_AREA_T ),
  48. m_area( 0.0 )
  49. {
  50. m_CornerSelection = nullptr; // no corner is selected
  51. m_IsFilled = false; // fill status : true when the zone is filled
  52. m_FillMode = ZONE_FILL_MODE::POLYGONS;
  53. m_hatchStyle = ZONE_HATCH_STYLE::DIAGONAL_EDGE;
  54. m_hatchPitch = GetDefaultHatchPitch();
  55. m_hv45 = false;
  56. m_HatchFillTypeThickness = 0;
  57. m_HatchFillTypeGap = 0;
  58. m_HatchFillTypeOrientation = 0.0;
  59. m_HatchFillTypeSmoothingLevel = 0; // Grid pattern smoothing type. 0 = no smoothing
  60. m_HatchFillTypeSmoothingValue = 0.1; // Grid pattern chamfer value relative to the gap value
  61. // used only if m_HatchFillTypeSmoothingLevel > 0
  62. m_priority = 0;
  63. m_cornerSmoothingType = ZONE_SETTINGS::SMOOTHING_NONE;
  64. SetIsKeepout( aInModule ? true : false ); // Zones living in modules have the keepout option.
  65. SetDoNotAllowCopperPour( false ); // has meaning only if m_isKeepout == true
  66. SetDoNotAllowVias( true ); // has meaning only if m_isKeepout == true
  67. SetDoNotAllowTracks( true ); // has meaning only if m_isKeepout == true
  68. SetDoNotAllowPads( true ); // has meaning only if m_isKeepout == true
  69. SetDoNotAllowFootprints( false ); // has meaning only if m_isKeepout == true
  70. m_cornerRadius = 0;
  71. SetLocalFlags( 0 ); // flags tempoarry used in zone calculations
  72. m_Poly = new SHAPE_POLY_SET(); // Outlines
  73. m_FilledPolysUseThickness = true; // set the "old" way to build filled polygon areas (before 6.0.x)
  74. aParent->GetZoneSettings().ExportSetting( *this );
  75. m_needRefill = false; // True only after some edition.
  76. }
  77. ZONE_CONTAINER::ZONE_CONTAINER( const ZONE_CONTAINER& aZone )
  78. : BOARD_CONNECTED_ITEM( aZone.GetParent(), PCB_ZONE_AREA_T )
  79. {
  80. initDataFromSrcInCopyCtor( aZone );
  81. }
  82. ZONE_CONTAINER& ZONE_CONTAINER::operator=( const ZONE_CONTAINER& aOther )
  83. {
  84. BOARD_CONNECTED_ITEM::operator=( aOther );
  85. // Replace the outlines for aOther outlines.
  86. delete m_Poly;
  87. m_Poly = new SHAPE_POLY_SET( *aOther.m_Poly );
  88. m_isKeepout = aOther.m_isKeepout;
  89. m_CornerSelection = nullptr; // for corner moving, corner index to (null if no selection)
  90. m_ZoneClearance = aOther.m_ZoneClearance; // clearance value
  91. m_ZoneMinThickness = aOther.m_ZoneMinThickness;
  92. m_FilledPolysUseThickness = aOther.m_FilledPolysUseThickness;
  93. m_FillMode = aOther.m_FillMode; // filling mode (segments/polygons)
  94. m_PadConnection = aOther.m_PadConnection;
  95. m_ThermalReliefGap = aOther.m_ThermalReliefGap;
  96. m_ThermalReliefCopperBridge = aOther.m_ThermalReliefCopperBridge;
  97. SetHatchStyle( aOther.GetHatchStyle() );
  98. SetHatchPitch( aOther.GetHatchPitch() );
  99. m_HatchLines = aOther.m_HatchLines; // copy vector <SEG>
  100. m_FilledPolysList.RemoveAllContours();
  101. m_FilledPolysList.Append( aOther.m_FilledPolysList );
  102. m_FillSegmList.clear();
  103. m_FillSegmList = aOther.m_FillSegmList;
  104. m_HatchFillTypeThickness = aOther.m_HatchFillTypeThickness;
  105. m_HatchFillTypeGap = aOther.m_HatchFillTypeGap;
  106. m_HatchFillTypeOrientation = aOther.m_HatchFillTypeOrientation;
  107. m_HatchFillTypeSmoothingLevel = aOther.m_HatchFillTypeSmoothingLevel;
  108. m_HatchFillTypeSmoothingValue = aOther.m_HatchFillTypeSmoothingValue;
  109. SetLayerSet( aOther.GetLayerSet() );
  110. return *this;
  111. }
  112. ZONE_CONTAINER::~ZONE_CONTAINER()
  113. {
  114. delete m_Poly;
  115. delete m_CornerSelection;
  116. }
  117. void ZONE_CONTAINER::initDataFromSrcInCopyCtor( const ZONE_CONTAINER& aZone )
  118. {
  119. // members are expected non initialize in this.
  120. // initDataFromSrcInCopyCtor() is expected to be called
  121. // only from a copy constructor.
  122. // Copy only useful EDA_ITEM flags:
  123. m_Flags = aZone.m_Flags;
  124. m_forceVisible = aZone.m_forceVisible;
  125. m_isKeepout = aZone.m_isKeepout;
  126. SetLayerSet( aZone.GetLayerSet() );
  127. m_Poly = new SHAPE_POLY_SET( *aZone.m_Poly );
  128. // For corner moving, corner index to drag, or nullptr if no selection
  129. m_CornerSelection = nullptr;
  130. m_IsFilled = aZone.m_IsFilled;
  131. m_ZoneClearance = aZone.m_ZoneClearance; // clearance value
  132. m_ZoneMinThickness = aZone.m_ZoneMinThickness;
  133. m_FilledPolysUseThickness = aZone.m_FilledPolysUseThickness;
  134. m_FillMode = aZone.m_FillMode; // Filling mode (segments/polygons)
  135. m_hv45 = aZone.m_hv45;
  136. m_priority = aZone.m_priority;
  137. m_PadConnection = aZone.m_PadConnection;
  138. m_ThermalReliefGap = aZone.m_ThermalReliefGap;
  139. m_ThermalReliefCopperBridge = aZone.m_ThermalReliefCopperBridge;
  140. m_FilledPolysList.Append( aZone.m_FilledPolysList );
  141. m_FillSegmList = aZone.m_FillSegmList; // vector <> copy
  142. m_doNotAllowCopperPour = aZone.m_doNotAllowCopperPour;
  143. m_doNotAllowVias = aZone.m_doNotAllowVias;
  144. m_doNotAllowTracks = aZone.m_doNotAllowTracks;
  145. m_doNotAllowPads = aZone.m_doNotAllowPads;
  146. m_doNotAllowFootprints = aZone.m_doNotAllowFootprints;
  147. m_cornerSmoothingType = aZone.m_cornerSmoothingType;
  148. m_cornerRadius = aZone.m_cornerRadius;
  149. m_hatchStyle = aZone.m_hatchStyle;
  150. m_hatchPitch = aZone.m_hatchPitch;
  151. m_HatchLines = aZone.m_HatchLines;
  152. m_HatchFillTypeThickness = aZone.m_HatchFillTypeThickness;
  153. m_HatchFillTypeGap = aZone.m_HatchFillTypeGap;
  154. m_HatchFillTypeOrientation = aZone.m_HatchFillTypeOrientation;
  155. m_HatchFillTypeSmoothingLevel = aZone.m_HatchFillTypeSmoothingLevel;
  156. m_HatchFillTypeSmoothingValue = aZone.m_HatchFillTypeSmoothingValue;
  157. SetLocalFlags( aZone.GetLocalFlags() );
  158. // Now zone type and layer are set, transfer net info
  159. // (has meaning only for copper zones)
  160. m_netinfo = aZone.m_netinfo;
  161. m_area = aZone.m_area;
  162. SetNeedRefill( aZone.NeedRefill() );
  163. }
  164. EDA_ITEM* ZONE_CONTAINER::Clone() const
  165. {
  166. return new ZONE_CONTAINER( *this );
  167. }
  168. bool ZONE_CONTAINER::UnFill()
  169. {
  170. bool change = ( !m_FilledPolysList.IsEmpty() || m_FillSegmList.size() > 0 );
  171. m_FilledPolysList.RemoveAllContours();
  172. m_FillSegmList.clear();
  173. m_IsFilled = false;
  174. return change;
  175. }
  176. const wxPoint ZONE_CONTAINER::GetPosition() const
  177. {
  178. return (wxPoint) GetCornerPosition( 0 );
  179. }
  180. PCB_LAYER_ID ZONE_CONTAINER::GetLayer() const
  181. {
  182. return BOARD_ITEM::GetLayer();
  183. }
  184. bool ZONE_CONTAINER::IsOnCopperLayer() const
  185. {
  186. if( GetIsKeepout() )
  187. {
  188. return ( m_layerSet & LSET::AllCuMask() ).count() > 0;
  189. }
  190. else
  191. {
  192. return IsCopperLayer( GetLayer() );
  193. }
  194. }
  195. bool ZONE_CONTAINER::CommonLayerExists( const LSET aLayerSet ) const
  196. {
  197. LSET common = GetLayerSet() & aLayerSet;
  198. return common.count() > 0;
  199. }
  200. void ZONE_CONTAINER::SetLayer( PCB_LAYER_ID aLayer )
  201. {
  202. SetLayerSet( LSET( aLayer ) );
  203. m_Layer = aLayer;
  204. }
  205. void ZONE_CONTAINER::SetLayerSet( LSET aLayerSet )
  206. {
  207. if( GetIsKeepout() )
  208. {
  209. // Keepouts can only exist on copper layers
  210. aLayerSet &= LSET::AllCuMask();
  211. }
  212. if( aLayerSet.count() == 0 )
  213. return;
  214. if( m_layerSet != aLayerSet )
  215. SetNeedRefill( true );
  216. m_layerSet = aLayerSet;
  217. // Set the single layer parameter.
  218. // For keepout zones that can be on many layers, this parameter does not have
  219. // really meaning and is a bit arbitrary if more than one layer is set.
  220. // But many functions are using it.
  221. // So we need to initialize it to a reasonable value.
  222. // Priority is F_Cu then B_Cu then to the first selected layer
  223. m_Layer = aLayerSet.Seq()[0];
  224. if( m_Layer != F_Cu && aLayerSet[B_Cu] )
  225. m_Layer = B_Cu;
  226. }
  227. LSET ZONE_CONTAINER::GetLayerSet() const
  228. {
  229. // TODO - Enable multi-layer zones for all zone types
  230. // not just keepout zones
  231. if( GetIsKeepout() )
  232. {
  233. return m_layerSet;
  234. }
  235. else
  236. {
  237. return LSET( m_Layer );
  238. }
  239. }
  240. void ZONE_CONTAINER::ViewGetLayers( int aLayers[], int& aCount ) const
  241. {
  242. if( GetIsKeepout() )
  243. {
  244. LSEQ layers = m_layerSet.Seq();
  245. for( unsigned int idx = 0; idx < layers.size(); idx++ )
  246. aLayers[idx] = layers[idx];
  247. aCount = layers.size();
  248. }
  249. else
  250. {
  251. aLayers[0] = m_Layer;
  252. aCount = 1;
  253. }
  254. }
  255. bool ZONE_CONTAINER::IsOnLayer( PCB_LAYER_ID aLayer ) const
  256. {
  257. if( GetIsKeepout() )
  258. return m_layerSet.test( aLayer );
  259. return BOARD_ITEM::IsOnLayer( aLayer );
  260. }
  261. const EDA_RECT ZONE_CONTAINER::GetBoundingBox() const
  262. {
  263. auto bb = m_Poly->BBox();
  264. EDA_RECT ret( (wxPoint) bb.GetOrigin(), wxSize( bb.GetWidth(), bb.GetHeight() ) );
  265. return ret;
  266. }
  267. int ZONE_CONTAINER::GetThermalReliefGap( D_PAD* aPad ) const
  268. {
  269. if( aPad == NULL || aPad->GetThermalGap() == 0 )
  270. return m_ThermalReliefGap;
  271. else
  272. return aPad->GetThermalGap();
  273. }
  274. int ZONE_CONTAINER::GetThermalReliefCopperBridge( D_PAD* aPad ) const
  275. {
  276. if( aPad == NULL || aPad->GetThermalWidth() == 0 )
  277. return m_ThermalReliefCopperBridge;
  278. else
  279. return aPad->GetThermalWidth();
  280. }
  281. void ZONE_CONTAINER::SetCornerRadius( unsigned int aRadius )
  282. {
  283. if( m_cornerRadius != aRadius )
  284. SetNeedRefill( true );
  285. m_cornerRadius = aRadius;
  286. }
  287. bool ZONE_CONTAINER::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  288. {
  289. // Normally accuracy is zoom-relative, but for the generic HitTest we just use
  290. // a fixed (small) value.
  291. int accuracy = std::max( aAccuracy, Millimeter2iu( 0.1 ) );
  292. return HitTestForCorner( aPosition, accuracy * 2 ) || HitTestForEdge( aPosition, accuracy );
  293. }
  294. void ZONE_CONTAINER::SetSelectedCorner( const wxPoint& aPosition, int aAccuracy )
  295. {
  296. SHAPE_POLY_SET::VERTEX_INDEX corner;
  297. // If there is some corner to be selected, assign it to m_CornerSelection
  298. if( HitTestForCorner( aPosition, aAccuracy * 2, corner )
  299. || HitTestForEdge( aPosition, aAccuracy, corner ) )
  300. {
  301. if( m_CornerSelection == nullptr )
  302. m_CornerSelection = new SHAPE_POLY_SET::VERTEX_INDEX;
  303. *m_CornerSelection = corner;
  304. }
  305. }
  306. bool ZONE_CONTAINER::HitTestForCorner( const wxPoint& refPos, int aAccuracy,
  307. SHAPE_POLY_SET::VERTEX_INDEX& aCornerHit ) const
  308. {
  309. return m_Poly->CollideVertex( VECTOR2I( refPos ), aCornerHit, aAccuracy );
  310. }
  311. bool ZONE_CONTAINER::HitTestForCorner( const wxPoint& refPos, int aAccuracy ) const
  312. {
  313. SHAPE_POLY_SET::VERTEX_INDEX dummy;
  314. return HitTestForCorner( refPos, aAccuracy, dummy );
  315. }
  316. bool ZONE_CONTAINER::HitTestForEdge( const wxPoint& refPos, int aAccuracy,
  317. SHAPE_POLY_SET::VERTEX_INDEX& aCornerHit ) const
  318. {
  319. return m_Poly->CollideEdge( VECTOR2I( refPos ), aCornerHit, aAccuracy );
  320. }
  321. bool ZONE_CONTAINER::HitTestForEdge( const wxPoint& refPos, int aAccuracy ) const
  322. {
  323. SHAPE_POLY_SET::VERTEX_INDEX dummy;
  324. return HitTestForEdge( refPos, aAccuracy, dummy );
  325. }
  326. bool ZONE_CONTAINER::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  327. {
  328. // Calculate bounding box for zone
  329. EDA_RECT bbox = GetBoundingBox();
  330. bbox.Normalize();
  331. EDA_RECT arect = aRect;
  332. arect.Normalize();
  333. arect.Inflate( aAccuracy );
  334. if( aContained )
  335. {
  336. return arect.Contains( bbox );
  337. }
  338. else // Test for intersection between aBox and the polygon
  339. // For a polygon, using its bounding box has no sense here
  340. {
  341. // Fast test: if aBox is outside the polygon bounding box, rectangles cannot intersect
  342. if( !arect.Intersects( bbox ) )
  343. return false;
  344. int count = m_Poly->TotalVertices();
  345. for( int ii = 0; ii < count; ii++ )
  346. {
  347. auto vertex = m_Poly->CVertex( ii );
  348. auto vertexNext = m_Poly->CVertex( ( ii + 1 ) % count );
  349. // Test if the point is within the rect
  350. if( arect.Contains( ( wxPoint ) vertex ) )
  351. {
  352. return true;
  353. }
  354. // Test if this edge intersects the rect
  355. if( arect.Intersects( ( wxPoint ) vertex, ( wxPoint ) vertexNext ) )
  356. {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. }
  363. int ZONE_CONTAINER::GetClearance( BOARD_CONNECTED_ITEM* aItem, wxString* aSource ) const
  364. {
  365. // The actual zone clearance is the biggest of the zone netclass clearance
  366. // and the zone clearance setting in the zone properties dialog.
  367. int myClearance = m_ZoneClearance;
  368. if( aSource )
  369. *aSource = _( "zone clearance" );
  370. if( !m_isKeepout ) // Net class has no meaning for a keepout area.
  371. {
  372. NETCLASSPTR myClass = GetNetClass();
  373. if( myClass->GetClearance() > myClearance )
  374. {
  375. myClearance = myClass->GetClearance();
  376. if( aSource )
  377. *aSource = wxString::Format( _( "'%s' netclass clearance" ), myClass->GetName() );
  378. }
  379. }
  380. // Get the final clearance between me and aItem
  381. if( aItem && aItem->GetClearance() > myClearance )
  382. return aItem->GetClearance( nullptr, aSource );
  383. return myClearance;
  384. }
  385. bool ZONE_CONTAINER::HitTestFilledArea( const wxPoint& aRefPos ) const
  386. {
  387. return m_FilledPolysList.Contains( VECTOR2I( aRefPos.x, aRefPos.y ) );
  388. }
  389. bool ZONE_CONTAINER::HitTestCutout( const VECTOR2I& aRefPos, int* aOutlineIdx, int* aHoleIdx ) const
  390. {
  391. // Iterate over each outline polygon in the zone and then iterate over
  392. // each hole it has to see if the point is in it.
  393. for( int i = 0; i < m_Poly->OutlineCount(); i++ )
  394. {
  395. for( int j = 0; j < m_Poly->HoleCount( i ); j++ )
  396. {
  397. if( m_Poly->Hole( i, j ).PointInside( aRefPos ) )
  398. {
  399. if( aOutlineIdx )
  400. *aOutlineIdx = i;
  401. if( aHoleIdx )
  402. *aHoleIdx = j;
  403. return true;
  404. }
  405. }
  406. }
  407. return false;
  408. }
  409. void ZONE_CONTAINER::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  410. {
  411. wxString msg;
  412. msg = _( "Zone Outline" );
  413. // Display Cutout instead of Outline for holes inside a zone
  414. // i.e. when num contour !=0
  415. // Check whether the selected corner is in a hole; i.e., in any contour but the first one.
  416. if( m_CornerSelection != nullptr && m_CornerSelection->m_contour > 0 )
  417. msg << wxT( " " ) << _( "(Cutout)" );
  418. aList.emplace_back( MSG_PANEL_ITEM( _( "Type" ), msg, DARKCYAN ) );
  419. if( GetIsKeepout() )
  420. {
  421. msg.Empty();
  422. if( GetDoNotAllowVias() )
  423. AccumulateDescription( msg, _( "No vias" ) );
  424. if( GetDoNotAllowTracks() )
  425. AccumulateDescription( msg, _( "No tracks" ) );
  426. if( GetDoNotAllowPads() )
  427. AccumulateDescription( msg, _( "No pads" ) );
  428. if( GetDoNotAllowCopperPour() )
  429. AccumulateDescription( msg, _( "No copper zones" ) );
  430. if( GetDoNotAllowFootprints() )
  431. AccumulateDescription( msg, _( "No footprints" ) );
  432. aList.emplace_back( MSG_PANEL_ITEM( _( "Keepout" ), msg, RED ) );
  433. }
  434. else if( IsOnCopperLayer() )
  435. {
  436. if( GetNetCode() >= 0 )
  437. {
  438. NETINFO_ITEM* net = GetNet();
  439. if( net )
  440. msg = UnescapeString( net->GetNetname() );
  441. else // Should not occur
  442. msg = _( "<unknown>" );
  443. }
  444. else // a netcode < 0 is an error
  445. msg = wxT( "<error>" );
  446. aList.emplace_back( MSG_PANEL_ITEM( _( "NetName" ), msg, RED ) );
  447. // Display net code : (useful in test or debug)
  448. msg.Printf( wxT( "%d" ), GetNetCode() );
  449. aList.emplace_back( MSG_PANEL_ITEM( _( "NetCode" ), msg, RED ) );
  450. // Display priority level
  451. msg.Printf( wxT( "%d" ), GetPriority() );
  452. aList.emplace_back( MSG_PANEL_ITEM( _( "Priority" ), msg, BLUE ) );
  453. }
  454. else
  455. {
  456. aList.emplace_back( MSG_PANEL_ITEM( _( "Non Copper Zone" ), wxEmptyString, RED ) );
  457. }
  458. aList.emplace_back( MSG_PANEL_ITEM( _( "Layer" ), GetLayerName(), BROWN ) );
  459. msg.Printf( wxT( "%d" ), (int) m_Poly->TotalVertices() );
  460. aList.emplace_back( MSG_PANEL_ITEM( _( "Vertices" ), msg, BLUE ) );
  461. switch( m_FillMode )
  462. {
  463. case ZONE_FILL_MODE::POLYGONS:
  464. msg = _( "Solid" ); break;
  465. case ZONE_FILL_MODE::HATCH_PATTERN:
  466. msg = _( "Hatched" ); break;
  467. default:
  468. msg = _( "Unknown" ); break;
  469. }
  470. aList.emplace_back( MSG_PANEL_ITEM( _( "Fill Mode" ), msg, BROWN ) );
  471. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_area, false, EDA_DATA_TYPE::AREA );
  472. aList.emplace_back( MSG_PANEL_ITEM( _( "Filled Area" ), msg, BLUE ) );
  473. // Useful for statistics :
  474. msg.Printf( wxT( "%d" ), (int) m_HatchLines.size() );
  475. aList.emplace_back( MSG_PANEL_ITEM( _( "Hatch Lines" ), msg, BLUE ) );
  476. if( !m_FilledPolysList.IsEmpty() )
  477. {
  478. msg.Printf( wxT( "%d" ), m_FilledPolysList.TotalVertices() );
  479. aList.emplace_back( MSG_PANEL_ITEM( _( "Corner Count" ), msg, BLUE ) );
  480. }
  481. }
  482. /* Geometric transforms: */
  483. void ZONE_CONTAINER::Move( const wxPoint& offset )
  484. {
  485. /* move outlines */
  486. m_Poly->Move( offset );
  487. Hatch();
  488. m_FilledPolysList.Move( offset );
  489. for( SEG& seg : m_FillSegmList )
  490. {
  491. seg.A += VECTOR2I( offset );
  492. seg.B += VECTOR2I( offset );
  493. }
  494. }
  495. void ZONE_CONTAINER::MoveEdge( const wxPoint& offset, int aEdge )
  496. {
  497. int next_corner;
  498. if( m_Poly->GetNeighbourIndexes( aEdge, nullptr, &next_corner ) )
  499. {
  500. m_Poly->SetVertex( aEdge, m_Poly->CVertex( aEdge ) + VECTOR2I( offset ) );
  501. m_Poly->SetVertex( next_corner, m_Poly->CVertex( next_corner ) + VECTOR2I( offset ) );
  502. Hatch();
  503. SetNeedRefill( true );
  504. }
  505. }
  506. void ZONE_CONTAINER::Rotate( const wxPoint& centre, double angle )
  507. {
  508. wxPoint pos;
  509. angle = -DECIDEG2RAD( angle );
  510. m_Poly->Rotate( angle, VECTOR2I( centre ) );
  511. Hatch();
  512. /* rotate filled areas: */
  513. m_FilledPolysList.Rotate( angle, VECTOR2I( centre ) );
  514. for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
  515. {
  516. wxPoint a( m_FillSegmList[ic].A );
  517. RotatePoint( &a, centre, angle );
  518. m_FillSegmList[ic].A = a;
  519. wxPoint b( m_FillSegmList[ic].B );
  520. RotatePoint( &b, centre, angle );
  521. m_FillSegmList[ic].B = a;
  522. }
  523. }
  524. void ZONE_CONTAINER::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  525. {
  526. Mirror( aCentre, aFlipLeftRight );
  527. int copperLayerCount = GetBoard()->GetCopperLayerCount();
  528. if( GetIsKeepout() )
  529. {
  530. SetLayerSet( FlipLayerMask( GetLayerSet(), copperLayerCount ) );
  531. }
  532. else
  533. {
  534. SetLayer( FlipLayer( GetLayer(), copperLayerCount ) );
  535. }
  536. }
  537. void ZONE_CONTAINER::Mirror( const wxPoint& aMirrorRef, bool aMirrorLeftRight )
  538. {
  539. // ZONE_CONTAINERs mirror about the x-axis (why?!?)
  540. m_Poly->Mirror( aMirrorLeftRight, !aMirrorLeftRight, VECTOR2I( aMirrorRef ) );
  541. Hatch();
  542. m_FilledPolysList.Mirror( aMirrorLeftRight, !aMirrorLeftRight, VECTOR2I( aMirrorRef ) );
  543. for( SEG& seg : m_FillSegmList )
  544. {
  545. if( aMirrorLeftRight )
  546. {
  547. MIRROR( seg.A.x, aMirrorRef.x );
  548. MIRROR( seg.B.x, aMirrorRef.x );
  549. }
  550. else
  551. {
  552. MIRROR( seg.A.y, aMirrorRef.y );
  553. MIRROR( seg.B.y, aMirrorRef.y );
  554. }
  555. }
  556. }
  557. ZONE_CONNECTION ZONE_CONTAINER::GetPadConnection( D_PAD* aPad ) const
  558. {
  559. if( aPad == NULL || aPad->GetZoneConnection() == ZONE_CONNECTION::INHERITED )
  560. return m_PadConnection;
  561. else
  562. return aPad->GetZoneConnection();
  563. }
  564. void ZONE_CONTAINER::RemoveCutout( int aOutlineIdx, int aHoleIdx )
  565. {
  566. // Ensure the requested cutout is valid
  567. if( m_Poly->OutlineCount() < aOutlineIdx || m_Poly->HoleCount( aOutlineIdx ) < aHoleIdx )
  568. return;
  569. SHAPE_POLY_SET cutPoly( m_Poly->Hole( aOutlineIdx, aHoleIdx ) );
  570. // Add the cutout back to the zone
  571. m_Poly->BooleanAdd( cutPoly, SHAPE_POLY_SET::PM_FAST );
  572. SetNeedRefill( true );
  573. }
  574. void ZONE_CONTAINER::AddPolygon( const SHAPE_LINE_CHAIN& aPolygon )
  575. {
  576. wxASSERT( aPolygon.IsClosed() );
  577. // Add the outline as a new polygon in the polygon set
  578. if( m_Poly->OutlineCount() == 0 )
  579. m_Poly->AddOutline( aPolygon );
  580. else
  581. m_Poly->AddHole( aPolygon );
  582. SetNeedRefill( true );
  583. }
  584. void ZONE_CONTAINER::AddPolygon( std::vector< wxPoint >& aPolygon )
  585. {
  586. if( aPolygon.empty() )
  587. return;
  588. SHAPE_LINE_CHAIN outline;
  589. // Create an outline and populate it with the points of aPolygon
  590. for( unsigned i = 0; i < aPolygon.size(); i++ )
  591. {
  592. outline.Append( VECTOR2I( aPolygon[i] ) );
  593. }
  594. outline.SetClosed( true );
  595. AddPolygon( outline );
  596. }
  597. bool ZONE_CONTAINER::AppendCorner( wxPoint aPosition, int aHoleIdx, bool aAllowDuplication )
  598. {
  599. // Ensure the main outline exists:
  600. if( m_Poly->OutlineCount() == 0 )
  601. m_Poly->NewOutline();
  602. // If aHoleIdx >= 0, the corner musty be added to the hole, index aHoleIdx.
  603. // (remember: the index of the first hole is 0)
  604. // Return error if if does dot exist.
  605. if( aHoleIdx >= m_Poly->HoleCount( 0 ) )
  606. return false;
  607. m_Poly->Append( aPosition.x, aPosition.y, -1, aHoleIdx, aAllowDuplication );
  608. SetNeedRefill( true );
  609. return true;
  610. }
  611. wxString ZONE_CONTAINER::GetSelectMenuText( EDA_UNITS aUnits ) const
  612. {
  613. wxString text;
  614. // Check whether the selected contour is a hole (contour index > 0)
  615. if( m_CornerSelection != nullptr && m_CornerSelection->m_contour > 0 )
  616. text << wxT( " " ) << _( "(Cutout)" );
  617. if( GetIsKeepout() )
  618. text << wxT( " " ) << _( "(Keepout)" );
  619. else
  620. text << GetNetnameMsg();
  621. return wxString::Format( _( "Zone Outline %s on %s" ), text, GetLayerName() );
  622. }
  623. int ZONE_CONTAINER::GetHatchPitch() const
  624. {
  625. return m_hatchPitch;
  626. }
  627. void ZONE_CONTAINER::SetHatch( ZONE_HATCH_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch )
  628. {
  629. SetHatchPitch( aHatchPitch );
  630. m_hatchStyle = aHatchStyle;
  631. if( aRebuildHatch )
  632. Hatch();
  633. }
  634. void ZONE_CONTAINER::SetHatchPitch( int aPitch )
  635. {
  636. m_hatchPitch = aPitch;
  637. }
  638. void ZONE_CONTAINER::UnHatch()
  639. {
  640. m_HatchLines.clear();
  641. }
  642. // Creates hatch lines inside the outline of the complex polygon
  643. // sort function used in ::Hatch to sort points by descending wxPoint.x values
  644. bool sortEndsByDescendingX( const VECTOR2I& ref, const VECTOR2I& tst )
  645. {
  646. return tst.x < ref.x;
  647. }
  648. void ZONE_CONTAINER::Hatch()
  649. {
  650. UnHatch();
  651. if( m_hatchStyle == ZONE_HATCH_STYLE::NO_HATCH || m_hatchPitch == 0 || m_Poly->IsEmpty() )
  652. return;
  653. // define range for hatch lines
  654. int min_x = m_Poly->CVertex( 0 ).x;
  655. int max_x = m_Poly->CVertex( 0 ).x;
  656. int min_y = m_Poly->CVertex( 0 ).y;
  657. int max_y = m_Poly->CVertex( 0 ).y;
  658. for( auto iterator = m_Poly->IterateWithHoles(); iterator; iterator++ )
  659. {
  660. if( iterator->x < min_x )
  661. min_x = iterator->x;
  662. if( iterator->x > max_x )
  663. max_x = iterator->x;
  664. if( iterator->y < min_y )
  665. min_y = iterator->y;
  666. if( iterator->y > max_y )
  667. max_y = iterator->y;
  668. }
  669. // Calculate spacing between 2 hatch lines
  670. int spacing;
  671. if( m_hatchStyle == ZONE_HATCH_STYLE::DIAGONAL_EDGE )
  672. spacing = m_hatchPitch;
  673. else
  674. spacing = m_hatchPitch * 2;
  675. // set the "length" of hatch lines (the length on horizontal axis)
  676. int hatch_line_len = m_hatchPitch;
  677. // To have a better look, give a slope depending on the layer
  678. LAYER_NUM layer = GetLayer();
  679. int slope_flag = (layer & 1) ? 1 : -1; // 1 or -1
  680. double slope = 0.707106 * slope_flag; // 45 degrees slope
  681. int max_a, min_a;
  682. if( slope_flag == 1 )
  683. {
  684. max_a = KiROUND( max_y - slope * min_x );
  685. min_a = KiROUND( min_y - slope * max_x );
  686. }
  687. else
  688. {
  689. max_a = KiROUND( max_y - slope * max_x );
  690. min_a = KiROUND( min_y - slope * min_x );
  691. }
  692. min_a = (min_a / spacing) * spacing;
  693. // calculate an offset depending on layer number,
  694. // for a better look of hatches on a multilayer board
  695. int offset = (layer * 7) / 8;
  696. min_a += offset;
  697. // loop through hatch lines
  698. #define MAXPTS 200 // Usually we store only few values per one hatch line
  699. // depending on the complexity of the zone outline
  700. static std::vector<VECTOR2I> pointbuffer;
  701. pointbuffer.clear();
  702. pointbuffer.reserve( MAXPTS + 2 );
  703. for( int a = min_a; a < max_a; a += spacing )
  704. {
  705. // get intersection points for this hatch line
  706. // Note: because we should have an even number of intersections with the
  707. // current hatch line and the zone outline (a closed polygon,
  708. // or a set of closed polygons), if an odd count is found
  709. // we skip this line (should not occur)
  710. pointbuffer.clear();
  711. // Iterate through all vertices
  712. for( auto iterator = m_Poly->IterateSegmentsWithHoles(); iterator; iterator++ )
  713. {
  714. double x, y, x2, y2;
  715. int ok;
  716. SEG segment = *iterator;
  717. ok = FindLineSegmentIntersection( a, slope,
  718. segment.A.x, segment.A.y,
  719. segment.B.x, segment.B.y,
  720. &x, &y, &x2, &y2 );
  721. if( ok )
  722. {
  723. VECTOR2I point( KiROUND( x ), KiROUND( y ) );
  724. pointbuffer.push_back( point );
  725. }
  726. if( ok == 2 )
  727. {
  728. VECTOR2I point( KiROUND( x2 ), KiROUND( y2 ) );
  729. pointbuffer.push_back( point );
  730. }
  731. if( pointbuffer.size() >= MAXPTS ) // overflow
  732. {
  733. wxASSERT( 0 );
  734. break;
  735. }
  736. }
  737. // ensure we have found an even intersection points count
  738. // because intersections are the ends of segments
  739. // inside the polygon(s) and a segment has 2 ends.
  740. // if not, this is a strange case (a bug ?) so skip this hatch
  741. if( pointbuffer.size() % 2 != 0 )
  742. continue;
  743. // sort points in order of descending x (if more than 2) to
  744. // ensure the starting point and the ending point of the same segment
  745. // are stored one just after the other.
  746. if( pointbuffer.size() > 2 )
  747. sort( pointbuffer.begin(), pointbuffer.end(), sortEndsByDescendingX );
  748. // creates lines or short segments inside the complex polygon
  749. for( unsigned ip = 0; ip < pointbuffer.size(); ip += 2 )
  750. {
  751. int dx = pointbuffer[ip + 1].x - pointbuffer[ip].x;
  752. // Push only one line for diagonal hatch,
  753. // or for small lines < twice the line length
  754. // else push 2 small lines
  755. if( m_hatchStyle == ZONE_HATCH_STYLE::DIAGONAL_FULL
  756. || std::abs( dx ) < 2 * hatch_line_len )
  757. {
  758. m_HatchLines.emplace_back( SEG( pointbuffer[ip], pointbuffer[ip + 1] ) );
  759. }
  760. else
  761. {
  762. double dy = pointbuffer[ip + 1].y - pointbuffer[ip].y;
  763. slope = dy / dx;
  764. if( dx > 0 )
  765. dx = hatch_line_len;
  766. else
  767. dx = -hatch_line_len;
  768. int x1 = KiROUND( pointbuffer[ip].x + dx );
  769. int x2 = KiROUND( pointbuffer[ip + 1].x - dx );
  770. int y1 = KiROUND( pointbuffer[ip].y + dx * slope );
  771. int y2 = KiROUND( pointbuffer[ip + 1].y - dx * slope );
  772. m_HatchLines.emplace_back( SEG( pointbuffer[ip].x, pointbuffer[ip].y, x1, y1 ) );
  773. m_HatchLines.emplace_back( SEG( pointbuffer[ip+1].x, pointbuffer[ip+1].y, x2, y2 ) );
  774. }
  775. }
  776. }
  777. }
  778. int ZONE_CONTAINER::GetDefaultHatchPitch()
  779. {
  780. return Mils2iu( 20 );
  781. }
  782. BITMAP_DEF ZONE_CONTAINER::GetMenuImage() const
  783. {
  784. return add_zone_xpm;
  785. }
  786. void ZONE_CONTAINER::SwapData( BOARD_ITEM* aImage )
  787. {
  788. assert( aImage->Type() == PCB_ZONE_AREA_T );
  789. std::swap( *((ZONE_CONTAINER*) this), *((ZONE_CONTAINER*) aImage) );
  790. }
  791. void ZONE_CONTAINER::CacheTriangulation()
  792. {
  793. m_FilledPolysList.CacheTriangulation();
  794. }
  795. /*
  796. * Some intersecting zones, despite being on the same layer with the same net, cannot be
  797. * merged due to other parameters such as fillet radius. The copper pour will end up
  798. * effectively merged though, so we want to keep the corners of such intersections sharp.
  799. */
  800. void ZONE_CONTAINER::GetColinearCorners( BOARD* aBoard, std::set<VECTOR2I>& aCorners )
  801. {
  802. int epsilon = Millimeter2iu( 0.001 );
  803. // Things get messy when zone of different nets intersect. To do it right we'd need to
  804. // run our colinear test with the final filled regions rather than the outline regions.
  805. // However, since there's no order dependance the only way to do that is to iterate
  806. // through successive zone fills until the results are no longer changing -- and that's
  807. // not going to happen. So we punt and ignore any "messy" corners.
  808. std::set<VECTOR2I> colinearCorners;
  809. std::set<VECTOR2I> messyCorners;
  810. for( ZONE_CONTAINER* candidate : aBoard->Zones() )
  811. {
  812. if( candidate == this )
  813. continue;
  814. if( candidate->GetLayerSet() != GetLayerSet() )
  815. continue;
  816. if( candidate->GetIsKeepout() != GetIsKeepout() )
  817. continue;
  818. for( auto iter = m_Poly->CIterate(); iter; iter++ )
  819. {
  820. if( candidate->m_Poly->Collide( iter.Get(), epsilon ) )
  821. {
  822. if( candidate->GetNetCode() == GetNetCode() )
  823. colinearCorners.insert( VECTOR2I( iter.Get() ) );
  824. else
  825. messyCorners.insert( VECTOR2I( iter.Get() ) );
  826. }
  827. }
  828. }
  829. for( VECTOR2I corner : colinearCorners )
  830. {
  831. if( messyCorners.count( corner ) == 0 )
  832. aCorners.insert( corner );
  833. }
  834. }
  835. bool ZONE_CONTAINER::BuildSmoothedPoly( SHAPE_POLY_SET& aSmoothedPoly,
  836. std::set<VECTOR2I>* aPreserveCorners ) const
  837. {
  838. if( GetNumCorners() <= 2 ) // malformed zone. polygon calculations do not like it ...
  839. return false;
  840. // Make a smoothed polygon out of the user-drawn polygon if required
  841. switch( m_cornerSmoothingType )
  842. {
  843. case ZONE_SETTINGS::SMOOTHING_CHAMFER:
  844. aSmoothedPoly = m_Poly->Chamfer( m_cornerRadius, aPreserveCorners );
  845. break;
  846. case ZONE_SETTINGS::SMOOTHING_FILLET:
  847. {
  848. auto board = GetBoard();
  849. int maxError = ARC_HIGH_DEF;
  850. if( board )
  851. maxError = board->GetDesignSettings().m_MaxError;
  852. aSmoothedPoly = m_Poly->Fillet( m_cornerRadius, maxError, aPreserveCorners );
  853. break;
  854. }
  855. default:
  856. // Acute angles between adjacent edges can create issues in calculations,
  857. // in inflate/deflate outlines transforms, especially when the angle is very small.
  858. // We can avoid issues by creating a very small chamfer which remove acute angles,
  859. // or left it without chamfer and use only CPOLYGONS_LIST::InflateOutline to create
  860. // clearance areas
  861. aSmoothedPoly = m_Poly->Chamfer( Millimeter2iu( 0.0 ), aPreserveCorners );
  862. break;
  863. }
  864. return true;
  865. };
  866. double ZONE_CONTAINER::CalculateFilledArea()
  867. {
  868. m_area = 0.0;
  869. // Iterate over each outline polygon in the zone and then iterate over
  870. // each hole it has to compute the total area.
  871. for( int i = 0; i < m_FilledPolysList.OutlineCount(); i++ )
  872. {
  873. m_area += m_FilledPolysList.Outline( i ).Area();
  874. for( int j = 0; j < m_FilledPolysList.HoleCount( i ); j++ )
  875. {
  876. m_area -= m_FilledPolysList.Hole( i, j ).Area();
  877. }
  878. }
  879. return m_area;
  880. }
  881. /* Function TransformOutlinesShapeWithClearanceToPolygon
  882. * Convert the zone filled areas polygons to polygons
  883. * inflated (optional) by max( aClearanceValue, the zone clearance)
  884. * and copy them in aCornerBuffer
  885. * @param aMinClearanceValue the min clearance around outlines
  886. * @param aUseNetClearance true to use a clearance which is the max value between
  887. * aMinClearanceValue and the net clearance
  888. * false to use aMinClearanceValue only
  889. * @param aPreserveCorners an optional set of corners which should not be chamfered/filleted
  890. */
  891. void ZONE_CONTAINER::TransformOutlinesShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  892. int aMinClearanceValue,
  893. bool aUseNetClearance,
  894. std::set<VECTOR2I>* aPreserveCorners ) const
  895. {
  896. // Creates the zone outline polygon (with holes if any)
  897. SHAPE_POLY_SET polybuffer;
  898. BuildSmoothedPoly( polybuffer, aPreserveCorners );
  899. // add clearance to outline
  900. int clearance = aMinClearanceValue;
  901. if( aUseNetClearance && IsOnCopperLayer() )
  902. {
  903. clearance = GetClearance();
  904. if( aMinClearanceValue > clearance )
  905. clearance = aMinClearanceValue;
  906. }
  907. // Calculate the polygon with clearance
  908. // holes are linked to the main outline, so only one polygon is created.
  909. if( clearance )
  910. {
  911. BOARD* board = GetBoard();
  912. int maxError = ARC_HIGH_DEF;
  913. if( board )
  914. maxError = board->GetDesignSettings().m_MaxError;
  915. int segCount = std::max( GetArcToSegmentCount( clearance, maxError, 360.0 ), 3 );
  916. polybuffer.Inflate( clearance, segCount );
  917. }
  918. polybuffer.Fracture( SHAPE_POLY_SET::PM_FAST );
  919. aCornerBuffer.Append( polybuffer );
  920. }
  921. //
  922. /********* MODULE_ZONE_CONTAINER **************/
  923. //
  924. MODULE_ZONE_CONTAINER::MODULE_ZONE_CONTAINER( BOARD_ITEM_CONTAINER* aParent ) :
  925. ZONE_CONTAINER( aParent, true )
  926. {
  927. // in a footprint, net classes are not managed.
  928. // so set the net to NETINFO_LIST::ORPHANED_ITEM
  929. SetNetCode( -1, true );
  930. }
  931. MODULE_ZONE_CONTAINER::MODULE_ZONE_CONTAINER( const MODULE_ZONE_CONTAINER& aZone )
  932. : ZONE_CONTAINER( aZone.GetParent(), true )
  933. {
  934. initDataFromSrcInCopyCtor( aZone );
  935. }
  936. MODULE_ZONE_CONTAINER& MODULE_ZONE_CONTAINER::operator=( const MODULE_ZONE_CONTAINER& aOther )
  937. {
  938. ZONE_CONTAINER::operator=( aOther );
  939. return *this;
  940. }
  941. EDA_ITEM* MODULE_ZONE_CONTAINER::Clone() const
  942. {
  943. return new MODULE_ZONE_CONTAINER( *this );
  944. }
  945. unsigned int MODULE_ZONE_CONTAINER::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  946. {
  947. const int HIDE = std::numeric_limits<unsigned int>::max();
  948. if( !aView )
  949. return 0;
  950. bool flipped = GetParent() && GetParent()->GetLayer() == B_Cu;
  951. // Handle Render tab switches
  952. if( !flipped && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  953. return HIDE;
  954. if( flipped && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  955. return HIDE;
  956. // Other layers are shown without any conditions
  957. return 0;
  958. }