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 std::map<KIID, EDA_ITEM*> itemMap;
77 m_board->FillItemMap( itemMap );
78
79 for( const DRC_ITEM& item : violations )
80 {
81 const KIID id = item.GetMainItemID();
82
83 if( id == niluuid )
84 continue;
85
86 auto it = itemMap.find( id );
87
88 if( it == itemMap.end() )
89 continue;
90
91 switch( it->second->Type() )
92 {
93 case PCB_VIA_T: viaViolations++; break;
94 case PCB_TRACE_T: trackViolations++; break;
95 default: break;
96 }
97 }
98
99 // Expect exact counts so that duplicate-marker regressions are caught.
100 BOOST_CHECK_MESSAGE( viaViolations == 2,
101 "Expected exactly 2 via keepout violations, got "
102 << viaViolations << " (total: " << violations.size() << ")" );
103 BOOST_CHECK_MESSAGE( trackViolations == 1,
104 "Expected exactly 1 track keepout violation, got "
105 << trackViolations );
106 BOOST_CHECK_MESSAGE( violations.size() == 3,
107 "Expected exactly 3 DRCE_ALLOWED_ITEMS violations, got "
108 << violations.size() );
109
110 if( viaViolations != 2 || trackViolations != 1 || violations.size() != 3 )
111 {
112 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
113
114 for( const DRC_ITEM& item : violations )
115 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
116 }
117}
118
119
120// Regression test for GitLab #24924: a footprint keepout that disallows footprints
121// must never flag its own footprint. The board is a real ESP32-C3-WROOM-02 (whose
122// keepout excludes footprints) plus a second rule area that duplicates the keepout's
123// UUID and wins the board's item-by-id cache. Duplicate zone UUIDs occur in the wild
124// (boards from older versions, importers, undo clones); the keepout self-exclusion must
125// resolve ownership from the rule's own zone rather than by re-resolving the UUID.
126BOOST_FIXTURE_TEST_CASE( DRCKeepoutFootprintExcludesItself, DRC_KEEPOUT_TEST_FIXTURE )
127{
128 KI_TEST::LoadBoard( m_settingsManager, "issue24924/keepout_self_exclude", m_board );
129
130 std::map<KIID, EDA_ITEM*> itemMap;
131 m_board->FillItemMap( itemMap );
132
133 int footprintViolations = 0;
134 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
135
145
147 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
148 const std::function<void( PCB_MARKER* )>& aPathGenerator )
149 {
150 if( aItem->GetErrorCode() != DRCE_ALLOWED_ITEMS )
151 return;
152
153 auto it = itemMap.find( aItem->GetMainItemID() );
154
155 if( it != itemMap.end() && it->second->Type() == PCB_FOOTPRINT_T )
156 footprintViolations++;
157 } );
158
159 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
160
161 BOOST_CHECK_MESSAGE( footprintViolations == 0,
162 "Footprint flagged by its own keepout, got "
163 << footprintViolations << " footprint keepout violations" );
164}
165
166
167// Regression test: a track outside a no-tracks keepout must not be flagged
168// when board->m_DRCMaxClearance is large. The antiTrackKeepouts Collide()
169// call previously used m_DRCMaxClearance as the collision distance, silently
170// inflating every keepout boundary by whatever the largest clearance on the
171// board happened to be.
172//
173// Board layout:
174// - Keepout rule area (no tracks): (130,60)-(140,90) on F.Cu
175// - Track segment at x=144.018 — 4 mm outside the keepout right edge
176// - Copper zone with 20 mm local clearance at (176,55)-(196,95)
177// - physical_clearance rule of 20 mm in .kicad_dru to widen R-tree search
178//
179// The track is outside the keepout so no violation should be reported.
180BOOST_FIXTURE_TEST_CASE( DRCKeepoutNoClearanceInflation, DRC_KEEPOUT_TEST_FIXTURE )
181{
182 KI_TEST::LoadBoard( m_settingsManager, "keepout_no_clearance/keepout_no_clearance", m_board );
183
184 std::vector<DRC_ITEM> violations;
185 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
186
194
196 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
197 const std::function<void( PCB_MARKER* )>& aPathGenerator )
198 {
199 if( aItem->GetErrorCode() == DRCE_ALLOWED_ITEMS )
200 violations.push_back( *aItem );
201 } );
202
203 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
204
205 if( !violations.empty() )
206 {
207 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
208 std::map<KIID, EDA_ITEM*> itemMap;
209 m_board->FillItemMap( itemMap );
210
211 for( const DRC_ITEM& item : violations )
212 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
213 }
214
215 BOOST_CHECK_MESSAGE( violations.empty(),
216 "Expected no keepout violations for track outside keepout, got " << violations.size() );
217}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
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
Definition kiid.h:46
@ DRCE_VIA_DIAMETER
Definition drc_item.h:59
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:37
@ DRCE_PADSTACK
Definition drc_item.h:60
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:80
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:70
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:58
@ DRCE_STARVED_THERMAL
Definition drc_item.h:47
@ DRCE_ALLOWED_ITEMS
Definition drc_item.h:39
@ DRCE_COPPER_SLIVER
Definition drc_item.h:91
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:81
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:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683