KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zone_filler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2017 CERN
5 * Copyright (C) 2014-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Włostowski <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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
26#include <future>
27#include <core/kicad_algo.h>
28#include <advanced_config.h>
29#include <board.h>
31#include <zone.h>
32#include <footprint.h>
33#include <pad.h>
34#include <pcb_target.h>
35#include <pcb_track.h>
36#include <pcb_text.h>
37#include <pcb_textbox.h>
38#include <pcb_tablecell.h>
39#include <pcb_table.h>
40#include <pcb_dimension.h>
43#include <board_commit.h>
44#include <progress_reporter.h>
48#include <geometry/vertex_set.h>
49#include <kidialog.h>
50#include <core/thread_pool.h>
51#include <math/util.h> // for KiROUND
52#include "zone_filler.h"
53
54// Helper classes for connect_nearby_polys
56{
57public:
58 RESULTS( int aOutline1, int aOutline2, int aVertex1, int aVertex2 ) :
59 m_outline1( aOutline1 ), m_outline2( aOutline2 ),
60 m_vertex1( aVertex1 ), m_vertex2( aVertex2 )
61 {
62 }
63
64 bool operator<( const RESULTS& aOther ) const
65 {
66 if( m_outline1 != aOther.m_outline1 )
67 return m_outline1 < aOther.m_outline1;
68 if( m_outline2 != aOther.m_outline2 )
69 return m_outline2 < aOther.m_outline2;
70 if( m_vertex1 != aOther.m_vertex1 )
71 return m_vertex1 < aOther.m_vertex1;
72 return m_vertex2 < aOther.m_vertex2;
73 }
74
79};
80
82{
83public:
84 VERTEX_CONNECTOR( const BOX2I& aBBox, const SHAPE_POLY_SET& aPolys, int aDist ) : VERTEX_SET( 0 )
85 {
86 SetBoundingBox( aBBox );
87 VERTEX* tail = nullptr;
88
89 for( int i = 0; i < aPolys.OutlineCount(); i++ )
90 tail = createList( aPolys.Outline( i ), tail, (void*)( intptr_t )( i ) );
91
92 tail->updateList();
93 m_dist = aDist;
94 }
95
96 VERTEX* getPoint( VERTEX* aPt ) const
97 {
98 // z-order range for the current point ± limit bounding box
99 const int32_t maxZ = zOrder( aPt->x + m_dist, aPt->y + m_dist );
100 const int32_t minZ = zOrder( aPt->x - m_dist, aPt->y - m_dist );
101 const SEG::ecoord limit2 = SEG::Square( m_dist );
102
103 // first look for points in increasing z-order
104 SEG::ecoord min_dist = std::numeric_limits<SEG::ecoord>::max();
105 VERTEX* retval = nullptr;
106
107 auto check_pt = [&]( VERTEX* p )
108 {
109 VECTOR2D diff( p->x - aPt->x, p->y - aPt->y );
110 SEG::ecoord dist2 = diff.SquaredEuclideanNorm();
111
112 if( dist2 < limit2 && dist2 < min_dist
113 && p->isEar() )
114 {
115 min_dist = dist2;
116 retval = p;
117 }
118 };
119
120 VERTEX* p = aPt->nextZ;
121
122 while( p && p->z <= maxZ )
123 {
124 check_pt( p );
125 p = p->nextZ;
126 }
127
128 p = aPt->prevZ;
129
130 while( p && p->z >= minZ )
131 {
132 check_pt( p );
133 p = p->prevZ;
134 }
135
136 return retval;
137 }
138
140 {
141 VERTEX* p = m_vertices.front().next;
142 std::set<VERTEX*> visited;
143
144 while( p != &m_vertices.front() )
145 {
146 // Skip points that are concave
147 if( !p->isEar() )
148 {
149 p = p->next;
150 continue;
151 }
152
153 VERTEX* q = nullptr;
154
155 if( ( visited.empty() || !visited.contains( p ) ) && ( q = getPoint( p ) ) )
156 {
157 visited.insert( p );
158
159 if( !visited.contains( q ) &&
160 m_results.emplace( (intptr_t) p->GetUserData(), (intptr_t) q->GetUserData(),
161 p->i, q->i ).second )
162 {
163 // We don't want to connect multiple points in the same vicinity, so skip
164 // 2 points before and after each point and match.
165 visited.insert( p->prev );
166 visited.insert( p->prev->prev );
167 visited.insert( p->next );
168 visited.insert( p->next->next );
169
170 visited.insert( q->prev );
171 visited.insert( q->prev->prev );
172 visited.insert( q->next );
173 visited.insert( q->next->next );
174
175 visited.insert( q );
176 }
177 }
178
179 p = p->next;
180 }
181 }
182
183 std::set<RESULTS> GetResults() const
184 {
185 return m_results;
186 }
187
188private:
189 std::set<RESULTS> m_results;
191};
192
193
195 m_board( aBoard ),
196 m_brdOutlinesValid( false ),
197 m_commit( aCommit ),
198 m_progressReporter( nullptr ),
199 m_maxError( ARC_HIGH_DEF ),
200 m_worstClearance( 0 )
201{
202 // To enable add "DebugZoneFiller=1" to kicad_advanced settings file.
204}
205
206
208{
209}
210
211
213{
214 m_progressReporter = aReporter;
215 wxASSERT_MSG( m_commit, wxT( "ZONE_FILLER must have a valid commit to call "
216 "SetProgressReporter" ) );
217}
218
219
230bool ZONE_FILLER::Fill( const std::vector<ZONE*>& aZones, bool aCheck, wxWindow* aParent )
231{
232 std::lock_guard<KISPINLOCK> lock( m_board->GetConnectivity()->GetLock() );
233
234 std::vector<std::pair<ZONE*, PCB_LAYER_ID>> toFill;
235 std::map<std::pair<ZONE*, PCB_LAYER_ID>, HASH_128> oldFillHashes;
236 std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>> isolatedIslandsMap;
237
238 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
239
240 // Rebuild (from scratch, ignoring dirty flags) just in case. This really needs to be reliable.
241 connectivity->ClearRatsnest();
242 connectivity->Build( m_board, m_progressReporter );
243
245
247 {
248 m_progressReporter->Report( aCheck ? _( "Checking zone fills..." )
249 : _( "Building zone fills..." ) );
250 m_progressReporter->SetMaxProgress( aZones.size() );
252 }
253
254 // The board outlines is used to clip solid areas inside the board (when outlines are valid)
257
258 // Update and cache zone bounding boxes and pad effective shapes so that we don't have to
259 // make them thread-safe.
260 //
261 for( ZONE* zone : m_board->Zones() )
262 zone->CacheBoundingBox();
263
264 for( FOOTPRINT* footprint : m_board->Footprints() )
265 {
266 for( PAD* pad : footprint->Pads() )
267 {
268 if( pad->IsDirty() )
269 {
270 pad->BuildEffectiveShapes( UNDEFINED_LAYER );
271 pad->BuildEffectivePolygon( ERROR_OUTSIDE );
272 }
273 }
274
275 for( ZONE* zone : footprint->Zones() )
276 zone->CacheBoundingBox();
277
278 // Rules may depend on insideCourtyard() or other expressions
279 footprint->BuildCourtyardCaches();
280 }
281
282 LSET boardCuMask = m_board->GetEnabledLayers() & LSET::AllCuMask();
283
284 auto findHighestPriorityZone = [&]( const BOX2I& aBBox, const PCB_LAYER_ID aItemLayer,
285 const int aNetcode,
286 const std::function<bool( const ZONE* )> aTestFn ) -> ZONE*
287 {
288 unsigned highestPriority = 0;
289 ZONE* highestPriorityZone = nullptr;
290
291 for( ZONE* zone : m_board->Zones() )
292 {
293 // Rule areas are not filled
294 if( zone->GetIsRuleArea() )
295 continue;
296
297 if( zone->GetAssignedPriority() < highestPriority )
298 continue;
299
300 if( !zone->IsOnLayer( aItemLayer ) )
301 continue;
302
303 // Degenerate zones will cause trouble; skip them
304 if( zone->GetNumCorners() <= 2 )
305 continue;
306
307 if( !zone->GetBoundingBox().Intersects( aBBox ) )
308 continue;
309
310 if( !aTestFn( zone ) )
311 continue;
312
313 // Prefer highest priority and matching netcode
314 if( zone->GetAssignedPriority() > highestPriority || zone->GetNetCode() == aNetcode )
315 {
316 highestPriority = zone->GetAssignedPriority();
317 highestPriorityZone = zone;
318 }
319 }
320
321 return highestPriorityZone;
322 };
323
324 auto isInPourKeepoutArea = [&]( const BOX2I& aBBox, const PCB_LAYER_ID aItemLayer,
325 const VECTOR2I aTestPoint ) -> bool
326 {
327 for( ZONE* zone : m_board->Zones() )
328 {
329 if( !zone->GetIsRuleArea() )
330 continue;
331
332 if( !zone->GetDoNotAllowCopperPour() )
333 continue;
334
335 if( !zone->IsOnLayer( aItemLayer ) )
336 continue;
337
338 // Degenerate zones will cause trouble; skip them
339 if( zone->GetNumCorners() <= 2 )
340 continue;
341
342 if( !zone->GetBoundingBox().Intersects( aBBox ) )
343 continue;
344
345 if( zone->Outline()->Contains( aTestPoint ) )
346 return true;
347 }
348
349 return false;
350 };
351
352 // Determine state of conditional via flashing
353 for( PCB_TRACK* track : m_board->Tracks() )
354 {
355 if( track->Type() == PCB_VIA_T )
356 {
357 PCB_VIA* via = static_cast<PCB_VIA*>( track );
358
359 via->ClearZoneLayerOverrides();
360
361 if( !via->GetRemoveUnconnected() )
362 continue;
363
364 BOX2I bbox = via->GetBoundingBox();
365 VECTOR2I center = via->GetPosition();
366 int testRadius = via->GetDrillValue() / 2 + 1;
367 unsigned netcode = via->GetNetCode();
368 LSET layers = via->GetLayerSet() & boardCuMask;
369
370 // Checking if the via hole touches the zone outline
371 auto viaTestFn = [&]( const ZONE* aZone ) -> bool
372 {
373 return aZone->Outline()->Contains( center, -1, testRadius );
374 };
375
376 for( PCB_LAYER_ID layer : layers.Seq() )
377 {
378 if( !via->ConditionallyFlashed( layer ) )
379 continue;
380
381 if( isInPourKeepoutArea( bbox, layer, center ) )
382 {
383 via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
384 }
385 else
386 {
387 ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, viaTestFn );
388
389 if( zone && zone->GetNetCode() == via->GetNetCode() )
390 via->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
391 else
392 via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
393 }
394 }
395 }
396 }
397
398 // Determine state of conditional pad flashing
399 for( FOOTPRINT* footprint : m_board->Footprints() )
400 {
401 for( PAD* pad : footprint->Pads() )
402 {
403 pad->ClearZoneLayerOverrides();
404
405 if( !pad->GetRemoveUnconnected() )
406 continue;
407
408 BOX2I bbox = pad->GetBoundingBox();
409 VECTOR2I center = pad->GetPosition();
410 unsigned netcode = pad->GetNetCode();
411 LSET layers = pad->GetLayerSet() & boardCuMask;
412
413 auto padTestFn = [&]( const ZONE* aZone ) -> bool
414 {
415 return aZone->Outline()->Contains( center );
416 };
417
418 for( PCB_LAYER_ID layer : layers.Seq() )
419 {
420 if( !pad->ConditionallyFlashed( layer ) )
421 continue;
422
423 if( isInPourKeepoutArea( bbox, layer, center ) )
424 {
425 pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
426 }
427 else
428 {
429 ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, padTestFn );
430
431 if( zone && zone->GetNetCode() == pad->GetNetCode() )
432 pad->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
433 else
434 pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
435 }
436 }
437 }
438 }
439
440 for( ZONE* zone : aZones )
441 {
442 // Rule areas are not filled
443 if( zone->GetIsRuleArea() )
444 continue;
445
446 // Degenerate zones will cause trouble; skip them
447 if( zone->GetNumCorners() <= 2 )
448 continue;
449
450 if( m_commit )
451 m_commit->Modify( zone );
452
453 // calculate the hash value for filled areas. it will be used later to know if the
454 // current filled areas are up to date
455 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
456 {
457 zone->BuildHashValue( layer );
458 oldFillHashes[ { zone, layer } ] = zone->GetHashValue( layer );
459
460 // Add the zone to the list of zones to test or refill
461 toFill.emplace_back( std::make_pair( zone, layer ) );
462
463 isolatedIslandsMap[ zone ][ layer ] = ISOLATED_ISLANDS();
464 }
465
466 // Remove existing fill first to prevent drawing invalid polygons on some platforms
467 zone->UnFill();
468 }
469
470 auto check_fill_dependency =
471 [&]( ZONE* aZone, PCB_LAYER_ID aLayer, ZONE* aOtherZone ) -> bool
472 {
473 // Check to see if we have to knock-out the filled areas of a higher-priority
474 // zone. If so we have to wait until said zone is filled before we can fill.
475
476 // If the other zone is already filled on the requested layer then we're
477 // good-to-go
478 if( aOtherZone->GetFillFlag( aLayer ) )
479 return false;
480
481 // Even if keepouts exclude copper pours, the exclusion is by outline rather than
482 // filled area, so we're good-to-go here too
483 if( aOtherZone->GetIsRuleArea() )
484 return false;
485
486 // If the other zone is never going to be filled then don't wait for it
487 if( aOtherZone->GetNumCorners() <= 2 )
488 return false;
489
490 // If the zones share no common layers
491 if( !aOtherZone->GetLayerSet().test( aLayer ) )
492 return false;
493
494 if( aZone->HigherPriority( aOtherZone ) )
495 return false;
496
497 // Same-net zones always use outlines to produce determinate results
498 if( aOtherZone->SameNet( aZone ) )
499 return false;
500
501 // A higher priority zone is found: if we intersect and it's not filled yet
502 // then we have to wait.
503 BOX2I inflatedBBox = aZone->GetBoundingBox();
504 inflatedBBox.Inflate( m_worstClearance );
505
506 if( !inflatedBBox.Intersects( aOtherZone->GetBoundingBox() ) )
507 return false;
508
509 return aZone->Outline()->Collide( aOtherZone->Outline(), m_worstClearance );
510 };
511
512 auto fill_lambda =
513 [&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
514 {
515 PCB_LAYER_ID layer = aFillItem.second;
516 ZONE* zone = aFillItem.first;
517 bool canFill = true;
518
519 // Check for any fill dependencies. If our zone needs to be clipped by
520 // another zone then we can't fill until that zone is filled.
521 for( ZONE* otherZone : aZones )
522 {
523 if( otherZone == zone )
524 continue;
525
526 if( check_fill_dependency( zone, layer, otherZone ) )
527 {
528 canFill = false;
529 break;
530 }
531 }
532
534 return 0;
535
536 if( !canFill )
537 return 0;
538
539 // Now we're ready to fill.
540 {
541 std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
542
543 if( !zoneLock.owns_lock() )
544 return 0;
545
546 SHAPE_POLY_SET fillPolys;
547
548 if( !fillSingleZone( zone, layer, fillPolys ) )
549 return 0;
550
551 zone->SetFilledPolysList( layer, fillPolys );
552 }
553
556
557 return 1;
558 };
559
560 auto tesselate_lambda =
561 [&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
562 {
564 return 0;
565
566 PCB_LAYER_ID layer = aFillItem.second;
567 ZONE* zone = aFillItem.first;
568
569 {
570 std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
571
572 if( !zoneLock.owns_lock() )
573 return 0;
574
575 zone->CacheTriangulation( layer );
576 zone->SetFillFlag( layer, true );
577 }
578
579 return 1;
580 };
581
582 // Calculate the copper fills (NB: this is multi-threaded)
583 //
584 std::vector<std::pair<std::future<int>, int>> returns;
585 returns.reserve( toFill.size() );
586 size_t finished = 0;
587 bool cancelled = false;
588
590
591 for( const std::pair<ZONE*, PCB_LAYER_ID>& fillItem : toFill )
592 returns.emplace_back( std::make_pair( tp.submit( fill_lambda, fillItem ), 0 ) );
593
594 while( !cancelled && finished != 2 * toFill.size() )
595 {
596 for( size_t ii = 0; ii < returns.size(); ++ii )
597 {
598 auto& ret = returns[ii];
599
600 if( ret.second > 1 )
601 continue;
602
603 std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
604
605 if( status == std::future_status::ready )
606 {
607 if( ret.first.get() ) // lambda completed
608 {
609 ++finished;
610 ret.second++; // go to next step
611 }
612
613 if( !cancelled )
614 {
615 // Queue the next step (will re-queue the existing step if it didn't complete)
616 if( ret.second == 0 )
617 returns[ii].first = tp.submit( fill_lambda, toFill[ii] );
618 else if( ret.second == 1 )
619 returns[ii].first = tp.submit( tesselate_lambda, toFill[ii] );
620 }
621 }
622 }
623
624 std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
625
626
628 {
630
632 cancelled = true;
633 }
634 }
635
636 // Make sure that all futures have finished.
637 // This can happen when the user cancels the above operation
638 for( auto& ret : returns )
639 {
640 if( ret.first.valid() )
641 {
642 std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
643
644 while( status != std::future_status::ready )
645 {
648
649 status = ret.first.wait_for( std::chrono::milliseconds( 100 ) );
650 }
651 }
652 }
653
654 // Now update the connectivity to check for isolated copper islands
655 // (NB: FindIsolatedCopperIslands() is multi-threaded)
656 //
658 {
660 return false;
661
663 m_progressReporter->Report( _( "Removing isolated copper islands..." ) );
665 }
666
667 connectivity->SetProgressReporter( m_progressReporter );
668 connectivity->FillIsolatedIslandsMap( isolatedIslandsMap );
669 connectivity->SetProgressReporter( nullptr );
670
672 return false;
673
674 for( ZONE* zone : aZones )
675 {
676 // Keepout zones are not filled
677 if( zone->GetIsRuleArea() )
678 continue;
679
680 zone->SetIsFilled( true );
681 }
682
683 // Now remove isolated copper islands according to the isolated islands strategy assigned
684 // by the user (always, never, below-certain-size).
685 //
686 for( const auto& [ zone, zoneIslands ] : isolatedIslandsMap )
687 {
688 // If *all* the polygons are islands, do not remove any of them
689 bool allIslands = true;
690
691 for( const auto& [ layer, layerIslands ] : zoneIslands )
692 {
693 if( layerIslands.m_IsolatedOutlines.size()
694 != static_cast<size_t>( zone->GetFilledPolysList( layer )->OutlineCount() ) )
695 {
696 allIslands = false;
697 break;
698 }
699 }
700
701 if( allIslands )
702 continue;
703
704 for( const auto& [ layer, layerIslands ] : zoneIslands )
705 {
706 if( m_debugZoneFiller && LSET::InternalCuMask().Contains( layer ) )
707 continue;
708
709 if( layerIslands.m_IsolatedOutlines.empty() )
710 continue;
711
712 std::vector<int> islands = layerIslands.m_IsolatedOutlines;
713
714 // The list of polygons to delete must be explored from last to first in list,
715 // to allow deleting a polygon from list without breaking the remaining of the list
716 std::sort( islands.begin(), islands.end(), std::greater<int>() );
717
718 std::shared_ptr<SHAPE_POLY_SET> poly = zone->GetFilledPolysList( layer );
719 long long int minArea = zone->GetMinIslandArea();
720 ISLAND_REMOVAL_MODE mode = zone->GetIslandRemovalMode();
721
722 for( int idx : islands )
723 {
724 SHAPE_LINE_CHAIN& outline = poly->Outline( idx );
725
726 if( mode == ISLAND_REMOVAL_MODE::ALWAYS )
727 poly->DeletePolygonAndTriangulationData( idx, false );
728 else if ( mode == ISLAND_REMOVAL_MODE::AREA && outline.Area( true ) < minArea )
729 poly->DeletePolygonAndTriangulationData( idx, false );
730 else
731 zone->SetIsIsland( layer, idx );
732 }
733
734 poly->UpdateTriangulationDataHash();
735 zone->CalculateFilledArea();
736
738 return false;
739 }
740 }
741
742 // Now remove islands which are either outside the board edge or fail to meet the minimum
743 // area requirements
744 using island_check_return = std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, int>>;
745
746 std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, double>> polys_to_check;
747
748 // rough estimate to save re-allocation time
749 polys_to_check.reserve( m_board->GetCopperLayerCount() * aZones.size() );
750
751 for( ZONE* zone : aZones )
752 {
753 // Don't check for connections on layers that only exist in the zone but
754 // were disabled in the board
755 BOARD* board = zone->GetBoard();
756 LSET zoneCopperLayers = zone->GetLayerSet() & LSET::AllCuMask() & board->GetEnabledLayers();
757
758 // Min-thickness is the web thickness. On the other hand, a blob min-thickness by
759 // min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
760 // arbitrarily choose "at least 3X the area".
761 double minArea = (double) zone->GetMinThickness() * zone->GetMinThickness() * 3;
762
763 for( PCB_LAYER_ID layer : zoneCopperLayers.Seq() )
764 {
766 continue;
767
768 polys_to_check.emplace_back( zone->GetFilledPolysList( layer ), minArea );
769 }
770 }
771
772 auto island_lambda =
773 [&]( int aStart, int aEnd ) -> island_check_return
774 {
775 island_check_return retval;
776
777 for( int ii = aStart; ii < aEnd && !cancelled; ++ii )
778 {
779 auto [poly, minArea] = polys_to_check[ii];
780
781 for( int jj = poly->OutlineCount() - 1; jj >= 0; jj-- )
782 {
783 SHAPE_POLY_SET island;
784 SHAPE_POLY_SET intersection;
785 const SHAPE_LINE_CHAIN& test_poly = poly->Polygon( jj ).front();
786 double island_area = test_poly.Area();
787
788 if( island_area < minArea )
789 continue;
790
791
792 island.AddOutline( test_poly );
793 intersection.BooleanIntersection( m_boardOutline, island,
795
796 // Nominally, all of these areas should be either inside or outside the
797 // board outline. So this test should be able to just compare areas (if
798 // they are equal, you are inside). But in practice, we sometimes have
799 // slight overlap at the edges, so testing against half-size area acts as
800 // a fail-safe.
801 if( intersection.Area() < island_area / 2.0 )
802 retval.emplace_back( poly, jj );
803 }
804 }
805
806 return retval;
807 };
808
809 auto island_returns = tp.parallelize_loop( 0, polys_to_check.size(), island_lambda );
810 cancelled = false;
811
812 // Allow island removal threads to finish
813 for( size_t ii = 0; ii < island_returns.size(); ++ii )
814 {
815 std::future<island_check_return>& ret = island_returns[ii];
816
817 if( ret.valid() )
818 {
819 std::future_status status = ret.wait_for( std::chrono::seconds( 0 ) );
820
821 while( status != std::future_status::ready )
822 {
824 {
826
828 cancelled = true;
829 }
830
831 status = ret.wait_for( std::chrono::milliseconds( 100 ) );
832 }
833 }
834 }
835
836 if( cancelled )
837 return false;
838
839 for( size_t ii = 0; ii < island_returns.size(); ++ii )
840 {
841 std::future<island_check_return>& ret = island_returns[ii];
842
843 if( ret.valid() )
844 {
845 for( auto& action_item : ret.get() )
846 action_item.first->DeletePolygonAndTriangulationData( action_item.second, true );
847 }
848 }
849
850 for( ZONE* zone : aZones )
851 zone->CalculateFilledArea();
852
853
854 if( aCheck )
855 {
856 bool outOfDate = false;
857
858 for( ZONE* zone : aZones )
859 {
860 // Keepout zones are not filled
861 if( zone->GetIsRuleArea() )
862 continue;
863
864 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
865 {
866 zone->BuildHashValue( layer );
867
868 if( oldFillHashes[ { zone, layer } ] != zone->GetHashValue( layer ) )
869 outOfDate = true;
870 }
871 }
872
873 if( outOfDate )
874 {
875 KIDIALOG dlg( aParent, _( "Zone fills are out-of-date. Refill?" ),
876 _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
877 dlg.SetOKCancelLabels( _( "Refill" ), _( "Continue without Refill" ) );
878 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
879
880 if( dlg.ShowModal() == wxID_CANCEL )
881 return false;
882 }
883 else
884 {
885 // No need to commit something that hasn't changed (and committing will set
886 // the modified flag).
887 return false;
888 }
889 }
890
892 {
894 return false;
895
898 }
899
900 return true;
901}
902
903
908void ZONE_FILLER::addKnockout( PAD* aPad, PCB_LAYER_ID aLayer, int aGap, SHAPE_POLY_SET& aHoles )
909{
910 if( aPad->GetShape() == PAD_SHAPE::CUSTOM )
911 {
912 SHAPE_POLY_SET poly;
913 aPad->TransformShapeToPolygon( poly, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
914
915 // the pad shape in zone can be its convex hull or the shape itself
917 {
918 std::vector<VECTOR2I> convex_hull;
919 BuildConvexHull( convex_hull, poly );
920
921 aHoles.NewOutline();
922
923 for( const VECTOR2I& pt : convex_hull )
924 aHoles.Append( pt );
925 }
926 else
927 aHoles.Append( poly );
928 }
929 else
930 {
931 aPad->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
932 }
933}
934
935
939void ZONE_FILLER::addHoleKnockout( PAD* aPad, int aGap, SHAPE_POLY_SET& aHoles )
940{
941 aPad->TransformHoleToPolygon( aHoles, aGap, m_maxError, ERROR_OUTSIDE );
942}
943
944
949void ZONE_FILLER::addKnockout( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aGap,
950 bool aIgnoreLineWidth, SHAPE_POLY_SET& aHoles )
951{
952 switch( aItem->Type() )
953 {
954 case PCB_FIELD_T:
955 case PCB_TEXT_T:
956 {
957 PCB_TEXT* text = static_cast<PCB_TEXT*>( aItem );
958
959 if( text->IsVisible() )
960 {
961 if( text->IsKnockout() )
962 {
963 // Knockout text should only leave holes where the text is, not where the copper fill
964 // around it would be.
965 PCB_TEXT textCopy = *text;
966 textCopy.SetIsKnockout( false );
967 textCopy.TransformShapeToPolygon( aHoles, aLayer, 0, m_maxError, ERROR_OUTSIDE );
968 }
969 else
970 {
971 text->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
972 }
973 }
974
975 break;
976 }
977
978 case PCB_TEXTBOX_T:
979 case PCB_TABLE_T:
980 case PCB_SHAPE_T:
981 case PCB_TARGET_T:
982 aItem->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE,
983 aIgnoreLineWidth );
984 break;
985
987 case PCB_DIM_LEADER_T:
988 case PCB_DIM_CENTER_T:
989 case PCB_DIM_RADIAL_T:
991 {
992 PCB_DIMENSION_BASE* dim = static_cast<PCB_DIMENSION_BASE*>( aItem );
993
994 dim->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE, false );
995 dim->PCB_TEXT::TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
996 break;
997 }
998
999 default:
1000 break;
1001 }
1002}
1003
1004
1010 SHAPE_POLY_SET& aFill,
1011 std::vector<PAD*>& aThermalConnectionPads,
1012 std::vector<PAD*>& aNoConnectionPads )
1013{
1015 ZONE_CONNECTION connection;
1016 DRC_CONSTRAINT constraint;
1017 int padClearance;
1018 std::shared_ptr<SHAPE> padShape;
1019 int holeClearance;
1020 SHAPE_POLY_SET holes;
1021
1022 for( FOOTPRINT* footprint : m_board->Footprints() )
1023 {
1024 for( PAD* pad : footprint->Pads() )
1025 {
1026 BOX2I padBBox = pad->GetBoundingBox();
1027 padBBox.Inflate( m_worstClearance );
1028
1029 if( !padBBox.Intersects( aZone->GetBoundingBox() ) )
1030 continue;
1031
1032 bool noConnection = pad->GetNetCode() != aZone->GetNetCode();
1033
1034 if( !aZone->IsTeardropArea() )
1035 {
1036 if( aZone->GetNetCode() == 0
1037 || pad->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
1038 {
1039 noConnection = true;
1040 }
1041 }
1042
1043 if( noConnection )
1044 {
1045 // collect these for knockout in buildCopperItemClearances()
1046 aNoConnectionPads.push_back( pad );
1047 continue;
1048 }
1049
1050 if( aZone->IsTeardropArea() )
1051 {
1052 connection = ZONE_CONNECTION::FULL;
1053 }
1054 else
1055 {
1056 constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
1057 connection = constraint.m_ZoneConnection;
1058 }
1059
1060 if( connection == ZONE_CONNECTION::THERMAL && !pad->CanFlashLayer( aLayer ) )
1061 connection = ZONE_CONNECTION::NONE;
1062
1063 switch( connection )
1064 {
1065 case ZONE_CONNECTION::THERMAL:
1066 padShape = pad->GetEffectiveShape( aLayer, FLASHING::ALWAYS_FLASHED );
1067
1068 if( aFill.Collide( padShape.get(), 0 ) )
1069 {
1070 constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad,
1071 aZone, aLayer );
1072 padClearance = constraint.GetValue().Min();
1073
1074 aThermalConnectionPads.push_back( pad );
1075 addKnockout( pad, aLayer, padClearance, holes );
1076 }
1077
1078 break;
1079
1080 case ZONE_CONNECTION::NONE:
1081 constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, pad,
1082 aZone, aLayer );
1083
1084 if( constraint.GetValue().Min() > aZone->GetLocalClearance().value() )
1085 padClearance = constraint.GetValue().Min();
1086 else
1087 padClearance = aZone->GetLocalClearance().value();
1088
1089 if( pad->FlashLayer( aLayer ) )
1090 {
1091 addKnockout( pad, aLayer, padClearance, holes );
1092 }
1093 else if( pad->GetDrillSize().x > 0 )
1094 {
1095 constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
1096 pad, aZone, aLayer );
1097
1098 if( constraint.GetValue().Min() > padClearance )
1099 holeClearance = constraint.GetValue().Min();
1100 else
1101 holeClearance = padClearance;
1102
1103 pad->TransformHoleToPolygon( holes, holeClearance, m_maxError, ERROR_OUTSIDE );
1104 }
1105
1106 break;
1107
1108 default:
1109 // No knockout
1110 continue;
1111 }
1112 }
1113 }
1114
1116}
1117
1118
1124 const std::vector<PAD*>& aNoConnectionPads,
1125 SHAPE_POLY_SET& aHoles )
1126{
1128 long ticker = 0;
1129
1130 auto checkForCancel =
1131 [&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
1132 {
1133 return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
1134 };
1135
1136 // A small extra clearance to be sure actual track clearances are not smaller than
1137 // requested clearance due to many approximations in calculations, like arc to segment
1138 // approx, rounding issues, etc.
1139 BOX2I zone_boundingbox = aZone->GetBoundingBox();
1141
1142 // Items outside the zone bounding box are skipped, so it needs to be inflated by the
1143 // largest clearance value found in the netclasses and rules
1144 zone_boundingbox.Inflate( m_worstClearance + extra_margin );
1145
1146 auto evalRulesForItems =
1147 [&bds]( DRC_CONSTRAINT_T aConstraint, const BOARD_ITEM* a, const BOARD_ITEM* b,
1148 PCB_LAYER_ID aEvalLayer ) -> int
1149 {
1150 DRC_CONSTRAINT c = bds.m_DRCEngine->EvalRules( aConstraint, a, b, aEvalLayer );
1151
1152 if( c.IsNull() )
1153 return -1;
1154 else
1155 return c.GetValue().Min();
1156 };
1157
1158 // Add non-connected pad clearances
1159 //
1160 auto knockoutPadClearance =
1161 [&]( PAD* aPad )
1162 {
1163 int init_gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone, aPad, aLayer );
1164 int gap = init_gap;
1165 bool hasHole = aPad->GetDrillSize().x > 0;
1166 bool flashLayer = aPad->FlashLayer( aLayer );
1167 bool platedHole = hasHole && aPad->GetAttribute() == PAD_ATTRIB::PTH;
1168
1169 if( flashLayer || platedHole )
1170 {
1171 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1172 aZone, aPad, aLayer ) );
1173 }
1174
1175 if( flashLayer && gap >= 0 )
1176 addKnockout( aPad, aLayer, gap + extra_margin, aHoles );
1177
1178 if( hasHole )
1179 {
1180 // NPTH do not need copper clearance gaps to their holes
1181 if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
1182 gap = init_gap;
1183
1184 gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
1185 aZone, aPad, aLayer ) );
1186
1187 gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
1188 aZone, aPad, aLayer ) );
1189
1190 if( gap >= 0 )
1191 addHoleKnockout( aPad, gap + extra_margin, aHoles );
1192 }
1193 };
1194
1195 for( PAD* pad : aNoConnectionPads )
1196 {
1197 if( checkForCancel( m_progressReporter ) )
1198 return;
1199
1200 knockoutPadClearance( pad );
1201 }
1202
1203 // Add non-connected track clearances
1204 //
1205 auto knockoutTrackClearance =
1206 [&]( PCB_TRACK* aTrack )
1207 {
1208 if( aTrack->GetBoundingBox().Intersects( zone_boundingbox ) )
1209 {
1210 bool sameNet = aTrack->GetNetCode() == aZone->GetNetCode();
1211
1212 if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
1213 sameNet = false;
1214
1215 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
1216 aZone, aTrack, aLayer );
1217
1218 if( aTrack->Type() == PCB_VIA_T )
1219 {
1220 PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
1221
1222 if( via->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
1223 sameNet = false;
1224 }
1225
1226 if( !sameNet )
1227 {
1228 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1229 aZone, aTrack, aLayer ) );
1230 }
1231
1232 if( aTrack->Type() == PCB_VIA_T )
1233 {
1234 PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
1235
1236 if( via->FlashLayer( aLayer ) && gap > 0 )
1237 {
1238 via->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
1240 }
1241
1242 gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
1243 aZone, via, aLayer ) );
1244
1245 if( !sameNet )
1246 {
1247 gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
1248 aZone, via, aLayer ) );
1249 }
1250
1251 if( gap >= 0 )
1252 {
1253 int radius = via->GetDrillValue() / 2;
1254
1255 TransformCircleToPolygon( aHoles, via->GetPosition(),
1256 radius + gap + extra_margin,
1258 }
1259 }
1260 else
1261 {
1262 if( gap >= 0 )
1263 {
1264 aTrack->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
1266 }
1267 }
1268 }
1269 };
1270
1271 for( PCB_TRACK* track : m_board->Tracks() )
1272 {
1273 if( !track->IsOnLayer( aLayer ) )
1274 continue;
1275
1276 if( checkForCancel( m_progressReporter ) )
1277 return;
1278
1279 knockoutTrackClearance( track );
1280 }
1281
1282 // Add graphic item clearances.
1283 //
1284 auto knockoutGraphicClearance =
1285 [&]( BOARD_ITEM* aItem )
1286 {
1287 int shapeNet = -1;
1288
1289 if( aItem->Type() == PCB_SHAPE_T )
1290 shapeNet = static_cast<PCB_SHAPE*>( aItem )->GetNetCode();
1291
1292 bool sameNet = shapeNet == aZone->GetNetCode();
1293
1294 if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
1295 sameNet = false;
1296
1297 // A item on the Edge_Cuts or Margin is always seen as on any layer:
1298 if( aItem->IsOnLayer( aLayer )
1299 || aItem->IsOnLayer( Edge_Cuts )
1300 || aItem->IsOnLayer( Margin ) )
1301 {
1302 if( aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
1303 {
1304 bool ignoreLineWidths = false;
1305 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
1306 aZone, aItem, aLayer );
1307
1308 if( aItem->IsOnLayer( aLayer ) && !sameNet )
1309 {
1310 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1311 aZone, aItem, aLayer ) );
1312 }
1313 else if( aItem->IsOnLayer( Edge_Cuts ) )
1314 {
1315 gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
1316 aZone, aItem, aLayer ) );
1317 ignoreLineWidths = true;
1318 }
1319 else if( aItem->IsOnLayer( Margin ) )
1320 {
1321 gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
1322 aZone, aItem, aLayer ) );
1323 }
1324
1325 if( gap >= 0 )
1326 {
1327 gap += extra_margin;
1328 addKnockout( aItem, aLayer, gap, ignoreLineWidths, aHoles );
1329 }
1330 }
1331 }
1332 };
1333
1334 auto knockoutCourtyardClearance =
1335 [&]( FOOTPRINT* aFootprint )
1336 {
1337 if( aFootprint->GetBoundingBox().Intersects( zone_boundingbox ) )
1338 {
1339 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone,
1340 aFootprint, aLayer );
1341
1342 if( gap == 0 )
1343 {
1344 aHoles.Append( aFootprint->GetCourtyard( aLayer ) );
1345 }
1346 else if( gap > 0 )
1347 {
1348 SHAPE_POLY_SET hole = aFootprint->GetCourtyard( aLayer );
1349 hole.Inflate( gap, CORNER_STRATEGY::ROUND_ALL_CORNERS, m_maxError );
1350 aHoles.Append( hole );
1351 }
1352 }
1353 };
1354
1355 for( FOOTPRINT* footprint : m_board->Footprints() )
1356 {
1357 knockoutCourtyardClearance( footprint );
1358 knockoutGraphicClearance( &footprint->Reference() );
1359 knockoutGraphicClearance( &footprint->Value() );
1360
1361 std::set<PAD*> allowedNetTiePads;
1362
1363 // Don't knock out holes for graphic items which implement a net-tie to the zone's net
1364 // on the layer being filled.
1365 if( footprint->IsNetTie() )
1366 {
1367 for( PAD* pad : footprint->Pads() )
1368 {
1369 bool sameNet = pad->GetNetCode() == aZone->GetNetCode();
1370
1371 if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
1372 sameNet = false;
1373
1374 if( sameNet )
1375 {
1376 if( pad->IsOnLayer( aLayer ) )
1377 allowedNetTiePads.insert( pad );
1378
1379 for( PAD* other : footprint->GetNetTiePads( pad ) )
1380 {
1381 if( other->IsOnLayer( aLayer ) )
1382 allowedNetTiePads.insert( other );
1383 }
1384 }
1385 }
1386 }
1387
1388 for( BOARD_ITEM* item : footprint->GraphicalItems() )
1389 {
1390 if( checkForCancel( m_progressReporter ) )
1391 return;
1392
1393 BOX2I itemBBox = item->GetBoundingBox();
1394
1395 if( !zone_boundingbox.Intersects( itemBBox ) )
1396 continue;
1397
1398 bool skipItem = false;
1399
1400 if( item->IsOnLayer( aLayer ) )
1401 {
1402 std::shared_ptr<SHAPE> itemShape = item->GetEffectiveShape();
1403
1404 for( PAD* pad : allowedNetTiePads )
1405 {
1406 if( pad->GetBoundingBox().Intersects( itemBBox )
1407 && pad->GetEffectiveShape()->Collide( itemShape.get() ) )
1408 {
1409 skipItem = true;
1410 break;
1411 }
1412 }
1413 }
1414
1415 if( !skipItem )
1416 knockoutGraphicClearance( item );
1417 }
1418 }
1419
1420 for( BOARD_ITEM* item : m_board->Drawings() )
1421 {
1422 if( checkForCancel( m_progressReporter ) )
1423 return;
1424
1425 knockoutGraphicClearance( item );
1426 }
1427
1428 // Add non-connected zone clearances
1429 //
1430 auto knockoutZoneClearance =
1431 [&]( ZONE* aKnockout )
1432 {
1433 // If the zones share no common layers
1434 if( !aKnockout->GetLayerSet().test( aLayer ) )
1435 return;
1436
1437 if( aKnockout->GetBoundingBox().Intersects( zone_boundingbox ) )
1438 {
1439 if( aKnockout->GetIsRuleArea() )
1440 {
1441 // Keepouts use outline with no clearance
1442 aKnockout->TransformSmoothedOutlineToPolygon( aHoles, 0, m_maxError,
1443 ERROR_OUTSIDE, nullptr );
1444 }
1445 else
1446 {
1447 int gap = std::max( 0, evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
1448 aZone, aKnockout, aLayer ) );
1449
1450 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1451 aZone, aKnockout, aLayer ) );
1452
1453 SHAPE_POLY_SET poly;
1454 aKnockout->TransformShapeToPolygon( poly, aLayer, gap + extra_margin,
1456 aHoles.Append( poly );
1457 }
1458 }
1459 };
1460
1461 for( ZONE* otherZone : m_board->Zones() )
1462 {
1463 if( checkForCancel( m_progressReporter ) )
1464 return;
1465
1466 // Negative clearance permits zones to short
1467 if( evalRulesForItems( CLEARANCE_CONSTRAINT, aZone, otherZone, aLayer ) < 0 )
1468 continue;
1469
1470 if( otherZone->GetIsRuleArea() )
1471 {
1472 if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
1473 knockoutZoneClearance( otherZone );
1474 }
1475 else if( otherZone->HigherPriority( aZone ) )
1476 {
1477 if( !otherZone->SameNet( aZone ) )
1478 knockoutZoneClearance( otherZone );
1479 }
1480 }
1481
1482 for( FOOTPRINT* footprint : m_board->Footprints() )
1483 {
1484 for( ZONE* otherZone : footprint->Zones() )
1485 {
1486 if( checkForCancel( m_progressReporter ) )
1487 return;
1488
1489 if( otherZone->GetIsRuleArea() )
1490 {
1491 if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
1492 knockoutZoneClearance( otherZone );
1493 }
1494 else if( otherZone->HigherPriority( aZone ) )
1495 {
1496 if( !otherZone->SameNet( aZone ) )
1497 knockoutZoneClearance( otherZone );
1498 }
1499 }
1500 }
1501
1503}
1504
1505
1511 SHAPE_POLY_SET& aRawFill )
1512{
1513 BOX2I zoneBBox = aZone->GetBoundingBox();
1514
1515 auto knockoutZoneOutline =
1516 [&]( ZONE* aKnockout )
1517 {
1518 // If the zones share no common layers
1519 if( !aKnockout->GetLayerSet().test( aLayer ) )
1520 return;
1521
1522 if( aKnockout->GetBoundingBox().Intersects( zoneBBox ) )
1523 {
1524 // Processing of arc shapes in zones is not yet supported because Clipper
1525 // can't do boolean operations on them. The poly outline must be converted to
1526 // segments first.
1527 SHAPE_POLY_SET outline = aKnockout->Outline()->CloneDropTriangulation();
1528 outline.ClearArcs();
1529
1530 aRawFill.BooleanSubtract( outline, SHAPE_POLY_SET::PM_FAST );
1531 }
1532 };
1533
1534 for( ZONE* otherZone : m_board->Zones() )
1535 {
1536 // Don't use the `HigherPriority()` check here because we _only_ want to knock out zones
1537 // with explicitly higher priorities, not those with equal priorities
1538 if( otherZone->SameNet( aZone )
1539 && otherZone->GetAssignedPriority() > aZone->GetAssignedPriority() )
1540 {
1541 // Do not remove teardrop area: it is not useful and not good
1542 if( !otherZone->IsTeardropArea() )
1543 knockoutZoneOutline( otherZone );
1544 }
1545 }
1546
1547 for( FOOTPRINT* footprint : m_board->Footprints() )
1548 {
1549 for( ZONE* otherZone : footprint->Zones() )
1550 {
1551 if( otherZone->SameNet( aZone ) && otherZone->HigherPriority( aZone ) )
1552 {
1553 // Do not remove teardrop area: it is not useful and not good
1554 if( !otherZone->IsTeardropArea() )
1555 knockoutZoneOutline( otherZone );
1556 }
1557 }
1558 }
1559}
1560
1561
1562void ZONE_FILLER::connect_nearby_polys( SHAPE_POLY_SET& aPolys, double aDistance )
1563{
1564 if( aPolys.OutlineCount() < 1 )
1565 return;
1566
1567 VERTEX_CONNECTOR vs( aPolys.BBoxFromCaches(), aPolys, aDistance );
1568
1569 vs.FindResults();
1570
1571 // This cannot be a reference because we need to do the comparison below while
1572 // changing the values
1573 std::map<int, std::vector<std::pair<int, VECTOR2I>>> insertion_points;
1574
1575 for( const RESULTS& result : vs.GetResults() )
1576 {
1577 SHAPE_LINE_CHAIN& line1 = aPolys.Outline( result.m_outline1 );
1578 SHAPE_LINE_CHAIN& line2 = aPolys.Outline( result.m_outline2 );
1579
1580 VECTOR2I pt1 = line1.CPoint( result.m_vertex1 );
1581 VECTOR2I pt2 = line2.CPoint( result.m_vertex2 );
1582
1583 // We want to insert the existing point first so that we can place the new point
1584 // between the two points at the same location.
1585 insertion_points[result.m_outline1].push_back( { result.m_vertex1, pt1 } );
1586 insertion_points[result.m_outline1].push_back( { result.m_vertex1, pt2 } );
1587 }
1588
1589 for( auto& [outline, vertices] : insertion_points )
1590 {
1591 SHAPE_LINE_CHAIN& line = aPolys.Outline( outline );
1592
1593 // Stable sort here because we want to make sure that we are inserting pt1 first and
1594 // pt2 second but still sorting the rest of the indices from highest to lowest.
1595 // This allows us to insert into the existing polygon without modifying the future
1596 // insertion points.
1597 std::stable_sort( vertices.begin(), vertices.end(),
1598 []( const std::pair<int, VECTOR2I>& a, const std::pair<int, VECTOR2I>& b )
1599 { return a.first > b.first; } );
1600
1601 for( const auto& [vertex, pt] : vertices )
1602 line.Insert( vertex + 1, pt ); // +1 here because we want to insert after the existing point
1603 }
1604}
1605
1606
1607#define DUMP_POLYS_TO_COPPER_LAYER( a, b, c ) \
1608 { if( m_debugZoneFiller && aDebugLayer == b ) \
1609 { \
1610 m_board->SetLayerName( b, c ); \
1611 SHAPE_POLY_SET d = a; \
1612 d.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE ); \
1613 aFillPolys = d; \
1614 return false; \
1615 } \
1616 }
1617
1618
1630bool ZONE_FILLER::fillCopperZone( const ZONE* aZone, PCB_LAYER_ID aLayer, PCB_LAYER_ID aDebugLayer,
1631 const SHAPE_POLY_SET& aSmoothedOutline,
1632 const SHAPE_POLY_SET& aMaxExtents, SHAPE_POLY_SET& aFillPolys )
1633{
1635
1636 // Features which are min_width should survive pruning; features that are *less* than
1637 // min_width should not. Therefore we subtract epsilon from the min_width when
1638 // deflating/inflating.
1639 int half_min_width = aZone->GetMinThickness() / 2;
1640 int epsilon = pcbIUScale.mmToIU( 0.001 );
1641
1642 // Solid polygons are deflated and inflated during calculations. Deflating doesn't cause
1643 // issues, but inflate is tricky as it can create excessively long and narrow spikes for
1644 // acute angles.
1645 // ALLOW_ACUTE_CORNERS cannot be used due to the spike problem.
1646 // CHAMFER_ACUTE_CORNERS is tempting, but can still produce spikes in some unusual
1647 // circumstances (https://gitlab.com/kicad/code/kicad/-/issues/5581).
1648 // It's unclear if ROUND_ACUTE_CORNERS would have the same issues, but is currently avoided
1649 // as a "less-safe" option.
1650 // ROUND_ALL_CORNERS produces the uniformly nicest shapes, but also a lot of segments.
1651 // CHAMFER_ALL_CORNERS improves the segment count.
1652 CORNER_STRATEGY fastCornerStrategy = CORNER_STRATEGY::CHAMFER_ALL_CORNERS;
1653 CORNER_STRATEGY cornerStrategy = CORNER_STRATEGY::ROUND_ALL_CORNERS;
1654
1655 std::vector<PAD*> thermalConnectionPads;
1656 std::vector<PAD*> noConnectionPads;
1657 std::deque<SHAPE_LINE_CHAIN> thermalSpokes;
1658 SHAPE_POLY_SET clearanceHoles;
1659
1660 aFillPolys = aSmoothedOutline;
1661 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In1_Cu, wxT( "smoothed-outline" ) );
1662
1664 return false;
1665
1666 /* -------------------------------------------------------------------------------------
1667 * Knockout thermal reliefs.
1668 */
1669
1670 knockoutThermalReliefs( aZone, aLayer, aFillPolys, thermalConnectionPads, noConnectionPads );
1671 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In2_Cu, wxT( "minus-thermal-reliefs" ) );
1672
1674 return false;
1675
1676 /* -------------------------------------------------------------------------------------
1677 * Knockout electrical clearances.
1678 */
1679
1680 buildCopperItemClearances( aZone, aLayer, noConnectionPads, clearanceHoles );
1681 DUMP_POLYS_TO_COPPER_LAYER( clearanceHoles, In3_Cu, wxT( "clearance-holes" ) );
1682
1684 return false;
1685
1686 /* -------------------------------------------------------------------------------------
1687 * Add thermal relief spokes.
1688 */
1689
1690 buildThermalSpokes( aZone, aLayer, thermalConnectionPads, thermalSpokes );
1691
1693 return false;
1694
1695 // Create a temporary zone that we can hit-test spoke-ends against. It's only temporary
1696 // because the "real" subtract-clearance-holes has to be done after the spokes are added.
1697 static const bool USE_BBOX_CACHES = true;
1698 SHAPE_POLY_SET testAreas = aFillPolys.CloneDropTriangulation();
1699 testAreas.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1700 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In4_Cu, wxT( "minus-clearance-holes" ) );
1701
1702 // Prune features that don't meet minimum-width criteria
1703 if( half_min_width - epsilon > epsilon )
1704 {
1705 testAreas.Deflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
1706 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In5_Cu, wxT( "spoke-test-deflated" ) );
1707
1708 testAreas.Inflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
1709 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In6_Cu, wxT( "spoke-test-reinflated" ) );
1710 }
1711
1713 return false;
1714
1715 // Spoke-end-testing is hugely expensive so we generate cached bounding-boxes to speed
1716 // things up a bit.
1717 testAreas.BuildBBoxCaches();
1718 int interval = 0;
1719
1720 SHAPE_POLY_SET debugSpokes;
1721
1722 for( const SHAPE_LINE_CHAIN& spoke : thermalSpokes )
1723 {
1724 const VECTOR2I& testPt = spoke.CPoint( 3 );
1725
1726 // Hit-test against zone body
1727 if( testAreas.Contains( testPt, -1, 1, USE_BBOX_CACHES ) )
1728 {
1729 if( m_debugZoneFiller )
1730 debugSpokes.AddOutline( spoke );
1731
1732 aFillPolys.AddOutline( spoke );
1733 continue;
1734 }
1735
1736 if( interval++ > 400 )
1737 {
1739 return false;
1740
1741 interval = 0;
1742 }
1743
1744 // Hit-test against other spokes
1745 for( const SHAPE_LINE_CHAIN& other : thermalSpokes )
1746 {
1747 // Hit test in both directions to avoid interactions with round-off errors.
1748 // (See https://gitlab.com/kicad/code/kicad/-/issues/13316.)
1749 if( &other != &spoke
1750 && other.PointInside( testPt, 1, USE_BBOX_CACHES )
1751 && spoke.PointInside( other.CPoint( 3 ), 1, USE_BBOX_CACHES ) )
1752 {
1753 if( m_debugZoneFiller )
1754 debugSpokes.AddOutline( spoke );
1755
1756 aFillPolys.AddOutline( spoke );
1757 break;
1758 }
1759 }
1760 }
1761
1762 DUMP_POLYS_TO_COPPER_LAYER( debugSpokes, In7_Cu, wxT( "spokes" ) );
1763
1765 return false;
1766
1767 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1768 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In8_Cu, wxT( "after-spoke-trimming" ) );
1769
1770 /* -------------------------------------------------------------------------------------
1771 * Prune features that don't meet minimum-width criteria
1772 */
1773
1774 if( half_min_width - epsilon > epsilon )
1775 aFillPolys.Deflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
1776
1777 // Min-thickness is the web thickness. On the other hand, a blob min-thickness by
1778 // min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
1779 // arbitrarily choose "at least 2X min-thickness on one axis". (Since we're doing this
1780 // during the deflated state, that means we test for "at least min-thickness".)
1781 for( int ii = aFillPolys.OutlineCount() - 1; ii >= 0; ii-- )
1782 {
1783 std::vector<SHAPE_LINE_CHAIN>& island = aFillPolys.Polygon( ii );
1784 BOX2I islandExtents;
1785
1786 for( const VECTOR2I& pt : island.front().CPoints() )
1787 {
1788 islandExtents.Merge( pt );
1789
1790 if( islandExtents.GetSizeMax() > aZone->GetMinThickness() )
1791 break;
1792 }
1793
1794 if( islandExtents.GetSizeMax() < aZone->GetMinThickness() )
1795 aFillPolys.DeletePolygon( ii );
1796 }
1797
1798 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In9_Cu, wxT( "deflated" ) );
1799
1801 return false;
1802
1803
1804 /* -------------------------------------------------------------------------------------
1805 * Process the hatch pattern (note that we do this while deflated)
1806 */
1807
1808 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
1809 {
1810 if( !addHatchFillTypeOnZone( aZone, aLayer, aDebugLayer, aFillPolys ) )
1811 return false;
1812 }
1813 else
1814 {
1815
1816 /* -------------------------------------------------------------------------------------
1817 * Connect nearby polygons
1818 */
1819
1820 if( ADVANCED_CFG::GetCfg().m_ZoneConnectionFiller )
1821 {
1822 aFillPolys.Fracture( SHAPE_POLY_SET::PM_FAST );
1823 connect_nearby_polys( aFillPolys, aZone->GetMinThickness() );
1824 }
1825
1826 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In10_Cu, wxT( "connected-nearby-polys" ) );
1827 }
1828
1830 return false;
1831
1832 /* -------------------------------------------------------------------------------------
1833 * Finish minimum-width pruning by re-inflating
1834 */
1835
1836 if( half_min_width - epsilon > epsilon )
1837 aFillPolys.Inflate( half_min_width - epsilon, cornerStrategy, m_maxError, true );
1838
1839 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In15_Cu, wxT( "after-reinflating" ) );
1840
1841 /* -------------------------------------------------------------------------------------
1842 * Ensure additive changes (thermal stubs and inflating acute corners) do not add copper
1843 * outside the zone boundary, inside the clearance holes, or between otherwise isolated
1844 * islands
1845 */
1846
1847 for( PAD* pad : thermalConnectionPads )
1848 addHoleKnockout( pad, 0, clearanceHoles );
1849
1850 aFillPolys.BooleanIntersection( aMaxExtents, SHAPE_POLY_SET::PM_FAST );
1851 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In16_Cu, wxT( "after-trim-to-outline" ) );
1852 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1853 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In17_Cu, wxT( "after-trim-to-clearance-holes" ) );
1854
1855 /* -------------------------------------------------------------------------------------
1856 * Lastly give any same-net but higher-priority zones control over their own area.
1857 */
1858
1859 subtractHigherPriorityZones( aZone, aLayer, aFillPolys );
1860 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In18_Cu, wxT( "minus-higher-priority-zones" ) );
1861
1862 aFillPolys.Fracture( SHAPE_POLY_SET::PM_FAST );
1863 return true;
1864}
1865
1866
1868 const SHAPE_POLY_SET& aSmoothedOutline,
1869 SHAPE_POLY_SET& aFillPolys )
1870{
1872 BOX2I zone_boundingbox = aZone->GetBoundingBox();
1873 SHAPE_POLY_SET clearanceHoles;
1874 long ticker = 0;
1875
1876 auto checkForCancel =
1877 [&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
1878 {
1879 return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
1880 };
1881
1882 auto knockoutGraphicClearance =
1883 [&]( BOARD_ITEM* aItem )
1884 {
1885 if( aItem->IsKnockout() && aItem->IsOnLayer( aLayer )
1886 && aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
1887 {
1889 aZone, aItem, aLayer );
1890
1891 addKnockout( aItem, aLayer, cc.GetValue().Min(), false, clearanceHoles );
1892 }
1893 };
1894
1895 for( FOOTPRINT* footprint : m_board->Footprints() )
1896 {
1897 if( checkForCancel( m_progressReporter ) )
1898 return false;
1899
1900 knockoutGraphicClearance( &footprint->Reference() );
1901 knockoutGraphicClearance( &footprint->Value() );
1902
1903 for( BOARD_ITEM* item : footprint->GraphicalItems() )
1904 knockoutGraphicClearance( item );
1905 }
1906
1907 for( BOARD_ITEM* item : m_board->Drawings() )
1908 {
1909 if( checkForCancel( m_progressReporter ) )
1910 return false;
1911
1912 knockoutGraphicClearance( item );
1913 }
1914
1915 aFillPolys = aSmoothedOutline;
1916 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1917
1918 for( ZONE* keepout : m_board->Zones() )
1919 {
1920 if( !keepout->GetIsRuleArea() )
1921 continue;
1922
1923 if( keepout->GetDoNotAllowCopperPour() && keepout->IsOnLayer( aLayer ) )
1924 {
1925 if( keepout->GetBoundingBox().Intersects( zone_boundingbox ) )
1926 aFillPolys.BooleanSubtract( *keepout->Outline(), SHAPE_POLY_SET::PM_FAST );
1927 }
1928 }
1929
1930 // Features which are min_width should survive pruning; features that are *less* than
1931 // min_width should not. Therefore we subtract epsilon from the min_width when
1932 // deflating/inflating.
1933 int half_min_width = aZone->GetMinThickness() / 2;
1934 int epsilon = pcbIUScale.mmToIU( 0.001 );
1935
1936 aFillPolys.Deflate( half_min_width - epsilon, CORNER_STRATEGY::CHAMFER_ALL_CORNERS, m_maxError );
1937
1938 // Remove the non filled areas due to the hatch pattern
1939 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
1940 {
1941 if( !addHatchFillTypeOnZone( aZone, aLayer, aLayer, aFillPolys ) )
1942 return false;
1943 }
1944
1945 // Re-inflate after pruning of areas that don't meet minimum-width criteria
1946 if( half_min_width - epsilon > epsilon )
1947 aFillPolys.Inflate( half_min_width - epsilon, CORNER_STRATEGY::ROUND_ALL_CORNERS, m_maxError );
1948
1950 return true;
1951}
1952
1953
1954/*
1955 * Build the filled solid areas data from real outlines (stored in m_Poly)
1956 * The solid areas can be more than one on copper layers, and do not have holes
1957 * ( holes are linked by overlapping segments to the main outline)
1958 */
1960{
1961 SHAPE_POLY_SET* boardOutline = m_brdOutlinesValid ? &m_boardOutline : nullptr;
1962 SHAPE_POLY_SET maxExtents;
1963 SHAPE_POLY_SET smoothedPoly;
1964 PCB_LAYER_ID debugLayer = UNDEFINED_LAYER;
1965
1966 if( m_debugZoneFiller && LSET::InternalCuMask().Contains( aLayer ) )
1967 {
1968 debugLayer = aLayer;
1969 aLayer = F_Cu;
1970 }
1971
1972 if( !aZone->BuildSmoothedPoly( maxExtents, aLayer, boardOutline, &smoothedPoly ) )
1973 return false;
1974
1976 return false;
1977
1978 if( aZone->IsOnCopperLayer() )
1979 {
1980 if( fillCopperZone( aZone, aLayer, debugLayer, smoothedPoly, maxExtents, aFillPolys ) )
1981 aZone->SetNeedRefill( false );
1982 }
1983 else
1984 {
1985 if( fillNonCopperZone( aZone, aLayer, smoothedPoly, aFillPolys ) )
1986 aZone->SetNeedRefill( false );
1987 }
1988
1989 return true;
1990}
1991
1992
1997 const std::vector<PAD*>& aSpokedPadsList,
1998 std::deque<SHAPE_LINE_CHAIN>& aSpokesList )
1999{
2001 BOX2I zoneBB = aZone->GetBoundingBox();
2002 DRC_CONSTRAINT constraint;
2003 int zone_half_width = aZone->GetMinThickness() / 2;
2004
2005 zoneBB.Inflate( std::max( bds.GetBiggestClearanceValue(), aZone->GetLocalClearance().value() ) );
2006
2007 // Is a point on the boundary of the polygon inside or outside?
2008 // The boundary may be off by MaxError
2009 int epsilon = bds.m_MaxError;
2010
2011 for( PAD* pad : aSpokedPadsList )
2012 {
2013 // We currently only connect to pads, not pad holes
2014 if( !pad->IsOnLayer( aLayer ) )
2015 continue;
2016
2017 constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone, aLayer );
2018 int thermalReliefGap = constraint.GetValue().Min();
2019
2020 constraint = bds.m_DRCEngine->EvalRules( THERMAL_SPOKE_WIDTH_CONSTRAINT, pad, aZone, aLayer );
2021 int spoke_w = constraint.GetValue().Opt();
2022
2023 // Spoke width should ideally be smaller than the pad minor axis.
2024 // Otherwise the thermal shape is not really a thermal relief,
2025 // and the algo to count the actual number of spokes can fail
2026 int spoke_max_allowed_w = std::min( pad->GetSize().x, pad->GetSize().y );
2027
2028 spoke_w = alg::clamp( constraint.Value().Min(), spoke_w, constraint.Value().Max() );
2029
2030 // ensure the spoke width is smaller than the pad minor size
2031 spoke_w = std::min( spoke_w, spoke_max_allowed_w );
2032
2033 // Cannot create stubs having a width < zone min thickness
2034 if( spoke_w < aZone->GetMinThickness() )
2035 continue;
2036
2037 int spoke_half_w = spoke_w / 2;
2038
2039 // Quick test here to possibly save us some work
2040 BOX2I itemBB = pad->GetBoundingBox();
2041 itemBB.Inflate( thermalReliefGap + epsilon );
2042
2043 if( !( itemBB.Intersects( zoneBB ) ) )
2044 continue;
2045
2046 bool customSpokes = false;
2047
2048 if( pad->GetShape() == PAD_SHAPE::CUSTOM )
2049 {
2050 for( const std::shared_ptr<PCB_SHAPE>& primitive : pad->GetPrimitives() )
2051 {
2052 if( primitive->IsProxyItem() && primitive->GetShape() == SHAPE_T::SEGMENT )
2053 {
2054 customSpokes = true;
2055 break;
2056 }
2057 }
2058 }
2059
2060 // Thermal spokes consist of square-ended segments from the pad center to points just
2061 // outside the thermal relief. The outside end has an extra center point (which must be
2062 // at idx 3) which is used for testing whether or not the spoke connects to copper in the
2063 // parent zone.
2064
2065 auto buildSpokesFromOrigin =
2066 [&]( const BOX2I& box, EDA_ANGLE angle )
2067 {
2068 VECTOR2I center = box.GetCenter();
2069 VECTOR2I half_size( box.GetWidth() / 2, box.GetHeight() / 2 );
2070
2071 // Function to find intersection of line with box edge
2072 auto intersectLineBox = [&](const VECTOR2D& direction) -> VECTOR2I {
2073 double dx = direction.x;
2074 double dy = direction.y;
2075
2076 // Shortcircuit the axis cases because they will be degenerate in the
2077 // intersection test
2078 if( direction.x == 0 )
2079 {
2080 return VECTOR2I(0, dy * half_size.y );
2081 }
2082 else if( direction.y == 0 )
2083 {
2084 return VECTOR2I(dx * half_size.x, 0);
2085 }
2086
2087 // We are going to intersect with one side or the other. Whichever
2088 // we hit first is the fraction of the spoke length we keep
2089 double tx = std::min( half_size.x / std::abs( dx ),
2090 half_size.y / std::abs( dy ) );
2091 return VECTOR2I( dx * tx, dy * tx );
2092 };
2093
2094 // Precalculate angles for four cardinal directions
2095 const EDA_ANGLE angles[4] = {
2096 EDA_ANGLE( 0.0, DEGREES_T ) + angle, // Right
2097 EDA_ANGLE( 90.0, DEGREES_T ) + angle, // Up
2098 EDA_ANGLE( 180.0, DEGREES_T ) + angle, // Left
2099 EDA_ANGLE( 270.0, DEGREES_T ) + angle // Down
2100 };
2101
2102 // Generate four spokes in cardinal directions
2103 for( const EDA_ANGLE& spokeAngle : angles )
2104 {
2105 VECTOR2D direction( spokeAngle.Cos(), spokeAngle.Sin() );
2106 VECTOR2D perpendicular = direction.Perpendicular();
2107
2108 VECTOR2I intersection = intersectLineBox( direction );
2109 VECTOR2I spoke_side = perpendicular.Resize( spoke_half_w );
2110
2111 SHAPE_LINE_CHAIN spoke;
2112 spoke.Append( center + spoke_side );
2113 spoke.Append( center - spoke_side );
2114 spoke.Append( center + intersection - spoke_side );
2115 spoke.Append( center + intersection ); // test pt
2116 spoke.Append( center + intersection + spoke_side );
2117 spoke.SetClosed( true );
2118 aSpokesList.push_back( std::move( spoke ) );
2119 }
2120 };
2121
2122 if( customSpokes )
2123 {
2124 SHAPE_POLY_SET thermalPoly;
2125 SHAPE_LINE_CHAIN thermalOutline;
2126
2127 pad->TransformShapeToPolygon( thermalPoly, aLayer, thermalReliefGap + epsilon,
2129
2130 if( thermalPoly.OutlineCount() )
2131 thermalOutline = thermalPoly.Outline( 0 );
2132
2133 for( const std::shared_ptr<PCB_SHAPE>& primitive : pad->GetPrimitives() )
2134 {
2135 if( primitive->IsProxyItem() && primitive->GetShape() == SHAPE_T::SEGMENT )
2136 {
2137 SEG seg( primitive->GetStart(), primitive->GetEnd() );
2138 SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
2139
2140 RotatePoint( seg.A, pad->GetOrientation() );
2141 RotatePoint( seg.B, pad->GetOrientation() );
2142 seg.A += pad->ShapePos();
2143 seg.B += pad->ShapePos();
2144
2145 // Make sure seg.A is the origin
2146 if( !pad->GetEffectivePolygon( ERROR_OUTSIDE )->Contains( seg.A ) )
2147 seg.Reverse();
2148
2149 // Trim seg.B to the thermal outline
2150 if( thermalOutline.Intersect( seg, intersections ) )
2151 {
2152 seg.B = intersections.front().p;
2153
2154 VECTOR2I offset = ( seg.B - seg.A ).Perpendicular().Resize( spoke_half_w );
2155 SHAPE_LINE_CHAIN spoke;
2156
2157 spoke.Append( seg.A + offset );
2158 spoke.Append( seg.A - offset );
2159 spoke.Append( seg.B - offset );
2160 spoke.Append( seg.B ); // test pt
2161 spoke.Append( seg.B + offset );
2162
2163 spoke.SetClosed( true );
2164 aSpokesList.push_back( std::move( spoke ) );
2165 }
2166 }
2167 }
2168 }
2169 else
2170 {
2171 // Since the bounding-box needs to be correclty rotated we use a dummy pad to keep
2172 // from dirtying the real pad's cached shapes.
2173 PAD dummy_pad( *pad );
2174 dummy_pad.SetOrientation( ANGLE_0 );
2175
2176 // Spokes are from center of pad shape, not from hole. So the dummy pad has no shape
2177 // offset and is at position 0,0
2178 dummy_pad.SetPosition( VECTOR2I( 0, 0 ) );
2179 dummy_pad.SetOffset( VECTOR2I( 0, 0 ) );
2180
2181 BOX2I spokesBox = dummy_pad.GetBoundingBox();
2182
2183 //Add the half width of the zone mininum width to the inflate amount to account for the fact that
2184 //the deflation procedure will shrink the results by half the half the zone min width
2185 spokesBox.Inflate( thermalReliefGap + epsilon + zone_half_width );
2186
2187 // This is a touchy case because the bounding box for circles overshoots the mark
2188 // when rotated at 45 degrees. So we just build spokes at 0 degrees and rotate
2189 // them later.
2190 if( pad->GetShape() == PAD_SHAPE::CIRCLE
2191 || ( pad->GetShape() == PAD_SHAPE::OVAL && pad->GetSizeX() == pad->GetSizeY() ) )
2192 {
2193 buildSpokesFromOrigin( spokesBox, ANGLE_0 );
2194
2195 if( pad->GetThermalSpokeAngle() != ANGLE_0 )
2196 {
2197 //Rotate the last four elements of aspokeslist
2198 for( auto it = aSpokesList.rbegin(); it != aSpokesList.rbegin() + 4; ++it )
2199 it->Rotate( pad->GetThermalSpokeAngle() );
2200 }
2201 }
2202 else
2203 {
2204 buildSpokesFromOrigin( spokesBox, pad->GetThermalSpokeAngle() );
2205 }
2206
2207 auto spokeIter = aSpokesList.rbegin();
2208
2209 for( int ii = 0; ii < 4; ++ii, ++spokeIter )
2210 {
2211 spokeIter->Rotate( pad->GetOrientation() );
2212 spokeIter->Move( pad->ShapePos() );
2213 }
2214
2215 // Remove group membership from dummy item before deleting
2216 dummy_pad.SetParentGroup( nullptr );
2217 }
2218 }
2219
2220 for( size_t ii = 0; ii < aSpokesList.size(); ++ii )
2221 aSpokesList[ii].GenerateBBoxCache();
2222}
2223
2224
2226 PCB_LAYER_ID aDebugLayer, SHAPE_POLY_SET& aFillPolys )
2227{
2228 // Build grid:
2229
2230 // obviously line thickness must be > zone min thickness.
2231 // It can happens if a board file was edited by hand by a python script
2232 // Use 1 micron margin to be *sure* there is no issue in Gerber files
2233 // (Gbr file unit = 1 or 10 nm) due to some truncation in coordinates or calculations
2234 // This margin also avoid problems due to rounding coordinates in next calculations
2235 // that can create incorrect polygons
2236 int thickness = std::max( aZone->GetHatchThickness(),
2237 aZone->GetMinThickness() + pcbIUScale.mmToIU( 0.001 ) );
2238
2239 int linethickness = thickness - aZone->GetMinThickness();
2240 int gridsize = thickness + aZone->GetHatchGap();
2241 int maxError = m_board->GetDesignSettings().m_MaxError;
2242
2243 SHAPE_POLY_SET filledPolys = aFillPolys.CloneDropTriangulation();
2244 // Use a area that contains the rotated bbox by orientation, and after rotate the result
2245 // by -orientation.
2246 if( !aZone->GetHatchOrientation().IsZero() )
2247 filledPolys.Rotate( - aZone->GetHatchOrientation() );
2248
2249 BOX2I bbox = filledPolys.BBox( 0 );
2250
2251 // Build hole shape
2252 // the hole size is aZone->GetHatchGap(), but because the outline thickness
2253 // is aZone->GetMinThickness(), the hole shape size must be larger
2254 SHAPE_LINE_CHAIN hole_base;
2255 int hole_size = aZone->GetHatchGap() + aZone->GetMinThickness();
2256 VECTOR2I corner( 0, 0 );;
2257 hole_base.Append( corner );
2258 corner.x += hole_size;
2259 hole_base.Append( corner );
2260 corner.y += hole_size;
2261 hole_base.Append( corner );
2262 corner.x = 0;
2263 hole_base.Append( corner );
2264 hole_base.SetClosed( true );
2265
2266 // Calculate minimal area of a grid hole.
2267 // All holes smaller than a threshold will be removed
2268 double minimal_hole_area = hole_base.Area() * aZone->GetHatchHoleMinArea();
2269
2270 // Now convert this hole to a smoothed shape:
2271 if( aZone->GetHatchSmoothingLevel() > 0 )
2272 {
2273 // the actual size of chamfer, or rounded corner radius is the half size
2274 // of the HatchFillTypeGap scaled by aZone->GetHatchSmoothingValue()
2275 // aZone->GetHatchSmoothingValue() = 1.0 is the max value for the chamfer or the
2276 // radius of corner (radius = half size of the hole)
2277 int smooth_value = KiROUND( aZone->GetHatchGap()
2278 * aZone->GetHatchSmoothingValue() / 2 );
2279
2280 // Minimal optimization:
2281 // make smoothing only for reasonable smooth values, to avoid a lot of useless segments
2282 // and if the smooth value is small, use chamfer even if fillet is requested
2283 #define SMOOTH_MIN_VAL_MM 0.02
2284 #define SMOOTH_SMALL_VAL_MM 0.04
2285
2286 if( smooth_value > pcbIUScale.mmToIU( SMOOTH_MIN_VAL_MM ) )
2287 {
2288 SHAPE_POLY_SET smooth_hole;
2289 smooth_hole.AddOutline( hole_base );
2290 int smooth_level = aZone->GetHatchSmoothingLevel();
2291
2292 if( smooth_value < pcbIUScale.mmToIU( SMOOTH_SMALL_VAL_MM ) && smooth_level > 1 )
2293 smooth_level = 1;
2294
2295 // Use a larger smooth_value to compensate the outline tickness
2296 // (chamfer is not visible is smooth value < outline thickess)
2297 smooth_value += aZone->GetMinThickness() / 2;
2298
2299 // smooth_value cannot be bigger than the half size oh the hole:
2300 smooth_value = std::min( smooth_value, aZone->GetHatchGap() / 2 );
2301
2302 // the error to approximate a circle by segments when smoothing corners by a arc
2303 maxError = std::max( maxError * 2, smooth_value / 20 );
2304
2305 switch( smooth_level )
2306 {
2307 case 1:
2308 // Chamfer() uses the distance from a corner to create a end point
2309 // for the chamfer.
2310 hole_base = smooth_hole.Chamfer( smooth_value ).Outline( 0 );
2311 break;
2312
2313 default:
2314 if( aZone->GetHatchSmoothingLevel() > 2 )
2315 maxError /= 2; // Force better smoothing
2316
2317 hole_base = smooth_hole.Fillet( smooth_value, maxError ).Outline( 0 );
2318 break;
2319
2320 case 0:
2321 break;
2322 };
2323 }
2324 }
2325
2326 // Build holes
2327 SHAPE_POLY_SET holes;
2328
2329 for( int xx = 0; ; xx++ )
2330 {
2331 int xpos = xx * gridsize;
2332
2333 if( xpos > bbox.GetWidth() )
2334 break;
2335
2336 for( int yy = 0; ; yy++ )
2337 {
2338 int ypos = yy * gridsize;
2339
2340 if( ypos > bbox.GetHeight() )
2341 break;
2342
2343 // Generate hole
2344 SHAPE_LINE_CHAIN hole( hole_base );
2345 hole.Move( VECTOR2I( xpos, ypos ) );
2346 holes.AddOutline( hole );
2347 }
2348 }
2349
2350 holes.Move( bbox.GetPosition() );
2351
2352 if( !aZone->GetHatchOrientation().IsZero() )
2353 holes.Rotate( aZone->GetHatchOrientation() );
2354
2355 DUMP_POLYS_TO_COPPER_LAYER( holes, In10_Cu, wxT( "hatch-holes" ) );
2356
2357 int outline_margin = aZone->GetMinThickness() * 1.1;
2358
2359 // Using GetHatchThickness() can look more consistent than GetMinThickness().
2360 if( aZone->GetHatchBorderAlgorithm() && aZone->GetHatchThickness() > outline_margin )
2361 outline_margin = aZone->GetHatchThickness();
2362
2363 // The fill has already been deflated to ensure GetMinThickness() so we just have to
2364 // account for anything beyond that.
2365 SHAPE_POLY_SET deflatedFilledPolys = aFillPolys.CloneDropTriangulation();
2366 deflatedFilledPolys.Deflate( outline_margin - aZone->GetMinThickness(),
2367 CORNER_STRATEGY::CHAMFER_ALL_CORNERS, maxError );
2368 holes.BooleanIntersection( deflatedFilledPolys, SHAPE_POLY_SET::PM_FAST );
2369 DUMP_POLYS_TO_COPPER_LAYER( holes, In11_Cu, wxT( "fill-clipped-hatch-holes" ) );
2370
2371 SHAPE_POLY_SET deflatedOutline = aZone->Outline()->CloneDropTriangulation();
2372 deflatedOutline.Deflate( outline_margin, CORNER_STRATEGY::CHAMFER_ALL_CORNERS, maxError );
2373 holes.BooleanIntersection( deflatedOutline, SHAPE_POLY_SET::PM_FAST );
2374 DUMP_POLYS_TO_COPPER_LAYER( holes, In12_Cu, wxT( "outline-clipped-hatch-holes" ) );
2375
2376 if( aZone->GetNetCode() != 0 )
2377 {
2378 // Vias and pads connected to the zone must not be allowed to become isolated inside
2379 // one of the holes. Effectively this means their copper outline needs to be expanded
2380 // to be at least as wide as the gap so that it is guaranteed to touch at least one
2381 // edge.
2382 BOX2I zone_boundingbox = aZone->GetBoundingBox();
2383 SHAPE_POLY_SET aprons;
2384 int min_apron_radius = ( aZone->GetHatchGap() * 10 ) / 19;
2385
2386 for( PCB_TRACK* track : m_board->Tracks() )
2387 {
2388 if( track->Type() == PCB_VIA_T )
2389 {
2390 PCB_VIA* via = static_cast<PCB_VIA*>( track );
2391
2392 if( via->GetNetCode() == aZone->GetNetCode()
2393 && via->IsOnLayer( aLayer )
2394 && via->GetBoundingBox().Intersects( zone_boundingbox ) )
2395 {
2396 int r = std::max( min_apron_radius,
2397 via->GetDrillValue() / 2 + outline_margin );
2398
2399 TransformCircleToPolygon( aprons, via->GetPosition(), r, maxError,
2400 ERROR_OUTSIDE );
2401 }
2402 }
2403 }
2404
2405 for( FOOTPRINT* footprint : m_board->Footprints() )
2406 {
2407 for( PAD* pad : footprint->Pads() )
2408 {
2409 if( pad->GetNetCode() == aZone->GetNetCode()
2410 && pad->IsOnLayer( aLayer )
2411 && pad->GetBoundingBox().Intersects( zone_boundingbox ) )
2412 {
2413 // What we want is to bulk up the pad shape so that the narrowest bit of
2414 // copper between the hole and the apron edge is at least outline_margin
2415 // wide (and that the apron itself meets min_apron_radius. But that would
2416 // take a lot of code and math, and the following approximation is close
2417 // enough.
2418 int pad_width = std::min( pad->GetSize().x, pad->GetSize().y );
2419 int slot_width = std::min( pad->GetDrillSize().x, pad->GetDrillSize().y );
2420 int min_annular_ring_width = ( pad_width - slot_width ) / 2;
2421 int clearance = std::max( min_apron_radius - pad_width / 2,
2422 outline_margin - min_annular_ring_width );
2423
2424 clearance = std::max( 0, clearance - linethickness / 2 );
2425 pad->TransformShapeToPolygon( aprons, aLayer, clearance, maxError,
2426 ERROR_OUTSIDE );
2427 }
2428 }
2429 }
2430
2432 }
2433 DUMP_POLYS_TO_COPPER_LAYER( holes, In13_Cu, wxT( "pad-via-clipped-hatch-holes" ) );
2434
2435 // Now filter truncated holes to avoid small holes in pattern
2436 // It happens for holes near the zone outline
2437 for( int ii = 0; ii < holes.OutlineCount(); )
2438 {
2439 double area = holes.Outline( ii ).Area();
2440
2441 if( area < minimal_hole_area ) // The current hole is too small: remove it
2442 holes.DeletePolygon( ii );
2443 else
2444 ++ii;
2445 }
2446
2447 // create grid. Use SHAPE_POLY_SET::PM_STRICTLY_SIMPLE to
2448 // generate strictly simple polygons needed by Gerber files and Fracture()
2449 aFillPolys.BooleanSubtract( aFillPolys, holes, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
2450 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In14_Cu, wxT( "after-hatching" ) );
2451
2452 return true;
2453}
constexpr int ARC_HIGH_DEF
Definition: base_units.h:120
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
@ ZLO_FORCE_NO_ZONE_CONNECTION
Definition: board_item.h:69
@ ZLO_FORCE_FLASHED
Definition: board_item.h:68
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Container for design settings for a BOARD object.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:92
virtual void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the item shape to a closed polygon.
Definition: board_item.cpp:221
virtual void SetIsKnockout(bool aKnockout)
Definition: board_item.h:313
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:47
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:288
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition: board.cpp:2451
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:755
const ZONES & Zones() const
Definition: board.h:333
int GetMaxClearanceValue() const
Returns the maximum clearance value for any object on the board.
Definition: board.cpp:878
int GetCopperLayerCount() const
Definition: board.cpp:731
const FOOTPRINTS & Footprints() const
Definition: board.h:329
const TRACKS & Tracks() const
Definition: board.h:327
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:872
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:466
const DRAWINGS & Drawings() const
Definition: board.h:331
const Vec & GetPosition() const
Definition: box2.h:201
int GetSizeMax() const
Definition: box2.h:225
size_type GetHeight() const
Definition: box2.h:205
const Vec GetCenter() const
Definition: box2.h:220
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:294
size_type GetWidth() const
Definition: box2.h:204
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:541
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:623
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
MINOPTMAX< int > & Value()
Definition: drc_rule.h:143
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:142
ZONE_CONNECTION m_ZoneConnection
Definition: drc_rule.h:175
bool IsNull() const
Definition: drc_rule.h:137
bool IsZero() const
Definition: eda_angle.h:133
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: kidialog.cpp:51
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Shows the 'do not show again' checkbox.
Definition: kidialog.h:53
int ShowModal() override
Definition: kidialog.cpp:95
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:391
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: lset.h:81
static LSET InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
Definition: lset.cpp:726
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:737
T Min() const
Definition: minoptmax.h:33
T Max() const
Definition: minoptmax.h:34
T Opt() const
Definition: minoptmax.h:35
Definition: pad.h:54
const BOX2I GetBoundingBox() const override
The bounding box is cached, so this will be efficient most of the time.
Definition: pad.cpp:736
void SetOffset(const VECTOR2I &aOffset)
Definition: pad.h:273
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc=ERROR_INSIDE, bool ignoreLineWidth=false) const override
Convert the pad shape to a closed polygon.
Definition: pad.cpp:1822
void SetPosition(const VECTOR2I &aPos) override
Definition: pad.h:195
PAD_SHAPE GetShape() const
Definition: pad.h:193
PADSTACK::CUSTOM_SHAPE_ZONE_MODE GetCustomShapeInZoneOpt() const
Definition: pad.h:214
void SetOrientation(const EDA_ANGLE &aAngle)
Set the rotation angle of the pad.
Definition: pad.cpp:801
bool TransformHoleToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Build the corner list of the polygonal drill shape in the board coordinate system.
Definition: pad.cpp:1805
Abstract dimension API.
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool aIgnoreLineWidth=false) const override
Convert the item shape to a closed polygon.
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc, bool aIgnoreLineWidth=false) const override
Convert the item shape to a closed polygon.
Definition: pcb_text.cpp:627
A progress reporter interface for use in multi-threaded environments.
virtual bool IsCancelled() const =0
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
virtual void AdvanceProgress()=0
Increment the progress bar length (inside the current virtual zone).
virtual void SetMaxProgress(int aMaxProgress)=0
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
int m_vertex2
Definition: zone_filler.cpp:78
RESULTS(int aOutline1, int aOutline2, int aVertex1, int aVertex2)
Definition: zone_filler.cpp:58
int m_outline2
Definition: zone_filler.cpp:76
int m_outline1
Definition: zone_filler.cpp:75
int m_vertex1
Definition: zone_filler.cpp:77
bool operator<(const RESULTS &aOther) const
Definition: zone_filler.cpp:64
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I::extended_type ecoord
Definition: seg.h:44
VECTOR2I B
Definition: seg.h:50
static SEG::ecoord Square(int a)
Definition: seg.h:123
void Reverse()
Definition: seg.h:361
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Move(const VECTOR2I &aVector) override
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Find all intersection points between our line chain and the segment aSeg.
double Area(bool aAbsolute=true) const
Return the area of this chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
void Insert(size_t aVertex, const VECTOR2I &aP)
std::vector< INTERSECTION > INTERSECTIONS
Represent a set of closed polygons.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
void RemoveAllContours()
Remove all outlines & holes (clears) the polygon set.
SHAPE_POLY_SET Chamfer(int aDistance)
Return a chamfered version of the polygon set.
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset difference For aFastMode meaning, see function booleanOp.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
void DeletePolygon(int aIdx)
Delete aIdx-th polygon from the set.
double Area()
Return the area of this poly set.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
void BooleanIntersection(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset intersection For aFastMode meaning, see function booleanOp.
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
void Simplify(POLYGON_MODE aFastMode)
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFastMo...
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
void BuildBBoxCaches() const
Construct BBoxCaches for Contains(), below.
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET Fillet(int aRadius, int aErrorMax)
Return a filleted version of the polygon set.
void Move(const VECTOR2I &aVector) override
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Return true if a given subpolygon contains the point aP.
SHAPE_POLY_SET CloneDropTriangulation() const
const BOX2I BBoxFromCaches() const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:302
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:309
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:380
VERTEX * getPoint(VERTEX *aPt) const
Definition: zone_filler.cpp:96
std::set< RESULTS > GetResults() const
VERTEX_CONNECTOR(const BOX2I &aBBox, const SHAPE_POLY_SET &aPolys, int aDist)
Definition: zone_filler.cpp:84
std::set< RESULTS > m_results
std::deque< VERTEX > m_vertices
Definition: vertex_set.h:339
int32_t zOrder(const double aX, const double aY) const
Note that while the inputs are doubles, these are scaled by the size of the bounding box to fit into ...
Definition: vertex_set.cpp:61
VERTEX * createList(const SHAPE_LINE_CHAIN &points, VERTEX *aTail=nullptr, void *aUserData=nullptr)
Create a list of vertices from a line chain.
Definition: vertex_set.cpp:9
void SetBoundingBox(const BOX2I &aBBox)
Definition: vertex_set.cpp:3
const double x
Definition: vertex_set.h:231
VERTEX * next
Definition: vertex_set.h:237
VERTEX * prevZ
Definition: vertex_set.h:243
void updateList()
After inserting or changing nodes, this function should be called to remove duplicate vertices and en...
Definition: vertex_set.h:121
VERTEX * nextZ
Definition: vertex_set.h:244
VERTEX * prev
Definition: vertex_set.h:236
const int i
Definition: vertex_set.h:230
void * GetUserData() const
Definition: vertex_set.h:79
int32_t z
Definition: vertex_set.h:240
bool isEar() const
Check whether the given vertex is in the middle of an ear.
Definition: vertex_set.cpp:241
const double y
Definition: vertex_set.h:232
COMMIT * m_commit
Definition: zone_filler.h:141
int m_worstClearance
Definition: zone_filler.h:145
void buildCopperItemClearances(const ZONE *aZone, PCB_LAYER_ID aLayer, const std::vector< PAD * > &aNoConnectionPads, SHAPE_POLY_SET &aHoles)
Removes clearance from the shape for copper items which share the zone's layer but are not connected ...
void addKnockout(PAD *aPad, PCB_LAYER_ID aLayer, int aGap, SHAPE_POLY_SET &aHoles)
Add a knockout for a pad.
bool m_debugZoneFiller
Definition: zone_filler.h:147
void connect_nearby_polys(SHAPE_POLY_SET &aPolys, double aDistance)
Create strands of zero-width between elements of SHAPE_POLY_SET that are within aDistance of each oth...
ZONE_FILLER(BOARD *aBoard, COMMIT *aCommit)
void buildThermalSpokes(const ZONE *box, PCB_LAYER_ID aLayer, const std::vector< PAD * > &aSpokedPadsList, std::deque< SHAPE_LINE_CHAIN > &aSpokes)
Function buildThermalSpokes Constructs a list of all thermal spokes for the given zone.
void subtractHigherPriorityZones(const ZONE *aZone, PCB_LAYER_ID aLayer, SHAPE_POLY_SET &aRawFill)
Removes the outlines of higher-proirity zones with the same net.
SHAPE_POLY_SET m_boardOutline
Definition: zone_filler.h:139
bool m_brdOutlinesValid
Definition: zone_filler.h:140
bool addHatchFillTypeOnZone(const ZONE *aZone, PCB_LAYER_ID aLayer, PCB_LAYER_ID aDebugLayer, SHAPE_POLY_SET &aFillPolys)
for zones having the ZONE_FILL_MODE::ZONE_FILL_MODE::HATCH_PATTERN, create a grid pattern in filled a...
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
BOARD * m_board
Definition: zone_filler.h:138
void knockoutThermalReliefs(const ZONE *aZone, PCB_LAYER_ID aLayer, SHAPE_POLY_SET &aFill, std::vector< PAD * > &aThermalConnectionPads, std::vector< PAD * > &aNoConnectionPads)
Removes thermal reliefs from the shape for any pads connected to the zone.
PROGRESS_REPORTER * m_progressReporter
Definition: zone_filler.h:142
bool fillCopperZone(const ZONE *aZone, PCB_LAYER_ID aLayer, PCB_LAYER_ID aDebugLayer, const SHAPE_POLY_SET &aSmoothedOutline, const SHAPE_POLY_SET &aMaxExtents, SHAPE_POLY_SET &aFillPolys)
Function fillCopperZone Add non copper areas polygons (pads and tracks with clearance) to a filled co...
void addHoleKnockout(PAD *aPad, int aGap, SHAPE_POLY_SET &aHoles)
Add a knockout for a pad's hole.
bool fillNonCopperZone(const ZONE *aZone, PCB_LAYER_ID aLayer, const SHAPE_POLY_SET &aSmoothedOutline, SHAPE_POLY_SET &aFillPolys)
bool fillSingleZone(ZONE *aZone, PCB_LAYER_ID aLayer, SHAPE_POLY_SET &aFillPolys)
Build the filled solid areas polygons from zone outlines (stored in m_Poly) The solid areas can be mo...
bool Fill(const std::vector< ZONE * > &aZones, bool aCheck=false, wxWindow *aParent=nullptr)
Fills the given list of zones.
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
void SetNeedRefill(bool aNeedRefill)
Definition: zone.h:265
int GetHatchBorderAlgorithm() const
Definition: zone.h:303
std::optional< int > GetLocalClearance() const override
Definition: zone.cpp:493
void CacheTriangulation(PCB_LAYER_ID aLayer=UNDEFINED_LAYER)
Create a list of triangles that "fill" the solid areas used for instance to draw these solid areas on...
Definition: zone.cpp:1074
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:346
SHAPE_POLY_SET * Outline()
Definition: zone.h:337
void SetFillFlag(PCB_LAYER_ID aLayer, bool aFlag)
Definition: zone.h:259
void SetFilledPolysList(PCB_LAYER_ID aLayer, const SHAPE_POLY_SET &aPolysList)
Set the list of filled polygons.
Definition: zone.h:637
int GetMinThickness() const
Definition: zone.h:270
bool HigherPriority(const ZONE *aOther) const
Definition: zone.cpp:186
int GetHatchThickness() const
Definition: zone.h:285
double GetHatchHoleMinArea() const
Definition: zone.h:300
bool IsTeardropArea() const
Definition: zone.h:695
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:291
bool BuildSmoothedPoly(SHAPE_POLY_SET &aSmoothedPoly, PCB_LAYER_ID aLayer, SHAPE_POLY_SET *aBoardOutline, SHAPE_POLY_SET *aSmoothedPolyWithApron=nullptr) const
Definition: zone.cpp:1138
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:193
int GetHatchGap() const
Definition: zone.h:288
double GetHatchSmoothingValue() const
Definition: zone.h:297
int GetHatchSmoothingLevel() const
Definition: zone.h:294
bool IsOnCopperLayer() const override
Definition: zone.cpp:257
std::mutex & GetLock()
Definition: zone.h:249
unsigned GetAssignedPriority() const
Definition: zone.h:120
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, const VECTOR2I &aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a circle to a polygon, using multiple straight lines.
void BuildConvexHull(std::vector< VECTOR2I > &aResult, const std::vector< VECTOR2I > &aPoly)
Calculate the convex hull of a list of points in counter-clockwise order.
Definition: convex_hull.cpp:87
CORNER_STRATEGY
define how inflate transform build inflated polygon
DRC_CONSTRAINT_T
Definition: drc_rule.h:46
@ EDGE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:51
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:72
@ CLEARANCE_CONSTRAINT
Definition: drc_rule.h:48
@ THERMAL_SPOKE_WIDTH_CONSTRAINT
Definition: drc_rule.h:61
@ THERMAL_RELIEF_GAP_CONSTRAINT
Definition: drc_rule.h:60
@ HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:49
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:71
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
@ DEGREES_T
Definition: eda_angle.h:31
a few functions useful in geometry calculations.
@ ERROR_OUTSIDE
double m_ExtraClearance
When filling zones, we add an extra amount of clearance to each zone to ensure that rounding errors d...
bool m_DebugZoneFiller
A mode that dumps the various stages of a F_Cu fill into In1_Cu through In9_Cu.
This file is part of the common library.
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ In11_Cu
Definition: layer_ids.h:75
@ In17_Cu
Definition: layer_ids.h:81
@ Edge_Cuts
Definition: layer_ids.h:113
@ In9_Cu
Definition: layer_ids.h:73
@ In7_Cu
Definition: layer_ids.h:71
@ In15_Cu
Definition: layer_ids.h:79
@ In2_Cu
Definition: layer_ids.h:66
@ In10_Cu
Definition: layer_ids.h:74
@ Margin
Definition: layer_ids.h:114
@ In4_Cu
Definition: layer_ids.h:68
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ In16_Cu
Definition: layer_ids.h:80
@ In1_Cu
Definition: layer_ids.h:65
@ In13_Cu
Definition: layer_ids.h:77
@ In8_Cu
Definition: layer_ids.h:72
@ In14_Cu
Definition: layer_ids.h:78
@ In12_Cu
Definition: layer_ids.h:76
@ In6_Cu
Definition: layer_ids.h:70
@ In5_Cu
Definition: layer_ids.h:69
@ In3_Cu
Definition: layer_ids.h:67
@ F_Cu
Definition: layer_ids.h:64
@ In18_Cu
Definition: layer_ids.h:82
T clamp(T min, T value, T max)
Definition: kicad_algo.h:205
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
const double epsilon
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
A storage class for 128-bit hash value.
Definition: hash_128.h:36
A struct recording the isolated and single-pad islands within a zone.
Definition: zone.h:60
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:121
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673
#define SMOOTH_MIN_VAL_MM
#define DUMP_POLYS_TO_COPPER_LAYER(a, b, c)
#define SMOOTH_SMALL_VAL_MM
ISLAND_REMOVAL_MODE
Whether or not to remove isolated islands from a zone.
Definition: zone_settings.h:59
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition: zones.h:47