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
23
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
39constexpr int MM = 1000000;
40
41
42std::unique_ptr<BOARD> loadBoard( const char* aBoardFile )
43{
44 PCB_IO_KICAD_SEXPR plugin;
45 std::unique_ptr<BOARD> board = plugin.LoadBoard( KI_TEST::GetPcbnewTestDataDir() + aBoardFile );
46 board->BuildConnectivity();
47
48 return board;
49}
50
51
52std::set<BOARD_CONNECTED_ITEM*> chainItems( BOARD* aBoard, const wxString& aChain )
53{
54 std::set<BOARD_CONNECTED_ITEM*> items;
55
56 for( PCB_TRACK* t : aBoard->Tracks() )
57 {
58 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
59 items.insert( t );
60 }
61
62 for( FOOTPRINT* fp : aBoard->Footprints() )
63 {
64 for( PAD* p : fp->Pads() )
65 {
66 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
67 items.insert( p );
68 }
69 }
70
71 return items;
72}
73
74
75// Tag every net whose name starts with "/NET_" into the named chain, and set
76// terminal pads to the first/last footprint's first pad on that chain. fp1
77// pad-1 is terminal[0], lastFp pad-1 is terminal[1].
78void tagChain( BOARD* aBoard, const wxString& aChain, const wxString& aFirstFpRef,
79 const wxString& aLastFpRef )
80{
81 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
82 {
83 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
84 n->SetNetChain( aChain );
85 }
86
87 PAD* termA = nullptr;
88 PAD* termB = nullptr;
89
90 for( FOOTPRINT* fp : aBoard->Footprints() )
91 {
92 if( fp->GetReference() == aFirstFpRef && !fp->Pads().empty() )
93 termA = fp->Pads().front();
94
95 if( fp->GetReference() == aLastFpRef && !fp->Pads().empty() )
96 termB = fp->Pads().back();
97 }
98
99 if( termA )
100 {
101 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
102 if( n && n->GetNetChain() == aChain )
103 n->SetTerminalPad( 0, termA );
104 }
105
106 if( termB )
107 {
108 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
109 if( n && n->GetNetChain() == aChain )
110 n->SetTerminalPad( 1, termB );
111 }
112}
113
114
115} // namespace
116
117
118BOOST_AUTO_TEST_SUITE( DRCChainTopology )
119
120
121// Three-net trunk through two passives.
122// pad@(0,0) — track 30mm — bridge 5mm — track 30mm — bridge 5mm — track 30mm — pad@(100,0).
123// Trunk = 100 mm, zero stubs.
124BOOST_AUTO_TEST_CASE( TopologyTreeOnSimpleTrunk )
125{
126 auto board = loadBoard( "net_chains/chain_topology_trunk.kicad_pcb" );
127 tagChain( board.get(), wxS( "SIG" ), wxS( "FP_START" ), wxS( "FP_END" ) );
128
129 auto items = chainItems( board.get(), wxS( "SIG" ) );
130 CHAIN_TOPOLOGY topo( board.get(), wxS( "SIG" ), items );
131
132 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
133 static_cast<int>( CHAIN_TOPOLOGY::STATUS::OK ) );
134 BOOST_CHECK( topo.IsValid() );
135 BOOST_CHECK_CLOSE( topo.TrunkLength(), 100.0 * MM, 5.0 );
136 BOOST_CHECK( topo.Stubs().empty() );
137}
138
139
140// Single-net trunk plus a perpendicular T-stub of 5 mm at the midpoint.
141BOOST_AUTO_TEST_CASE( TopologyDetectsTStub )
142{
143 auto board = loadBoard( "net_chains/chain_topology_t_stub.kicad_pcb" );
144 tagChain( board.get(), wxS( "TSIG" ), wxS( "FP_START" ), wxS( "FP_END" ) );
145
146 auto items = chainItems( board.get(), wxS( "TSIG" ) );
147 CHAIN_TOPOLOGY topo( board.get(), wxS( "TSIG" ), items );
148
149 BOOST_CHECK( topo.IsValid() );
150 BOOST_REQUIRE_EQUAL( topo.Stubs().size(), 1u );
151
152 // Fixture trunk runs (32,49) to (82,49); the stub taps it at the midpoint
153 const CHAIN_TOPOLOGY::STUB& stub = topo.Stubs().front();
154 BOOST_CHECK_LE( std::abs( stub.branchPoint.x - 57 * MM ), 100 );
155 BOOST_CHECK_LE( std::abs( stub.branchPoint.y - 49 * MM ), 100 );
156 BOOST_CHECK_CLOSE( stub.length, 5.0 * MM, 5.0 );
157
158 // Trunk length is 50 mm and the stub does not contribute.
159 BOOST_CHECK_CLOSE( topo.TrunkLength(), 50.0 * MM, 5.0 );
160}
161
162
163// Only one terminal pad set: NO_TERMINAL_PADS.
164BOOST_AUTO_TEST_CASE( TopologyMissingTerminalPad )
165{
166 auto board = loadBoard( "net_chains/chain_topology_missing_terminal.kicad_pcb" );
167 // Only set chain — no second terminal-pad anchor footprint.
168 for( NETINFO_ITEM* n : board->GetNetInfo() )
169 {
170 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
171 n->SetNetChain( wxS( "MISSING" ) );
172 }
173
174 PAD* termA = board->Footprints().empty()
175 ? nullptr
176 : board->Footprints().front()->Pads().empty()
177 ? nullptr
178 : board->Footprints().front()->Pads().front();
179
180 if( termA )
181 {
182 for( NETINFO_ITEM* n : board->GetNetInfo() )
183 if( n && n->GetNetChain() == wxS( "MISSING" ) )
184 n->SetTerminalPad( 0, termA );
185 }
186
187 auto items = chainItems( board.get(), wxS( "MISSING" ) );
188 CHAIN_TOPOLOGY topo( board.get(), wxS( "MISSING" ), items );
189
190 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
191 static_cast<int>( CHAIN_TOPOLOGY::STATUS::NO_TERMINAL_PADS ) );
192}
193
194
195// Both terminals set, no track between them: DISCONNECTED.
196BOOST_AUTO_TEST_CASE( TopologyDisconnected )
197{
198 auto board = loadBoard( "net_chains/chain_topology_disconnected.kicad_pcb" );
199 tagChain( board.get(), wxS( "DISC" ), wxS( "FP_START" ), wxS( "FP_END" ) );
200
201 auto items = chainItems( board.get(), wxS( "DISC" ) );
202 CHAIN_TOPOLOGY topo( board.get(), wxS( "DISC" ), items );
203
204 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
205 static_cast<int>( CHAIN_TOPOLOGY::STATUS::DISCONNECTED ) );
206}
207
208
209// Two parallel paths between terminals → CYCLE_DETECTED.
210BOOST_AUTO_TEST_CASE( TopologyCycleDetected )
211{
212 auto board = loadBoard( "net_chains/chain_topology_cycle.kicad_pcb" );
213 tagChain( board.get(), wxS( "LOOP" ), wxS( "FP_START" ), wxS( "FP_END" ) );
214
215 auto items = chainItems( board.get(), wxS( "LOOP" ) );
216 CHAIN_TOPOLOGY topo( board.get(), wxS( "LOOP" ), items );
217
218 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
219 static_cast<int>( CHAIN_TOPOLOGY::STATUS::CYCLE_DETECTED ) );
220}
221
222
General utilities for PCB file IO for QA programs.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1207
const FOOTPRINTS & Footprints() const
Definition board.h:463
const TRACKS & Tracks() const
Definition board.h:461
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:50
Definition pad.h:61
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
std::unique_ptr< BOARD > LoadBoard(const wxString &aFileName, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr)
Load information from some input file format that this PCB_IO implementation knows about into new BOA...
Definition pcb_io.cpp:72
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:411
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")