KiCad PCB EDA Suite
zone_filler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2017 CERN
5 * Copyright (C) 2014-2023 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 <fp_text.h>
39#include <fp_textbox.h>
42#include <board_commit.h>
43#include <progress_reporter.h>
47#include <confirm.h>
48#include <thread_pool.h>
49#include <math/util.h> // for KiROUND
50#include "zone_filler.h"
51
52
54 m_board( aBoard ),
55 m_brdOutlinesValid( false ),
56 m_commit( aCommit ),
57 m_progressReporter( nullptr ),
58 m_maxError( ARC_HIGH_DEF ),
59 m_worstClearance( 0 )
60{
61 // To enable add "DebugZoneFiller=1" to kicad_advanced settings file.
63}
64
65
67{
68}
69
70
72{
73 m_progressReporter = aReporter;
74 wxASSERT_MSG( m_commit, wxT( "ZONE_FILLER must have a valid commit to call "
75 "SetProgressReporter" ) );
76}
77
78
89bool ZONE_FILLER::Fill( std::vector<ZONE*>& aZones, bool aCheck, wxWindow* aParent )
90{
91 std::lock_guard<KISPINLOCK> lock( m_board->GetConnectivity()->GetLock() );
92
93 std::vector<std::pair<ZONE*, PCB_LAYER_ID>> toFill;
94 std::map<std::pair<ZONE*, PCB_LAYER_ID>, MD5_HASH> oldFillHashes;
95 std::vector<CN_ZONE_ISOLATED_ISLAND_LIST> islandsList;
96
97 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
98
99 // Rebuild (from scratch, ignoring dirty flags) just in case. This really needs to be reliable.
100 connectivity->ClearRatsnest();
101 connectivity->Build( m_board, m_progressReporter );
102
104
106
108 {
109 m_progressReporter->Report( aCheck ? _( "Checking zone fills..." )
110 : _( "Building zone fills..." ) );
111 m_progressReporter->SetMaxProgress( aZones.size() );
113 }
114
115 // The board outlines is used to clip solid areas inside the board (when outlines are valid)
118
119 // Update and cache zone bounding boxes and pad effective shapes so that we don't have to
120 // make them thread-safe.
121 //
122 for( ZONE* zone : m_board->Zones() )
123 {
124 zone->CacheBoundingBox();
125 m_worstClearance = std::max( m_worstClearance, zone->GetLocalClearance() );
126 }
127
128 for( FOOTPRINT* footprint : m_board->Footprints() )
129 {
130 for( PAD* pad : footprint->Pads() )
131 {
132 if( pad->IsDirty() )
133 {
134 pad->BuildEffectiveShapes( UNDEFINED_LAYER );
135 pad->BuildEffectivePolygon();
136 }
137
138 m_worstClearance = std::max( m_worstClearance, pad->GetLocalClearance() );
139 }
140
141 for( ZONE* zone : footprint->Zones() )
142 {
143 zone->CacheBoundingBox();
144 m_worstClearance = std::max( m_worstClearance, zone->GetLocalClearance() );
145 }
146
147 // Rules may depend on insideCourtyard() or other expressions
148 footprint->BuildCourtyardCaches();
149 }
150
151 LSET boardCuMask = m_board->GetEnabledLayers() & LSET::AllCuMask();
152
153 auto findHighestPriorityZone = [&]( const BOX2I& aBBox, const PCB_LAYER_ID aItemLayer,
154 const int aNetcode,
155 const std::function<bool( const ZONE* )> aTestFn ) -> ZONE*
156 {
157 unsigned highestPriority = 0;
158 ZONE* highestPriorityZone = nullptr;
159
160 for( ZONE* zone : m_board->Zones() )
161 {
162 // Rule areas are not filled
163 if( zone->GetIsRuleArea() )
164 continue;
165
166 if( zone->GetAssignedPriority() < highestPriority )
167 continue;
168
169 if( !zone->IsOnLayer( aItemLayer ) )
170 continue;
171
172 // Degenerate zones will cause trouble; skip them
173 if( zone->GetNumCorners() <= 2 )
174 continue;
175
176 if( !zone->GetBoundingBox().Intersects( aBBox ) )
177 continue;
178
179 if( !aTestFn( zone ) )
180 continue;
181
182 // Prefer highest priority and matching netcode
183 if( zone->GetAssignedPriority() > highestPriority || zone->GetNetCode() == aNetcode )
184 {
185 highestPriority = zone->GetAssignedPriority();
186 highestPriorityZone = zone;
187 }
188 }
189
190 return highestPriorityZone;
191 };
192
193 auto isInPourKeepoutArea = [&]( const BOX2I& aBBox, const PCB_LAYER_ID aItemLayer,
194 const VECTOR2I aTestPoint ) -> bool
195 {
196 for( ZONE* zone : m_board->Zones() )
197 {
198 if( !zone->GetIsRuleArea() )
199 continue;
200
201 if( !zone->GetDoNotAllowCopperPour() )
202 continue;
203
204 if( !zone->IsOnLayer( aItemLayer ) )
205 continue;
206
207 // Degenerate zones will cause trouble; skip them
208 if( zone->GetNumCorners() <= 2 )
209 continue;
210
211 if( !zone->GetBoundingBox().Intersects( aBBox ) )
212 continue;
213
214 if( zone->Outline()->Contains( aTestPoint ) )
215 return true;
216 }
217
218 return false;
219 };
220
221 // Determine state of conditional via flashing
222 for( PCB_TRACK* track : m_board->Tracks() )
223 {
224 if( track->Type() == PCB_VIA_T )
225 {
226 PCB_VIA* via = static_cast<PCB_VIA*>( track );
227
228 via->ClearZoneLayerOverrides();
229
230 if( !via->GetRemoveUnconnected() )
231 continue;
232
233 BOX2I bbox = via->GetBoundingBox();
234 VECTOR2I center = via->GetPosition();
235 int testRadius = via->GetDrillValue() / 2 + 1;
236 unsigned netcode = via->GetNetCode();
237 LSET layers = via->GetLayerSet() & boardCuMask;
238
239 // Checking if the via hole touches the zone outline
240 auto viaTestFn = [&]( const ZONE* aZone ) -> bool
241 {
242 return aZone->Outline()->Contains( center, -1, testRadius );
243 };
244
245 for( PCB_LAYER_ID layer : layers.Seq() )
246 {
247 if( !via->ConditionallyFlashed( layer ) )
248 continue;
249
250 if( isInPourKeepoutArea( bbox, layer, center ) )
251 {
252 via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
253 }
254 else
255 {
256 ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, viaTestFn );
257
258 if( zone && zone->GetNetCode() == via->GetNetCode() )
259 via->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
260 else
261 via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
262 }
263 }
264 }
265 }
266
267 // Determine state of conditional pad flashing
268 for( FOOTPRINT* footprint : m_board->Footprints() )
269 {
270 for( PAD* pad : footprint->Pads() )
271 {
272 pad->ClearZoneLayerOverrides();
273
274 if( !pad->GetRemoveUnconnected() )
275 continue;
276
277 BOX2I bbox = pad->GetBoundingBox();
278 VECTOR2I center = pad->GetPosition();
279 unsigned netcode = pad->GetNetCode();
280 LSET layers = pad->GetLayerSet() & boardCuMask;
281
282 auto padTestFn = [&]( const ZONE* aZone ) -> bool
283 {
284 return aZone->Outline()->Contains( center );
285 };
286
287 for( PCB_LAYER_ID layer : layers.Seq() )
288 {
289 if( !pad->ConditionallyFlashed( layer ) )
290 continue;
291
292 if( isInPourKeepoutArea( bbox, layer, center ) )
293 {
294 pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
295 }
296 else
297 {
298 ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, padTestFn );
299
300 if( zone && zone->GetNetCode() == pad->GetNetCode() )
301 pad->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
302 else
303 pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
304 }
305 }
306 }
307 }
308
309 for( ZONE* zone : aZones )
310 {
311 // Rule areas are not filled
312 if( zone->GetIsRuleArea() )
313 continue;
314
315 // Degenerate zones will cause trouble; skip them
316 if( zone->GetNumCorners() <= 2 )
317 continue;
318
319 if( m_commit )
320 m_commit->Modify( zone );
321
322 // calculate the hash value for filled areas. it will be used later to know if the
323 // current filled areas are up to date
324 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
325 {
326 zone->BuildHashValue( layer );
327 oldFillHashes[ { zone, layer } ] = zone->GetHashValue( layer );
328
329 // Add the zone to the list of zones to test or refill
330 toFill.emplace_back( std::make_pair( zone, layer ) );
331 }
332
333 islandsList.emplace_back( CN_ZONE_ISOLATED_ISLAND_LIST( zone ) );
334
335 // Remove existing fill first to prevent drawing invalid polygons on some platforms
336 zone->UnFill();
337 }
338
339 auto check_fill_dependency =
340 [&]( ZONE* aZone, PCB_LAYER_ID aLayer, ZONE* aOtherZone ) -> bool
341 {
342 // Check to see if we have to knock-out the filled areas of a higher-priority
343 // zone. If so we have to wait until said zone is filled before we can fill.
344
345 // If the other zone is already filled on the requested layer then we're
346 // good-to-go
347 if( aOtherZone->GetFillFlag( aLayer ) )
348 return false;
349
350 // Even if keepouts exclude copper pours, the exclusion is by outline rather than
351 // filled area, so we're good-to-go here too
352 if( aOtherZone->GetIsRuleArea() )
353 return false;
354
355 // If the other zone is never going to be filled then don't wait for it
356 if( aOtherZone->GetNumCorners() <= 2 )
357 return false;
358
359 // If the zones share no common layers
360 if( !aOtherZone->GetLayerSet().test( aLayer ) )
361 return false;
362
363 if( aZone->HigherPriority( aOtherZone ) )
364 return false;
365
366 // Same-net zones always use outlines to produce determinate results
367 if( aOtherZone->SameNet( aZone ) )
368 return false;
369
370 // A higher priority zone is found: if we intersect and it's not filled yet
371 // then we have to wait.
372 BOX2I inflatedBBox = aZone->GetBoundingBox();
373 inflatedBBox.Inflate( m_worstClearance );
374
375 if( !inflatedBBox.Intersects( aOtherZone->GetBoundingBox() ) )
376 return false;
377
378 return aZone->Outline()->Collide( aOtherZone->Outline(), m_worstClearance );
379 };
380
381 auto fill_lambda =
382 [&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
383 {
384 PCB_LAYER_ID layer = aFillItem.second;
385 ZONE* zone = aFillItem.first;
386 bool canFill = true;
387
388 // Check for any fill dependencies. If our zone needs to be clipped by
389 // another zone then we can't fill until that zone is filled.
390 for( ZONE* otherZone : aZones )
391 {
392 if( otherZone == zone )
393 continue;
394
395 if( check_fill_dependency( zone, layer, otherZone ) )
396 {
397 canFill = false;
398 break;
399 }
400 }
401
403 return 0;
404
405 if( !canFill )
406 return 0;
407
408 // Now we're ready to fill.
409 {
410 std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
411
412 if( !zoneLock.owns_lock() )
413 return 0;
414
415 SHAPE_POLY_SET fillPolys;
416
417 if( !fillSingleZone( zone, layer, fillPolys ) )
418 return 0;
419
420 zone->SetFilledPolysList( layer, fillPolys );
421 }
422
425
426 return 1;
427 };
428
429 auto tesselate_lambda =
430 [&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
431 {
433 return 0;
434
435 PCB_LAYER_ID layer = aFillItem.second;
436 ZONE* zone = aFillItem.first;
437
438 {
439 std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
440
441 if( !zoneLock.owns_lock() )
442 return 0;
443
444 zone->CacheTriangulation( layer );
445 zone->SetFillFlag( layer, true );
446 }
447
448 return 1;
449 };
450
451 // Calculate the copper fills (NB: this is multi-threaded)
452 //
453 std::vector<std::pair<std::future<int>, int>> returns;
454 returns.reserve( toFill.size() );
455 size_t finished = 0;
456 bool cancelled = false;
457
459
460 for( const std::pair<ZONE*, PCB_LAYER_ID>& fillItem : toFill )
461 returns.emplace_back( std::make_pair( tp.submit( fill_lambda, fillItem ), 0 ) );
462
463 while( !cancelled && finished != 2 * toFill.size() )
464 {
465 for( size_t ii = 0; ii < returns.size(); ++ii )
466 {
467 auto& ret = returns[ii];
468
469 if( ret.second > 1 )
470 continue;
471
472 std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
473
474 if( status == std::future_status::ready )
475 {
476 if( ret.first.get() ) // lambda completed
477 {
478 ++finished;
479 ret.second++; // go to next step
480 }
481
482 if( !cancelled )
483 {
484 // Queue the next step (will re-queue the existing step if it didn't complete)
485 if( ret.second == 0 )
486 returns[ii].first = tp.submit( fill_lambda, toFill[ii] );
487 else if( ret.second == 1 )
488 returns[ii].first = tp.submit( tesselate_lambda, toFill[ii] );
489 }
490 }
491 }
492
493 std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
494
495
497 {
499
501 cancelled = true;
502 }
503 }
504
505 // Make sure that all futures have finished.
506 // This can happen when the user cancels the above operation
507 for( auto& ret : returns )
508 {
509 if( ret.first.valid() )
510 {
511 std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
512
513 while( status != std::future_status::ready )
514 {
517
518 status = ret.first.wait_for( std::chrono::milliseconds( 100 ) );
519 }
520 }
521 }
522
523 // Now update the connectivity to check for isolated copper islands
524 // (NB: FindIsolatedCopperIslands() is multi-threaded)
525 //
527 {
529 return false;
530
532 m_progressReporter->Report( _( "Removing isolated copper islands..." ) );
534 }
535
536 connectivity->SetProgressReporter( m_progressReporter );
537 connectivity->FindIsolatedCopperIslands( islandsList );
538 connectivity->SetProgressReporter( nullptr );
539
541 return false;
542
543 for( ZONE* zone : aZones )
544 {
545 // Keepout zones are not filled
546 if( zone->GetIsRuleArea() )
547 continue;
548
549 zone->SetIsFilled( true );
550 }
551
552 // Now remove isolated copper islands according to the isolated islands strategy assigned
553 // by the user (always, never, below-certain-size).
554 //
555 for( CN_ZONE_ISOLATED_ISLAND_LIST& zone : islandsList )
556 {
557 // If *all* the polygons are islands, do not remove any of them
558 bool allIslands = true;
559
560 for( PCB_LAYER_ID layer : zone.m_zone->GetLayerSet().Seq() )
561 {
562 std::shared_ptr<SHAPE_POLY_SET> poly = zone.m_zone->GetFilledPolysList( layer );
563
564 if( !zone.m_islands.count( layer ) )
565 {
566 if( poly->OutlineCount() > 0 )
567 allIslands = false;
568
569 continue;
570 }
571
572 std::vector<int>& islands = zone.m_islands.at( layer );
573
574 if( islands.size() != static_cast<size_t>( poly->OutlineCount() ) )
575 {
576 allIslands = false;
577 break;
578 }
579 }
580
581 if( allIslands )
582 continue;
583
584 for( PCB_LAYER_ID layer : zone.m_zone->GetLayerSet().Seq() )
585 {
586 if( m_debugZoneFiller && LSET::InternalCuMask().Contains( layer ) )
587 continue;
588
589 if( !zone.m_islands.count( layer ) )
590 continue;
591
592 std::vector<int>& islands = zone.m_islands.at( layer );
593
594 // The list of polygons to delete must be explored from last to first in list,
595 // to allow deleting a polygon from list without breaking the remaining of the list
596 std::sort( islands.begin(), islands.end(), std::greater<int>() );
597
598 std::shared_ptr<SHAPE_POLY_SET> poly = zone.m_zone->GetFilledPolysList( layer );
599 long long int minArea = zone.m_zone->GetMinIslandArea();
600 ISLAND_REMOVAL_MODE mode = zone.m_zone->GetIslandRemovalMode();
601
602 for( int idx : islands )
603 {
604 SHAPE_LINE_CHAIN& outline = poly->Outline( idx );
605
606 if( mode == ISLAND_REMOVAL_MODE::ALWAYS )
607 poly->DeletePolygonAndTriangulationData( idx, false );
608 else if ( mode == ISLAND_REMOVAL_MODE::AREA && outline.Area( true ) < minArea )
609 poly->DeletePolygonAndTriangulationData( idx, false );
610 else
611 zone.m_zone->SetIsIsland( layer, idx );
612 }
613
614 poly->UpdateTriangulationDataHash();
615 zone.m_zone->CalculateFilledArea();
616
618 return false;
619 }
620 }
621
622 // Now remove islands which are either outside the board edge or fail to meet the minimum
623 // area requirements
624 using island_check_return = std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, int>>;
625
626 std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, double>> polys_to_check;
627
628 // rough estimate to save re-allocation time
629 polys_to_check.reserve( m_board->GetCopperLayerCount() * aZones.size() );
630
631 for( ZONE* zone : aZones )
632 {
633 LSET zoneCopperLayers = zone->GetLayerSet() & LSET::AllCuMask( MAX_CU_LAYERS );
634
635 // Min-thickness is the web thickness. On the other hand, a blob min-thickness by
636 // min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
637 // arbitrarily choose "at least 2X the area".
638 double minArea = (double) zone->GetMinThickness() * zone->GetMinThickness() * 2;
639
640 for( PCB_LAYER_ID layer : zoneCopperLayers.Seq() )
641 {
643 continue;
644
645 polys_to_check.emplace_back( zone->GetFilledPolysList( layer ), minArea );
646 }
647 }
648
649 auto island_lambda = [&]( int aStart, int aEnd ) -> island_check_return
650 {
651 island_check_return retval;
652
653 for( int ii = aStart; ii < aEnd && !cancelled; ++ii )
654 {
655 auto [poly, minArea] = polys_to_check[ii];
656
657 for( int jj = poly->OutlineCount() - 1; jj >= 0; jj-- )
658 {
659 SHAPE_POLY_SET island;
660 SHAPE_POLY_SET intersection;
661 const SHAPE_LINE_CHAIN& test_poly = poly->Polygon( jj ).front();
662 double island_area = test_poly.Area();
663
664 if( island_area < minArea )
665 continue;
666
667
668 island.AddOutline( test_poly );
669 intersection.BooleanIntersection( m_boardOutline, island, SHAPE_POLY_SET::POLYGON_MODE::PM_FAST );
670
671 // Nominally, all of these areas should be either inside or outside the board outline. So this test
672 // should be able to just compare areas (if they are equal, you are inside). But in practice,
673 // we sometimes can have slight overlap at the edges. So testing against half-size area is
674 // a fail-safe
675 if( intersection.Area() < island_area / 2.0 )
676 retval.emplace_back( poly, jj );
677 }
678 }
679
680 return retval;
681 };
682
683 auto island_returns = tp.parallelize_loop( 0, polys_to_check.size(), island_lambda );
684 cancelled = false;
685
686 // Allow island removal threads to finish
687 for( size_t ii = 0; ii < island_returns.size(); ++ii )
688 {
689 std::future<island_check_return>& ret = island_returns[ii];
690
691 if( ret.valid() )
692 {
693 std::future_status status = ret.wait_for( std::chrono::seconds( 0 ) );
694
695 while( status != std::future_status::ready )
696 {
698 {
700
702 cancelled = true;
703 }
704
705 status = ret.wait_for( std::chrono::milliseconds( 100 ) );
706 }
707 }
708 }
709
710 if( cancelled )
711 return false;
712
713 for( size_t ii = 0; ii < island_returns.size(); ++ii )
714 {
715 std::future<island_check_return>& ret = island_returns[ii];
716
717 if( ret.valid() )
718 {
719 for( auto& action_item : ret.get() )
720 action_item.first->DeletePolygonAndTriangulationData( action_item.second, true );
721 }
722 }
723
724 for( ZONE* zone : aZones )
725 zone->CalculateFilledArea();
726
727
728 if( aCheck )
729 {
730 bool outOfDate = false;
731
732 for( ZONE* zone : aZones )
733 {
734 // Keepout zones are not filled
735 if( zone->GetIsRuleArea() )
736 continue;
737
738 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
739 {
740 zone->BuildHashValue( layer );
741
742 if( oldFillHashes[ { zone, layer } ] != zone->GetHashValue( layer ) )
743 outOfDate = true;
744 }
745 }
746
747 if( outOfDate )
748 {
749 KIDIALOG dlg( aParent, _( "Zone fills are out-of-date. Refill?" ),
750 _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
751 dlg.SetOKCancelLabels( _( "Refill" ), _( "Continue without Refill" ) );
752 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
753
754 if( dlg.ShowModal() == wxID_CANCEL )
755 return false;
756 }
757 else
758 {
759 // No need to commit something that hasn't changed (and committing will set
760 // the modified flag).
761 return false;
762 }
763 }
764
766 {
768 return false;
769
772 }
773
774 return true;
775}
776
777
782void ZONE_FILLER::addKnockout( PAD* aPad, PCB_LAYER_ID aLayer, int aGap, SHAPE_POLY_SET& aHoles )
783{
784 if( aPad->GetShape() == PAD_SHAPE::CUSTOM )
785 {
786 SHAPE_POLY_SET poly;
787 aPad->TransformShapeToPolygon( poly, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
788
789 // the pad shape in zone can be its convex hull or the shape itself
791 {
792 std::vector<VECTOR2I> convex_hull;
793 BuildConvexHull( convex_hull, poly );
794
795 aHoles.NewOutline();
796
797 for( const VECTOR2I& pt : convex_hull )
798 aHoles.Append( pt );
799 }
800 else
801 aHoles.Append( poly );
802 }
803 else
804 {
805 aPad->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
806 }
807}
808
809
813void ZONE_FILLER::addHoleKnockout( PAD* aPad, int aGap, SHAPE_POLY_SET& aHoles )
814{
815 aPad->TransformHoleToPolygon( aHoles, aGap, m_maxError, ERROR_OUTSIDE );
816}
817
818
823void ZONE_FILLER::addKnockout( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aGap,
824 bool aIgnoreLineWidth, SHAPE_POLY_SET& aHoles )
825{
826 EDA_TEXT* text = nullptr;
827
828 switch( aItem->Type() )
829 {
830 case PCB_TEXT_T: text = static_cast<PCB_TEXT*>( aItem ); break;
831 case PCB_TEXTBOX_T: text = static_cast<PCB_TEXTBOX*>( aItem ); break;
832 case PCB_FP_TEXT_T: text = static_cast<FP_TEXT*>( aItem ); break;
833 case PCB_FP_TEXTBOX_T: text = static_cast<FP_TEXTBOX*>( aItem ); break;
834 default: break;
835 }
836
837 if( text )
838 aGap += GetKnockoutTextMargin( text->GetTextSize(), text->GetTextThickness() );
839
840 switch( aItem->Type() )
841 {
842 case PCB_SHAPE_T:
843 case PCB_TEXT_T:
844 case PCB_TEXTBOX_T:
845 case PCB_FP_TEXTBOX_T:
846 case PCB_FP_SHAPE_T:
847 case PCB_TARGET_T:
848 aItem->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE,
849 aIgnoreLineWidth );
850 break;
851
852 case PCB_FP_TEXT_T:
853 if( text->IsVisible() )
854 {
855 aItem->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE,
856 aIgnoreLineWidth );
857 }
858
859 break;
860
861 default:
862 break;
863 }
864}
865
866
872 SHAPE_POLY_SET& aFill,
873 std::vector<PAD*>& aThermalConnectionPads,
874 std::vector<PAD*>& aNoConnectionPads )
875{
877 ZONE_CONNECTION connection;
878 DRC_CONSTRAINT constraint;
879 int padClearance;
880 int holeClearance;
881 SHAPE_POLY_SET holes;
882
883 for( FOOTPRINT* footprint : m_board->Footprints() )
884 {
885 for( PAD* pad : footprint->Pads() )
886 {
887 BOX2I padBBox = pad->GetBoundingBox();
888 padBBox.Inflate( m_worstClearance );
889
890 if( !padBBox.Intersects( aZone->GetBoundingBox() ) )
891 continue;
892
893 if( pad->GetNetCode() != aZone->GetNetCode()
894 || pad->GetNetCode() <= 0
895 || pad->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
896 {
897 // collect these for knockout in buildCopperItemClearances()
898 aNoConnectionPads.push_back( pad );
899 continue;
900 }
901
902 if( aZone->IsTeardropArea() )
903 {
904 connection = ZONE_CONNECTION::FULL;
905 }
906 else
907 {
908 constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
909 connection = constraint.m_ZoneConnection;
910 }
911
912 switch( connection )
913 {
915 constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone,
916 aLayer );
917 padClearance = constraint.GetValue().Min();
918
919 if( pad->CanFlashLayer( aLayer ) )
920 {
921 aThermalConnectionPads.push_back( pad );
922 addKnockout( pad, aLayer, padClearance, holes );
923 }
924 else if( pad->GetDrillSize().x > 0 )
925 {
926 pad->TransformHoleToPolygon( holes, padClearance, m_maxError, ERROR_OUTSIDE );
927 }
928
929 break;
930
932 constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, pad,
933 aZone, aLayer );
934
935 if( constraint.GetValue().Min() > aZone->GetLocalClearance() )
936 padClearance = constraint.GetValue().Min();
937 else
938 padClearance = aZone->GetLocalClearance();
939
940 if( pad->FlashLayer( aLayer ) )
941 {
942 addKnockout( pad, aLayer, padClearance, holes );
943 }
944 else if( pad->GetDrillSize().x > 0 )
945 {
946 constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
947 pad, aZone, aLayer );
948
949 if( constraint.GetValue().Min() > padClearance )
950 holeClearance = constraint.GetValue().Min();
951 else
952 holeClearance = padClearance;
953
954 pad->TransformHoleToPolygon( holes, holeClearance, m_maxError, ERROR_OUTSIDE );
955 }
956
957 break;
958
959 default:
960 // No knockout
961 continue;
962 }
963 }
964 }
965
967}
968
969
975 const std::vector<PAD*> aNoConnectionPads,
976 SHAPE_POLY_SET& aHoles )
977{
979 long ticker = 0;
980
981 auto checkForCancel =
982 [&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
983 {
984 return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
985 };
986
987 // A small extra clearance to be sure actual track clearances are not smaller than
988 // requested clearance due to many approximations in calculations, like arc to segment
989 // approx, rounding issues, etc.
990 BOX2I zone_boundingbox = aZone->GetBoundingBox();
992
993 // Items outside the zone bounding box are skipped, so it needs to be inflated by the
994 // largest clearance value found in the netclasses and rules
995 zone_boundingbox.Inflate( m_worstClearance + extra_margin );
996
997 auto evalRulesForItems =
998 [&bds]( DRC_CONSTRAINT_T aConstraint, const BOARD_ITEM* a, const BOARD_ITEM* b,
999 PCB_LAYER_ID aEvalLayer ) -> int
1000 {
1001 DRC_CONSTRAINT c = bds.m_DRCEngine->EvalRules( aConstraint, a, b, aEvalLayer );
1002 return c.GetValue().Min();
1003 };
1004
1005 // Add non-connected pad clearances
1006 //
1007 auto knockoutPadClearance =
1008 [&]( PAD* aPad )
1009 {
1010 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone, aPad, aLayer );
1011 bool hasHole = aPad->GetDrillSize().x > 0;
1012 bool flashLayer = aPad->FlashLayer( aLayer );
1013 bool platedHole = hasHole && aPad->GetAttribute() == PAD_ATTRIB::PTH;
1014
1015 if( flashLayer || platedHole )
1016 {
1017 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1018 aZone, aPad, aLayer ) );
1019 }
1020
1021 if( flashLayer && gap > 0 )
1022 addKnockout( aPad, aLayer, gap + extra_margin, aHoles );
1023
1024 if( hasHole )
1025 {
1026 gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
1027 aZone, aPad, aLayer ) );
1028
1029 gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
1030 aZone, aPad, aLayer ) );
1031
1032 if( gap > 0 )
1033 addHoleKnockout( aPad, gap + extra_margin, aHoles );
1034 }
1035 };
1036
1037 for( PAD* pad : aNoConnectionPads )
1038 {
1039 if( checkForCancel( m_progressReporter ) )
1040 return;
1041
1042 knockoutPadClearance( pad );
1043 }
1044
1045 // Add non-connected track clearances
1046 //
1047 auto knockoutTrackClearance =
1048 [&]( PCB_TRACK* aTrack )
1049 {
1050 if( aTrack->GetBoundingBox().Intersects( zone_boundingbox ) )
1051 {
1052 bool sameNet = aTrack->GetNetCode() == aZone->GetNetCode()
1053 && aZone->GetNetCode() != 0;
1054
1055 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
1056 aZone, aTrack, aLayer );
1057
1058 if( aTrack->Type() == PCB_VIA_T )
1059 {
1060 PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
1061
1062 if( via->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
1063 sameNet = false;
1064 }
1065
1066 if( !sameNet )
1067 {
1068 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1069 aZone, aTrack, aLayer ) );
1070 }
1071
1072 if( aTrack->Type() == PCB_VIA_T )
1073 {
1074 PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
1075
1076 if( via->FlashLayer( aLayer ) && gap > 0 )
1077 {
1078 via->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
1080 }
1081
1082 gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
1083 aZone, via, aLayer ) );
1084
1085 if( !sameNet )
1086 {
1087 gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
1088 aZone, via, aLayer ) );
1089 }
1090
1091 if( gap > 0 )
1092 {
1093 int radius = via->GetDrillValue() / 2;
1094
1095 TransformCircleToPolygon( aHoles, via->GetPosition(),
1096 radius + gap + extra_margin,
1098 }
1099 }
1100 else
1101 {
1102 if( gap > 0 )
1103 {
1104 aTrack->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
1106 }
1107 }
1108 }
1109 };
1110
1111 for( PCB_TRACK* track : m_board->Tracks() )
1112 {
1113 if( !track->IsOnLayer( aLayer ) )
1114 continue;
1115
1116 if( checkForCancel( m_progressReporter ) )
1117 return;
1118
1119 knockoutTrackClearance( track );
1120 }
1121
1122 // Add graphic item clearances. They are by definition unconnected, and have no clearance
1123 // definitions of their own.
1124 //
1125 auto knockoutGraphicClearance =
1126 [&]( BOARD_ITEM* aItem )
1127 {
1128 // A item on the Edge_Cuts or Margin is always seen as on any layer:
1129 if( aItem->IsOnLayer( aLayer )
1130 || aItem->IsOnLayer( Edge_Cuts )
1131 || aItem->IsOnLayer( Margin ) )
1132 {
1133 if( aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
1134 {
1135 bool ignoreLineWidths = false;
1136 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
1137 aZone, aItem, aLayer );
1138
1139 if( aItem->IsOnLayer( aLayer ) )
1140 {
1141 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
1142 aZone, aItem, aLayer ) );
1143 }
1144 else if( aItem->IsOnLayer( Edge_Cuts ) )
1145 {
1146 gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
1147 aZone, aItem, Edge_Cuts ) );
1148 ignoreLineWidths = true;
1149 }
1150 else if( aItem->IsOnLayer( Margin ) )
1151 {
1152 gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
1153 aZone, aItem, Margin ) );
1154 }
1155
1156 addKnockout( aItem, aLayer, gap + extra_margin, ignoreLineWidths, aHoles );
1157 }
1158 }
1159 };
1160
1161 for( FOOTPRINT* footprint : m_board->Footprints() )
1162 {
1163 knockoutGraphicClearance( &footprint->Reference() );
1164 knockoutGraphicClearance( &footprint->Value() );
1165
1166 std::set<PAD*> allowedNetTiePads;
1167
1168 // Don't knock out holes for graphic items which implement a net-tie to the zone's net
1169 // on the layer being filled.
1170 if( footprint->IsNetTie() )
1171 {
1172 for( PAD* pad : footprint->Pads() )
1173 {
1174 if( pad->GetNetCode() == aZone->GetNetCode() )
1175 {
1176 if( pad->IsOnLayer( aLayer ) )
1177 allowedNetTiePads.insert( pad );
1178
1179 for( PAD* other : footprint->GetNetTiePads( pad ) )
1180 {
1181 if( other->IsOnLayer( aLayer ) )
1182 allowedNetTiePads.insert( other );
1183 }
1184 }
1185 }
1186 }
1187
1188 for( BOARD_ITEM* item : footprint->GraphicalItems() )
1189 {
1190 if( checkForCancel( m_progressReporter ) )
1191 return;
1192
1193 BOX2I itemBBox = item->GetBoundingBox();
1194
1195 if( !zone_boundingbox.Intersects( itemBBox ) )
1196 continue;
1197
1198 bool skipItem = false;
1199
1200 if( item->IsOnLayer( aLayer ) )
1201 {
1202 std::shared_ptr<SHAPE> itemShape = item->GetEffectiveShape();
1203
1204 for( PAD* pad : allowedNetTiePads )
1205 {
1206 if( pad->GetBoundingBox().Intersects( itemBBox )
1207 && pad->GetEffectiveShape()->Collide( itemShape.get() ) )
1208 {
1209 skipItem = true;
1210 break;
1211 }
1212 }
1213 }
1214
1215 if( !skipItem )
1216 knockoutGraphicClearance( item );
1217 }
1218 }
1219
1220 for( BOARD_ITEM* item : m_board->Drawings() )
1221 {
1222 if( checkForCancel( m_progressReporter ) )
1223 return;
1224
1225 knockoutGraphicClearance( item );
1226 }
1227
1228 // Add non-connected zone clearances
1229 //
1230 auto knockoutZoneClearance =
1231 [&]( ZONE* aKnockout )
1232 {
1233 // If the zones share no common layers
1234 if( !aKnockout->GetLayerSet().test( aLayer ) )
1235 return;
1236
1237 if( aKnockout->GetBoundingBox().Intersects( zone_boundingbox ) )
1238 {
1239 if( aKnockout->GetIsRuleArea() )
1240 {
1241 // Keepouts use outline with no clearance
1242 aKnockout->TransformSmoothedOutlineToPolygon( aHoles, 0, m_maxError,
1243 ERROR_OUTSIDE, nullptr );
1244 }
1245 else
1246 {
1247 int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone,
1248 aKnockout, aLayer );
1249
1250 gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT, aZone,
1251 aKnockout, aLayer ) );
1252
1253 SHAPE_POLY_SET poly;
1254 aKnockout->TransformShapeToPolygon( poly, aLayer, gap + extra_margin,
1256 aHoles.Append( poly );
1257 }
1258 }
1259 };
1260
1261 for( ZONE* otherZone : m_board->Zones() )
1262 {
1263 if( checkForCancel( m_progressReporter ) )
1264 return;
1265
1266 // Negative clearance permits zones to short
1267 if( evalRulesForItems( CLEARANCE_CONSTRAINT, aZone, otherZone, aLayer ) < 0 )
1268 continue;
1269
1270 if( otherZone->GetIsRuleArea() )
1271 {
1272 if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
1273 knockoutZoneClearance( otherZone );
1274 }
1275 else if( otherZone->HigherPriority( aZone ) )
1276 {
1277 if( !otherZone->SameNet( aZone ) )
1278 knockoutZoneClearance( otherZone );
1279 }
1280 }
1281
1282 for( FOOTPRINT* footprint : m_board->Footprints() )
1283 {
1284 for( ZONE* otherZone : footprint->Zones() )
1285 {
1286 if( checkForCancel( m_progressReporter ) )
1287 return;
1288
1289 if( otherZone->GetIsRuleArea() )
1290 {
1291 if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
1292 knockoutZoneClearance( otherZone );
1293 }
1294 else if( otherZone->HigherPriority( aZone ) )
1295 {
1296 if( !otherZone->SameNet( aZone ) )
1297 knockoutZoneClearance( otherZone );
1298 }
1299 }
1300 }
1301
1303}
1304
1305
1311 SHAPE_POLY_SET& aRawFill )
1312{
1313 BOX2I zoneBBox = aZone->GetBoundingBox();
1314
1315 auto knockoutZoneOutline =
1316 [&]( ZONE* aKnockout )
1317 {
1318 // If the zones share no common layers
1319 if( !aKnockout->GetLayerSet().test( aLayer ) )
1320 return;
1321
1322 if( aKnockout->GetBoundingBox().Intersects( zoneBBox ) )
1323 {
1324 // Processing of arc shapes in zones is not yet supported because Clipper
1325 // can't do boolean operations on them. The poly outline must be converted to
1326 // segments first.
1327 SHAPE_POLY_SET outline = aKnockout->Outline()->CloneDropTriangulation();
1328 outline.ClearArcs();
1329
1330 aRawFill.BooleanSubtract( outline, SHAPE_POLY_SET::PM_FAST );
1331 }
1332 };
1333
1334 for( ZONE* otherZone : m_board->Zones() )
1335 {
1336 // Don't use the `HigherPriority()` check here because we _only_ want to knock out zones
1337 // with explicitly higher priorities, not those with equal priorities
1338 if( otherZone->SameNet( aZone )
1339 && otherZone->GetAssignedPriority() > aZone->GetAssignedPriority() )
1340 {
1341 // Do not remove teardrop area: it is not useful and not good
1342 if( !otherZone->IsTeardropArea() )
1343 knockoutZoneOutline( otherZone );
1344 }
1345 }
1346
1347 for( FOOTPRINT* footprint : m_board->Footprints() )
1348 {
1349 for( ZONE* otherZone : footprint->Zones() )
1350 {
1351 if( otherZone->SameNet( aZone ) && otherZone->HigherPriority( aZone ) )
1352 {
1353 // Do not remove teardrop area: it is not useful and not good
1354 if( !otherZone->IsTeardropArea() )
1355 knockoutZoneOutline( otherZone );
1356 }
1357 }
1358 }
1359}
1360
1361
1362#define DUMP_POLYS_TO_COPPER_LAYER( a, b, c ) \
1363 { if( m_debugZoneFiller && aDebugLayer == b ) \
1364 { \
1365 m_board->SetLayerName( b, c ); \
1366 SHAPE_POLY_SET d = a; \
1367 d.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE ); \
1368 aFillPolys = d; \
1369 return false; \
1370 } \
1371 }
1372
1373
1385bool ZONE_FILLER::fillCopperZone( const ZONE* aZone, PCB_LAYER_ID aLayer, PCB_LAYER_ID aDebugLayer,
1386 const SHAPE_POLY_SET& aSmoothedOutline,
1387 const SHAPE_POLY_SET& aMaxExtents, SHAPE_POLY_SET& aFillPolys )
1388{
1390
1391 // Features which are min_width should survive pruning; features that are *less* than
1392 // min_width should not. Therefore we subtract epsilon from the min_width when
1393 // deflating/inflating.
1394 int half_min_width = aZone->GetMinThickness() / 2;
1395 int epsilon = pcbIUScale.mmToIU( 0.001 );
1396 int numSegs = GetArcToSegmentCount( half_min_width, m_maxError, FULL_CIRCLE );
1397
1398 // Solid polygons are deflated and inflated during calculations. Deflating doesn't cause
1399 // issues, but inflate is tricky as it can create excessively long and narrow spikes for
1400 // acute angles.
1401 // ALLOW_ACUTE_CORNERS cannot be used due to the spike problem.
1402 // CHAMFER_ACUTE_CORNERS is tempting, but can still produce spikes in some unusual
1403 // circumstances (https://gitlab.com/kicad/code/kicad/-/issues/5581).
1404 // It's unclear if ROUND_ACUTE_CORNERS would have the same issues, but is currently avoided
1405 // as a "less-safe" option.
1406 // ROUND_ALL_CORNERS produces the uniformly nicest shapes, but also a lot of segments.
1407 // CHAMFER_ALL_CORNERS improves the segment count.
1410
1411 std::vector<PAD*> thermalConnectionPads;
1412 std::vector<PAD*> noConnectionPads;
1413 std::deque<SHAPE_LINE_CHAIN> thermalSpokes;
1414 SHAPE_POLY_SET clearanceHoles;
1415
1416 aFillPolys = aSmoothedOutline;
1417 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In1_Cu, wxT( "smoothed-outline" ) );
1418
1420 return false;
1421
1422 /* -------------------------------------------------------------------------------------
1423 * Knockout thermal reliefs.
1424 */
1425
1426 knockoutThermalReliefs( aZone, aLayer, aFillPolys, thermalConnectionPads, noConnectionPads );
1427 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In2_Cu, wxT( "minus-thermal-reliefs" ) );
1428
1430 return false;
1431
1432 /* -------------------------------------------------------------------------------------
1433 * Knockout electrical clearances.
1434 */
1435
1436 buildCopperItemClearances( aZone, aLayer, noConnectionPads, clearanceHoles );
1437 DUMP_POLYS_TO_COPPER_LAYER( clearanceHoles, In3_Cu, wxT( "clearance-holes" ) );
1438
1440 return false;
1441
1442 /* -------------------------------------------------------------------------------------
1443 * Add thermal relief spokes.
1444 */
1445
1446 buildThermalSpokes( aZone, aLayer, thermalConnectionPads, thermalSpokes );
1447
1449 return false;
1450
1451 // Create a temporary zone that we can hit-test spoke-ends against. It's only temporary
1452 // because the "real" subtract-clearance-holes has to be done after the spokes are added.
1453 static const bool USE_BBOX_CACHES = true;
1454 SHAPE_POLY_SET testAreas = aFillPolys.CloneDropTriangulation();
1455 testAreas.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1456 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In4_Cu, wxT( "minus-clearance-holes" ) );
1457
1458 // Prune features that don't meet minimum-width criteria
1459 if( half_min_width - epsilon > epsilon )
1460 {
1461 testAreas.Deflate( half_min_width - epsilon, numSegs, fastCornerStrategy );
1462 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In5_Cu, wxT( "spoke-test-deflated" ) );
1463
1464 testAreas.Inflate( half_min_width - epsilon, numSegs, fastCornerStrategy );
1465 DUMP_POLYS_TO_COPPER_LAYER( testAreas, In6_Cu, wxT( "spoke-test-reinflated" ) );
1466 }
1467
1469 return false;
1470
1471 // Spoke-end-testing is hugely expensive so we generate cached bounding-boxes to speed
1472 // things up a bit.
1473 testAreas.BuildBBoxCaches();
1474 int interval = 0;
1475
1476 SHAPE_POLY_SET debugSpokes;
1477
1478 for( const SHAPE_LINE_CHAIN& spoke : thermalSpokes )
1479 {
1480 const VECTOR2I& testPt = spoke.CPoint( 3 );
1481
1482 // Hit-test against zone body
1483 if( testAreas.Contains( testPt, -1, 1, USE_BBOX_CACHES ) )
1484 {
1485 if( m_debugZoneFiller )
1486 debugSpokes.AddOutline( spoke );
1487
1488 aFillPolys.AddOutline( spoke );
1489 continue;
1490 }
1491
1492 if( interval++ > 400 )
1493 {
1495 return false;
1496
1497 interval = 0;
1498 }
1499
1500 // Hit-test against other spokes
1501 for( const SHAPE_LINE_CHAIN& other : thermalSpokes )
1502 {
1503 // Hit test in both directions to avoid interactions with round-off errors.
1504 // (See https://gitlab.com/kicad/code/kicad/-/issues/13316.)
1505 if( &other != &spoke
1506 && other.PointInside( testPt, 1, USE_BBOX_CACHES )
1507 && spoke.PointInside( other.CPoint( 3 ), 1, USE_BBOX_CACHES ) )
1508 {
1509 if( m_debugZoneFiller )
1510 debugSpokes.AddOutline( spoke );
1511
1512 aFillPolys.AddOutline( spoke );
1513 break;
1514 }
1515 }
1516 }
1517
1518 DUMP_POLYS_TO_COPPER_LAYER( debugSpokes, In7_Cu, wxT( "spokes" ) );
1519
1521 return false;
1522
1523 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1524 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In8_Cu, wxT( "after-spoke-trimming" ) );
1525
1526 /* -------------------------------------------------------------------------------------
1527 * Prune features that don't meet minimum-width criteria
1528 */
1529
1530 if( half_min_width - epsilon > epsilon )
1531 aFillPolys.Deflate( half_min_width - epsilon, numSegs, fastCornerStrategy );
1532
1533 // Min-thickness is the web thickness. On the other hand, a blob min-thickness by
1534 // min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
1535 // arbitrarily choose "at least 1/2 min-thickness on one axis".
1536 for( int ii = aFillPolys.OutlineCount() - 1; ii >= 0; ii-- )
1537 {
1538 std::vector<SHAPE_LINE_CHAIN>& island = aFillPolys.Polygon( ii );
1539 BOX2I islandExtents = island.front().BBox();
1540
1541 if( islandExtents.GetSizeMax() < half_min_width )
1542 aFillPolys.DeletePolygon( ii );
1543 }
1544
1545 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In9_Cu, wxT( "deflated" ) );
1546
1548 return false;
1549
1550 /* -------------------------------------------------------------------------------------
1551 * Process the hatch pattern (note that we do this while deflated)
1552 */
1553
1555 {
1556 if( !addHatchFillTypeOnZone( aZone, aLayer, aDebugLayer, aFillPolys ) )
1557 return false;
1558 }
1559
1561 return false;
1562
1563 /* -------------------------------------------------------------------------------------
1564 * Finish minimum-width pruning by re-inflating
1565 */
1566
1567 if( half_min_width - epsilon > epsilon )
1568 aFillPolys.Inflate( half_min_width - epsilon, numSegs, cornerStrategy );
1569
1570 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In15_Cu, wxT( "after-reinflating" ) );
1571
1572 /* -------------------------------------------------------------------------------------
1573 * Ensure additive changes (thermal stubs and particularly inflating acute corners) do not
1574 * add copper outside the zone boundary or inside the clearance holes
1575 */
1576
1577 aFillPolys.BooleanIntersection( aMaxExtents, SHAPE_POLY_SET::PM_FAST );
1578 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In16_Cu, wxT( "after-trim-to-outline" ) );
1579 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1580 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In17_Cu, wxT( "after-trim-to-clearance-holes" ) );
1581
1582 /* -------------------------------------------------------------------------------------
1583 * Lastly give any same-net but higher-priority zones control over their own area.
1584 */
1585
1586 subtractHigherPriorityZones( aZone, aLayer, aFillPolys );
1587 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In18_Cu, wxT( "minus-higher-priority-zones" ) );
1588
1589 aFillPolys.Fracture( SHAPE_POLY_SET::PM_FAST );
1590 return true;
1591}
1592
1593
1595 const SHAPE_POLY_SET& aSmoothedOutline,
1596 SHAPE_POLY_SET& aFillPolys )
1597{
1599 BOX2I zone_boundingbox = aZone->GetBoundingBox();
1600 SHAPE_POLY_SET clearanceHoles;
1601 long ticker = 0;
1602
1603 auto checkForCancel =
1604 [&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
1605 {
1606 return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
1607 };
1608
1609 auto knockoutGraphicClearance =
1610 [&]( BOARD_ITEM* aItem )
1611 {
1612 if( aItem->IsKnockout() && aItem->IsOnLayer( aLayer )
1613 && aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
1614 {
1616 aZone, aItem, aLayer );
1617
1618 addKnockout( aItem, aLayer, cc.GetValue().Min(), false, clearanceHoles );
1619 }
1620 };
1621
1622 for( FOOTPRINT* footprint : m_board->Footprints() )
1623 {
1624 if( checkForCancel( m_progressReporter ) )
1625 return false;
1626
1627 knockoutGraphicClearance( &footprint->Reference() );
1628 knockoutGraphicClearance( &footprint->Value() );
1629
1630 for( BOARD_ITEM* item : footprint->GraphicalItems() )
1631 knockoutGraphicClearance( item );
1632 }
1633
1634 for( BOARD_ITEM* item : m_board->Drawings() )
1635 {
1636 if( checkForCancel( m_progressReporter ) )
1637 return false;
1638
1639 knockoutGraphicClearance( item );
1640 }
1641
1642 aFillPolys = aSmoothedOutline;
1643 aFillPolys.BooleanSubtract( clearanceHoles, SHAPE_POLY_SET::PM_FAST );
1644
1645 // Features which are min_width should survive pruning; features that are *less* than
1646 // min_width should not. Therefore we subtract epsilon from the min_width when
1647 // deflating/inflating.
1648 int half_min_width = aZone->GetMinThickness() / 2;
1649 int epsilon = pcbIUScale.mmToIU( 0.001 );
1650 int numSegs = GetArcToSegmentCount( half_min_width, m_maxError, FULL_CIRCLE );
1651
1652 aFillPolys.Deflate( half_min_width - epsilon, numSegs );
1653
1654 // Remove the non filled areas due to the hatch pattern
1656 {
1657 if( !addHatchFillTypeOnZone( aZone, aLayer, aLayer, aFillPolys ) )
1658 return false;
1659 }
1660
1661 // Re-inflate after pruning of areas that don't meet minimum-width criteria
1662 if( half_min_width - epsilon > epsilon )
1663 aFillPolys.Inflate( half_min_width - epsilon, numSegs );
1664
1666 return true;
1667}
1668
1669
1670/*
1671 * Build the filled solid areas data from real outlines (stored in m_Poly)
1672 * The solid areas can be more than one on copper layers, and do not have holes
1673 * ( holes are linked by overlapping segments to the main outline)
1674 */
1676{
1677 SHAPE_POLY_SET* boardOutline = m_brdOutlinesValid ? &m_boardOutline : nullptr;
1678 SHAPE_POLY_SET maxExtents;
1679 SHAPE_POLY_SET smoothedPoly;
1680 PCB_LAYER_ID debugLayer = UNDEFINED_LAYER;
1681
1682 if( m_debugZoneFiller && LSET::InternalCuMask().Contains( aLayer ) )
1683 {
1684 debugLayer = aLayer;
1685 aLayer = F_Cu;
1686 }
1687
1688 if( !aZone->BuildSmoothedPoly( maxExtents, aLayer, boardOutline, &smoothedPoly ) )
1689 return false;
1690
1692 return false;
1693
1694 if( aZone->IsOnCopperLayer() )
1695 {
1696 if( fillCopperZone( aZone, aLayer, debugLayer, smoothedPoly, maxExtents, aFillPolys ) )
1697 aZone->SetNeedRefill( false );
1698 }
1699 else
1700 {
1701 if( fillNonCopperZone( aZone, aLayer, smoothedPoly, aFillPolys ) )
1702 aZone->SetNeedRefill( false );
1703 }
1704
1705 return true;
1706}
1707
1708
1713 const std::vector<PAD*>& aSpokedPadsList,
1714 std::deque<SHAPE_LINE_CHAIN>& aSpokesList )
1715{
1717 BOX2I zoneBB = aZone->GetBoundingBox();
1718 DRC_CONSTRAINT constraint;
1719
1720 zoneBB.Inflate( std::max( bds.GetBiggestClearanceValue(), aZone->GetLocalClearance() ) );
1721
1722 // Is a point on the boundary of the polygon inside or outside? This small epsilon lets
1723 // us avoid the question.
1724 int epsilon = KiROUND( pcbIUScale.IU_PER_MM * 0.04 ); // about 1.5 mil
1725
1726 for( PAD* pad : aSpokedPadsList )
1727 {
1728 // We currently only connect to pads, not pad holes
1729 if( !pad->IsOnLayer( aLayer ) )
1730 continue;
1731
1732 constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone, aLayer );
1733 int thermalReliefGap = constraint.GetValue().Min();
1734
1735 constraint = bds.m_DRCEngine->EvalRules( THERMAL_SPOKE_WIDTH_CONSTRAINT, pad, aZone, aLayer );
1736 int spoke_w = constraint.GetValue().Opt();
1737
1738 // Spoke width should ideally be smaller than the pad minor axis.
1739 // Otherwise the thermal shape is not really a thermal relief,
1740 // and the algo to count the actual number of spokes can fail
1741 int spoke_max_allowed_w = std::min( pad->GetSize().x, pad->GetSize().y );
1742
1743 spoke_w = std::max( spoke_w, constraint.Value().Min() );
1744 spoke_w = std::min( spoke_w, constraint.Value().Max() );
1745
1746 // ensure the spoke width is smaller than the pad minor size
1747 spoke_w = std::min( spoke_w, spoke_max_allowed_w );
1748
1749 // Cannot create stubs having a width < zone min thickness
1750 if( spoke_w < aZone->GetMinThickness() )
1751 continue;
1752
1753 int spoke_half_w = spoke_w / 2;
1754
1755 // Quick test here to possibly save us some work
1756 BOX2I itemBB = pad->GetBoundingBox();
1757 itemBB.Inflate( thermalReliefGap + epsilon );
1758
1759 if( !( itemBB.Intersects( zoneBB ) ) )
1760 continue;
1761
1762 // Thermal spokes consist of square-ended segments from the pad center to points just
1763 // outside the thermal relief. The outside end has an extra center point (which must be
1764 // at idx 3) which is used for testing whether or not the spoke connects to copper in the
1765 // parent zone.
1766
1767 auto buildSpokesFromOrigin =
1768 [&]( const BOX2I& box )
1769 {
1770 for( int i = 0; i < 4; i++ )
1771 {
1772 SHAPE_LINE_CHAIN spoke;
1773
1774 switch( i )
1775 {
1776 case 0: // lower stub
1777 spoke.Append( +spoke_half_w, -spoke_half_w );
1778 spoke.Append( -spoke_half_w, -spoke_half_w );
1779 spoke.Append( -spoke_half_w, box.GetBottom() );
1780 spoke.Append( 0, box.GetBottom() ); // test pt
1781 spoke.Append( +spoke_half_w, box.GetBottom() );
1782 break;
1783
1784 case 1: // upper stub
1785 spoke.Append( +spoke_half_w, +spoke_half_w );
1786 spoke.Append( -spoke_half_w, +spoke_half_w );
1787 spoke.Append( -spoke_half_w, box.GetTop() );
1788 spoke.Append( 0, box.GetTop() ); // test pt
1789 spoke.Append( +spoke_half_w, box.GetTop() );
1790 break;
1791
1792 case 2: // right stub
1793 spoke.Append( -spoke_half_w, +spoke_half_w );
1794 spoke.Append( -spoke_half_w, -spoke_half_w );
1795 spoke.Append( box.GetRight(), -spoke_half_w );
1796 spoke.Append( box.GetRight(), 0 ); // test pt
1797 spoke.Append( box.GetRight(), +spoke_half_w );
1798 break;
1799
1800 case 3: // left stub
1801 spoke.Append( +spoke_half_w, +spoke_half_w );
1802 spoke.Append( +spoke_half_w, -spoke_half_w );
1803 spoke.Append( box.GetLeft(), -spoke_half_w );
1804 spoke.Append( box.GetLeft(), 0 ); // test pt
1805 spoke.Append( box.GetLeft(), +spoke_half_w );
1806 break;
1807 }
1808
1809 spoke.SetClosed( true );
1810 aSpokesList.push_back( std::move( spoke ) );
1811 }
1812 };
1813
1814 // If the spokes are at a cardinal angle then we can generate them from a bounding box
1815 // without trig.
1816 if( ( pad->GetOrientation() + pad->GetThermalSpokeAngle() ).IsCardinal() )
1817 {
1818 BOX2I spokesBox = pad->GetBoundingBox();
1819 spokesBox.Inflate( thermalReliefGap + epsilon );
1820
1821 // Spokes are from center of pad shape, not from hole.
1822 spokesBox.Offset( - pad->ShapePos() );
1823
1824 buildSpokesFromOrigin( spokesBox );
1825
1826 auto spokeIter = aSpokesList.rbegin();
1827
1828 for( int ii = 0; ii < 4; ++ii, ++spokeIter )
1829 spokeIter->Move( pad->ShapePos() );
1830 }
1831 // Even if the spokes are rotated, we can fudge it for round and square pads by rotating
1832 // the bounding box to match the spokes.
1833 else if( pad->GetSizeX() == pad->GetSizeY() && pad->GetShape() != PAD_SHAPE::CUSTOM )
1834 {
1835 // Since the bounding-box needs to be correclty rotated we use a dummy pad to keep
1836 // from dirtying the real pad's cached shapes.
1837 PAD dummy_pad( *pad );
1838 dummy_pad.SetOrientation( pad->GetThermalSpokeAngle() );
1839
1840 // Spokes are from center of pad shape, not from hole. So the dummy pad has no shape
1841 // offset and is at position 0,0
1842 dummy_pad.SetPosition( VECTOR2I( 0, 0 ) );
1843 dummy_pad.SetOffset( VECTOR2I( 0, 0 ) );
1844
1845 BOX2I spokesBox = dummy_pad.GetBoundingBox();
1846 spokesBox.Inflate( thermalReliefGap + epsilon );
1847
1848 buildSpokesFromOrigin( spokesBox );
1849
1850 auto spokeIter = aSpokesList.rbegin();
1851
1852 for( int ii = 0; ii < 4; ++ii, ++spokeIter )
1853 {
1854 spokeIter->Rotate( pad->GetOrientation() + pad->GetThermalSpokeAngle() );
1855 spokeIter->Move( pad->ShapePos() );
1856 }
1857 }
1858 // And lastly, even when we have to resort to trig, we can use it only in a post-process
1859 // after the rotated-bounding-box trick from above.
1860 else
1861 {
1862 // Since the bounding-box needs to be correclty rotated we use a dummy pad to keep
1863 // from dirtying the real pad's cached shapes.
1864 PAD dummy_pad( *pad );
1865 dummy_pad.SetOrientation( pad->GetThermalSpokeAngle() );
1866
1867 // Spokes are from center of pad shape, not from hole. So the dummy pad has no shape
1868 // offset and is at position 0,0
1869 dummy_pad.SetPosition( VECTOR2I( 0, 0 ) );
1870 dummy_pad.SetOffset( VECTOR2I( 0, 0 ) );
1871
1872 BOX2I spokesBox = dummy_pad.GetBoundingBox();
1873
1874 // In this case make the box -big-; we're going to clip to the "real" bbox later.
1875 spokesBox.Inflate( thermalReliefGap + spokesBox.GetWidth() + spokesBox.GetHeight() );
1876
1877 buildSpokesFromOrigin( spokesBox );
1878
1879 BOX2I realBBox = pad->GetBoundingBox();
1880 realBBox.Inflate( thermalReliefGap + epsilon );
1881
1882 auto spokeIter = aSpokesList.rbegin();
1883
1884 for( int ii = 0; ii < 4; ++ii, ++spokeIter )
1885 {
1886 spokeIter->Rotate( pad->GetOrientation() + pad->GetThermalSpokeAngle() );
1887 spokeIter->Move( pad->ShapePos() );
1888
1889 VECTOR2I origin_p = spokeIter->GetPoint( 0 );
1890 VECTOR2I origin_m = spokeIter->GetPoint( 1 );
1891 VECTOR2I origin = ( origin_p + origin_m ) / 2;
1892 VECTOR2I end_m = spokeIter->GetPoint( 2 );
1893 VECTOR2I end = spokeIter->GetPoint( 3 );
1894 VECTOR2I end_p = spokeIter->GetPoint( 4 );
1895
1896 ClipLine( &realBBox, origin_p.x, origin_p.y, end_p.x, end_p.y );
1897 ClipLine( &realBBox, origin_m.x, origin_m.y, end_m.x, end_m.y );
1898 ClipLine( &realBBox, origin.x, origin.y, end.x, end.y );
1899
1900 spokeIter->SetPoint( 2, end_m );
1901 spokeIter->SetPoint( 3, end );
1902 spokeIter->SetPoint( 4, end_p );
1903 }
1904 }
1905 }
1906
1907 for( size_t ii = 0; ii < aSpokesList.size(); ++ii )
1908 aSpokesList[ii].GenerateBBoxCache();
1909}
1910
1911
1913 PCB_LAYER_ID aDebugLayer, SHAPE_POLY_SET& aFillPolys )
1914{
1915 // Build grid:
1916
1917 // obviously line thickness must be > zone min thickness.
1918 // It can happens if a board file was edited by hand by a python script
1919 // Use 1 micron margin to be *sure* there is no issue in Gerber files
1920 // (Gbr file unit = 1 or 10 nm) due to some truncation in coordinates or calculations
1921 // This margin also avoid problems due to rounding coordinates in next calculations
1922 // that can create incorrect polygons
1923 int thickness = std::max( aZone->GetHatchThickness(),
1924 aZone->GetMinThickness() + pcbIUScale.mmToIU( 0.001 ) );
1925
1926 int linethickness = thickness - aZone->GetMinThickness();
1927 int gridsize = thickness + aZone->GetHatchGap();
1928
1929 SHAPE_POLY_SET filledPolys = aFillPolys.CloneDropTriangulation();
1930 // Use a area that contains the rotated bbox by orientation, and after rotate the result
1931 // by -orientation.
1932 if( !aZone->GetHatchOrientation().IsZero() )
1933 filledPolys.Rotate( - aZone->GetHatchOrientation() );
1934
1935 BOX2I bbox = filledPolys.BBox( 0 );
1936
1937 // Build hole shape
1938 // the hole size is aZone->GetHatchGap(), but because the outline thickness
1939 // is aZone->GetMinThickness(), the hole shape size must be larger
1940 SHAPE_LINE_CHAIN hole_base;
1941 int hole_size = aZone->GetHatchGap() + aZone->GetMinThickness();
1942 VECTOR2I corner( 0, 0 );;
1943 hole_base.Append( corner );
1944 corner.x += hole_size;
1945 hole_base.Append( corner );
1946 corner.y += hole_size;
1947 hole_base.Append( corner );
1948 corner.x = 0;
1949 hole_base.Append( corner );
1950 hole_base.SetClosed( true );
1951
1952 // Calculate minimal area of a grid hole.
1953 // All holes smaller than a threshold will be removed
1954 double minimal_hole_area = hole_base.Area() * aZone->GetHatchHoleMinArea();
1955
1956 // Now convert this hole to a smoothed shape:
1957 if( aZone->GetHatchSmoothingLevel() > 0 )
1958 {
1959 // the actual size of chamfer, or rounded corner radius is the half size
1960 // of the HatchFillTypeGap scaled by aZone->GetHatchSmoothingValue()
1961 // aZone->GetHatchSmoothingValue() = 1.0 is the max value for the chamfer or the
1962 // radius of corner (radius = half size of the hole)
1963 int smooth_value = KiROUND( aZone->GetHatchGap()
1964 * aZone->GetHatchSmoothingValue() / 2 );
1965
1966 // Minimal optimization:
1967 // make smoothing only for reasonable smooth values, to avoid a lot of useless segments
1968 // and if the smooth value is small, use chamfer even if fillet is requested
1969 #define SMOOTH_MIN_VAL_MM 0.02
1970 #define SMOOTH_SMALL_VAL_MM 0.04
1971
1972 if( smooth_value > pcbIUScale.mmToIU( SMOOTH_MIN_VAL_MM ) )
1973 {
1974 SHAPE_POLY_SET smooth_hole;
1975 smooth_hole.AddOutline( hole_base );
1976 int smooth_level = aZone->GetHatchSmoothingLevel();
1977
1978 if( smooth_value < pcbIUScale.mmToIU( SMOOTH_SMALL_VAL_MM ) && smooth_level > 1 )
1979 smooth_level = 1;
1980
1981 // Use a larger smooth_value to compensate the outline tickness
1982 // (chamfer is not visible is smooth value < outline thickess)
1983 smooth_value += aZone->GetMinThickness() / 2;
1984
1985 // smooth_value cannot be bigger than the half size oh the hole:
1986 smooth_value = std::min( smooth_value, aZone->GetHatchGap() / 2 );
1987
1988 // the error to approximate a circle by segments when smoothing corners by a arc
1989 int error_max = std::max( pcbIUScale.mmToIU( 0.01 ), smooth_value / 20 );
1990
1991 switch( smooth_level )
1992 {
1993 case 1:
1994 // Chamfer() uses the distance from a corner to create a end point
1995 // for the chamfer.
1996 hole_base = smooth_hole.Chamfer( smooth_value ).Outline( 0 );
1997 break;
1998
1999 default:
2000 if( aZone->GetHatchSmoothingLevel() > 2 )
2001 error_max /= 2; // Force better smoothing
2002
2003 hole_base = smooth_hole.Fillet( smooth_value, error_max ).Outline( 0 );
2004 break;
2005
2006 case 0:
2007 break;
2008 };
2009 }
2010 }
2011
2012 // Build holes
2013 SHAPE_POLY_SET holes;
2014
2015 for( int xx = 0; ; xx++ )
2016 {
2017 int xpos = xx * gridsize;
2018
2019 if( xpos > bbox.GetWidth() )
2020 break;
2021
2022 for( int yy = 0; ; yy++ )
2023 {
2024 int ypos = yy * gridsize;
2025
2026 if( ypos > bbox.GetHeight() )
2027 break;
2028
2029 // Generate hole
2030 SHAPE_LINE_CHAIN hole( hole_base );
2031 hole.Move( VECTOR2I( xpos, ypos ) );
2032 holes.AddOutline( hole );
2033 }
2034 }
2035
2036 holes.Move( bbox.GetPosition() );
2037
2038 if( !aZone->GetHatchOrientation().IsZero() )
2039 holes.Rotate( aZone->GetHatchOrientation() );
2040
2041 DUMP_POLYS_TO_COPPER_LAYER( holes, In10_Cu, wxT( "hatch-holes" ) );
2042
2043 int outline_margin = aZone->GetMinThickness() * 1.1;
2044
2045 // Using GetHatchThickness() can look more consistent than GetMinThickness().
2046 if( aZone->GetHatchBorderAlgorithm() && aZone->GetHatchThickness() > outline_margin )
2047 outline_margin = aZone->GetHatchThickness();
2048
2049 // The fill has already been deflated to ensure GetMinThickness() so we just have to
2050 // account for anything beyond that.
2051 SHAPE_POLY_SET deflatedFilledPolys = aFillPolys.CloneDropTriangulation();
2052 deflatedFilledPolys.Deflate( outline_margin - aZone->GetMinThickness(), 16 );
2053 holes.BooleanIntersection( deflatedFilledPolys, SHAPE_POLY_SET::PM_FAST );
2054 DUMP_POLYS_TO_COPPER_LAYER( holes, In11_Cu, wxT( "fill-clipped-hatch-holes" ) );
2055
2056 SHAPE_POLY_SET deflatedOutline = aZone->Outline()->CloneDropTriangulation();
2057 deflatedOutline.Deflate( outline_margin, 16 );
2058 holes.BooleanIntersection( deflatedOutline, SHAPE_POLY_SET::PM_FAST );
2059 DUMP_POLYS_TO_COPPER_LAYER( holes, In12_Cu, wxT( "outline-clipped-hatch-holes" ) );
2060
2061 if( aZone->GetNetCode() != 0 )
2062 {
2063 // Vias and pads connected to the zone must not be allowed to become isolated inside
2064 // one of the holes. Effectively this means their copper outline needs to be expanded
2065 // to be at least as wide as the gap so that it is guaranteed to touch at least one
2066 // edge.
2067 BOX2I zone_boundingbox = aZone->GetBoundingBox();
2068 SHAPE_POLY_SET aprons;
2069 int min_apron_radius = ( aZone->GetHatchGap() * 10 ) / 19;
2070
2071 for( PCB_TRACK* track : m_board->Tracks() )
2072 {
2073 if( track->Type() == PCB_VIA_T )
2074 {
2075 PCB_VIA* via = static_cast<PCB_VIA*>( track );
2076
2077 if( via->GetNetCode() == aZone->GetNetCode()
2078 && via->IsOnLayer( aLayer )
2079 && via->GetBoundingBox().Intersects( zone_boundingbox ) )
2080 {
2081 int r = std::max( min_apron_radius,
2082 via->GetDrillValue() / 2 + outline_margin );
2083
2084 TransformCircleToPolygon( aprons, via->GetPosition(), r, ARC_HIGH_DEF,
2085 ERROR_OUTSIDE );
2086 }
2087 }
2088 }
2089
2090 for( FOOTPRINT* footprint : m_board->Footprints() )
2091 {
2092 for( PAD* pad : footprint->Pads() )
2093 {
2094 if( pad->GetNetCode() == aZone->GetNetCode()
2095 && pad->IsOnLayer( aLayer )
2096 && pad->GetBoundingBox().Intersects( zone_boundingbox ) )
2097 {
2098 // What we want is to bulk up the pad shape so that the narrowest bit of
2099 // copper between the hole and the apron edge is at least outline_margin
2100 // wide (and that the apron itself meets min_apron_radius. But that would
2101 // take a lot of code and math, and the following approximation is close
2102 // enough.
2103 int pad_width = std::min( pad->GetSize().x, pad->GetSize().y );
2104 int slot_width = std::min( pad->GetDrillSize().x, pad->GetDrillSize().y );
2105 int min_annular_ring_width = ( pad_width - slot_width ) / 2;
2106 int clearance = std::max( min_apron_radius - pad_width / 2,
2107 outline_margin - min_annular_ring_width );
2108
2109 clearance = std::max( 0, clearance - linethickness / 2 );
2110 pad->TransformShapeToPolygon( aprons, aLayer, clearance, ARC_HIGH_DEF,
2111 ERROR_OUTSIDE );
2112 }
2113 }
2114 }
2115
2117 }
2118 DUMP_POLYS_TO_COPPER_LAYER( holes, In13_Cu, wxT( "pad-via-clipped-hatch-holes" ) );
2119
2120 // Now filter truncated holes to avoid small holes in pattern
2121 // It happens for holes near the zone outline
2122 for( int ii = 0; ii < holes.OutlineCount(); )
2123 {
2124 double area = holes.Outline( ii ).Area();
2125
2126 if( area < minimal_hole_area ) // The current hole is too small: remove it
2127 holes.DeletePolygon( ii );
2128 else
2129 ++ii;
2130 }
2131
2132 // create grid. Use SHAPE_POLY_SET::PM_STRICTLY_SIMPLE to
2133 // generate strictly simple polygons needed by Gerber files and Fracture()
2134 aFillPolys.BooleanSubtract( aFillPolys, holes, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
2135 DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In14_Cu, wxT( "after-hatching" ) );
2136
2137 return true;
2138}
constexpr int ARC_HIGH_DEF
Definition: base_units.h:121
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
@ ZLO_FORCE_NO_ZONE_CONNECTION
Definition: board_item.h:60
@ ZLO_FORCE_FLASHED
Definition: board_item.h:59
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:70
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:196
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:587
ZONES & Zones()
Definition: board.h:317
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition: board.cpp:1972
FOOTPRINTS & Footprints()
Definition: board.h:311
int GetCopperLayerCount() const
Definition: board.cpp:563
TRACKS & Tracks()
Definition: board.h:308
DRAWINGS & Drawings()
Definition: board.h:314
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:704
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:430
const Vec & GetPosition() const
Definition: box2.h:184
int GetSizeMax() const
Definition: box2.h:200
void Offset(coord_type dx, coord_type dy)
Definition: box2.h:224
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetWidth() const
Definition: box2.h:187
void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: box2.h:111
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:72
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
MINOPTMAX< int > & Value()
Definition: drc_rule.h:142
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:141
ZONE_CONNECTION m_ZoneConnection
Definition: drc_rule.h:174
bool IsZero() const
Definition: eda_angle.h:169
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:46
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:76
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Shows the 'do not show again' checkbox.
Definition: confirm.h:56
int ShowModal() override
Definition: confirm.cpp:120
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:532
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: layer_ids.h:602
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:733
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:773
T Min() const
Definition: minoptmax.h:33
T Max() const
Definition: minoptmax.h:34
T Opt() const
Definition: minoptmax.h:35
Definition: pad.h:60
const BOX2I GetBoundingBox() const override
The bounding box is cached, so this will be efficient most of the time.
Definition: pad.cpp:635
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the pad shape to a closed polygon.
Definition: pad.cpp:1586
bool TransformHoleToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc) const
Build the corner list of the polygonal drill shape in the board coordinate system.
Definition: pad.cpp:1569
void SetOffset(const VECTOR2I &aOffset)
Definition: pad.h:274
CUST_PAD_SHAPE_IN_ZONE GetCustomShapeInZoneOpt() const
Definition: pad.h:213
void SetPosition(const VECTOR2I &aPos) override
Definition: pad.h:197
PAD_SHAPE GetShape() const
Definition: pad.h:195
void SetOrientation(const EDA_ANGLE &aAngle)
Set the rotation angle of the pad.
Definition: pad.cpp:694
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).
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.
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.
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.
SHAPE_POLY_SET Chamfer(int aDistance)
Return a chamfered version of the polygon set.
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset intersection For aFastMode meaning, see function booleanOp.
void Fracture(POLYGON_MODE aFastMode)
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
void ClearArcs()
Appends a vertex at the end of the given outline/hole (default: the last outline)
CORNER_STRATEGY
< define how inflate transform build inflated polygon
@ CHAMFER_ALL_CORNERS
All angles are chamfered.
@ ROUND_ALL_CORNERS
All angles are rounded.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new hole to the given outline (default: last) and returns its index.
void DeletePolygon(int aIdx)
Delete aIdx-th polygon and its triangulation data from the set.
double Area()
Count the number of arc shapes present.
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)
void Deflate(int aAmount, int aCircleSegmentsCount, CORNER_STRATEGY aCornerStrategy=CHAMFER_ALL_CORNERS)
void Inflate(int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy=ROUND_ALL_CORNERS)
Perform outline inflation/deflation.
void BooleanIntersection(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset union between a and b, store the result in it self For aFastMode meaning,...
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...
void Simplify(POLYGON_MODE aFastMode)
SHAPE_LINE_CHAIN & Outline(int aIndex)
int NewOutline()
Creates a new hole in a given outline.
void BuildBBoxCaches() const
Construct BBoxCaches for Contains(), below.
int OutlineCount() const
Return the number of vertices in a given outline/hole.
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
Creates a new empty polygon in the set and returns its index.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
COMMIT * m_commit
Definition: zone_filler.h:134
int m_worstClearance
Definition: zone_filler.h:138
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:140
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 ...
ZONE_FILLER(BOARD *aBoard, COMMIT *aCommit)
Definition: zone_filler.cpp:53
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:132
bool m_brdOutlinesValid
Definition: zone_filler.h:133
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)
Definition: zone_filler.cpp:71
bool Fill(std::vector< ZONE * > &aZones, bool aCheck=false, wxWindow *aParent=nullptr)
Fills the given list of zones.
Definition: zone_filler.cpp:89
BOARD * m_board
Definition: zone_filler.h:131
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:135
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...
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
void SetNeedRefill(bool aNeedRefill)
Definition: zone.h:246
int GetHatchBorderAlgorithm() const
Definition: zone.h:284
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:1011
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:329
SHAPE_POLY_SET * Outline()
Definition: zone.h:318
void SetFillFlag(PCB_LAYER_ID aLayer, bool aFlag)
Definition: zone.h:240
int GetLocalClearance(wxString *aSource) const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: zone.cpp:476
void SetFilledPolysList(PCB_LAYER_ID aLayer, const SHAPE_POLY_SET &aPolysList)
Set the list of filled polygons.
Definition: zone.h:629
int GetMinThickness() const
Definition: zone.h:251
bool HigherPriority(const ZONE *aOther) const
Definition: zone.cpp:192
int GetHatchThickness() const
Definition: zone.h:266
double GetHatchHoleMinArea() const
Definition: zone.h:281
bool IsTeardropArea() const
Definition: zone.h:687
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:272
bool BuildSmoothedPoly(SHAPE_POLY_SET &aSmoothedPoly, PCB_LAYER_ID aLayer, SHAPE_POLY_SET *aBoardOutline, SHAPE_POLY_SET *aSmoothedPolyWithApron=nullptr) const
Definition: zone.cpp:1075
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:174
int GetHatchGap() const
Definition: zone.h:269
double GetHatchSmoothingValue() const
Definition: zone.h:278
int GetHatchSmoothingLevel() const
Definition: zone.h:275
bool IsOnCopperLayer() const override
Definition: zone.cpp:260
std::mutex & GetLock()
Definition: zone.h:230
unsigned GetAssignedPriority() const
Definition: zone.h:112
This file is part of the common library.
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
DRC_CONSTRAINT_T
Definition: drc_rule.h:45
@ EDGE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:50
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:71
@ CLEARANCE_CONSTRAINT
Definition: drc_rule.h:47
@ THERMAL_SPOKE_WIDTH_CONSTRAINT
Definition: drc_rule.h:60
@ THERMAL_RELIEF_GAP_CONSTRAINT
Definition: drc_rule.h:59
@ HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:48
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:70
#define _(s)
static constexpr EDA_ANGLE & FULL_CIRCLE
Definition: eda_angle.h:427
a few functions useful in geometry calculations.
@ ERROR_OUTSIDE
bool ClipLine(const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
int GetArcToSegmentCount(int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
int GetKnockoutTextMargin(const VECTOR2I &aSize, int aThickness)
Returns the margin for knocking out text.
Definition: gr_text.h:104
double m_ExtraClearance
Extra fill clearance for zone fills.
bool m_DebugZoneFiller
A mode that dumps the various stages of a F_Cu fill into In1_Cu through In9_Cu.
#define MAX_CU_LAYERS
Definition: layer_ids.h:140
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ In11_Cu
Definition: layer_ids.h:75
@ In17_Cu
Definition: layer_ids.h:81
@ Edge_Cuts
Definition: layer_ids.h:113
@ In9_Cu
Definition: layer_ids.h:73
@ In7_Cu
Definition: layer_ids.h:71
@ In15_Cu
Definition: layer_ids.h:79
@ In2_Cu
Definition: layer_ids.h:66
@ In10_Cu
Definition: layer_ids.h:74
@ Margin
Definition: layer_ids.h:114
@ In4_Cu
Definition: layer_ids.h:68
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
@ In16_Cu
Definition: layer_ids.h:80
@ In1_Cu
Definition: layer_ids.h:65
@ In13_Cu
Definition: layer_ids.h:77
@ In8_Cu
Definition: layer_ids.h:72
@ In14_Cu
Definition: layer_ids.h:78
@ In12_Cu
Definition: layer_ids.h:76
@ In6_Cu
Definition: layer_ids.h:70
@ In5_Cu
Definition: layer_ids.h:69
@ In3_Cu
Definition: layer_ids.h:67
@ F_Cu
Definition: layer_ids.h:64
@ In18_Cu
Definition: layer_ids.h:82
@ CUST_PAD_SHAPE_IN_ZONE_CONVEXHULL
Definition: pad.h:46
@ PTH
Plated through hole pad.
A structure used for calculating isolated islands on a given zone across all its layers.
const double IU_PER_MM
Definition: base_units.h:77
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102
@ PCB_FP_TEXTBOX_T
class FP_TEXTBOX, wrapped text in a footprint
Definition: typeinfo.h:93
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:91
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:111
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
#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:57
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition: zones.h:49
@ THERMAL
Use thermal relief for pads.
@ NONE
Pads are not covered.
@ FULL
pads are covered by copper