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