KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_barcode_load_save.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 <filesystem>
21
22#include <qa_utils/file_utils.h>
24#include <boost/test/unit_test.hpp>
25
26#include <board.h>
27#include <footprint.h>
28#include <pcb_text.h>
29#include <pcb_barcode.h>
34
35BOOST_AUTO_TEST_CASE( BarcodeWriteRead )
36{
37 SETTINGS_MANAGER settingsManager;
38
39 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
40
41 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
42 barcode->SetText( wxT( "12345" ) );
43 barcode->SetLayer( F_SilkS );
44 barcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 1.0 ), pcbIUScale.mmToIU( 2.0 ) ) );
45 barcode->SetWidth( pcbIUScale.mmToIU( 3.0 ) );
46 barcode->SetHeight( pcbIUScale.mmToIU( 3.0 ) );
47 barcode->SetTextSize( pcbIUScale.mmToIU( 1.5 ) );
48 barcode->SetKind( BARCODE_T::QR_CODE );
50 barcode->AssembleBarcode();
51
52 const KIID id = barcode->m_Uuid;
53
54 board->Add( barcode, ADD_MODE::APPEND, true );
55
56 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_qa_barcode_roundtrip" );
57 const std::filesystem::path savePath = tempDir.Path() / "barcode_roundtrip.kicad_pcb";
58
59 KI_TEST::DumpBoardToFile( *board, savePath.string() );
60 std::unique_ptr<BOARD> board2 = KI_TEST::ReadBoardFromFileOrStream( savePath.string() );
62 PCB_BARCODE& loaded = static_cast<PCB_BARCODE&>( item2 );
63
64 BOOST_CHECK( loaded.GetText() == barcode->GetText() );
65 BOOST_CHECK_EQUAL( (int) loaded.GetKind(), (int) barcode->GetKind() );
66 BOOST_CHECK_EQUAL( (int) loaded.GetErrorCorrection(), (int) barcode->GetErrorCorrection() );
67 BOOST_CHECK_EQUAL( loaded.GetWidth(), barcode->GetWidth() );
68 BOOST_CHECK_EQUAL( loaded.GetHeight(), barcode->GetHeight() );
69 BOOST_CHECK_EQUAL( loaded.GetTextSize(), barcode->GetTextSize() );
70 BOOST_CHECK_EQUAL( loaded.GetPolyShape().BBox().Centre(), barcode->GetPolyShape().BBox().Centre() );
71}
72
73
74BOOST_AUTO_TEST_CASE( BarcodeFootprintWriteRead )
75{
76 SETTINGS_MANAGER settingsManager;
77
78 FOOTPRINT footprint( nullptr );
79
80 PCB_BARCODE* barcode = new PCB_BARCODE( &footprint );
81 barcode->SetText( wxT( "12345" ) );
82 barcode->SetLayer( F_SilkS );
83 barcode->SetPosition( VECTOR2I( 1000000, 2000000 ) );
84 barcode->SetWidth( 3000000 );
85 barcode->SetHeight( 3000000 );
86 barcode->SetTextSize( pcbIUScale.mmToIU( 1.5 ) );
87 barcode->SetKind( BARCODE_T::QR_CODE );
89 barcode->AssembleBarcode();
90
91 const KIID id = barcode->m_Uuid;
92
93 footprint.Add( barcode, ADD_MODE::APPEND, true );
94
95 // Saving a footprint validates its whole containing directory as a library, so use a private
96 // temp directory rather than littering (and reading stray files from) the system temp root.
97 KI_TEST::SCOPED_TEMP_DIR tempLib( "kicad_qa_barcode_roundtrip" );
98 const std::filesystem::path libPath = tempLib.CreateChildDir( "barcode_roundtrip.pretty" );
99 const std::filesystem::path savePath = libPath / "barcode_roundtrip.kicad_mod";
100
101 footprint.SetFPID( LIB_ID( "", savePath.stem().string() ) );
102
103 KI_TEST::DumpFootprintToFile( footprint, savePath );
104 std::unique_ptr<FOOTPRINT> footprint2 = KI_TEST::ReadFootprintFromFile( savePath, savePath.stem().string() );
105
106 BOOST_REQUIRE( footprint2 );
107 PCB_BARCODE* loaded = nullptr;
108
109 for( BOARD_ITEM* item : footprint2->GraphicalItems() )
110 {
111 if( item->Type() == PCB_BARCODE_T && item->m_Uuid == id )
112 {
113 loaded = static_cast<PCB_BARCODE*>( item );
114 break;
115 }
116 }
117
118 BOOST_REQUIRE( loaded != nullptr );
119 BOOST_CHECK( loaded->GetText() == barcode->GetText() );
120 BOOST_CHECK_EQUAL( (int) loaded->GetKind(), (int) barcode->GetKind() );
121 BOOST_CHECK_EQUAL( (int) loaded->GetErrorCorrection(), (int) barcode->GetErrorCorrection() );
122 BOOST_CHECK_EQUAL( loaded->GetWidth(), barcode->GetWidth() );
123 BOOST_CHECK_EQUAL( loaded->GetHeight(), barcode->GetHeight() );
124 BOOST_CHECK_EQUAL( loaded->GetTextSize(), barcode->GetTextSize() );
125 BOOST_CHECK_EQUAL( loaded->GetPolyShape().BBox().Centre(), barcode->GetPolyShape().BBox().Centre() );
126}
127
128
129BOOST_AUTO_TEST_CASE( BarcodePositioningAlignment )
130{
131 SETTINGS_MANAGER settingsManager;
132
133 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
134
135 // Test multiple barcode types and positions to ensure consistent alignment
136 struct TestCase
137 {
138 BARCODE_T kind;
139 VECTOR2I position;
140 int width;
141 int height;
142 bool withText;
143 bool knockout;
144 double angle;
145 wxString text;
146 };
147
148 std::vector<TestCase> testCases = {
149 // Basic QR codes at different positions
150 { BARCODE_T::QR_CODE, VECTOR2I( 0, 0 ), 2000000, 2000000, false, false, 0.0, "TEST1" },
151 { BARCODE_T::QR_CODE, VECTOR2I( 5000000, 3000000 ), 3000000, 3000000, false, false, 0.0, "TEST2" },
152 { BARCODE_T::QR_CODE, VECTOR2I( -2000000, -1000000 ), 1500000, 1500000, false, false, 0.0, "TEST3" },
153
154 // With text
155 { BARCODE_T::QR_CODE, VECTOR2I( 1000000, 2000000 ), 2500000, 2500000, true, false, 0.0, "WITHTEXT" },
156
157 // With knockout
158 { BARCODE_T::QR_CODE, VECTOR2I( 2000000, 1000000 ), 2000000, 2000000, false, true, 0.0, "KNOCKOUT" },
159
160 // With rotation
161 { BARCODE_T::QR_CODE, VECTOR2I( 3000000, 2000000 ), 2000000, 2000000, false, false, 45.0, "ROTATED" },
162
163 // Different barcode types
164 { BARCODE_T::CODE_39, VECTOR2I( 4000000, 1000000 ), 3000000, 800000, false, false, 0.0, "CODE39TEST" },
165 { BARCODE_T::CODE_128, VECTOR2I( 1000000, 4000000 ), 3500000, 1000000, false, false, 0.0, "CODE128" },
166 { BARCODE_T::DATA_MATRIX, VECTOR2I( 3000000, 3000000 ), 1800000, 1800000, false, false, 0.0, "DATAMATRIX" },
167 { BARCODE_T::MICRO_QR_CODE, VECTOR2I( 2000000, 4000000 ), 1200000, 1200000, false, false, 0.0, "microQR" },
168
169 // Combined scenarios
170 { BARCODE_T::QR_CODE, VECTOR2I( 1500000, 1500000 ), 2200000, 2200000, true, true, 90.0, "COMPLEX" },
171 };
172
173 for( size_t i = 0; i < testCases.size(); ++i )
174 {
175 const auto& tc = testCases[i];
176
177 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
178 barcode->SetText( tc.text );
179 barcode->SetShowText( false );
180 barcode->SetLayer( F_SilkS );
181 barcode->SetWidth( tc.width );
182 barcode->SetHeight( tc.height );
183 barcode->SetKind( tc.kind );
185
186 barcode->AssembleBarcode();
187 SHAPE_POLY_SET canonicalPoly = barcode->GetPolyShape();
188
189 barcode->SetPosition( tc.position );
190
191 if( tc.angle != 0.0 )
192 barcode->Rotate( tc.position, EDA_ANGLE( tc.angle, DEGREES_T ) );
193
194 barcode->SetShowText( tc.withText );
195 barcode->SetIsKnockout( tc.knockout );
196
197 barcode->AssembleBarcode();
198 SHAPE_POLY_SET barcodePoly = barcode->GetPolyShape();
199
200 // Barcode poly should completely cover canonical poly
201 canonicalPoly.Rotate( barcode->GetAngle() );
202 canonicalPoly.Move( barcode->GetPosition() );
203
204 SHAPE_POLY_SET noHolesPoly;
205 barcodePoly.Unfracture();
206
207 for( int ii = 0; ii < barcodePoly.OutlineCount(); ++ii )
208 noHolesPoly.AddOutline( barcodePoly.Outline( ii ) );
209
210 // Handle rounding errors
211 if( tc.angle != 0.0 )
213
214 canonicalPoly.BooleanSubtract( noHolesPoly );
215 BOOST_CHECK_MESSAGE( canonicalPoly.IsEmpty(),
216 "Test case " << i << " (" << tc.text.ToStdString() << "): "
217 "barcode poly isn't aligned with canonical shape" );
218
219 board->Add( barcode, ADD_MODE::APPEND, true );
220 }
221}
222
223
224BOOST_AUTO_TEST_CASE( BarcodeTextVariableExpansion )
225{
226 SETTINGS_MANAGER settingsManager;
227
228 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
229
230 // Set up board-level text variables that should be expanded
231 std::map<wxString, wxString> properties;
232 properties[wxT( "PART_NUMBER" )] = wxT( "PN12345" );
233 properties[wxT( "VERSION" )] = wxT( "1.0" );
234 board->SetProperties( properties );
235
236 // Create a barcode with text variables in the content
237 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
238 barcode->SetText( wxT( "${PART_NUMBER}_${VERSION}" ) );
239 barcode->SetLayer( F_SilkS );
240 barcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 50.0 ), pcbIUScale.mmToIU( 50.0 ) ) );
241 barcode->SetWidth( pcbIUScale.mmToIU( 10.0 ) );
242 barcode->SetHeight( pcbIUScale.mmToIU( 10.0 ) );
243 barcode->SetKind( BARCODE_T::QR_CODE );
245
246 board->Add( barcode, ADD_MODE::APPEND, true );
247
248 // Verify GetText returns the raw text with variables
249 BOOST_CHECK_EQUAL( barcode->GetText(), wxT( "${PART_NUMBER}_${VERSION}" ) );
250
251 // Verify GetShownText returns the expanded text
252 BOOST_CHECK_EQUAL( barcode->GetShownText( FOR_CANVAS ), wxT( "PN12345_1.0" ) );
253
254 // Assemble the barcode and verify the QR code encodes the expanded text
255 barcode->AssembleBarcode();
256
257 // Create a reference barcode with the literal expanded text to compare polygon shapes
258 PCB_BARCODE* refBarcode = new PCB_BARCODE( board.get() );
259 refBarcode->SetText( wxT( "PN12345_1.0" ) );
260 refBarcode->SetLayer( F_SilkS );
261 refBarcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 100.0 ), pcbIUScale.mmToIU( 50.0 ) ) );
262 refBarcode->SetWidth( pcbIUScale.mmToIU( 10.0 ) );
263 refBarcode->SetHeight( pcbIUScale.mmToIU( 10.0 ) );
264 refBarcode->SetKind( BARCODE_T::QR_CODE );
266 refBarcode->AssembleBarcode();
267
268 board->Add( refBarcode, ADD_MODE::APPEND, true );
269
270 // The symbol polygons should have the same structure since they encode the same content.
271 // Compare the number of outlines as a proxy for encoding the same data.
273 refBarcode->GetSymbolPoly().OutlineCount() );
274
275 // The bounding boxes should be the same size since they encode the same content
276 BOX2I varBbox = barcode->GetSymbolPoly().BBox();
277 BOX2I refBbox = refBarcode->GetSymbolPoly().BBox();
278 BOOST_CHECK_EQUAL( varBbox.GetWidth(), refBbox.GetWidth() );
279 BOOST_CHECK_EQUAL( varBbox.GetHeight(), refBbox.GetHeight() );
280}
281
282
283BOOST_AUTO_TEST_CASE( BarcodeUndefinedVariable )
284{
285 SETTINGS_MANAGER settingsManager;
286
287 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
288
289 // Don't set any properties - variable won't be resolvable
290
291 // Create a barcode with an undefined variable
292 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
293 barcode->SetText( wxT( "${UNDEFINED_VAR}" ) );
294 barcode->SetLayer( F_SilkS );
295 barcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 50.0 ), pcbIUScale.mmToIU( 50.0 ) ) );
296 barcode->SetWidth( pcbIUScale.mmToIU( 10.0 ) );
297 barcode->SetHeight( pcbIUScale.mmToIU( 10.0 ) );
298 barcode->SetKind( BARCODE_T::QR_CODE );
300
301 board->Add( barcode, ADD_MODE::APPEND, true );
302
303 // Verify GetText returns the raw text with the variable reference
304 BOOST_CHECK_EQUAL( barcode->GetText(), wxT( "${UNDEFINED_VAR}" ) );
305
306 // Verify GetShownText returns the unexpanded text (since variable is undefined)
307 BOOST_CHECK_EQUAL( barcode->GetShownText( FOR_CANVAS ), wxT( "${UNDEFINED_VAR}" ) );
308
309 // Assemble the barcode - for QR codes this should still work since QR can encode any text
310 barcode->AssembleBarcode();
311
312 // QR codes should still generate a polygon even with ${} in the text
313 BOOST_CHECK( barcode->GetSymbolPoly().OutlineCount() > 0 );
314}
315
316
317BOOST_AUTO_TEST_CASE( BarcodeCode39UndefinedVariable )
318{
319 SETTINGS_MANAGER settingsManager;
320
321 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
322
323 // Don't set any properties - variable won't be resolvable
324
325 // Create a CODE_39 barcode with an undefined variable
326 // CODE_39 cannot encode $ { } characters, so this should fail to generate
327 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
328 barcode->SetText( wxT( "${UNDEFINED_VAR}" ) );
329 barcode->SetLayer( F_SilkS );
330 barcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 50.0 ), pcbIUScale.mmToIU( 50.0 ) ) );
331 barcode->SetWidth( pcbIUScale.mmToIU( 20.0 ) );
332 barcode->SetHeight( pcbIUScale.mmToIU( 5.0 ) );
333 barcode->SetKind( BARCODE_T::CODE_39 );
334
335 board->Add( barcode, ADD_MODE::APPEND, true );
336
337 // Assemble the barcode
338 barcode->AssembleBarcode();
339
340 // CODE_39 cannot encode ${} so the polygon should be empty
341 // This is expected behavior - invalid characters cause encoding to fail
343}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
constexpr int ARC_LOW_DEF
Definition base_units.h:143
General utilities for PCB file IO for QA programs.
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr Vec Centre() const
Definition box2.h:100
constexpr size_type GetHeight() const
Definition box2.h:218
const KIID m_Uuid
Definition eda_item.h:599
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:418
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Adds an item to the container.
Definition kiid.h:46
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:61
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
void SetKind(BARCODE_T aKind)
void SetTextSize(int aTextSize)
Change the height of the human-readable text displayed below the barcode.
void SetErrorCorrection(BARCODE_ECC_T aErrorCorrection)
Set the error correction level used for QR codes.
wxString GetShownText(RESOLUTION_CONTEXT aContext) const
void SetShowText(bool aShow)
void SetWidth(int aWidth)
void AssembleBarcode() const
Assemble the barcode polygon and text polygons into a single polygonal representation.
void SetHeight(int aHeight)
VECTOR2I GetPosition() const override
Get the position (center) of the barcode in internal units.
wxString GetText() const
void SetPosition(const VECTOR2I &aPos) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the drawing layer for the barcode and its text.
int GetTextSize() const
int GetHeight() const
Get the barcode height (in internal units).
void SetIsKnockout(bool aEnable) override
const SHAPE_POLY_SET & GetPolyShape() const
Access the underlying polygonal representation generated for the barcode.
const SHAPE_POLY_SET & GetSymbolPoly() const
Access the cached polygon for the barcode symbol only (no text, no margins/knockout).
BARCODE_ECC_T GetErrorCorrection() const
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate the barcode around a given centre by the given angle.
EDA_ANGLE GetAngle() const
BARCODE_T GetKind() const
Returns the type of the barcode (QR, CODE_39, etc.).
int GetWidth() const
Get the barcode width (in internal units).
void SetText(const wxString &aText)
Set the barcode content text to encode.
Represent a set of closed polygons.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
void Unfracture()
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
void Move(const VECTOR2I &aVector) override
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
@ FOR_CANVAS
Definition common.h:92
@ ROUND_ALL_CORNERS
All angles are rounded.
@ DEGREES_T
Definition eda_angle.h:30
@ F_SilkS
Definition layer_ids.h:96
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::filesystem::path &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
std::unique_ptr< FOOTPRINT > ReadFootprintFromFile(const std::filesystem::path &aFilename, const wxString &aFootprintName)
Read a footprint object aFootprintName from aFileName.
void DumpBoardToFile(BOARD &board, const std::filesystem::path &aFilename)
Utility function to simply write a Board out to a file.
BOARD_ITEM & RequireBoardItemWithTypeAndId(const BOARD &aBoard, KICAD_T aItemType, const KIID &aID)
Get an item from the given board with a certain type and UUID.
BARCODE class definition.
BARCODE_T
Definition pcb_barcode.h:40
BOOST_AUTO_TEST_CASE(BarcodeWriteRead)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708