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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <filesystem>
25
27#include <boost/test/unit_test.hpp>
28
29#include <board.h>
30#include <footprint.h>
31#include <pcb_text.h>
32#include <pcb_barcode.h>
37
38BOOST_AUTO_TEST_CASE( BarcodeWriteRead )
39{
40 SETTINGS_MANAGER settingsManager;
41
42 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
43
44 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
45 barcode->SetText( wxT( "12345" ) );
46 barcode->SetLayer( F_SilkS );
47 barcode->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 1.0 ), pcbIUScale.mmToIU( 2.0 ) ) );
48 barcode->SetWidth( pcbIUScale.mmToIU( 3.0 ) );
49 barcode->SetHeight( pcbIUScale.mmToIU( 3.0 ) );
50 barcode->SetTextSize( pcbIUScale.mmToIU( 1.5 ) );
51 barcode->SetKind( BARCODE_T::QR_CODE );
53 barcode->AssembleBarcode();
54
55 const KIID id = barcode->m_Uuid;
56
57 board->Add( barcode, ADD_MODE::APPEND, true );
58
59 const std::filesystem::path savePath = std::filesystem::temp_directory_path() / "barcode_roundtrip.kicad_pcb";
60
61 std::filesystem::remove( savePath );
62 KI_TEST::DumpBoardToFile( *board, savePath.string() );
63 std::unique_ptr<BOARD> board2 = KI_TEST::ReadBoardFromFileOrStream( savePath.string() );
65 PCB_BARCODE& loaded = static_cast<PCB_BARCODE&>( item2 );
66
67 BOOST_CHECK( loaded.GetText() == barcode->GetText() );
68 BOOST_CHECK_EQUAL( (int) loaded.GetKind(), (int) barcode->GetKind() );
69 BOOST_CHECK_EQUAL( (int) loaded.GetErrorCorrection(), (int) barcode->GetErrorCorrection() );
70 BOOST_CHECK_EQUAL( loaded.GetWidth(), barcode->GetWidth() );
71 BOOST_CHECK_EQUAL( loaded.GetHeight(), barcode->GetHeight() );
72 BOOST_CHECK_EQUAL( loaded.GetTextSize(), barcode->GetTextSize() );
73 BOOST_CHECK_EQUAL( loaded.GetPolyShape().BBox().Centre(), barcode->GetPolyShape().BBox().Centre() );
74}
75
76
77BOOST_AUTO_TEST_CASE( BarcodeFootprintWriteRead )
78{
79 SETTINGS_MANAGER settingsManager;
80
81 FOOTPRINT footprint( nullptr );
82
83 PCB_BARCODE* barcode = new PCB_BARCODE( &footprint );
84 barcode->SetText( wxT( "12345" ) );
85 barcode->SetLayer( F_SilkS );
86 barcode->SetPosition( VECTOR2I( 1000000, 2000000 ) );
87 barcode->SetWidth( 3000000 );
88 barcode->SetHeight( 3000000 );
89 barcode->SetTextSize( pcbIUScale.mmToIU( 1.5 ) );
90 barcode->SetKind( BARCODE_T::QR_CODE );
92 barcode->AssembleBarcode();
93
94 const KIID id = barcode->m_Uuid;
95
96 footprint.Add( barcode, ADD_MODE::APPEND, true );
97
98 const std::filesystem::path savePath = std::filesystem::temp_directory_path() / "barcode_roundtrip.kicad_mod";
99
100 std::filesystem::remove( savePath );
101 KI_TEST::DumpFootprintToFile( footprint, savePath.string() );
102 std::unique_ptr<FOOTPRINT> footprint2 = KI_TEST::ReadFootprintFromFileOrStream( savePath.string() );
103
104 PCB_BARCODE* loaded = nullptr;
105
106 for( BOARD_ITEM* item : footprint2->GraphicalItems() )
107 {
108 if( item->Type() == PCB_BARCODE_T && item->m_Uuid == id )
109 {
110 loaded = static_cast<PCB_BARCODE*>( item );
111 break;
112 }
113 }
114
115 BOOST_REQUIRE( loaded != nullptr );
116 BOOST_CHECK( loaded->GetText() == barcode->GetText() );
117 BOOST_CHECK_EQUAL( (int) loaded->GetKind(), (int) barcode->GetKind() );
118 BOOST_CHECK_EQUAL( (int) loaded->GetErrorCorrection(), (int) barcode->GetErrorCorrection() );
119 BOOST_CHECK_EQUAL( loaded->GetWidth(), barcode->GetWidth() );
120 BOOST_CHECK_EQUAL( loaded->GetHeight(), barcode->GetHeight() );
121 BOOST_CHECK_EQUAL( loaded->GetTextSize(), barcode->GetTextSize() );
122 BOOST_CHECK_EQUAL( loaded->GetPolyShape().BBox().Centre(), barcode->GetPolyShape().BBox().Centre() );
123}
124
125
126BOOST_AUTO_TEST_CASE( BarcodePositioningAlignment )
127{
128 SETTINGS_MANAGER settingsManager;
129
130 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
131
132 // Test multiple barcode types and positions to ensure consistent alignment
133 struct TestCase
134 {
135 BARCODE_T kind;
136 VECTOR2I position;
137 int width;
138 int height;
139 bool withText;
140 bool knockout;
141 double angle;
142 wxString text;
143 };
144
145 std::vector<TestCase> testCases = {
146 // Basic QR codes at different positions
147 { BARCODE_T::QR_CODE, VECTOR2I( 0, 0 ), 2000000, 2000000, false, false, 0.0, "TEST1" },
148 { BARCODE_T::QR_CODE, VECTOR2I( 5000000, 3000000 ), 3000000, 3000000, false, false, 0.0, "TEST2" },
149 { BARCODE_T::QR_CODE, VECTOR2I( -2000000, -1000000 ), 1500000, 1500000, false, false, 0.0, "TEST3" },
150
151 // With text
152 { BARCODE_T::QR_CODE, VECTOR2I( 1000000, 2000000 ), 2500000, 2500000, true, false, 0.0, "WITHTEXT" },
153
154 // With knockout
155 { BARCODE_T::QR_CODE, VECTOR2I( 2000000, 1000000 ), 2000000, 2000000, false, true, 0.0, "KNOCKOUT" },
156
157 // With rotation
158 { BARCODE_T::QR_CODE, VECTOR2I( 3000000, 2000000 ), 2000000, 2000000, false, false, 45.0, "ROTATED" },
159
160 // Different barcode types
161 { BARCODE_T::CODE_39, VECTOR2I( 4000000, 1000000 ), 3000000, 800000, false, false, 0.0, "CODE39TEST" },
162 { BARCODE_T::CODE_128, VECTOR2I( 1000000, 4000000 ), 3500000, 1000000, false, false, 0.0, "CODE128" },
163 { BARCODE_T::DATA_MATRIX, VECTOR2I( 3000000, 3000000 ), 1800000, 1800000, false, false, 0.0, "DATAMATRIX" },
164 { BARCODE_T::MICRO_QR_CODE, VECTOR2I( 2000000, 4000000 ), 1200000, 1200000, false, false, 0.0, "microQR" },
165
166 // Combined scenarios
167 { BARCODE_T::QR_CODE, VECTOR2I( 1500000, 1500000 ), 2200000, 2200000, true, true, 90.0, "COMPLEX" },
168 };
169
170 for( size_t i = 0; i < testCases.size(); ++i )
171 {
172 const auto& tc = testCases[i];
173
174 PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
175 barcode->SetText( tc.text );
176 barcode->Text().SetVisible( false );
177 barcode->SetLayer( F_SilkS );
178 barcode->SetWidth( tc.width );
179 barcode->SetHeight( tc.height );
180 barcode->SetKind( tc.kind );
182
183 barcode->AssembleBarcode();
184 SHAPE_POLY_SET canonicalPoly = barcode->GetPolyShape();
185
186 barcode->SetPosition( tc.position );
187
188 if( tc.angle != 0.0 )
189 barcode->Rotate( tc.position, EDA_ANGLE( tc.angle, DEGREES_T ) );
190
191 barcode->Text().SetVisible( tc.withText );
192 barcode->SetIsKnockout( tc.knockout );
193
194 barcode->AssembleBarcode();
195 SHAPE_POLY_SET barcodePoly = barcode->GetPolyShape();
196
197 // Barcode poly should completely cover canonical poly
198 canonicalPoly.Rotate( barcode->GetAngle() );
199 canonicalPoly.Move( barcode->GetPosition() );
200
201 SHAPE_POLY_SET noHolesPoly;
202 barcodePoly.Unfracture();
203
204 for( int ii = 0; ii < barcodePoly.OutlineCount(); ++ii )
205 noHolesPoly.AddOutline( barcodePoly.Outline( ii ) );
206
207 // Handle rounding errors
208 if( tc.angle != 0.0 )
210
211 canonicalPoly.BooleanSubtract( noHolesPoly );
212 BOOST_CHECK_MESSAGE( canonicalPoly.IsEmpty(),
213 "Test case " << i << " (" << tc.text.ToStdString() << "): "
214 "barcode poly isn't aligned with canonical shape" );
215
216 board->Add( barcode, ADD_MODE::APPEND, true );
217 }
218}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
constexpr int ARC_LOW_DEF
Definition base_units.h:128
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:83
constexpr Vec Centre() const
Definition box2.h:97
const KIID m_Uuid
Definition eda_item.h:522
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:395
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition kiid.h:49
PCB_TEXT & Text()
Access the internal PCB_TEXT object used for showing the human-readable text.
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.
void SetWidth(int aWidth)
void SetHeight(int aHeight)
void AssembleBarcode()
Assemble the barcode polygon and text polygons into a single polygonal representation.
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.
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.
@ ROUND_ALL_CORNERS
All angles are rounded.
@ DEGREES_T
Definition eda_angle.h:31
@ F_SilkS
Definition layer_ids.h:100
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 > ReadFootprintFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::string &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
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:43
BOOST_AUTO_TEST_CASE(BarcodeWriteRead)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695