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: a track outside a no-tracks keepout must not be flagged
121// when board->m_DRCMaxClearance is large. The antiTrackKeepouts Collide()
122// call previously used m_DRCMaxClearance as the collision distance, silently
123// inflating every keepout boundary by whatever the largest clearance on the
124// board happened to be.
125//
126// Board layout:
127// - Keepout rule area (no tracks): (130,60)-(140,90) on F.Cu
128// - Track segment at x=144.018 — 4 mm outside the keepout right edge
129// - Copper zone with 20 mm local clearance at (176,55)-(196,95)
130// - physical_clearance rule of 20 mm in .kicad_dru to widen R-tree search
131//
132// The track is outside the keepout so no violation should be reported.
133BOOST_FIXTURE_TEST_CASE( DRCKeepoutNoClearanceInflation, DRC_KEEPOUT_TEST_FIXTURE )
134{
135 KI_TEST::LoadBoard( m_settingsManager, "keepout_no_clearance/keepout_no_clearance", m_board );
136
137 std::vector<DRC_ITEM> violations;
138 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
139
147
149 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
150 const std::function<void( PCB_MARKER* )>& aPathGenerator )
151 {
152 if( aItem->GetErrorCode() == DRCE_ALLOWED_ITEMS )
153 violations.push_back( *aItem );
154 } );
155
156 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
157
158 if( !violations.empty() )
159 {
160 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
161 std::map<KIID, EDA_ITEM*> itemMap;
162 m_board->FillItemMap( itemMap );
163
164 for( const DRC_ITEM& item : violations )
165 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
166 }
167
168 BOOST_CHECK_MESSAGE( violations.empty(),
169 "Expected no keepout violations for track outside keepout, got " << violations.size() );
170}
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:44
@ DRCE_VIA_DIAMETER
Definition drc_item.h:58
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:36
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:79
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:69
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:57
@ DRCE_STARVED_THERMAL
Definition drc_item.h:46
@ DRCE_ALLOWED_ITEMS
Definition drc_item.h:38
@ DRCE_COPPER_SLIVER
Definition drc_item.h:90
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:80
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_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:90
@ 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