KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <boost/test/data/test_case.hpp>
26
28#include <board.h>
30#include <pad.h>
31#include <pcb_track.h>
32#include <footprint.h>
33#include <zone.h>
34#include <drc/drc_item.h>
37#include <advanced_config.h>
38
39
48
49
50int delta = KiROUND( 0.006 * pcbIUScale.IU_PER_MM );
51
52
54{
55 KI_TEST::LoadBoard( m_settingsManager, "zone_filler", m_board );
56
57 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
58
59 KI_TEST::FillZones( m_board.get() );
60
61 // Now that the zones are filled we're going to increase the size of -some- pads and
62 // tracks so that they generate DRC errors. The test then makes sure that those errors
63 // are generated, and that the other pads and tracks do -not- generate errors.
64
65 for( PAD* pad : m_board->Footprints()[0]->Pads() )
66 {
67 if( pad->GetNumber() == "2" || pad->GetNumber() == "4" || pad->GetNumber() == "6" )
68 {
69 pad->SetSize( PADSTACK::ALL_LAYERS,
70 pad->GetSize( PADSTACK::ALL_LAYERS ) + VECTOR2I( delta, delta ) );
71 }
72 }
73
74 int ii = 0;
75 KIID arc8;
76 KIID arc12;
77
78 for( PCB_TRACK* track : m_board->Tracks() )
79 {
80 if( track->Type() == PCB_ARC_T )
81 {
82 ii++;
83
84 if( ii == 8 )
85 {
86 arc8 = track->m_Uuid;
87 track->SetWidth( track->GetWidth() + delta + delta );
88 }
89 else if( ii == 12 )
90 {
91 arc12 = track->m_Uuid;
92 track->Move( VECTOR2I( -delta, -delta ) );
93 }
94 }
95 }
96
97 bool foundPad2Error = false;
98 bool foundPad4Error = false;
99 bool foundPad6Error = false;
100 bool foundArc8Error = false;
101 bool foundArc12Error = false;
102 bool foundOtherError = false;
103
104 bds.m_DRCEngine->InitEngine( wxFileName() ); // Just to be sure to be sure
105
107 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
108 const std::function<void( PCB_MARKER* )>& aPathGenerator )
109 {
110 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
111 {
112 BOARD_ITEM* item_a = m_board->ResolveItem( aItem->GetMainItemID() );
113 PAD* pad_a = dynamic_cast<PAD*>( item_a );
114 PCB_TRACK* trk_a = dynamic_cast<PCB_TRACK*>( item_a );
115
116 BOARD_ITEM* item_b = m_board->ResolveItem( aItem->GetAuxItemID() );
117 PAD* pad_b = dynamic_cast<PAD*>( item_b );
118 PCB_TRACK* trk_b = dynamic_cast<PCB_TRACK*>( item_b );
119
120 if( pad_a && pad_a->GetNumber() == "2" ) foundPad2Error = true;
121 else if( pad_a && pad_a->GetNumber() == "4" ) foundPad4Error = true;
122 else if( pad_a && pad_a->GetNumber() == "6" ) foundPad6Error = true;
123 else if( pad_b && pad_b->GetNumber() == "2" ) foundPad2Error = true;
124 else if( pad_b && pad_b->GetNumber() == "4" ) foundPad4Error = true;
125 else if( pad_b && pad_b->GetNumber() == "6" ) foundPad6Error = true;
126 else if( trk_a && trk_a->m_Uuid == arc8 ) foundArc8Error = true;
127 else if( trk_a && trk_a->m_Uuid == arc12 ) foundArc12Error = true;
128 else if( trk_b && trk_b->m_Uuid == arc8 ) foundArc8Error = true;
129 else if( trk_b && trk_b->m_Uuid == arc12 ) foundArc12Error = true;
130 else foundOtherError = true;
131
132 }
133 } );
134
135 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
136
137 BOOST_CHECK_EQUAL( foundPad2Error, true );
138 BOOST_CHECK_EQUAL( foundPad4Error, true );
139 BOOST_CHECK_EQUAL( foundPad6Error, true );
140 BOOST_CHECK_EQUAL( foundArc8Error, true );
141 BOOST_CHECK_EQUAL( foundArc12Error, true );
142 BOOST_CHECK_EQUAL( foundOtherError, false );
143}
144
145
147{
148 KI_TEST::LoadBoard( m_settingsManager, "notched_zones", m_board );
149
150 // Older algorithms had trouble where the filleted zones intersected and left notches.
151 // See:
152 // https://gitlab.com/kicad/code/kicad/-/issues/2737
153 // https://gitlab.com/kicad/code/kicad/-/issues/2752
154 SHAPE_POLY_SET frontCopper;
155
156 KI_TEST::FillZones( m_board.get() );
157
158 frontCopper = SHAPE_POLY_SET();
159
160 for( ZONE* zone : m_board->Zones() )
161 {
162 if( zone->GetLayerSet().Contains( F_Cu ) )
163 {
164 frontCopper.BooleanAdd( *zone->GetFilledPolysList( F_Cu ) );
165 }
166 }
167
168 BOOST_CHECK_EQUAL( frontCopper.OutlineCount(), 2 );
169}
170
171
172static const std::vector<wxString> RegressionZoneFillTests_tests = {
173 "issue18",
174 "issue2568",
175 "issue3812",
176 "issue5102",
177 "issue5313",
178 "issue5320",
179 "issue5567",
180 "issue5830",
181 "issue6039",
182 "issue6260",
183 "issue6284",
184 "issue7086",
185 "issue14294", // Bad Clipper2 fill
186 "fill_bad" // Missing zone clearance expansion
187};
188
189
191 boost::unit_test::data::make( RegressionZoneFillTests_tests ), relPath )
192{
193 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
194
195 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
196
197 KI_TEST::FillZones( m_board.get() );
198
199 std::vector<DRC_ITEM> violations;
200
202 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
203 const std::function<void( PCB_MARKER* )>& aPathGenerator )
204 {
205 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
206 violations.push_back( *aItem );
207 } );
208
209 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
210
211 if( violations.empty() )
212 {
213 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
214 BOOST_TEST_MESSAGE( wxString::Format( "Zone fill regression: %s passed", relPath ) );
215 }
216 else
217 {
218 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
219
220 std::map<KIID, EDA_ITEM*> itemMap;
221 m_board->FillItemMap( itemMap );
222
223 for( const DRC_ITEM& item : violations )
224 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
225
226 BOOST_ERROR( wxString::Format( "Zone fill regression: %s failed", relPath ) );
227 }
228}
229
230
231static const std::vector<wxString> RegressionSliverZoneFillTests_tests = {
232 "issue16182" // Slivers
233};
234
235
236BOOST_DATA_TEST_CASE_F( ZONE_FILL_TEST_FIXTURE, RegressionSliverZoneFillTests,
237 boost::unit_test::data::make( RegressionSliverZoneFillTests_tests ),
238 relPath )
239{
240 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
241
242 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
243
244 KI_TEST::FillZones( m_board.get() );
245
246 std::vector<DRC_ITEM> violations;
247
249 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
250 const std::function<void( PCB_MARKER* )>& aPathGenerator )
251 {
252 if( aItem->GetErrorCode() == DRCE_COPPER_SLIVER )
253 violations.push_back( *aItem );
254 } );
255
256 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
257
258 if( violations.empty() )
259 {
260 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
261 BOOST_TEST_MESSAGE( wxString::Format( "Zone fill copper sliver regression: %s passed", relPath ) );
262 }
263 else
264 {
265 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
266
267 std::map<KIID, EDA_ITEM*> itemMap;
268 m_board->FillItemMap( itemMap );
269
270 for( const DRC_ITEM& item : violations )
271 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
272
273 BOOST_ERROR( wxString::Format( "Zone fill copper sliver regression: %s failed", relPath ) );
274 }
275}
276
277
278static const std::vector<std::pair<wxString,int>> RegressionTeardropFill_tests = {
279 { "teardrop_issue_JPC2", 5 }, // Arcs with teardrops connecting to pads
280};
281
282
284 boost::unit_test::data::make( RegressionTeardropFill_tests ), test )
285{
286 const wxString& relPath = test.first;
287 const int count = test.second;
288
289 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
290
291 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
292
293 KI_TEST::FillZones( m_board.get() );
294
295 int zoneCount = 0;
296
297 for( ZONE* zone : m_board->Zones() )
298 {
299 if( zone->IsTeardropArea() )
300 zoneCount++;
301 }
302
303 BOOST_CHECK_MESSAGE( zoneCount == count, "Expected " << count << " teardrop zones in "
304 << relPath << ", found "
305 << zoneCount );
306}
307
308
310{
311
312 std::vector<wxString> tests = { { "issue19956/issue19956" } // Arcs with teardrops connecting to pads
313 };
314
315 for( const wxString& relPath : tests )
316 {
317 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
318 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
319 KI_TEST::FillZones( m_board.get() );
320
321 for( ZONE* zone : m_board->Zones() )
322 {
323 for( PCB_LAYER_ID layer : zone->GetLayerSet() )
324 {
325 std::shared_ptr<SHAPE> a_shape( zone->GetEffectiveShape( layer ) );
326
327 for( PAD* pad : m_board->GetPads() )
328 {
329 std::shared_ptr<SHAPE> pad_shape( pad->GetEffectiveShape( layer ) );
330 int clearance = pad_shape->GetClearance( a_shape.get() );
331 BOOST_CHECK_MESSAGE( pad->GetNetCode() == zone->GetNetCode() || clearance != 0,
332 wxString::Format( "Pad %s from Footprint %s has net code %s and "
333 "is connected to zone with net code %s",
334 pad->GetNumber(),
335 pad->GetParentFootprint()->GetReferenceAsString(),
336 pad->GetNetname(),
337 zone->GetNetname() ) );
338 }
339 }
340 }
341 }
342}
343
344
359BOOST_FIXTURE_TEST_CASE( RegressionZonePriorityIsolatedIslands, ZONE_FILL_TEST_FIXTURE )
360{
361 // Enable iterative refill to fix issue 21746
362 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
363 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
364 cfg.m_ZoneFillIterativeRefill = true;
365
366 // Restore config at end of scope to avoid polluting other tests
367 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } } guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
368
369 KI_TEST::LoadBoard( m_settingsManager, "issue21746/issue21746", m_board );
370
371 KI_TEST::FillZones( m_board.get() );
372
373 // Find the GND zone
374 ZONE* gndZone = nullptr;
375
376 for( ZONE* zone : m_board->Zones() )
377 {
378 if( zone->GetNetname() == "GND" )
379 {
380 gndZone = zone;
381 break;
382 }
383 }
384
385 BOOST_REQUIRE_MESSAGE( gndZone != nullptr, "GND zone not found in test board" );
386
387 // Calculate board outline area
388 SHAPE_POLY_SET boardOutline;
389 bool hasOutline = m_board->GetBoardPolygonOutlines( boardOutline, true );
390 BOOST_REQUIRE_MESSAGE( hasOutline, "Board outline not found" );
391
392 double boardArea = 0.0;
393
394 for( int i = 0; i < boardOutline.OutlineCount(); i++ )
395 boardArea += boardOutline.Outline( i ).Area();
396
397 // Get GND zone filled area
398 gndZone->CalculateFilledArea();
399 double gndFilledArea = gndZone->GetFilledArea();
400
401 // The GND zone should fill at least 25% of the board area
402 // With the bug, it fills almost nothing because VDD knocks it out
403 double fillRatio = gndFilledArea / boardArea;
404
405 BOOST_TEST_MESSAGE( wxString::Format( "Board area: %.2f sq mm, GND filled area: %.2f sq mm, "
406 "Fill ratio: %.1f%%",
407 boardArea / 1e6, gndFilledArea / 1e6,
408 fillRatio * 100.0 ) );
409
410 BOOST_CHECK_MESSAGE( fillRatio >= 0.25,
411 wxString::Format( "GND zone fill ratio %.1f%% is less than expected 25%%. "
412 "This indicates issue 21746 - lower priority zones not "
413 "filling areas where higher priority isolated islands "
414 "were removed.",
415 fillRatio * 100.0 ) );
416}
417
418
432BOOST_FIXTURE_TEST_CASE( RegressionViaFlashingUnreachableZone, ZONE_FILL_TEST_FIXTURE )
433{
434 KI_TEST::LoadBoard( m_settingsManager, "issue22010/issue22010", m_board );
435
436 KI_TEST::FillZones( m_board.get() );
437
438 // Find vias with zone_layer_connections set for In1.Cu or In2.Cu
439 // After filling, vias that the zone doesn't actually reach should NOT be flashed
440 int viasWithUnreachableFlashing = 0;
441 int totalConditionalVias = 0;
442
443 PCB_LAYER_ID in1Cu = m_board->GetLayerID( wxT( "In1.Cu" ) );
444 PCB_LAYER_ID in2Cu = m_board->GetLayerID( wxT( "In2.Cu" ) );
445
446 for( PCB_TRACK* track : m_board->Tracks() )
447 {
448 if( track->Type() != PCB_VIA_T )
449 continue;
450
451 PCB_VIA* via = static_cast<PCB_VIA*>( track );
452
453 if( !via->GetRemoveUnconnected() )
454 continue;
455
456 totalConditionalVias++;
457
458 // Check if via is flashed on In1.Cu or In2.Cu
459 bool flashedOnIn1 = via->FlashLayer( in1Cu );
460 bool flashedOnIn2 = via->FlashLayer( in2Cu );
461
462 if( !flashedOnIn1 && !flashedOnIn2 )
463 continue;
464
465 VECTOR2I viaCenter = via->GetPosition();
466 int holeRadius = via->GetDrillValue() / 2;
467
468 // Check if any zone fill actually reaches this via
469 bool zoneReachesVia = false;
470
471 for( ZONE* zone : m_board->Zones() )
472 {
473 if( zone->GetIsRuleArea() )
474 continue;
475
476 if( zone->GetNetCode() != via->GetNetCode() )
477 continue;
478
479 for( PCB_LAYER_ID layer : { in1Cu, in2Cu } )
480 {
481 if( !zone->IsOnLayer( layer ) )
482 continue;
483
484 if( !zone->HasFilledPolysForLayer( layer ) )
485 continue;
486
487 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
488
489 if( fill->Contains( viaCenter, -1, holeRadius ) )
490 {
491 zoneReachesVia = true;
492 break;
493 }
494 }
495
496 if( zoneReachesVia )
497 break;
498 }
499
500 // If via is flashed but zone doesn't reach it, that's the bug
501 if( !zoneReachesVia && ( flashedOnIn1 || flashedOnIn2 ) )
502 viasWithUnreachableFlashing++;
503 }
504
505 BOOST_TEST_MESSAGE( wxString::Format( "Total conditional vias: %d, Vias with unreachable "
506 "flashing: %d", totalConditionalVias,
507 viasWithUnreachableFlashing ) );
508
509 BOOST_CHECK_MESSAGE( viasWithUnreachableFlashing == 0,
510 wxString::Format( "Found %d vias flashed on zone layers where the zone "
511 "fill doesn't actually reach them. This indicates "
512 "issue 22010 is not fixed.",
513 viasWithUnreachableFlashing ) );
514}
515
516
530{
531 KI_TEST::LoadBoard( m_settingsManager, "issue12964/issue12964", m_board );
532
533 KI_TEST::FillZones( m_board.get() );
534
535 int viasShortingZones = 0;
536 int totalConditionalVias = 0;
537
538 for( PCB_TRACK* track : m_board->Tracks() )
539 {
540 if( track->Type() != PCB_VIA_T )
541 continue;
542
543 PCB_VIA* via = static_cast<PCB_VIA*>( track );
544
545 if( !via->GetRemoveUnconnected() )
546 continue;
547
548 totalConditionalVias++;
549
550 VECTOR2I viaCenter = via->GetPosition();
551
552 for( ZONE* zone : m_board->Zones() )
553 {
554 if( zone->GetIsRuleArea() )
555 continue;
556
557 if( zone->GetNetCode() == via->GetNetCode() )
558 continue;
559
560 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
561 {
562 if( !via->FlashLayer( layer ) )
563 continue;
564
565 if( !zone->HasFilledPolysForLayer( layer ) )
566 continue;
567
568 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
569 int viaRadius = via->GetWidth( layer ) / 2;
570
571 if( fill->Contains( viaCenter, -1, viaRadius ) )
572 {
573 BOOST_TEST_MESSAGE( wxString::Format(
574 "Via at (%d, %d) on net %s is flashing on layer %s where zone "
575 "net %s is filled - this creates a short!",
576 viaCenter.x, viaCenter.y, via->GetNetname(),
577 m_board->GetLayerName( layer ), zone->GetNetname() ) );
578 viasShortingZones++;
579 }
580 }
581 }
582 }
583
584 BOOST_TEST_MESSAGE( wxString::Format( "Total conditional vias: %d, Vias shorting zones: %d",
585 totalConditionalVias, viasShortingZones ) );
586
587 BOOST_CHECK_MESSAGE( viasShortingZones == 0,
588 wxString::Format( "Found %d vias flashed on layers where they short to "
589 "zones with different nets. This indicates issue 12964 "
590 "is not fixed.",
591 viasShortingZones ) );
592}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
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:83
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:152
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
const KIID m_Uuid
Definition eda_item.h:522
Definition kiid.h:49
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:55
const wxString & GetNumber() const
Definition pad.h:137
double Area(bool aAbsolute=true) const
Return the area of this chain.
Represent a set of closed polygons.
void BooleanAdd(const SHAPE_POLY_SET &b)
Perform boolean polyset union.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
Handle a list of polygons defining a copper zone.
Definition zone.h:74
double GetFilledArea()
This area is cached from the most recent call to CalculateFilledArea().
Definition zone.h:261
double CalculateFilledArea()
Compute the area currently occupied by the zone fill.
Definition zone.cpp:1529
@ DRCE_CLEARANCE
Definition drc_item.h:44
@ DRCE_COPPER_SLIVER
Definition drc_item.h:93
bool m_ZoneFillIterativeRefill
Enable iterative zone filling to handle isolated islands in higher priority zones.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_Cu
Definition layer_ids.h:64
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
void FillZones(BOARD *m_board)
@ RPT_SEVERITY_ERROR
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
int clearance
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
static const std::vector< wxString > RegressionZoneFillTests_tests
int delta
static const std::vector< std::pair< wxString, int > > RegressionTeardropFill_tests
BOOST_DATA_TEST_CASE_F(ZONE_FILL_TEST_FIXTURE, RegressionZoneFillTests, boost::unit_test::data::make(RegressionZoneFillTests_tests), relPath)
static const std::vector< wxString > RegressionSliverZoneFillTests_tests
BOOST_FIXTURE_TEST_CASE(BasicZoneFills, ZONE_FILL_TEST_FIXTURE)
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:98
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695