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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
26#include <board.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <footprint.h>
31#include <pad.h>
32#include <pcb_marker.h>
35
36
37/*
38 * Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23469
39 *
40 * Custom DRC rules that key on A.Reference or B.Reference were silently skipped
41 * whenever the matched item was a pad, graphic or other sub-item of a footprint,
42 * because the Reference property was only registered on FOOTPRINT. The custom
43 * rule therefore never overrode the implicit board-wide edge clearance and
44 * edge-clearance violations were reported against pads that the rule was meant
45 * to exempt.
46 */
47
48
56
57
58BOOST_FIXTURE_TEST_CASE( DRCIssue23469_ReferenceReducesEdgeClearance, DRC_ISSUE23469_FIXTURE )
59{
60 KI_TEST::LoadBoard( m_settingsManager, "issue23469/issue23469", m_board );
61
62 std::vector<DRC_ITEM> edgeViolations;
63 std::vector<DRC_ITEM> holeViolations;
64 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
65
72
74 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
75 const std::function<void( PCB_MARKER* )>& )
76 {
77 if( aItem->GetErrorCode() == DRCE_EDGE_CLEARANCE )
78 edgeViolations.push_back( *aItem );
79 else if( aItem->GetErrorCode() == DRCE_HOLE_CLEARANCE )
80 holeViolations.push_back( *aItem );
81 } );
82
83 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
84
85 std::map<KIID, EDA_ITEM*> itemMap;
86 m_board->FillItemMap( itemMap );
87
88 // Count violations where at least one (or both) of the affected items belong to a
89 // footprint whose reference designator matches aRef. aRequireBothItems is used to
90 // verify rules that key on both A.Reference and B.Reference.
91 auto countViolationsForRef =
92 [&]( const std::vector<DRC_ITEM>& aViolations, const wxString& aRef, bool aRequireBothItems )
93 {
94 int count = 0;
95
96 for( const DRC_ITEM& item : aViolations )
97 {
98 int hits = 0;
99
100 for( KIID uuid : { item.GetMainItemID(), item.GetAuxItemID() } )
101 {
102 if( uuid == niluuid )
103 continue;
104
105 auto it = itemMap.find( uuid );
106
107 if( it == itemMap.end() )
108 continue;
109
110 BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( it->second );
111
112 if( !boardItem )
113 continue;
114
115 FOOTPRINT* fp = boardItem->GetParentFootprint();
116
117 if( fp && fp->GetReference() == aRef )
118 ++hits;
119 }
120
121 if( aRequireBothItems ? hits >= 2 : hits > 0 )
122 ++count;
123 }
124
125 return count;
126 };
127
128 // Positive control: the reproduction board has an NPTH mounting hole on SW1 whose
129 // drilled opening crosses the board edge. SW1 is not covered by any custom rule, so
130 // these violations must still be reported. A regression that silenced edge-clearance
131 // DRC entirely would otherwise hide the fix.
132 BOOST_REQUIRE_GT( countViolationsForRef( edgeViolations, wxString( "SW1" ), false ), 0 );
133
134 // Primary assertion: the custom rule "(condition "A.Reference == 'J1'")" must reduce
135 // the default edge clearance so that J1 pads no longer violate.
136 BOOST_CHECK_EQUAL( countViolationsForRef( edgeViolations, wxString( "J1" ), false ), 0 );
137
138 // Secondary assertion: the reproduction rule set also contains a hole_clearance rule
139 // that keys on both A.Reference and B.Reference. Verify the B.Reference side of the
140 // fix as well (PAD-PAD hole clearance within J1 would currently never match).
141 BOOST_CHECK_EQUAL( countViolationsForRef( holeViolations, wxString( "J1" ), true ), 0 );
142
143 if( countViolationsForRef( edgeViolations, wxString( "J1" ), false ) != 0
144 || countViolationsForRef( holeViolations, wxString( "J1" ), true ) != 0 )
145 {
146 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
147
148 for( const DRC_ITEM& item : edgeViolations )
149 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
150
151 for( const DRC_ITEM& item : holeViolations )
152 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
153 }
154}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
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:84
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:168
const wxString & GetReference() const
Definition footprint.h:829
Definition kiid.h:48
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:55
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:40
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:83
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:73
@ DRCE_EDGE_CLEARANCE
Definition drc_item.h:47
@ DRCE_STARVED_THERMAL
Definition drc_item.h:50
@ DRCE_COPPER_SLIVER
Definition drc_item.h:93
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:84
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:687