KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_chain_topology.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
20#include <boost/test/unit_test.hpp>
21
22#include <filesystem>
23#include <fstream>
24#include <set>
25
26#include <board.h>
29#include <footprint.h>
30#include <netinfo.h>
31#include <pad.h>
32#include <pcb_track.h>
34
35
36namespace
37{
38
39// Header that produces a minimal layer set sufficient for the test fixtures.
40constexpr const char* PCB_HEADER = R"(
41(kicad_pcb
42 (version 20250904)
43 (generator "pcbnew")
44 (generator_version "9.99")
45 (layers
46 (0 "F.Cu" signal)
47 (2 "B.Cu" signal)
48 (44 "Edge.Cuts" user)
49 )
50)";
51
52constexpr int MM = 1000000;
53
54
55std::unique_ptr<BOARD> loadFromString( const std::string& aPcbText,
56 const std::string& aSubdir )
57{
58 namespace fs = std::filesystem;
59 fs::path tmpDir = fs::temp_directory_path() / aSubdir;
60 fs::create_directories( tmpDir );
61 fs::path pcbPath = tmpDir / "topo.kicad_pcb";
62
63 {
64 std::ofstream out( pcbPath );
65 out << aPcbText;
66 }
67
68 PCB_IO_KICAD_SEXPR plugin;
69 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
70 plugin.LoadBoard( pcbPath.string(), board.get() );
71 board->BuildConnectivity();
72
73 fs::remove( pcbPath );
74 return board;
75}
76
77
78std::set<BOARD_CONNECTED_ITEM*> chainItems( BOARD* aBoard, const wxString& aChain )
79{
80 std::set<BOARD_CONNECTED_ITEM*> items;
81
82 for( PCB_TRACK* t : aBoard->Tracks() )
83 {
84 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
85 items.insert( t );
86 }
87
88 for( FOOTPRINT* fp : aBoard->Footprints() )
89 {
90 for( PAD* p : fp->Pads() )
91 {
92 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
93 items.insert( p );
94 }
95 }
96
97 return items;
98}
99
100
101// Tag every net whose name starts with "/NET_" into the named chain, and set
102// terminal pads to the first/last footprint's first pad on that chain. fp1
103// pad-1 is terminal[0], lastFp pad-1 is terminal[1].
104void tagChain( BOARD* aBoard, const wxString& aChain, const wxString& aFirstFpRef,
105 const wxString& aLastFpRef )
106{
107 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
108 {
109 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
110 n->SetNetChain( aChain );
111 }
112
113 PAD* termA = nullptr;
114 PAD* termB = nullptr;
115
116 for( FOOTPRINT* fp : aBoard->Footprints() )
117 {
118 if( fp->GetReference() == aFirstFpRef && !fp->Pads().empty() )
119 termA = fp->Pads().front();
120
121 if( fp->GetReference() == aLastFpRef && !fp->Pads().empty() )
122 termB = fp->Pads().back();
123 }
124
125 if( termA )
126 {
127 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
128 if( n && n->GetNetChain() == aChain )
129 n->SetTerminalPad( 0, termA );
130 }
131
132 if( termB )
133 {
134 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
135 if( n && n->GetNetChain() == aChain )
136 n->SetTerminalPad( 1, termB );
137 }
138}
139
140
141// Footprint stamper for SMD passives. Each pad on its own net. Pad-net
142// references use the (net N "name") format the modern parser expects.
143std::string passive( const wxString& aRef, double aXmm, double aYmm,
144 double aPadSpanMm,
145 int aNetA, const wxString& aNetAName,
146 int aNetB, const wxString& aNetBName )
147{
148 return wxString::Format(
149 R"(
150 (footprint "Passive" (layer "F.Cu") (uuid "00000000-0000-0000-0000-000000%06d")
151 (at %f %f)
152 (property "Reference" "%s" (at 0 -1) (layer "F.Cu") (hide yes) (uuid "00000000-0000-0000-0000-000000%06da") (effects (font (size 1 1) (thickness 0.15))))
153 (pad "1" smd rect (at %f 0) (size 0.8 0.8) (layers "F.Cu") (net %d "%s") (uuid "00000000-0000-0000-0000-000000%06d1"))
154 (pad "2" smd rect (at %f 0) (size 0.8 0.8) (layers "F.Cu") (net %d "%s") (uuid "00000000-0000-0000-0000-000000%06d2"))
155 )
156)", static_cast<int>( aXmm ), aXmm, aYmm, aRef, static_cast<int>( aXmm ),
157 -aPadSpanMm / 2, aNetA, aNetAName, static_cast<int>( aXmm ),
158 aPadSpanMm / 2, aNetB, aNetBName, static_cast<int>( aXmm ) ).ToStdString();
159}
160
161
162std::string segment( double aX1mm, double aY1mm, double aX2mm, double aY2mm, int aNet )
163{
164 return wxString::Format(
165 " (segment (start %f %f) (end %f %f) (width 0.2) (layer \"F.Cu\") (net %d))\n",
166 aX1mm, aY1mm, aX2mm, aY2mm, aNet ).ToStdString();
167}
168
169} // namespace
170
171
172BOOST_AUTO_TEST_SUITE( DRCChainTopology )
173
174
175// Three-net trunk through two passives.
176// pad@(0,0) — track 30mm — bridge 5mm — track 30mm — bridge 5mm — track 30mm — pad@(100,0).
177// Trunk = 100 mm, zero stubs.
178BOOST_AUTO_TEST_CASE( TopologyTreeOnSimpleTrunk )
179{
180 std::string pcb = std::string( PCB_HEADER ) +
181 R"(
182 (net 0 "")
183 (net 1 "/NET_A")
184 (net 2 "/NET_B")
185 (net 3 "/NET_C")
186 (gr_line (start -5 -5) (end 110 -5) (layer "Edge.Cuts") (width 0.05))
187 (gr_line (start 110 -5) (end 110 5) (layer "Edge.Cuts") (width 0.05))
188 (gr_line (start 110 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
189 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
190)" +
191 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
192 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
193 passive( wxS( "R1" ), 32.5, 0.0, 5.0,
194 1, wxS( "/NET_A" ), 2, wxS( "/NET_B" ) ) +
195 passive( wxS( "R2" ), 67.5, 0.0, 5.0,
196 2, wxS( "/NET_B" ), 3, wxS( "/NET_C" ) ) +
197 passive( wxS( "FP_END" ), 100.0, 0.0, 0.0,
198 3, wxS( "/NET_C" ), 3, wxS( "/NET_C" ) ) +
199 segment( 0.0, 0.0, 30.0, 0.0, 1 ) +
200 segment( 35.0, 0.0, 65.0, 0.0, 2 ) +
201 segment( 70.0, 0.0, 100.0, 0.0, 3 ) +
202 "\n)";
203
204 auto board = loadFromString( pcb, "kicad_drc_topo_simple_trunk" );
205 tagChain( board.get(), wxS( "SIG" ), wxS( "FP_START" ), wxS( "FP_END" ) );
206
207 auto items = chainItems( board.get(), wxS( "SIG" ) );
208 CHAIN_TOPOLOGY topo( board.get(), wxS( "SIG" ), items );
209
210 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
211 static_cast<int>( CHAIN_TOPOLOGY::STATUS::OK ) );
212 BOOST_CHECK( topo.IsValid() );
213 BOOST_CHECK_CLOSE( topo.TrunkLength(), 100.0 * MM, 5.0 );
214 BOOST_CHECK( topo.Stubs().empty() );
215}
216
217
218// Single-net trunk plus a perpendicular T-stub of 5 mm at the midpoint.
219BOOST_AUTO_TEST_CASE( TopologyDetectsTStub )
220{
221 std::string pcb = std::string( PCB_HEADER ) +
222 R"(
223 (net 0 "")
224 (net 1 "/NET_A")
225 (gr_line (start -5 -10) (end 60 -10) (layer "Edge.Cuts") (width 0.05))
226 (gr_line (start 60 -10) (end 60 15) (layer "Edge.Cuts") (width 0.05))
227 (gr_line (start 60 15) (end -5 15) (layer "Edge.Cuts") (width 0.05))
228 (gr_line (start -5 15) (end -5 -10) (layer "Edge.Cuts") (width 0.05))
229)" +
230 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
231 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
232 passive( wxS( "FP_END" ), 50.0, 0.0, 0.0,
233 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
234 // Trunk 0..50 along y=0; stub branches off at x=25 going to (25,5).
235 segment( 0.0, 0.0, 50.0, 0.0, 1 ) +
236 segment( 25.0, 0.0, 25.0, 5.0, 1 ) +
237 "\n)";
238
239 auto board = loadFromString( pcb, "kicad_drc_topo_t_stub" );
240 tagChain( board.get(), wxS( "TSIG" ), wxS( "FP_START" ), wxS( "FP_END" ) );
241
242 auto items = chainItems( board.get(), wxS( "TSIG" ) );
243 CHAIN_TOPOLOGY topo( board.get(), wxS( "TSIG" ), items );
244
245 BOOST_CHECK( topo.IsValid() );
246 BOOST_REQUIRE_EQUAL( topo.Stubs().size(), 1u );
247
248 const CHAIN_TOPOLOGY::STUB& stub = topo.Stubs().front();
249 BOOST_CHECK_LE( std::abs( stub.branchPoint.x - 25 * MM ), 100 );
250 BOOST_CHECK_LE( std::abs( stub.branchPoint.y ), 100 );
251 BOOST_CHECK_CLOSE( stub.length, 5.0 * MM, 5.0 );
252
253 // Trunk length is 50 mm and the stub does not contribute.
254 BOOST_CHECK_CLOSE( topo.TrunkLength(), 50.0 * MM, 5.0 );
255}
256
257
258// Only one terminal pad set: NO_TERMINAL_PADS.
259BOOST_AUTO_TEST_CASE( TopologyMissingTerminalPad )
260{
261 std::string pcb = std::string( PCB_HEADER ) +
262 R"(
263 (net 0 "")
264 (net 1 "/NET_A")
265 (gr_line (start -5 -5) (end 40 -5) (layer "Edge.Cuts") (width 0.05))
266 (gr_line (start 40 -5) (end 40 5) (layer "Edge.Cuts") (width 0.05))
267 (gr_line (start 40 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
268 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
269)" +
270 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
271 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
272 segment( 0.0, 0.0, 30.0, 0.0, 1 ) +
273 "\n)";
274
275 auto board = loadFromString( pcb, "kicad_drc_topo_missing_term" );
276 // Only set chain — no second terminal-pad anchor footprint.
277 for( NETINFO_ITEM* n : board->GetNetInfo() )
278 {
279 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
280 n->SetNetChain( wxS( "MISSING" ) );
281 }
283 PAD* termA = board->Footprints().empty()
284 ? nullptr
285 : board->Footprints().front()->Pads().empty()
286 ? nullptr
287 : board->Footprints().front()->Pads().front();
288
289 if( termA )
290 {
291 for( NETINFO_ITEM* n : board->GetNetInfo() )
292 if( n && n->GetNetChain() == wxS( "MISSING" ) )
293 n->SetTerminalPad( 0, termA );
294 }
295
296 auto items = chainItems( board.get(), wxS( "MISSING" ) );
297 CHAIN_TOPOLOGY topo( board.get(), wxS( "MISSING" ), items );
298
299 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
300 static_cast<int>( CHAIN_TOPOLOGY::STATUS::NO_TERMINAL_PADS ) );
301}
302
303
304// Both terminals set, no track between them: DISCONNECTED.
305BOOST_AUTO_TEST_CASE( TopologyDisconnected )
306{
307 std::string pcb = std::string( PCB_HEADER ) +
308 R"(
309 (net 0 "")
310 (net 1 "/NET_A")
311 (gr_line (start -5 -5) (end 110 -5) (layer "Edge.Cuts") (width 0.05))
312 (gr_line (start 110 -5) (end 110 5) (layer "Edge.Cuts") (width 0.05))
313 (gr_line (start 110 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
314 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
315)" +
316 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
317 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
318 passive( wxS( "FP_END" ), 100.0, 0.0, 0.0,
319 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
320 "\n)";
321
322 auto board = loadFromString( pcb, "kicad_drc_topo_disconnected" );
323 tagChain( board.get(), wxS( "DISC" ), wxS( "FP_START" ), wxS( "FP_END" ) );
324
325 auto items = chainItems( board.get(), wxS( "DISC" ) );
326 CHAIN_TOPOLOGY topo( board.get(), wxS( "DISC" ), items );
327
328 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
329 static_cast<int>( CHAIN_TOPOLOGY::STATUS::DISCONNECTED ) );
330}
331
332
333// Two parallel paths between terminals → CYCLE_DETECTED.
334BOOST_AUTO_TEST_CASE( TopologyCycleDetected )
335{
336 std::string pcb = std::string( PCB_HEADER ) +
337 R"(
338 (net 0 "")
339 (net 1 "/NET_A")
340 (gr_line (start -5 -10) (end 60 -10) (layer "Edge.Cuts") (width 0.05))
341 (gr_line (start 60 -10) (end 60 10) (layer "Edge.Cuts") (width 0.05))
342 (gr_line (start 60 10) (end -5 10) (layer "Edge.Cuts") (width 0.05))
343 (gr_line (start -5 10) (end -5 -10) (layer "Edge.Cuts") (width 0.05))
344)" +
345 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
346 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
347 passive( wxS( "FP_END" ), 50.0, 0.0, 0.0,
348 1, wxS( "/NET_A" ), 1, wxS( "/NET_A" ) ) +
349 // Two routes start→end forming a loop.
350 segment( 0.0, 0.0, 25.0, 5.0, 1 ) +
351 segment( 25.0, 5.0, 50.0, 0.0, 1 ) +
352 segment( 0.0, 0.0, 25.0, -5.0, 1 ) +
353 segment( 25.0, -5.0, 50.0, 0.0, 1 ) +
354 "\n)";
355
356 auto board = loadFromString( pcb, "kicad_drc_topo_cycle" );
357 tagChain( board.get(), wxS( "LOOP" ), wxS( "FP_START" ), wxS( "FP_END" ) );
358
359 auto items = chainItems( board.get(), wxS( "LOOP" ) );
360 CHAIN_TOPOLOGY topo( board.get(), wxS( "LOOP" ), items );
361
362 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
363 static_cast<int>( CHAIN_TOPOLOGY::STATUS::CYCLE_DETECTED ) );
364}
365
366
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1086
const FOOTPRINTS & Footprints() const
Definition board.h:420
const TRACKS & Tracks() const
Definition board.h:418
Build a topological view of a single named net chain's routed copper.
const std::vector< STUB > & Stubs() const
STATUS GetStatus() const
double TrunkLength() const
bool IsValid() const
Handle the data for a net.
Definition netinfo.h:46
Definition pad.h:61
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(TopologyTreeOnSimpleTrunk)
BOOST_AUTO_TEST_SUITE_END()
static const long long MM
BOOST_CHECK_EQUAL(result, "25.4")