KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_solder_mask_expansion.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 <pad.h>
25#include <pcb_track.h>
26#include <pcb_shape.h>
27#include <footprint.h>
28#include <drc/drc_engine.h>
29#include <core/profile.h>
31
32
41
42
44{
45 // This test verifies that footprint-level solder mask expansion override is correctly
46 // applied to pads that don't have their own local override.
47 // Regression test for https://gitlab.com/kicad/code/kicad/-/issues/22751
48
49 KI_TEST::LoadBoard( m_settingsManager, "issue22751/issue22751", m_board );
50
51 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
52 bds.m_DRCEngine->InitEngine( wxFileName() );
53
54 // Board default solder mask expansion is 0.05mm (50000 IU)
56
57 // Find footprints and their pads
58 PAD* padWithFootprintMargin = nullptr;
59 PAD* padWithNoMargin = nullptr;
60 PAD* padWithOwnMargin = nullptr;
61
62 for( FOOTPRINT* fp : m_board->Footprints() )
63 {
64 if( fp->GetFPIDAsString() == "TestFootprint_WithSolderMaskMargin" )
65 {
66 // Footprint has solder mask margin of 0.1mm (100000 IU)
67 BOOST_CHECK( fp->GetLocalSolderMaskMargin().has_value() );
68 BOOST_CHECK_EQUAL( fp->GetLocalSolderMaskMargin().value(), 100000 );
69
70 for( PAD* pad : fp->Pads() )
71 {
72 if( pad->GetNumber() == "1" )
73 {
74 padWithFootprintMargin = pad;
75
76 // Pad should NOT have its own local margin
77 BOOST_CHECK( !pad->GetLocalSolderMaskMargin().has_value() );
78 }
79 }
80 }
81 else if( fp->GetFPIDAsString() == "TestFootprint_NoSolderMaskMargin" )
82 {
83 // Footprint has no solder mask margin
84 BOOST_CHECK( !fp->GetLocalSolderMaskMargin().has_value() );
85
86 for( PAD* pad : fp->Pads() )
87 {
88 if( pad->GetNumber() == "1" )
89 padWithNoMargin = pad;
90 }
91 }
92 else if( fp->GetFPIDAsString() == "TestFootprint_PadOverridesSolderMaskMargin" )
93 {
94 // Footprint has solder mask margin of 0.1mm
95 BOOST_CHECK( fp->GetLocalSolderMaskMargin().has_value() );
96 BOOST_CHECK_EQUAL( fp->GetLocalSolderMaskMargin().value(), 100000 );
97
98 for( PAD* pad : fp->Pads() )
99 {
100 if( pad->GetNumber() == "1" )
101 {
102 padWithOwnMargin = pad;
103
104 // Pad has its own margin of 0.2mm (200000 IU)
105 BOOST_CHECK( pad->GetLocalSolderMaskMargin().has_value() );
106 BOOST_CHECK_EQUAL( pad->GetLocalSolderMaskMargin().value(), 200000 );
107 }
108 }
109 }
110 }
111
112 BOOST_REQUIRE( padWithFootprintMargin != nullptr );
113 BOOST_REQUIRE( padWithNoMargin != nullptr );
114 BOOST_REQUIRE( padWithOwnMargin != nullptr );
115
116 // Test GetSolderMaskExpansion which uses the DRC engine to evaluate the constraint
117
118 // Pad in footprint with solder mask margin should use footprint's margin (0.1mm = 100000 IU)
119 int expansionWithFpMargin = padWithFootprintMargin->GetSolderMaskExpansion( F_Mask );
120 BOOST_CHECK_EQUAL( expansionWithFpMargin, 100000 );
121
122 // Pad in footprint without solder mask margin should use board default (0.05mm = 50000 IU)
123 int expansionNoMargin = padWithNoMargin->GetSolderMaskExpansion( F_Mask );
124 BOOST_CHECK_EQUAL( expansionNoMargin, 50000 );
125
126 // Pad with its own solder mask margin should use pad's margin (0.2mm = 200000 IU),
127 // overriding the footprint's margin
128 int expansionWithPadMargin = padWithOwnMargin->GetSolderMaskExpansion( F_Mask );
129 BOOST_CHECK_EQUAL( expansionWithPadMargin, 200000 );
130}
131
132
134{
135 // Verify that GetSolderMaskExpansion is fast when no custom DRC rules exist.
136 // Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23213
137 //
138 // Before the fix, GetSolderMaskExpansion called EvalRules for every item even when no
139 // custom rules existed, causing a 10-14x DRC slowdown vs KiCad 9.
140
141 KI_TEST::LoadBoard( m_settingsManager, "stonehenge", m_board );
142
143 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
144 bds.m_DRCEngine->InitEngine( wxFileName() );
145
147
148 // Collect all items that would be queried during the solder mask test
149 std::vector<PAD*> pads;
150 std::vector<PCB_TRACK*> tracks;
151 std::vector<PCB_SHAPE*> shapes;
152
153 for( FOOTPRINT* fp : m_board->Footprints() )
154 {
155 for( PAD* pad : fp->Pads() )
156 pads.push_back( pad );
157
158 for( BOARD_ITEM* item : fp->GraphicalItems() )
159 {
160 if( item->Type() == PCB_SHAPE_T )
161 shapes.push_back( static_cast<PCB_SHAPE*>( item ) );
162 }
163 }
164
165 for( PCB_TRACK* track : m_board->Tracks() )
166 tracks.push_back( track );
167
168 for( BOARD_ITEM* item : m_board->Drawings() )
169 {
170 if( item->Type() == PCB_SHAPE_T )
171 shapes.push_back( static_cast<PCB_SHAPE*>( item ) );
172 }
173
174 // Simulate the access pattern from the solder mask bridge test: call GetSolderMaskExpansion
175 // many times for each item (once per potential collision pair).
176 const int iterations = 1000;
177
178 PROF_TIMER timer;
179
180 for( int i = 0; i < iterations; ++i )
181 {
182 for( PAD* pad : pads )
183 pad->GetSolderMaskExpansion( F_Mask );
184
185 for( PCB_TRACK* track : tracks )
186 track->GetSolderMaskExpansion();
187
188 for( PCB_SHAPE* shape : shapes )
189 shape->GetSolderMaskExpansion();
190 }
191
192 timer.Stop();
193
194 int totalCalls = iterations * ( pads.size() + tracks.size() + shapes.size() );
195
196 BOOST_TEST_MESSAGE( wxString::Format( "%d calls to GetSolderMaskExpansion took %0.1f ms "
197 "(%0.0f ns/call)",
198 totalCalls, timer.msecs(),
199 timer.msecs() * 1e6 / totalCalls ) );
200
201 // With the fix (direct property lookup), this should be well under 100ms.
202 // Without the fix (EvalRules for every call), this was ~500ms+ even in debug builds.
203 // Use a generous threshold to avoid flakiness on slow CI machines.
204 BOOST_CHECK_MESSAGE( timer.msecs() < 500.0,
205 wxString::Format( "GetSolderMaskExpansion too slow: %0.1f ms for %d calls",
206 timer.msecs(), totalCalls ) );
207}
Container for design settings for a BOARD object.
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
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition pad.h:61
int GetSolderMaskExpansion(PCB_LAYER_ID aLayer) const
Definition pad.cpp:1951
A small class to help profiling.
Definition profile.h:46
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
double msecs(bool aSinceLast=false)
Definition profile.h:147
@ SOLDER_MASK_EXPANSION_CONSTRAINT
Definition drc_rule.h:68
@ F_Mask
Definition layer_ids.h:93
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
BOOST_FIXTURE_TEST_CASE(FootprintLevelSolderMaskExpansion, DRC_SOLDER_MASK_EXPANSION_TEST_FIXTURE)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
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))
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:81