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.

1258 lines
36 KiB

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_ITEM* aItem, wxString* aSource ) const
  364. {
  365. if( m_isKeepout )
  366. return 0;
  367. BOARD* board = GetBoard();
  368. // No clearance if "this" is not (yet) linked to a board
  369. if( !board )
  370. return 0;
  371. DRC_RULE* rule = GetRule( this, aItem, CLEARANCE_CONSTRAINT );
  372. if( rule )
  373. {
  374. if( aSource )
  375. *aSource = wxString::Format( _( "'%s' rule clearance" ), rule->m_Name );
  376. return rule->m_Clearance.Min;
  377. }
  378. BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
  379. int clearance = bds.m_MinClearance;
  380. if( aSource )
  381. *aSource = _( "board minimum" );
  382. NETCLASS* netclass = GetEffectiveNetclass();
  383. if( netclass && netclass->GetClearance() > clearance )
  384. {
  385. clearance = netclass->GetClearance();
  386. if( aSource )
  387. *aSource = wxString::Format( _( "'%s' netclass" ), netclass->GetName() );
  388. }
  389. if( aItem && aItem->IsConnected() )
  390. {
  391. netclass = static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetEffectiveNetclass();
  392. if( netclass && netclass->GetClearance() > clearance )
  393. {
  394. clearance = netclass->GetClearance();
  395. if( aSource )
  396. *aSource = wxString::Format( _( "'%s' netclass" ), netclass->GetName() );
  397. }
  398. }
  399. if( aItem && aItem->GetLayer() == Edge_Cuts && bds.m_CopperEdgeClearance > clearance )
  400. {
  401. clearance = bds.m_CopperEdgeClearance;
  402. if( aSource )
  403. *aSource = _( "board edge" );
  404. }
  405. if( m_ZoneClearance > clearance )
  406. {
  407. clearance = m_ZoneClearance;
  408. if( aSource )
  409. *aSource = _( "zone" );
  410. }
  411. return clearance;
  412. }
  413. bool ZONE_CONTAINER::HitTestFilledArea( const wxPoint& aRefPos ) const
  414. {
  415. return m_FilledPolysList.Contains( VECTOR2I( aRefPos.x, aRefPos.y ) );
  416. }
  417. bool ZONE_CONTAINER::HitTestCutout( const VECTOR2I& aRefPos, int* aOutlineIdx, int* aHoleIdx ) const
  418. {
  419. // Iterate over each outline polygon in the zone and then iterate over
  420. // each hole it has to see if the point is in it.
  421. for( int i = 0; i < m_Poly->OutlineCount(); i++ )
  422. {
  423. for( int j = 0; j < m_Poly->HoleCount( i ); j++ )
  424. {
  425. if( m_Poly->Hole( i, j ).PointInside( aRefPos ) )
  426. {
  427. if( aOutlineIdx )
  428. *aOutlineIdx = i;
  429. if( aHoleIdx )
  430. *aHoleIdx = j;
  431. return true;
  432. }
  433. }
  434. }
  435. return false;
  436. }
  437. void ZONE_CONTAINER::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  438. {
  439. EDA_UNITS units = aFrame->GetUserUnits();
  440. wxString msg, msg2;
  441. if( GetIsKeepout() )
  442. msg = _( "Keepout Area" );
  443. else if( IsOnCopperLayer() )
  444. msg = _( "Copper Zone" );
  445. else
  446. msg = _( "Non-copper Zone" );
  447. // Display Cutout instead of Outline for holes inside a zone
  448. // i.e. when num contour !=0
  449. // Check whether the selected corner is in a hole; i.e., in any contour but the first one.
  450. if( m_CornerSelection != nullptr && m_CornerSelection->m_contour > 0 )
  451. msg << wxT( " " ) << _( "Cutout" );
  452. aList.emplace_back( _( "Type" ), msg, DARKCYAN );
  453. if( GetIsKeepout() )
  454. {
  455. msg.Empty();
  456. if( GetDoNotAllowVias() )
  457. AccumulateDescription( msg, _( "No vias" ) );
  458. if( GetDoNotAllowTracks() )
  459. AccumulateDescription( msg, _( "No tracks" ) );
  460. if( GetDoNotAllowPads() )
  461. AccumulateDescription( msg, _( "No pads" ) );
  462. if( GetDoNotAllowCopperPour() )
  463. AccumulateDescription( msg, _( "No copper zones" ) );
  464. if( GetDoNotAllowFootprints() )
  465. AccumulateDescription( msg, _( "No footprints" ) );
  466. aList.emplace_back( MSG_PANEL_ITEM( _( "Keepout" ), msg, RED ) );
  467. }
  468. else if( IsOnCopperLayer() )
  469. {
  470. if( GetNetCode() >= 0 )
  471. {
  472. NETINFO_ITEM* net = GetNet();
  473. NETCLASS* netclass = nullptr;
  474. if( net )
  475. {
  476. if( net->GetNet() )
  477. netclass = GetNetClass();
  478. else
  479. netclass = GetBoard()->GetDesignSettings().GetDefault();
  480. msg = UnescapeString( net->GetNetname() );
  481. }
  482. else
  483. {
  484. msg = wxT( "<no name>" );
  485. }
  486. aList.emplace_back( _( "Net" ), msg, RED );
  487. if( netclass )
  488. aList.emplace_back( _( "NetClass" ), netclass->GetName(), DARKMAGENTA );
  489. }
  490. // Display priority level
  491. msg.Printf( wxT( "%d" ), GetPriority() );
  492. aList.emplace_back( _( "Priority" ), msg, BLUE );
  493. }
  494. aList.emplace_back( _( "Layer" ), LayerMaskDescribe( GetBoard(), m_layerSet ), DARKGREEN );
  495. switch( m_FillMode )
  496. {
  497. case ZONE_FILL_MODE::POLYGONS: msg = _( "Solid" ); break;
  498. case ZONE_FILL_MODE::HATCH_PATTERN: msg = _( "Hatched" ); break;
  499. default: msg = _( "Unknown" ); break;
  500. }
  501. aList.emplace_back( _( "Fill Mode" ), msg, BROWN );
  502. msg = MessageTextFromValue( units, m_area, false, EDA_DATA_TYPE::AREA );
  503. aList.emplace_back( _( "Filled Area" ), msg, BLUE );
  504. wxString source;
  505. int clearance = GetClearance( nullptr, &source );
  506. msg.Printf( _( "Min Clearance: %s" ), MessageTextFromValue( units, clearance, true ) );
  507. msg2.Printf( _( "(from %s)" ), source );
  508. aList.emplace_back( msg, msg2, BLACK );
  509. }
  510. /* Geometric transforms: */
  511. void ZONE_CONTAINER::Move( const wxPoint& offset )
  512. {
  513. /* move outlines */
  514. m_Poly->Move( offset );
  515. Hatch();
  516. m_FilledPolysList.Move( offset );
  517. for( SEG& seg : m_FillSegmList )
  518. {
  519. seg.A += VECTOR2I( offset );
  520. seg.B += VECTOR2I( offset );
  521. }
  522. }
  523. void ZONE_CONTAINER::MoveEdge( const wxPoint& offset, int aEdge )
  524. {
  525. int next_corner;
  526. if( m_Poly->GetNeighbourIndexes( aEdge, nullptr, &next_corner ) )
  527. {
  528. m_Poly->SetVertex( aEdge, m_Poly->CVertex( aEdge ) + VECTOR2I( offset ) );
  529. m_Poly->SetVertex( next_corner, m_Poly->CVertex( next_corner ) + VECTOR2I( offset ) );
  530. Hatch();
  531. SetNeedRefill( true );
  532. }
  533. }
  534. void ZONE_CONTAINER::Rotate( const wxPoint& centre, double angle )
  535. {
  536. wxPoint pos;
  537. angle = -DECIDEG2RAD( angle );
  538. m_Poly->Rotate( angle, VECTOR2I( centre ) );
  539. Hatch();
  540. /* rotate filled areas: */
  541. m_FilledPolysList.Rotate( angle, VECTOR2I( centre ) );
  542. for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
  543. {
  544. wxPoint a( m_FillSegmList[ic].A );
  545. RotatePoint( &a, centre, angle );
  546. m_FillSegmList[ic].A = a;
  547. wxPoint b( m_FillSegmList[ic].B );
  548. RotatePoint( &b, centre, angle );
  549. m_FillSegmList[ic].B = a;
  550. }
  551. }
  552. void ZONE_CONTAINER::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  553. {
  554. Mirror( aCentre, aFlipLeftRight );
  555. int copperLayerCount = GetBoard()->GetCopperLayerCount();
  556. if( GetIsKeepout() )
  557. SetLayerSet( FlipLayerMask( GetLayerSet(), copperLayerCount ) );
  558. else
  559. SetLayer( FlipLayer( GetLayer(), copperLayerCount ) );
  560. }
  561. void ZONE_CONTAINER::Mirror( const wxPoint& aMirrorRef, bool aMirrorLeftRight )
  562. {
  563. // ZONE_CONTAINERs mirror about the x-axis (why?!?)
  564. m_Poly->Mirror( aMirrorLeftRight, !aMirrorLeftRight, VECTOR2I( aMirrorRef ) );
  565. Hatch();
  566. m_FilledPolysList.Mirror( aMirrorLeftRight, !aMirrorLeftRight, VECTOR2I( aMirrorRef ) );
  567. for( SEG& seg : m_FillSegmList )
  568. {
  569. if( aMirrorLeftRight )
  570. {
  571. MIRROR( seg.A.x, aMirrorRef.x );
  572. MIRROR( seg.B.x, aMirrorRef.x );
  573. }
  574. else
  575. {
  576. MIRROR( seg.A.y, aMirrorRef.y );
  577. MIRROR( seg.B.y, aMirrorRef.y );
  578. }
  579. }
  580. }
  581. ZONE_CONNECTION ZONE_CONTAINER::GetPadConnection( D_PAD* aPad ) const
  582. {
  583. if( aPad == NULL || aPad->GetZoneConnection() == ZONE_CONNECTION::INHERITED )
  584. return m_PadConnection;
  585. else
  586. return aPad->GetZoneConnection();
  587. }
  588. void ZONE_CONTAINER::RemoveCutout( int aOutlineIdx, int aHoleIdx )
  589. {
  590. // Ensure the requested cutout is valid
  591. if( m_Poly->OutlineCount() < aOutlineIdx || m_Poly->HoleCount( aOutlineIdx ) < aHoleIdx )
  592. return;
  593. SHAPE_POLY_SET cutPoly( m_Poly->Hole( aOutlineIdx, aHoleIdx ) );
  594. // Add the cutout back to the zone
  595. m_Poly->BooleanAdd( cutPoly, SHAPE_POLY_SET::PM_FAST );
  596. SetNeedRefill( true );
  597. }
  598. void ZONE_CONTAINER::AddPolygon( const SHAPE_LINE_CHAIN& aPolygon )
  599. {
  600. wxASSERT( aPolygon.IsClosed() );
  601. // Add the outline as a new polygon in the polygon set
  602. if( m_Poly->OutlineCount() == 0 )
  603. m_Poly->AddOutline( aPolygon );
  604. else
  605. m_Poly->AddHole( aPolygon );
  606. SetNeedRefill( true );
  607. }
  608. void ZONE_CONTAINER::AddPolygon( std::vector< wxPoint >& aPolygon )
  609. {
  610. if( aPolygon.empty() )
  611. return;
  612. SHAPE_LINE_CHAIN outline;
  613. // Create an outline and populate it with the points of aPolygon
  614. for( const wxPoint& pt : aPolygon)
  615. outline.Append( pt );
  616. outline.SetClosed( true );
  617. AddPolygon( outline );
  618. }
  619. bool ZONE_CONTAINER::AppendCorner( wxPoint aPosition, int aHoleIdx, bool aAllowDuplication )
  620. {
  621. // Ensure the main outline exists:
  622. if( m_Poly->OutlineCount() == 0 )
  623. m_Poly->NewOutline();
  624. // If aHoleIdx >= 0, the corner musty be added to the hole, index aHoleIdx.
  625. // (remember: the index of the first hole is 0)
  626. // Return error if if does dot exist.
  627. if( aHoleIdx >= m_Poly->HoleCount( 0 ) )
  628. return false;
  629. m_Poly->Append( aPosition.x, aPosition.y, -1, aHoleIdx, aAllowDuplication );
  630. SetNeedRefill( true );
  631. return true;
  632. }
  633. wxString ZONE_CONTAINER::GetSelectMenuText( EDA_UNITS aUnits ) const
  634. {
  635. wxString text;
  636. // Check whether the selected contour is a hole (contour index > 0)
  637. if( m_CornerSelection != nullptr && m_CornerSelection->m_contour > 0 )
  638. text << wxT( " " ) << _( "(Cutout)" );
  639. if( GetIsKeepout() )
  640. text << wxT( " " ) << _( "(Keepout)" );
  641. else
  642. text << GetNetnameMsg();
  643. return wxString::Format( _( "Zone Outline %s on %s" ), text, GetLayerName() );
  644. }
  645. int ZONE_CONTAINER::GetHatchPitch() const
  646. {
  647. return m_hatchPitch;
  648. }
  649. void ZONE_CONTAINER::SetHatch( ZONE_HATCH_STYLE aHatchStyle, int aHatchPitch, bool aRebuildHatch )
  650. {
  651. SetHatchPitch( aHatchPitch );
  652. m_hatchStyle = aHatchStyle;
  653. if( aRebuildHatch )
  654. Hatch();
  655. }
  656. void ZONE_CONTAINER::SetHatchPitch( int aPitch )
  657. {
  658. m_hatchPitch = aPitch;
  659. }
  660. void ZONE_CONTAINER::UnHatch()
  661. {
  662. m_HatchLines.clear();
  663. }
  664. // Creates hatch lines inside the outline of the complex polygon
  665. // sort function used in ::Hatch to sort points by descending wxPoint.x values
  666. bool sortEndsByDescendingX( const VECTOR2I& ref, const VECTOR2I& tst )
  667. {
  668. return tst.x < ref.x;
  669. }
  670. void ZONE_CONTAINER::Hatch()
  671. {
  672. UnHatch();
  673. if( m_hatchStyle == ZONE_HATCH_STYLE::NO_HATCH || m_hatchPitch == 0 || m_Poly->IsEmpty() )
  674. return;
  675. // define range for hatch lines
  676. int min_x = m_Poly->CVertex( 0 ).x;
  677. int max_x = m_Poly->CVertex( 0 ).x;
  678. int min_y = m_Poly->CVertex( 0 ).y;
  679. int max_y = m_Poly->CVertex( 0 ).y;
  680. for( auto iterator = m_Poly->IterateWithHoles(); iterator; iterator++ )
  681. {
  682. if( iterator->x < min_x )
  683. min_x = iterator->x;
  684. if( iterator->x > max_x )
  685. max_x = iterator->x;
  686. if( iterator->y < min_y )
  687. min_y = iterator->y;
  688. if( iterator->y > max_y )
  689. max_y = iterator->y;
  690. }
  691. // Calculate spacing between 2 hatch lines
  692. int spacing;
  693. if( m_hatchStyle == ZONE_HATCH_STYLE::DIAGONAL_EDGE )
  694. spacing = m_hatchPitch;
  695. else
  696. spacing = m_hatchPitch * 2;
  697. // set the "length" of hatch lines (the length on horizontal axis)
  698. int hatch_line_len = m_hatchPitch;
  699. // To have a better look, give a slope depending on the layer
  700. LAYER_NUM layer = GetLayer();
  701. int slope_flag = (layer & 1) ? 1 : -1; // 1 or -1
  702. double slope = 0.707106 * slope_flag; // 45 degrees slope
  703. int max_a, min_a;
  704. if( slope_flag == 1 )
  705. {
  706. max_a = KiROUND( max_y - slope * min_x );
  707. min_a = KiROUND( min_y - slope * max_x );
  708. }
  709. else
  710. {
  711. max_a = KiROUND( max_y - slope * max_x );
  712. min_a = KiROUND( min_y - slope * min_x );
  713. }
  714. min_a = (min_a / spacing) * spacing;
  715. // calculate an offset depending on layer number,
  716. // for a better look of hatches on a multilayer board
  717. int offset = (layer * 7) / 8;
  718. min_a += offset;
  719. // loop through hatch lines
  720. #define MAXPTS 200 // Usually we store only few values per one hatch line
  721. // depending on the complexity of the zone outline
  722. static std::vector<VECTOR2I> pointbuffer;
  723. pointbuffer.clear();
  724. pointbuffer.reserve( MAXPTS + 2 );
  725. for( int a = min_a; a < max_a; a += spacing )
  726. {
  727. // get intersection points for this hatch line
  728. // Note: because we should have an even number of intersections with the
  729. // current hatch line and the zone outline (a closed polygon,
  730. // or a set of closed polygons), if an odd count is found
  731. // we skip this line (should not occur)
  732. pointbuffer.clear();
  733. // Iterate through all vertices
  734. for( auto iterator = m_Poly->IterateSegmentsWithHoles(); iterator; iterator++ )
  735. {
  736. double x, y, x2, y2;
  737. int ok;
  738. SEG segment = *iterator;
  739. ok = FindLineSegmentIntersection( a, slope,
  740. segment.A.x, segment.A.y,
  741. segment.B.x, segment.B.y,
  742. &x, &y, &x2, &y2 );
  743. if( ok )
  744. {
  745. VECTOR2I point( KiROUND( x ), KiROUND( y ) );
  746. pointbuffer.push_back( point );
  747. }
  748. if( ok == 2 )
  749. {
  750. VECTOR2I point( KiROUND( x2 ), KiROUND( y2 ) );
  751. pointbuffer.push_back( point );
  752. }
  753. if( pointbuffer.size() >= MAXPTS ) // overflow
  754. {
  755. wxASSERT( 0 );
  756. break;
  757. }
  758. }
  759. // ensure we have found an even intersection points count
  760. // because intersections are the ends of segments
  761. // inside the polygon(s) and a segment has 2 ends.
  762. // if not, this is a strange case (a bug ?) so skip this hatch
  763. if( pointbuffer.size() % 2 != 0 )
  764. continue;
  765. // sort points in order of descending x (if more than 2) to
  766. // ensure the starting point and the ending point of the same segment
  767. // are stored one just after the other.
  768. if( pointbuffer.size() > 2 )
  769. sort( pointbuffer.begin(), pointbuffer.end(), sortEndsByDescendingX );
  770. // creates lines or short segments inside the complex polygon
  771. for( unsigned ip = 0; ip < pointbuffer.size(); ip += 2 )
  772. {
  773. int dx = pointbuffer[ip + 1].x - pointbuffer[ip].x;
  774. // Push only one line for diagonal hatch,
  775. // or for small lines < twice the line length
  776. // else push 2 small lines
  777. if( m_hatchStyle == ZONE_HATCH_STYLE::DIAGONAL_FULL
  778. || std::abs( dx ) < 2 * hatch_line_len )
  779. {
  780. m_HatchLines.emplace_back( SEG( pointbuffer[ip], pointbuffer[ip + 1] ) );
  781. }
  782. else
  783. {
  784. double dy = pointbuffer[ip + 1].y - pointbuffer[ip].y;
  785. slope = dy / dx;
  786. if( dx > 0 )
  787. dx = hatch_line_len;
  788. else
  789. dx = -hatch_line_len;
  790. int x1 = KiROUND( pointbuffer[ip].x + dx );
  791. int x2 = KiROUND( pointbuffer[ip + 1].x - dx );
  792. int y1 = KiROUND( pointbuffer[ip].y + dx * slope );
  793. int y2 = KiROUND( pointbuffer[ip + 1].y - dx * slope );
  794. m_HatchLines.emplace_back( SEG( pointbuffer[ip].x, pointbuffer[ip].y, x1, y1 ) );
  795. m_HatchLines.emplace_back( SEG( pointbuffer[ip+1].x, pointbuffer[ip+1].y, x2, y2 ) );
  796. }
  797. }
  798. }
  799. }
  800. int ZONE_CONTAINER::GetDefaultHatchPitch()
  801. {
  802. return Mils2iu( 20 );
  803. }
  804. BITMAP_DEF ZONE_CONTAINER::GetMenuImage() const
  805. {
  806. return add_zone_xpm;
  807. }
  808. void ZONE_CONTAINER::SwapData( BOARD_ITEM* aImage )
  809. {
  810. assert( aImage->Type() == PCB_ZONE_AREA_T );
  811. std::swap( *((ZONE_CONTAINER*) this), *((ZONE_CONTAINER*) aImage) );
  812. }
  813. void ZONE_CONTAINER::CacheTriangulation()
  814. {
  815. m_FilledPolysList.CacheTriangulation();
  816. }
  817. /*
  818. * Some intersecting zones, despite being on the same layer with the same net, cannot be
  819. * merged due to other parameters such as fillet radius. The copper pour will end up
  820. * effectively merged though, so we want to keep the corners of such intersections sharp.
  821. */
  822. void ZONE_CONTAINER::GetColinearCorners( BOARD* aBoard, std::set<VECTOR2I>& aCorners )
  823. {
  824. int epsilon = Millimeter2iu( 0.001 );
  825. // Things get messy when zone of different nets intersect. To do it right we'd need to
  826. // run our colinear test with the final filled regions rather than the outline regions.
  827. // However, since there's no order dependance the only way to do that is to iterate
  828. // through successive zone fills until the results are no longer changing -- and that's
  829. // not going to happen. So we punt and ignore any "messy" corners.
  830. std::set<VECTOR2I> colinearCorners;
  831. std::set<VECTOR2I> messyCorners;
  832. for( ZONE_CONTAINER* candidate : aBoard->Zones() )
  833. {
  834. if( candidate == this )
  835. continue;
  836. if( candidate->GetLayerSet() != GetLayerSet() )
  837. continue;
  838. if( candidate->GetIsKeepout() != GetIsKeepout() )
  839. continue;
  840. for( auto iter = m_Poly->CIterate(); iter; iter++ )
  841. {
  842. if( candidate->m_Poly->Collide( iter.Get(), epsilon ) )
  843. {
  844. if( candidate->GetNetCode() == GetNetCode() )
  845. colinearCorners.insert( VECTOR2I( iter.Get() ) );
  846. else
  847. messyCorners.insert( VECTOR2I( iter.Get() ) );
  848. }
  849. }
  850. }
  851. for( VECTOR2I corner : colinearCorners )
  852. {
  853. if( messyCorners.count( corner ) == 0 )
  854. aCorners.insert( corner );
  855. }
  856. }
  857. bool ZONE_CONTAINER::BuildSmoothedPoly( SHAPE_POLY_SET& aSmoothedPoly,
  858. std::set<VECTOR2I>* aPreserveCorners ) const
  859. {
  860. if( GetNumCorners() <= 2 ) // malformed zone. polygon calculations do not like it ...
  861. return false;
  862. // Make a smoothed polygon out of the user-drawn polygon if required
  863. switch( m_cornerSmoothingType )
  864. {
  865. case ZONE_SETTINGS::SMOOTHING_CHAMFER:
  866. aSmoothedPoly = m_Poly->Chamfer( m_cornerRadius, aPreserveCorners );
  867. break;
  868. case ZONE_SETTINGS::SMOOTHING_FILLET:
  869. {
  870. auto board = GetBoard();
  871. int maxError = ARC_HIGH_DEF;
  872. if( board )
  873. maxError = board->GetDesignSettings().m_MaxError;
  874. aSmoothedPoly = m_Poly->Fillet( m_cornerRadius, maxError, aPreserveCorners );
  875. break;
  876. }
  877. default:
  878. // Acute angles between adjacent edges can create issues in calculations,
  879. // in inflate/deflate outlines transforms, especially when the angle is very small.
  880. // We can avoid issues by creating a very small chamfer which remove acute angles,
  881. // or left it without chamfer and use only CPOLYGONS_LIST::InflateOutline to create
  882. // clearance areas
  883. aSmoothedPoly = m_Poly->Chamfer( Millimeter2iu( 0.0 ), aPreserveCorners );
  884. break;
  885. }
  886. return true;
  887. };
  888. double ZONE_CONTAINER::CalculateFilledArea()
  889. {
  890. m_area = 0.0;
  891. // Iterate over each outline polygon in the zone and then iterate over
  892. // each hole it has to compute the total area.
  893. for( int i = 0; i < m_FilledPolysList.OutlineCount(); i++ )
  894. {
  895. m_area += m_FilledPolysList.Outline( i ).Area();
  896. for( int j = 0; j < m_FilledPolysList.HoleCount( i ); j++ )
  897. {
  898. m_area -= m_FilledPolysList.Hole( i, j ).Area();
  899. }
  900. }
  901. return m_area;
  902. }
  903. /* Function TransformOutlinesShapeWithClearanceToPolygon
  904. * Convert the zone filled areas polygons to polygons
  905. * inflated (optional) by max( aClearanceValue, the zone clearance)
  906. * and copy them in aCornerBuffer
  907. * @param aClearance the clearance around outlines
  908. * @param aPreserveCorners an optional set of corners which should not be chamfered/filleted
  909. */
  910. void ZONE_CONTAINER::TransformOutlinesShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  911. int aClearance, std::set<VECTOR2I>* aPreserveCorners ) const
  912. {
  913. // Creates the zone outline polygon (with holes if any)
  914. SHAPE_POLY_SET polybuffer;
  915. BuildSmoothedPoly( polybuffer, aPreserveCorners );
  916. // Calculate the polygon with clearance
  917. // holes are linked to the main outline, so only one polygon is created.
  918. if( aClearance )
  919. {
  920. BOARD* board = GetBoard();
  921. int maxError = ARC_HIGH_DEF;
  922. if( board )
  923. maxError = board->GetDesignSettings().m_MaxError;
  924. int segCount = std::max( GetArcToSegmentCount( aClearance, maxError, 360.0 ), 3 );
  925. polybuffer.Inflate( aClearance, segCount );
  926. }
  927. polybuffer.Fracture( SHAPE_POLY_SET::PM_FAST );
  928. aCornerBuffer.Append( polybuffer );
  929. }
  930. //
  931. /********* MODULE_ZONE_CONTAINER **************/
  932. //
  933. MODULE_ZONE_CONTAINER::MODULE_ZONE_CONTAINER( BOARD_ITEM_CONTAINER* aParent ) :
  934. ZONE_CONTAINER( aParent, true )
  935. {
  936. // in a footprint, net classes are not managed.
  937. // so set the net to NETINFO_LIST::ORPHANED_ITEM
  938. SetNetCode( -1, true );
  939. }
  940. MODULE_ZONE_CONTAINER::MODULE_ZONE_CONTAINER( const MODULE_ZONE_CONTAINER& aZone )
  941. : ZONE_CONTAINER( aZone.GetParent(), true )
  942. {
  943. initDataFromSrcInCopyCtor( aZone );
  944. }
  945. MODULE_ZONE_CONTAINER& MODULE_ZONE_CONTAINER::operator=( const MODULE_ZONE_CONTAINER& aOther )
  946. {
  947. ZONE_CONTAINER::operator=( aOther );
  948. return *this;
  949. }
  950. EDA_ITEM* MODULE_ZONE_CONTAINER::Clone() const
  951. {
  952. return new MODULE_ZONE_CONTAINER( *this );
  953. }
  954. unsigned int MODULE_ZONE_CONTAINER::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  955. {
  956. const int HIDE = std::numeric_limits<unsigned int>::max();
  957. if( !aView )
  958. return 0;
  959. bool flipped = GetParent() && GetParent()->GetLayer() == B_Cu;
  960. // Handle Render tab switches
  961. if( !flipped && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  962. return HIDE;
  963. if( flipped && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  964. return HIDE;
  965. // Other layers are shown without any conditions
  966. return 0;
  967. }