KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_keepout_disallow.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.
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, see <https://www.gnu.org/licenses/>.
18 */
19
22#include <board.h>
24#include <drc/drc_engine.h>
25#include <drc/drc_item.h>
26#include <pcb_marker.h>
28
29
38
39
40// Regression test for GitLab #23911: Missing DRC notification for Rule Areas.
41// The test board has a rule area that disallows tracks, vias, pads, zones, and
42// footprints. It contains two vias fully inside the keepout and a track that
43// crosses into the keepout. All three items must produce DRCE_ALLOWED_ITEMS
44// violations.
45BOOST_FIXTURE_TEST_CASE( DRCKeepoutDisallowViasAndTracks, DRC_KEEPOUT_TEST_FIXTURE )
46{
47 KI_TEST::LoadBoard( m_settingsManager, "keepout_disallow/keepout_disallow", m_board );
48
49 std::vector<DRC_ITEM> violations;
50 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
51
52 // Suppress unrelated checks that fire on a bare-bones board.
62
64 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
65 const std::function<void( PCB_MARKER* )>& aPathGenerator )
66 {
67 if( aItem->GetErrorCode() == DRCE_ALLOWED_ITEMS )
68 violations.push_back( *aItem );
69 } );
70
71 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
72
73 int trackViolations = 0;
74 int viaViolations = 0;
75
76 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
77
78 for( const DRC_ITEM& item : violations )
79 {
80 const KIID id = item.GetMainItemID();
81
82 if( id == niluuid )
83 continue;
84
85 const EDA_ITEM* resolved = resolver.ResolveItemById( id );
86
87 if( !resolved )
88 continue;
89
90 switch( resolved->Type() )
91 {
92 case PCB_VIA_T: viaViolations++; break;
93 case PCB_TRACE_T: trackViolations++; break;
94 default: break;
95 }
96 }
97
98 // Expect exact counts so that duplicate-marker regressions are caught.
99 BOOST_CHECK_MESSAGE( viaViolations == 2,
100 "Expected exactly 2 via keepout violations, got "
101 << viaViolations << " (total: " << violations.size() << ")" );
102 BOOST_CHECK_MESSAGE( trackViolations == 1,
103 "Expected exactly 1 track keepout violation, got "
104 << trackViolations );
105 BOOST_CHECK_MESSAGE( violations.size() == 3,
106 "Expected exactly 3 DRCE_ALLOWED_ITEMS violations, got "
107 << violations.size() );
108
109 if( viaViolations != 2 || trackViolations != 1 || violations.size() != 3 )
110 {
111 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
112
113 for( const DRC_ITEM& item : violations )
114 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, resolver ) );
115 }
116}
117
118
119// Regression test for GitLab #24924: a footprint keepout that disallows footprints
120// must never flag its own footprint. The board is a real ESP32-C3-WROOM-02 (whose
121// keepout excludes footprints) plus a second rule area that duplicates the keepout's
122// UUID and wins the board's item-by-id cache. Duplicate zone UUIDs occur in the wild
123// (boards from older versions, importers, undo clones); the keepout self-exclusion must
124// resolve ownership from the rule's own zone rather than by re-resolving the UUID.
125BOOST_FIXTURE_TEST_CASE( DRCKeepoutFootprintExcludesItself, DRC_KEEPOUT_TEST_FIXTURE )
126{
127 KI_TEST::LoadBoard( m_settingsManager, "issue24924/keepout_self_exclude", m_board );
128
129 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
130
131 int footprintViolations = 0;
132 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
133
143
145 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
146 const std::function<void( PCB_MARKER* )>& aPathGenerator )
147 {
148 if( aItem->GetErrorCode() != DRCE_ALLOWED_ITEMS )
149 return;
150
151 const EDA_ITEM* resolved = resolver.ResolveItemById( aItem->GetMainItemID() );
152
153 if( resolved && resolved->Type() == PCB_FOOTPRINT_T )
154 footprintViolations++;
155 } );
156
157 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
158
159 BOOST_CHECK_MESSAGE( footprintViolations == 0,
160 "Footprint flagged by its own keepout, got "
161 << footprintViolations << " footprint keepout violations" );
162}
163
164
165// Regression test: a track outside a no-tracks keepout must not be flagged
166// when board->m_DRCMaxClearance is large. The antiTrackKeepouts Collide()
167// call previously used m_DRCMaxClearance as the collision distance, silently
168// inflating every keepout boundary by whatever the largest clearance on the
169// board happened to be.
170//
171// Board layout:
172// - Keepout rule area (no tracks): (130,60)-(140,90) on F.Cu
173// - Track segment at x=144.018 — 4 mm outside the keepout right edge
174// - Copper zone with 20 mm local clearance at (176,55)-(196,95)
175// - physical_clearance rule of 20 mm in .kicad_dru to widen R-tree search
176//
177// The track is outside the keepout so no violation should be reported.
178BOOST_FIXTURE_TEST_CASE( DRCKeepoutNoClearanceInflation, DRC_KEEPOUT_TEST_FIXTURE )
179{
180 KI_TEST::LoadBoard( m_settingsManager, "keepout_no_clearance/keepout_no_clearance", m_board );
181
182 std::vector<DRC_ITEM> violations;
183 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
184
192
194 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
195 const std::function<void( PCB_MARKER* )>& aPathGenerator )
196 {
197 if( aItem->GetErrorCode() == DRCE_ALLOWED_ITEMS )
198 violations.push_back( *aItem );
199 } );
200
201 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
202
203 if( !violations.empty() )
204 {
205 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
206 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
207
208 for( const DRC_ITEM& item : violations )
209 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, resolver ) );
210 }
211
212 BOOST_CHECK_MESSAGE( violations.empty(),
213 "Expected no keepout violations for track outside keepout, got " << violations.size() );
214}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
Implements the RC_ITEM_RESOLVER interface to resolve PCB items by their KIID.
Definition drc_item.h:349
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:164
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Definition kiid.h:46
EDA_ITEM * ResolveItemById(const KIID &aKiid) const
Resolve an item by its KIID.
Definition rc_item.h:98
@ DRCE_VIA_DIAMETER
Definition drc_item.h:70
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:48
@ DRCE_PADSTACK
Definition drc_item.h:71
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:96
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:86
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:69
@ DRCE_STARVED_THERMAL
Definition drc_item.h:58
@ DRCE_ALLOWED_ITEMS
Definition drc_item.h:50
@ DRCE_COPPER_SLIVER
Definition drc_item.h:107
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:97
KIID niluuid(0)
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_FIXTURE_TEST_CASE(DRCKeepoutDisallowViasAndTracks, DRC_KEEPOUT_TEST_FIXTURE)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708