KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_footprint_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
21#include <boost/test/data/test_case.hpp>
22
23#include <qa_utils/file_utils.h>
24
25#include <board.h>
26#include <kiid.h>
27#include <footprint.h>
29#include <pcb_field.h>
30#include <pcb_text.h>
34
35#include <filesystem>
36
37
38namespace
39{
40
41struct FOOTPRINT_LOAD_TEST_CASE
42{
43 // Which footprint to look at in the file
44 KIID m_footprintUuid;
45 // Expected values
46 bool m_expectedLocked;
47 VECTOR2I m_expectedPos;
48};
49
50
51struct FOOTPRINT_LOAD_BOARD_TEST_CASE: public KI_TEST::BOARD_LOAD_TEST_CASE
52{
53 // List of images to check
54 std::vector<FOOTPRINT_LOAD_TEST_CASE> m_fpCases;
55};
56
57
58const std::vector<FOOTPRINT_LOAD_BOARD_TEST_CASE> FootprintLoadSave_testCases{
59 {
60 "footprints_load_save",
61 std::nullopt,
62 {
63 // From top to bottom in the board file
64 {
65 "898cf321-03c7-40bb-8d78-4bc5e52986c2",
66 false,
67 { 100, 80 },
68 },
69 {
70 "0775cd70-2e84-4592-a160-456c37a8f4f6",
71 true,
72 { 100, 100 },
73 },
74 },
75 },
76};
77
78} // namespace
79
85BOOST_DATA_TEST_CASE( FootprintLoadSave,
86 boost::unit_test::data::make( FootprintLoadSave_testCases ), testCase )
87{
88 const auto doBoardTest = [&]( const BOARD& aBoard )
89 {
90 for( const FOOTPRINT_LOAD_TEST_CASE& fooprintTestCase : testCase.m_fpCases )
91 {
92 BOOST_TEST_MESSAGE( "Checking for footprint with UUID: "
93 << fooprintTestCase.m_footprintUuid.AsString() );
94
95 const auto& fp = static_cast<FOOTPRINT&>( KI_TEST::RequireBoardItemWithTypeAndId(
96 aBoard, PCB_FOOTPRINT_T, fooprintTestCase.m_footprintUuid ) );
97
98 BOOST_CHECK_EQUAL( fp.IsLocked(), fooprintTestCase.m_expectedLocked );
99 BOOST_CHECK_EQUAL( fp.GetPosition(), fooprintTestCase.m_expectedPos * 1000000 );
100 }
101 };
102
103 KI_TEST::LoadAndTestBoardFile( testCase.m_BoardFileRelativePath, true, doBoardTest,
104 testCase.m_ExpectedBoardVersion );
105}
106
107
112BOOST_AUTO_TEST_CASE( FootprintFieldLockRoundTrip )
113{
114 BOARD board;
115
116 FOOTPRINT* fp = new FOOTPRINT( &board );
117 fp->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 20 ), 0 ) );
118 fp->SetReference( wxS( "U1" ) );
119 fp->GetField( FIELD_T::REFERENCE )->SetLocked( true );
120
121 PCB_FIELD* userField = new PCB_FIELD( fp, FIELD_T::USER, wxS( "Label" ) );
122 userField->SetText( wxS( "BezelSW0" ) );
123 userField->SetLayer( F_SilkS );
124 userField->SetLocked( true );
125 fp->Add( userField );
126
127 // A plain footprint text is the control: it has always round-tripped its lock
128 PCB_TEXT* fpText = new PCB_TEXT( fp );
129 fpText->SetText( wxS( "fp text" ) );
130 fpText->SetLayer( F_SilkS );
131 fpText->SetLocked( true );
132 fp->Add( fpText );
133
134 board.Add( fp );
135
136 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_qa_field_lock" );
137 const std::filesystem::path path = tempDir.Path() / "field_lock.kicad_pcb";
138
140 std::unique_ptr<BOARD> restored = KI_TEST::ReadBoardFromFileOrStream( path.string() );
141 BOOST_REQUIRE( restored );
142 BOOST_REQUIRE_EQUAL( restored->Footprints().size(), 1 );
143
144 FOOTPRINT* loaded = restored->Footprints().front();
145
147 BOOST_REQUIRE( loaded->GetField( wxS( "Label" ) ) );
148 BOOST_CHECK( loaded->GetField( wxS( "Label" ) )->IsLocked() );
149
150 for( BOARD_ITEM* item : loaded->GraphicalItems() )
151 {
152 if( item->Type() == PCB_TEXT_T )
153 BOOST_CHECK( item->IsLocked() );
154 }
155}
156
157
162BOOST_AUTO_TEST_CASE( FootprintUserFieldKeepsAutoThickness )
163{
164 BOARD board;
165
166 // Auto thickness only differs from the board default when that default is not zero
168
169 FOOTPRINT* fp = new FOOTPRINT( &board );
170 fp->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 20 ), 0 ) );
171 fp->SetReference( wxS( "U1" ) );
172
173 PCB_FIELD* userField = new PCB_FIELD( fp, FIELD_T::USER, wxS( "Label" ) );
174 userField->SetText( wxS( "BezelSW0" ) );
175 userField->SetLayer( F_SilkS );
176 userField->SetAutoThickness( true );
177 fp->Add( userField );
178
179 board.Add( fp );
180
181 BOOST_REQUIRE( userField->GetAutoThickness() );
182
183 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_qa_auto_thickness" );
184 const std::filesystem::path path = tempDir.Path() / "auto_thickness.kicad_pcb";
185
187 std::unique_ptr<BOARD> restored = KI_TEST::ReadBoardFromFileOrStream( path.string() );
188 BOOST_REQUIRE( restored );
189 BOOST_REQUIRE_EQUAL( restored->Footprints().size(), 1 );
190
191 PCB_FIELD* loaded = restored->Footprints().front()->GetField( wxS( "Label" ) );
192 BOOST_REQUIRE( loaded );
193 BOOST_CHECK( loaded->GetAutoThickness() );
194}
195
196
201BOOST_AUTO_TEST_CASE( LegacyFootprintPropertyTakesBoardDefaults )
202{
203 const auto doBoardTest = [&]( const BOARD& aBoard )
204 {
205 BOOST_REQUIRE( !aBoard.Footprints().empty() );
206
207 PCB_FIELD* field = aBoard.Footprints().front()->GetField( wxS( "myfield" ) );
208
209 BOOST_REQUIRE( field );
210 BOOST_CHECK( !field->GetAutoThickness() );
212 aBoard.GetDesignSettings().GetTextThickness( field->GetLayer() ) );
213 };
214
215 // Saving upgrades the file, so the roundtrip also covers the field once it has its own effects
216 KI_TEST::LoadAndTestBoardFile( "custom_fields", true, doBoardTest );
217}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
@ LAYER_CLASS_SILK
General utilities for PCB file IO for QA programs.
int m_TextThickness[LAYER_CLASS_COUNT]
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
void SetLocked(bool aLocked) override
Definition board_item.h:441
bool IsLocked() const override
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:397
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
bool GetAutoThickness() const
Definition eda_text.h:170
void SetAutoThickness(bool aAuto)
Definition eda_text.cpp:259
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
void SetPosition(const VECTOR2I &aPos) override
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
void SetReference(const wxString &aReference)
Definition footprint.h:799
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Adds an item to the container.
DRAWINGS & GraphicalItems()
Definition footprint.h:358
wxString AsString() const
Definition kiid.cpp:264
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:61
int GetTextThickness() const override
Definition pcb_text.cpp:499
@ F_SilkS
Definition layer_ids.h:96
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
void LoadAndTestBoardFile(const wxString aRelativePath, bool aRoundtrip, std::function< void(BOARD &)> aBoardTestFunction, std::optional< int > aExpectedBoardVersion)
Perform "some test" on a board file loaded from the path, then optionally save and reload and run the...
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.
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_DATA_TEST_CASE(FootprintLoadSave, boost::unit_test::data::make(FootprintLoadSave_testCases), testCase)
Simple tests cases that load a board file and check expected properties of the footprints.
BOOST_AUTO_TEST_CASE(FootprintFieldLockRoundTrip)
Fields are written without a header of their own, inside the footprint's (property ....
std::string path
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708