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 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
82
83 // Count violations where at least one (or both) of the affected items belong to a
84 // footprint whose reference designator matches aRef. aRequireBothItems is used to
85 // verify rules that key on both A.Reference and B.Reference.
86 auto countViolationsForRef =
87 [&]( const std::vector<DRC_ITEM>& aViolations, const wxString& aRef, bool aRequireBothItems )
88 {
89 int count = 0;
90
91 for( const DRC_ITEM& item : aViolations )
92 {
93 int hits = 0;
94
95 for( KIID uuid : { item.GetMainItemID(), item.GetAuxItemID() } )
96 {
97 if( uuid == niluuid )
98 continue;
99
100 const EDA_ITEM* resolved = resolver.ResolveItemById( uuid );
101
102 if( !resolved )
103 continue;
104
105 const BOARD_ITEM* boardItem = dynamic_cast<const BOARD_ITEM*>( resolved );
106
107 if( !boardItem )
108 continue;
109
110 FOOTPRINT* fp = boardItem->GetParentFootprint();
111
112 if( fp && fp->GetReference() == aRef )
113 ++hits;
114 }
115
116 if( aRequireBothItems ? hits >= 2 : hits > 0 )
117 ++count;
118 }
119
120 return count;
121 };
122
123 // Primary assertion: the custom rule "(condition "A.Reference == 'J1'")" must reduce
124 // the default edge clearance so that J1 pads no longer violate.
125 BOOST_CHECK_EQUAL( countViolationsForRef( edgeViolations, wxString( "J1" ), false ), 0 );
126
127 // Secondary assertion: the reproduction rule set also contains a hole_clearance rule
128 // that keys on both A.Reference and B.Reference. Verify the B.Reference side of the
129 // fix as well (PAD-PAD hole clearance within J1 would currently never match).
130 BOOST_CHECK_EQUAL( countViolationsForRef( holeViolations, wxString( "J1" ), true ), 0 );
131
132 if( countViolationsForRef( edgeViolations, wxString( "J1" ), false ) != 0
133 || countViolationsForRef( holeViolations, wxString( "J1" ), true ) != 0 )
134 {
135 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
136
137 for( const DRC_ITEM& item : edgeViolations )
138 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, resolver ) );
139
140 for( const DRC_ITEM& item : holeViolations )
141 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, resolver ) );
142 }
143
144 // Without the custom rules J1 violates the board defaults, so the clean
145 // results above came from the rules matching and not from DRC being silent.
146 edgeViolations.clear();
147 holeViolations.clear();
148
149 bds.m_DRCEngine->InitEngine( wxFileName() );
150 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
151
152 BOOST_REQUIRE_GT( countViolationsForRef( edgeViolations, wxString( "J1" ), false ), 0 );
153 BOOST_REQUIRE_GT( countViolationsForRef( holeViolations, wxString( "J1" ), true ), 0 );
154}
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
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:85
FOOTPRINT * GetParentFootprint() const
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
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const wxString & GetReference() const
Definition footprint.h:793
Definition kiid.h:46
EDA_ITEM * ResolveItemById(const KIID &aKiid) const
Resolve an item by its KIID.
Definition rc_item.h:98
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:63
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:48
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:96
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:86
@ DRCE_EDGE_CLEARANCE
Definition drc_item.h:55
@ DRCE_STARVED_THERMAL
Definition drc_item.h:58
@ 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
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
BOOST_FIXTURE_TEST_CASE(DRCIssue23469_ReferenceReducesEdgeClearance, DRC_ISSUE23469_FIXTURE)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708