KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_board_design_settings.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
21
22#include <board.h>
24#include <drc/drc_engine.h>
25#include <drc/drc_rule.h>
28
29
30namespace
31{
32struct BDS_TEST_FIXTURE
33{
34 BDS_TEST_FIXTURE() :
35 m_board( new BOARD() )
36 { }
37
38 SETTINGS_MANAGER m_settingsManager;
39 std::unique_ptr<BOARD> m_board;
40};
41} // namespace
42
43
44BOOST_FIXTURE_TEST_SUITE( BoardDesignSettings, BDS_TEST_FIXTURE )
45
46
47
54BOOST_AUTO_TEST_CASE( NegativeSilkClearanceRoundTrip )
55{
56 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
57
58 int negativeValue = pcbIUScale.mmToIU( -0.1 );
59 bds.m_SilkClearance = negativeValue;
60
61 // Store all params into the JSON backing store
62 bds.Store();
63
64 // Reset to default
65 bds.m_SilkClearance = 0;
66
67 // Load back from JSON
68 bds.Load();
69
70 BOOST_CHECK_EQUAL( bds.m_SilkClearance, negativeValue );
71}
72
73
83BOOST_AUTO_TEST_CASE( BiggestClearanceIncludesPhysicalHoleClearance )
84{
85 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
86
87 // Larger than any default clearance, so it can only come from the rule below.
88 const int ruleClearance = pcbIUScale.mmToIU( 4.0 );
89
90 auto rule = std::make_shared<DRC_RULE>( wxT( "NPTH Hole to Track Clearance" ) );
91
93 constraint.Value().SetMin( ruleClearance );
94 rule->AddConstraint( constraint );
95
96 auto engine = std::make_shared<DRC_ENGINE>( m_board.get(), &bds );
97 engine->InitEngine( rule );
98 bds.m_DRCEngine = engine;
99
100 BOOST_CHECK_GE( bds.GetBiggestClearanceValue(), ruleClearance );
101}
102
103
104// Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24646
105// Cycling predefined sizes must skip the index-0 netclass placeholder on roll-over and stay
106// monotonic.
107BOOST_AUTO_TEST_CASE( PredefinedTrackWidthCyclingMonotonic )
108{
109 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
110
111 // Index 0 is the netclass placeholder; indices 1..5 are sorted predefined sizes.
112 bds.m_TrackWidthList = { 0,
113 pcbIUScale.mmToIU( 0.5 ),
114 pcbIUScale.mmToIU( 0.6 ),
115 pcbIUScale.mmToIU( 0.8 ),
116 pcbIUScale.mmToIU( 1.0 ),
117 pcbIUScale.mmToIU( 1.2 ) };
118
119 const int lastReal = (int) bds.m_TrackWidthList.size() - 1;
120
121 // Increment: netclass(0) -> smallest(1) -> ... -> largest, then roll over to smallest, never 0.
122 BOOST_CHECK_EQUAL( bds.GetNextTrackWidthIndex( 0, true ), 1 );
123
124 int idx = 1;
125
126 for( int step = 2; step <= lastReal; ++step )
127 {
128 idx = bds.GetNextTrackWidthIndex( idx, true );
129 BOOST_CHECK_EQUAL( idx, step );
130 }
131
132 // Roll-over from the largest must skip the placeholder and land on the smallest real size.
133 BOOST_CHECK_EQUAL( bds.GetNextTrackWidthIndex( lastReal, true ), 1 );
134
135 // Decrement: smallest(1) rolls over to the largest, never to the placeholder.
136 BOOST_CHECK_EQUAL( bds.GetNextTrackWidthIndex( 1, false ), lastReal );
137
138 idx = lastReal;
139
140 for( int step = lastReal - 1; step >= 1; --step )
141 {
142 idx = bds.GetNextTrackWidthIndex( idx, false );
143 BOOST_CHECK_EQUAL( idx, step );
144 }
145
146 // A full forward cycle must never report the netclass placeholder once stepping has started.
147 idx = bds.GetNextTrackWidthIndex( 0, true );
148
149 for( int step = 0; step < lastReal * 3; ++step )
150 {
151 BOOST_CHECK_GE( idx, 1 );
152 idx = bds.GetNextTrackWidthIndex( idx, true );
153 }
154
155 // An empty list (no placeholder, no predefined sizes) must stay at index 0 and never store a
156 // negative index through SetTrackWidthIndex.
157 bds.m_TrackWidthList.clear();
158 BOOST_CHECK_EQUAL( bds.GetNextTrackWidthIndex( 0, true ), 0 );
159 BOOST_CHECK_EQUAL( bds.GetNextTrackWidthIndex( 0, false ), 0 );
160 bds.SetTrackWidthIndex( bds.GetNextTrackWidthIndex( 0, false ) );
162}
163
164
165BOOST_AUTO_TEST_CASE( PredefinedViaSizeCyclingMonotonic )
166{
167 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
168
169 // Index 0 is the netclass placeholder; indices 1..3 are sorted predefined via sizes.
170 bds.m_ViasDimensionsList = { { 0, 0 },
171 { pcbIUScale.mmToIU( 0.6 ), pcbIUScale.mmToIU( 0.3 ) },
172 { pcbIUScale.mmToIU( 0.8 ), pcbIUScale.mmToIU( 0.4 ) },
173 { pcbIUScale.mmToIU( 1.0 ), pcbIUScale.mmToIU( 0.5 ) } };
174
175 const int lastReal = (int) bds.m_ViasDimensionsList.size() - 1;
176
177 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 0, true ), 1 );
178 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( lastReal, true ), 1 );
179 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 1, false ), lastReal );
180
181 // A list holding only the netclass placeholder must stay put in both directions.
182 bds.m_ViasDimensionsList = { { 0, 0 } };
183 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 0, true ), 0 );
184 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 0, false ), 0 );
185
186 // An empty list must stay at index 0 and never store a negative index.
187 bds.m_ViasDimensionsList.clear();
188 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 0, true ), 0 );
189 BOOST_CHECK_EQUAL( bds.GetNextViaSizeIndex( 0, false ), 0 );
190 bds.SetViaSizeIndex( bds.GetNextViaSizeIndex( 0, false ) );
192}
193
194
195BOOST_AUTO_TEST_CASE( PredefinedDiffPairCyclingMonotonic )
196{
197 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
198
199 // Index 0 is the netclass placeholder; indices 1..3 are sorted predefined diff-pair dimensions.
200 bds.m_DiffPairDimensionsList = { { 0, 0, 0 },
201 { pcbIUScale.mmToIU( 0.2 ), pcbIUScale.mmToIU( 0.2 ), 0 },
202 { pcbIUScale.mmToIU( 0.25 ), pcbIUScale.mmToIU( 0.25 ), 0 },
203 { pcbIUScale.mmToIU( 0.3 ), pcbIUScale.mmToIU( 0.3 ), 0 } };
204
205 const int lastReal = (int) bds.m_DiffPairDimensionsList.size() - 1;
206
207 BOOST_CHECK_EQUAL( bds.GetNextDiffPairIndex( 0, true ), 1 );
208 BOOST_CHECK_EQUAL( bds.GetNextDiffPairIndex( lastReal, true ), 1 );
209 BOOST_CHECK_EQUAL( bds.GetNextDiffPairIndex( 1, false ), lastReal );
210
211 // An empty list must stay at index 0 and never store a negative index.
212 bds.m_DiffPairDimensionsList.clear();
213 BOOST_CHECK_EQUAL( bds.GetNextDiffPairIndex( 0, true ), 0 );
214 BOOST_CHECK_EQUAL( bds.GetNextDiffPairIndex( 0, false ), 0 );
215 bds.SetDiffPairIndex( bds.GetNextDiffPairIndex( 0, false ) );
217}
218
219
220// Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24644
221// Switching track width from the connected-width placeholder must advance to a real size on the
222// first press. The router gating is GUI bound, so this drives the same GetNextTrackWidthIndex
223// stepping the hotkeys call.
224BOOST_AUTO_TEST_CASE( TrackWidthSwitchAdvancesFromConnectedWidth )
225{
226 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
227
228 const int size1 = pcbIUScale.mmToIU( 0.25 );
229 const int size2 = pcbIUScale.mmToIU( 0.50 );
230 const int size3 = pcbIUScale.mmToIU( 1.00 );
231
232 // Index 0 is the netclass/connected-width placeholder; the rest are real predefined sizes.
233 bds.m_TrackWidthList = { 0, size1, size2, size3 };
234 bds.UseCustomTrackViaSize( false );
235
236 // Start on the connected-width placeholder, as the router does before any override.
237 bds.SetTrackWidthIndex( 0 );
239
240 auto incIndex = [&]() { bds.SetTrackWidthIndex( bds.GetNextTrackWidthIndex( bds.GetTrackWidthIndex(), true ) ); };
241 auto decIndex = [&]() { bds.SetTrackWidthIndex( bds.GetNextTrackWidthIndex( bds.GetTrackWidthIndex(), false ) ); };
242
243 // First "Next" press must move to the first real predefined size, not no-op.
244 incIndex();
247
248 // Subsequent presses step through the list, then roll over to the smallest real size, skipping
249 // the netclass placeholder at index 0.
250 incIndex();
252 incIndex();
254 incIndex();
257
258 // First "Previous" press from the placeholder must roll over to the last real size.
259 bds.SetTrackWidthIndex( 0 );
260 decIndex();
263}
264
265
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Container for design settings for a BOARD object.
void UseCustomTrackViaSize(bool aEnabled)
Enables/disables custom track/via size settings.
int GetNextDiffPairIndex(int aIndex, bool aForward) const
Compute the next diff pair dimensions list index when cycling predefined sizes, skipping the index-0 ...
void SetViaSizeIndex(int aIndex)
Set the current via size list index to aIndex.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
std::vector< DIFF_PAIR_DIMENSION > m_DiffPairDimensionsList
int GetNextTrackWidthIndex(int aIndex, bool aForward) const
Compute the next track width list index when cycling predefined sizes, skipping the index-0 netclass ...
void SetTrackWidthIndex(int aIndex)
Set the current track width list index to aIndex.
int GetNextViaSizeIndex(int aIndex, bool aForward) const
Compute the next via size list index when cycling predefined sizes, skipping the index-0 netclass pla...
std::vector< int > m_TrackWidthList
std::vector< VIA_DIMENSION > m_ViasDimensionsList
MINOPTMAX< int > & Value()
Definition drc_rule.h:197
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
void SetMin(T v)
Definition minoptmax.h:37
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition drc_rule.h:83
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(NegativeSilkClearanceRoundTrip)
Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23327.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")