KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_eagle_board_import.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
24
28
30
31#include <board.h>
32#include <footprint.h>
33#include <pcb_shape.h>
34#include <netinfo.h>
35#include <pcb_text.h>
36#include <pcb_track.h>
37
38#include <map>
39#include <vector>
40#include <wx/filename.h>
41
42
47
48
49BOOST_FIXTURE_TEST_SUITE( EagleBoardImport, EAGLE_BOARD_IMPORT_FIXTURE )
50
51
52
62BOOST_AUTO_TEST_CASE( ViaNetAssignment )
63{
64 std::string dataPath = KI_TEST::GetPcbnewTestDataDir()
65 + "plugins/eagle/Adafruit-AHT20-PCB/"
66 "Adafruit AHT20 Temperature & Humidity.brd";
67
68 BOOST_REQUIRE_MESSAGE( wxFileName::FileExists( dataPath ),
69 "Test board file not found: " + dataPath );
70
71 PCB_IO_EAGLE eaglePlugin;
72 std::unique_ptr<BOARD> board;
73
74 try
75 {
76 board = eaglePlugin.LoadBoard( dataPath );
77 }
78 catch( const IO_ERROR& e )
79 {
80 BOOST_FAIL( "IO_ERROR loading Eagle board: " + e.What().ToStdString() );
81 }
82 catch( const std::exception& e )
83 {
84 BOOST_FAIL( std::string( "Exception loading Eagle board: " ) + e.what() );
85 }
86
87 BOOST_REQUIRE( board );
88
89 // Collect vias grouped by net name
90 std::map<wxString, int> viasPerNet;
91
92 for( PCB_TRACK* track : board->Tracks() )
93 {
94 if( track->Type() == PCB_VIA_T )
95 {
96 NETINFO_ITEM* net = track->GetNet();
97 BOOST_REQUIRE( net );
98
99 wxString netName = net->GetNetname();
100 viasPerNet[netName]++;
101 }
102 }
103
104 // The Eagle board has vias on these nets (verified from the XML source)
105 BOOST_CHECK_GT( viasPerNet[wxT( "SDA" )], 0 );
106 BOOST_CHECK_GT( viasPerNet[wxT( "SCL" )], 0 );
107 BOOST_CHECK_GT( viasPerNet[wxT( "GND" )], 0 );
108 BOOST_CHECK_GT( viasPerNet[wxT( "VCC" )], 0 );
109 BOOST_CHECK_GT( viasPerNet[wxT( "VDD" )], 0 );
110
111 // Verify exact counts from the Eagle XML
112 BOOST_CHECK_EQUAL( viasPerNet[wxT( "SDA" )], 2 );
113 BOOST_CHECK_EQUAL( viasPerNet[wxT( "SCL" )], 2 );
114 BOOST_CHECK_EQUAL( viasPerNet[wxT( "GND" )], 3 );
115 BOOST_CHECK_EQUAL( viasPerNet[wxT( "VCC" )], 2 );
116 BOOST_CHECK_EQUAL( viasPerNet[wxT( "VDD" )], 2 );
117
118 // No via should be unassigned (net code 0)
119 BOOST_CHECK_EQUAL( viasPerNet[wxT( "" )], 0 );
120}
121
122
136BOOST_AUTO_TEST_CASE( TextJustification )
137{
138 std::string dataPath = KI_TEST::GetPcbnewTestDataDir()
139 + "plugins/eagle/test_eagle_23016/test_eagle.brd";
140
141 BOOST_REQUIRE_MESSAGE( wxFileName::FileExists( dataPath ),
142 "Test board file not found: " + dataPath );
143
144 PCB_IO_EAGLE eaglePlugin;
145 std::unique_ptr<BOARD> board;
146
147 try
148 {
149 board = eaglePlugin.LoadBoard( dataPath );
150 }
151 catch( const IO_ERROR& e )
152 {
153 BOOST_FAIL( "IO_ERROR loading Eagle board: " + e.What().ToStdString() );
154 }
155 catch( const std::exception& e )
156 {
157 BOOST_FAIL( std::string( "Exception loading Eagle board: " ) + e.what() );
158 }
159
160 BOOST_REQUIRE( board );
161
162 // Board should have 13 elements from the Eagle file
163 BOOST_CHECK_EQUAL( board->Footprints().size(), 13 );
164
165 // Find the I2C connector footprints and verify reference text justification.
166 // I2C0 and I2C1 are smashed but have no NAME attribute, so their Reference text
167 // goes through the non-smashed orientFPText() code path. With the footprint at R90
168 // and the package text at 0 degrees, the combined angle is 90 degrees,
169 // not triggering the justification flip to RIGHT/TOP.
170 for( FOOTPRINT* fp : board->Footprints() )
171 {
172 wxString ref = fp->GetReference();
173
174 if( ref == wxT( "I2C0" ) || ref == wxT( "I2C1" ) )
175 {
176 PCB_TEXT& refText = fp->Reference();
177
178 BOOST_CHECK_MESSAGE(
180 ref + " reference horizontal justification should be LEFT, got "
181 + std::to_string( refText.GetHorizJustify() ) );
182
183 BOOST_CHECK_MESSAGE(
185 ref + " reference vertical justification should be BOTTOM, got "
186 + std::to_string( refText.GetVertJustify() ) );
187 }
188 }
189
190 // Verify the plain text pin labels have correct justification.
191 // "G SDA SCL V+" on F.SilkS (Eagle layer 21) should have LEFT/BOTTOM justification
192 // with 90-degree rotation so the text reads bottom-to-top matching the pin order.
193 int topPinLabelCount = 0;
194
195 for( BOARD_ITEM* item : board->Drawings() )
196 {
197 if( item->Type() != PCB_TEXT_T )
198 continue;
199
200 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
201
202 if( text->GetText() != wxT( "G SDA SCL V+" ) )
203 continue;
204
205 if( text->GetLayer() != F_SilkS )
206 continue;
207
208 topPinLabelCount++;
209
210 BOOST_CHECK_MESSAGE(
211 text->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT,
212 "Pin label text horizontal justification should be LEFT" );
213
214 BOOST_CHECK_MESSAGE(
215 text->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM,
216 "Pin label text vertical justification should be BOTTOM" );
217
218 BOOST_CHECK_MESSAGE(
219 text->GetTextAngle() == EDA_ANGLE( 90, DEGREES_T ),
220 "Pin label text angle should be 90 degrees" );
221 }
222
223 // Two instances of "G SDA SCL V+" on F.SilkS (one per connector)
224 BOOST_CHECK_EQUAL( topPinLabelCount, 2 );
225}
226
227
239BOOST_AUTO_TEST_CASE( UserLayerMapping )
240{
241 std::string dataPath = KI_TEST::GetPcbnewTestDataDir()
242 + "plugins/eagle/lbr/user-layers-test.lbr";
243
244 BOOST_REQUIRE_MESSAGE( wxFileName::FileExists( dataPath ),
245 "Test library file not found: " + dataPath );
246
247 PCB_IO_EAGLE eaglePlugin;
248
249 std::unique_ptr<FOOTPRINT> fp;
250
251 try
252 {
253 fp = eaglePlugin.FootprintLoad( dataPath, wxT( "USER_LAYERS_TEST" ), false, nullptr );
254 }
255 catch( const IO_ERROR& e )
256 {
257 BOOST_FAIL( "IO_ERROR loading Eagle footprint: " + e.What().ToStdString() );
258 }
259 catch( const std::exception& e )
260 {
261 BOOST_FAIL( std::string( "Exception loading Eagle footprint: " ) + e.what() );
262 }
263
264 BOOST_REQUIRE( fp );
265
266 // Each wire in the test library is on a unique Eagle layer at a unique Y-offset (mm).
267 // Wires are 10mm long (x=-5 to x=5) and spaced 2mm apart starting at y=5mm.
268 struct LAYER_CHECK
269 {
270 int yOffsetNm; // Y-offset in nanometers
271 PCB_LAYER_ID expectedLayer;
272 const char* description;
273 };
274
275 // Y is negated because KiCad's Y axis is inverted relative to Eagle's
276 // clang-format off
277 std::vector<LAYER_CHECK> checks = {
278 { -5000000, Eco1_User, "Eagle 160 -> Eco1.User" },
279 { -7000000, Eco2_User, "Eagle 161 -> Eco2.User" },
280 { -9000000, Dwgs_User, "Eagle 162 -> Dwgs.User" },
281 { -11000000, Margin, "Eagle 163 -> Margin" },
282 { -13000000, User_1, "Eagle 170 -> User.1" },
283 { -15000000, User_2, "Eagle 171 -> User.2" },
284 { -17000000, User_3, "Eagle 172 -> User.3" },
285 { -19000000, User_4, "Eagle 173 -> User.4" },
286 { -21000000, User_5, "Eagle 174 -> User.5" },
287 { -23000000, User_6, "Eagle 175 -> User.6" },
288 { -25000000, User_7, "Eagle 176 -> User.7" },
289 { -27000000, User_8, "Eagle 177 -> User.8" },
290 { -29000000, User_9, "Eagle 178 -> User.9" },
291 };
292 // clang-format on
293
294 // Collect all segment shapes keyed by Y midpoint (exact nm).
295 // Test wires are horizontal and at integer mm offsets, so midpoints are exact.
296 std::map<int, PCB_LAYER_ID> linesByY;
297
298 for( BOARD_ITEM* item : fp->GraphicalItems() )
299 {
300 if( item->Type() != PCB_SHAPE_T )
301 continue;
302
303 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
304
305 if( shape->GetShape() != SHAPE_T::SEGMENT )
306 continue;
307
308 int yMid = ( shape->GetStart().y + shape->GetEnd().y ) / 2;
309
310 // A duplicate Y would let a stray segment mask a mis-imported wire.
311 BOOST_CHECK_MESSAGE( linesByY.emplace( yMid, shape->GetLayer() ).second,
312 "Duplicate segment at y=" + std::to_string( yMid ) + "nm" );
313 }
314
315 BOOST_CHECK_EQUAL( linesByY.size(), checks.size() );
316
317 for( const LAYER_CHECK& check : checks )
318 {
319 auto it = linesByY.find( check.yOffsetNm );
320
321 BOOST_CHECK_MESSAGE( it != linesByY.end(),
322 std::string( check.description )
323 + " - no line found at y=" + std::to_string( check.yOffsetNm )
324 + "nm" );
325
326 if( it != linesByY.end() )
327 {
328 BOOST_CHECK_MESSAGE( it->second == check.expectedLayer,
329 std::string( check.description )
330 + " - expected layer " + std::to_string( check.expectedLayer )
331 + " but got " + std::to_string( it->second ) );
332 }
333 }
334}
335
336
General utilities for PCB file IO for QA programs.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
SHAPE_T GetShape() const
Definition eda_shape.h:175
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:325
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:275
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:239
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const char * what() const override
std::exception interface, returned as UTF-8
Handle the data for a net.
Definition netinfo.h:50
const wxString & GetNetname() const
Definition netinfo.h:110
Works with Eagle 6.x XML board files and footprints to implement the Pcbnew #PLUGIN API or a portion ...
std::unique_ptr< FOOTPRINT > FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
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
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition pcb_shape.h:68
@ DEGREES_T
Definition eda_angle.h:31
@ SEGMENT
Definition eda_shape.h:56
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ User_8
Definition layer_ids.h:127
@ Dwgs_User
Definition layer_ids.h:103
@ User_6
Definition layer_ids.h:125
@ User_7
Definition layer_ids.h:126
@ User_5
Definition layer_ids.h:124
@ Eco1_User
Definition layer_ids.h:105
@ User_9
Definition layer_ids.h:128
@ Margin
Definition layer_ids.h:109
@ F_SilkS
Definition layer_ids.h:96
@ Eco2_User
Definition layer_ids.h:106
@ User_3
Definition layer_ids.h:122
@ User_1
Definition layer_ids.h:120
@ User_4
Definition layer_ids.h:123
@ User_2
Definition layer_ids.h:121
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(ViaNetAssignment)
Verify that vias imported from an Eagle board are assigned to the correct nets.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84