KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_issue23469.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, 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 <footprint.h>
27#include <pad.h>
28#include <pcb_marker.h>
31
32
33/*
34 * Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23469
35 *
36 * Custom DRC rules that key on A.Reference or B.Reference were silently skipped
37 * whenever the matched item was a pad, graphic or other sub-item of a footprint,
38 * because the Reference property was only registered on FOOTPRINT. The custom
39 * rule therefore never overrode the implicit board-wide edge clearance and
40 * edge-clearance violations were reported against pads that the rule was meant
41 * to exempt.
42 */
43
44
52
53
54BOOST_FIXTURE_TEST_CASE( DRCIssue23469_ReferenceReducesEdgeClearance, DRC_ISSUE23469_FIXTURE )
55{
56 KI_TEST::LoadBoard( m_settingsManager, "issue23469/issue23469", m_board );
57
58 std::vector<DRC_ITEM> edgeViolations;
59 std::vector<DRC_ITEM> holeViolations;
60 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
61
68
70 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
71 const std::function<void( PCB_MARKER* )>& )
72 {
73 if( aItem->GetErrorCode() == DRCE_EDGE_CLEARANCE )
74 edgeViolations.push_back( *aItem );
75 else if( aItem->GetErrorCode() == DRCE_HOLE_CLEARANCE )
76 holeViolations.push_back( *aItem );
77 } );
78
79 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
80
81 std::map<KIID, EDA_ITEM*> itemMap;
82 m_board->FillItemMap( itemMap );
83
84 // Count violations where at least one (or both) of the affected items belong to a
85 // footprint whose reference designator matches aRef. aRequireBothItems is used to
86 // verify rules that key on both A.Reference and B.Reference.
87 auto countViolationsForRef =
88 [&]( const std::vector<DRC_ITEM>& aViolations, const wxString& aRef, bool aRequireBothItems )
89 {
90 int count = 0;
91
92 for( const DRC_ITEM& item : aViolations )
93 {
94 int hits = 0;
95
96 for( KIID uuid : { item.GetMainItemID(), item.GetAuxItemID() } )
97 {
98 if( uuid == niluuid )
99 continue;
100
101 auto it = itemMap.find( uuid );
102
103 if( it == itemMap.end() )
104 continue;
105
106 BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( it->second );
107
108 if( !boardItem )
109 continue;
110
111 FOOTPRINT* fp = boardItem->GetParentFootprint();
112
113 if( fp && fp->GetReference() == aRef )
114 ++hits;
115 }
116
117 if( aRequireBothItems ? hits >= 2 : hits > 0 )
118 ++count;
119 }
120
121 return count;
122 };
123
124 // Positive control: the reproduction board has an NPTH mounting hole on SW1 whose
125 // drilled opening crosses the board edge. SW1 is not covered by any custom rule, so
126 // these violations must still be reported. A regression that silenced edge-clearance
127 // DRC entirely would otherwise hide the fix.
128 BOOST_REQUIRE_GT( countViolationsForRef( edgeViolations, wxString( "SW1" ), false ), 0 );
129
130 // Primary assertion: the custom rule "(condition "A.Reference == 'J1'")" must reduce
131 // the default edge clearance so that J1 pads no longer violate.
132 BOOST_CHECK_EQUAL( countViolationsForRef( edgeViolations, wxString( "J1" ), false ), 0 );
133
134 // Secondary assertion: the reproduction rule set also contains a hole_clearance rule
135 // that keys on both A.Reference and B.Reference. Verify the B.Reference side of the
136 // fix as well (PAD-PAD hole clearance within J1 would currently never match).
137 BOOST_CHECK_EQUAL( countViolationsForRef( holeViolations, wxString( "J1" ), true ), 0 );
138
139 if( countViolationsForRef( edgeViolations, wxString( "J1" ), false ) != 0
140 || countViolationsForRef( holeViolations, wxString( "J1" ), true ) != 0 )
141 {
142 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
143
144 for( const DRC_ITEM& item : edgeViolations )
145 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
146
147 for( const DRC_ITEM& item : holeViolations )
148 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
149 }
150}
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
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
FOOTPRINT * GetParentFootprint() const
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
const wxString & GetReference() const
Definition footprint.h:841
Definition kiid.h:44
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:51
@ 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_EDGE_CLEARANCE
Definition drc_item.h:43
@ DRCE_STARVED_THERMAL
Definition drc_item.h:46
@ 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
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
BOOST_FIXTURE_TEST_CASE(DRCIssue23469_ReferenceReducesEdgeClearance, DRC_ISSUE23469_FIXTURE)
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683