21#include <boost/test/unit_test.hpp>
41constexpr const char* PCB_HEADER = R
"(
45 (generator_version "9.99")
53constexpr int MM = 1000000;
56std::unique_ptr<BOARD> loadFromString(
const std::string& aPcbText,
57 const std::string& aSubdir )
59 namespace fs = std::filesystem;
60 fs::path tmpDir = fs::temp_directory_path() / aSubdir;
61 fs::create_directories( tmpDir );
62 fs::path pcbPath = tmpDir /
"topo.kicad_pcb";
65 std::ofstream out( pcbPath );
70 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
71 plugin.
LoadBoard( pcbPath.string(), board.get() );
72 board->BuildConnectivity();
74 fs::remove( pcbPath );
79std::set<BOARD_CONNECTED_ITEM*> chainItems(
BOARD* aBoard,
const wxString& aChain )
81 std::set<BOARD_CONNECTED_ITEM*> items;
85 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
91 for(
PAD* p : fp->Pads() )
93 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
105void tagChain(
BOARD* aBoard,
const wxString& aChain,
const wxString& aFirstFpRef,
106 const wxString& aLastFpRef )
110 if( n && n->GetNetname().StartsWith( wxS(
"/NET_" ) ) )
111 n->SetNetChain( aChain );
114 PAD* termA =
nullptr;
115 PAD* termB =
nullptr;
119 if( fp->GetReference() == aFirstFpRef && !fp->Pads().empty() )
120 termA = fp->Pads().front();
122 if( fp->GetReference() == aLastFpRef && !fp->Pads().empty() )
123 termB = fp->Pads().back();
129 if( n && n->GetNetChain() == aChain )
130 n->SetTerminalPad( 0, termA );
136 if( n && n->GetNetChain() == aChain )
137 n->SetTerminalPad( 1, termB );
144std::string passive(
const wxString& aRef,
double aXmm,
double aYmm,
146 int aNetA,
const wxString& aNetAName,
147 int aNetB,
const wxString& aNetBName )
149 return wxString::Format(
151 (footprint "Passive" (layer "F.Cu") (uuid "00000000-0000-0000-0000-000000%06d")
153 (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))))
154 (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"))
155 (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"))
157)", static_cast<int>( aXmm ), aXmm, aYmm, aRef,
static_cast<int>( aXmm ),
158 -aPadSpanMm / 2, aNetA, aNetAName,
static_cast<int>( aXmm ),
159 aPadSpanMm / 2, aNetB, aNetBName,
static_cast<int>( aXmm ) ).ToStdString();
163std::string segment(
double aX1mm,
double aY1mm,
double aX2mm,
double aY2mm,
int aNet )
165 return wxString::Format(
166 " (segment (start %f %f) (end %f %f) (width 0.2) (layer \"F.Cu\") (net %d))\n",
167 aX1mm, aY1mm, aX2mm, aY2mm, aNet ).ToStdString();
181 std::string pcb = std::string( PCB_HEADER ) +
187 (gr_line (start -5 -5) (end 110 -5) (layer "Edge.Cuts") (width 0.05))
188 (gr_line (start 110 -5) (end 110 5) (layer "Edge.Cuts") (width 0.05))
189 (gr_line (start 110 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
190 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
192 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
193 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
194 passive( wxS(
"R1" ), 32.5, 0.0, 5.0,
195 1, wxS(
"/NET_A" ), 2, wxS(
"/NET_B" ) ) +
196 passive( wxS(
"R2" ), 67.5, 0.0, 5.0,
197 2, wxS(
"/NET_B" ), 3, wxS(
"/NET_C" ) ) +
198 passive( wxS(
"FP_END" ), 100.0, 0.0, 0.0,
199 3, wxS(
"/NET_C" ), 3, wxS(
"/NET_C" ) ) +
200 segment( 0.0, 0.0, 30.0, 0.0, 1 ) +
201 segment( 35.0, 0.0, 65.0, 0.0, 2 ) +
202 segment( 70.0, 0.0, 100.0, 0.0, 3 ) +
205 auto board = loadFromString( pcb,
"kicad_drc_topo_simple_trunk" );
206 tagChain( board.get(), wxS(
"SIG" ), wxS(
"FP_START" ), wxS(
"FP_END" ) );
208 auto items = chainItems( board.get(), wxS(
"SIG" ) );
215 BOOST_CHECK( topo.
Stubs().empty() );
222 std::string pcb = std::string( PCB_HEADER ) +
226 (gr_line (start -5 -10) (end 60 -10) (layer "Edge.Cuts") (width 0.05))
227 (gr_line (start 60 -10) (end 60 15) (layer "Edge.Cuts") (width 0.05))
228 (gr_line (start 60 15) (end -5 15) (layer "Edge.Cuts") (width 0.05))
229 (gr_line (start -5 15) (end -5 -10) (layer "Edge.Cuts") (width 0.05))
231 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
232 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
233 passive( wxS(
"FP_END" ), 50.0, 0.0, 0.0,
234 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
236 segment( 0.0, 0.0, 50.0, 0.0, 1 ) +
237 segment( 25.0, 0.0, 25.0, 5.0, 1 ) +
240 auto board = loadFromString( pcb,
"kicad_drc_topo_t_stub" );
241 tagChain( board.get(), wxS(
"TSIG" ), wxS(
"FP_START" ), wxS(
"FP_END" ) );
243 auto items = chainItems( board.get(), wxS(
"TSIG" ) );
247 BOOST_REQUIRE_EQUAL( topo.
Stubs().size(), 1u );
252 BOOST_CHECK_CLOSE( stub.
length, 5.0 *
MM, 5.0 );
262 std::string pcb = std::string( PCB_HEADER ) +
266 (gr_line (start -5 -5) (end 40 -5) (layer "Edge.Cuts") (width 0.05))
267 (gr_line (start 40 -5) (end 40 5) (layer "Edge.Cuts") (width 0.05))
268 (gr_line (start 40 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
269 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
271 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
272 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
273 segment( 0.0, 0.0, 30.0, 0.0, 1 ) +
276 auto board = loadFromString( pcb,
"kicad_drc_topo_missing_term" );
280 if( n && n->GetNetname().StartsWith( wxS(
"/NET_" ) ) )
281 n->SetNetChain( wxS(
"MISSING" ) );
284 PAD* termA = board->Footprints().empty()
286 : board->Footprints().front()->Pads().empty()
288 : board->Footprints().front()->Pads().front();
293 if( n && n->GetNetChain() == wxS(
"MISSING" ) )
294 n->SetTerminalPad( 0, termA );
297 auto items = chainItems( board.get(), wxS(
"MISSING" ) );
308 std::string pcb = std::string( PCB_HEADER ) +
312 (gr_line (start -5 -5) (end 110 -5) (layer "Edge.Cuts") (width 0.05))
313 (gr_line (start 110 -5) (end 110 5) (layer "Edge.Cuts") (width 0.05))
314 (gr_line (start 110 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
315 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
317 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
318 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
319 passive( wxS(
"FP_END" ), 100.0, 0.0, 0.0,
320 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
323 auto board = loadFromString( pcb,
"kicad_drc_topo_disconnected" );
324 tagChain( board.get(), wxS(
"DISC" ), wxS(
"FP_START" ), wxS(
"FP_END" ) );
326 auto items = chainItems( board.get(), wxS(
"DISC" ) );
337 std::string pcb = std::string( PCB_HEADER ) +
341 (gr_line (start -5 -10) (end 60 -10) (layer "Edge.Cuts") (width 0.05))
342 (gr_line (start 60 -10) (end 60 10) (layer "Edge.Cuts") (width 0.05))
343 (gr_line (start 60 10) (end -5 10) (layer "Edge.Cuts") (width 0.05))
344 (gr_line (start -5 10) (end -5 -10) (layer "Edge.Cuts") (width 0.05))
346 passive( wxS( "FP_START" ), 0.0, 0.0, 0.0,
347 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
348 passive( wxS(
"FP_END" ), 50.0, 0.0, 0.0,
349 1, wxS(
"/NET_A" ), 1, wxS(
"/NET_A" ) ) +
351 segment( 0.0, 0.0, 25.0, 5.0, 1 ) +
352 segment( 25.0, 5.0, 50.0, 0.0, 1 ) +
353 segment( 0.0, 0.0, 25.0, -5.0, 1 ) +
354 segment( 25.0, -5.0, 50.0, 0.0, 1 ) +
357 auto board = loadFromString( pcb,
"kicad_drc_topo_cycle" );
358 tagChain( board.get(), wxS(
"LOOP" ), wxS(
"FP_START" ), wxS(
"FP_END" ) );
360 auto items = chainItems( board.get(), wxS(
"LOOP" ) );
Information pertinent to a Pcbnew printed circuit board.
const NETINFO_LIST & GetNetInfo() const
const FOOTPRINTS & Footprints() const
const TRACKS & Tracks() const
Build a topological view of a single named net chain's routed copper.
const std::vector< STUB > & Stubs() const
double TrunkLength() const
Handle the data for a net.
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)
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")