KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_api_proto.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 (C) 2024 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <boost/test/unit_test.hpp>
22#include <memory>
23#include <limits>
24#include <import_export.h>
29
30#include <api/board/board_types.pb.h>
31#include <api/common/types/embedded_files.pb.h>
32
33#include <board.h>
34#include <embedded_files.h>
35#include <footprint.h>
36#include <pcb_barcode.h>
37#include <pcb_dimension.h>
38#include <pcb_drill_chart.h>
39#include <pcb_drill_map.h>
40#include <pcb_grid_item.h>
41#include <pcb_reference_image.h>
42#include <pcb_shape.h>
43#include <pcb_table.h>
44#include <pcb_text.h>
45#include <pcb_textbox.h>
46#include <pcb_track.h>
47#include <zone.h>
48
49
50BOOST_AUTO_TEST_SUITE( ApiProto )
51
60
61
63{
64 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
65
66 int barcodeCount = 0;
67 int drillChartCount = 0;
68 int drillMapCount = 0;
69 int referenceImageCount = 0;
70 int tableCount = 0;
71 int textCount = 0;
72 int textBoxCount = 0;
73 int shapeCount = 0;
74
75 for( PCB_TRACK* track : m_board->Tracks() )
76 {
77 switch( track->Type() )
78 {
79 case PCB_TRACE_T:
81 break;
82
83 case PCB_ARC_T:
85 m_board.get() );
86 break;
87
88 case PCB_VIA_T:
90 m_board.get() );
91 break;
92
93 default:
94 wxFAIL;
95 }
96 }
97
98 for( FOOTPRINT* footprint : m_board->Footprints() )
100
101 for( ZONE* zone : m_board->Zones() )
103
104 for( BOARD_ITEM* item : m_board->Drawings() )
105 {
106 switch( item->Type() )
107 {
110 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
111 break;
112
115 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
116 break;
117
118 case PCB_DIM_CENTER_T:
120 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
121 break;
122
123 case PCB_DIM_LEADER_T:
125 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
126 break;
127
128 case PCB_DIM_RADIAL_T:
130 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
131 break;
132
133 case PCB_BARCODE_T:
135 static_cast<PCB_BARCODE*>( item ), m_board.get() );
136 ++barcodeCount;
137 break;
138
141 static_cast<PCB_REFERENCE_IMAGE*>( item ), m_board.get() );
142 ++referenceImageCount;
143 break;
144
147 static_cast<PCB_DRILL_CHART*>( item ), m_board.get() );
148 ++drillChartCount;
149 break;
150
151 case PCB_DRILL_MAP_T:
153 static_cast<PCB_DRILL_MAP*>( item ), m_board.get() );
154 ++drillMapCount;
155 break;
156
157 case PCB_TABLE_T:
158 testProtoFromKiCadObject<kiapi::board::types::Table>( static_cast<PCB_TABLE*>( item ), m_board.get() );
159 ++tableCount;
160 break;
161
162 case PCB_TEXT_T:
164 static_cast<PCB_TEXT*>( item ), m_board.get() );
165 ++textCount;
166 break;
167
168 case PCB_TEXTBOX_T:
170 static_cast<PCB_TEXTBOX*>( item ), m_board.get() );
171 ++textBoxCount;
172 break;
173
174 case PCB_SHAPE_T:
176 static_cast<PCB_SHAPE*>( item ), m_board.get() );
177 ++shapeCount;
178 break;
179
180 default: break;
181 }
182 }
183
184 BOOST_CHECK_GT( barcodeCount, 0 );
185 BOOST_CHECK_GT( drillChartCount, 0 );
186 BOOST_CHECK_GT( drillMapCount, 0 );
187 BOOST_CHECK_GT( referenceImageCount, 0 );
188 BOOST_CHECK_GT( tableCount, 0 );
189 BOOST_CHECK_GT( textCount, 0 );
190 BOOST_CHECK_GT( textBoxCount, 0 );
191 BOOST_CHECK_GT( shapeCount, 0 );
192}
193
194
195BOOST_AUTO_TEST_CASE( FootprintExcludeFromSimulationRoundTrip )
196{
197 BOARD board;
198 FOOTPRINT footprint( &board );
199
200 footprint.SetExcludedFromSim( true );
201
203}
204
205
207{
208 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
209
210 PCB_DRILL_CHART* chart = nullptr;
211
212 for( BOARD_ITEM* item : m_board->Drawings() )
213 {
214 if( item->Type() == PCB_DRILL_CHART_T )
215 {
216 chart = static_cast<PCB_DRILL_CHART*>( item );
217 break;
218 }
219 }
220
221 BOOST_REQUIRE( chart );
222
223 google::protobuf::Any any;
224 chart->Serialize( any );
225 kiapi::board::types::DrillChart proto;
226 BOOST_REQUIRE( any.UnpackTo( &proto ) );
227 BOOST_REQUIRE_GT( proto.columns_size(), 0 );
228 BOOST_CHECK_EQUAL( proto.columns( 0 ).width().value_nm(), 16000000 );
229
230 for( int64_t width : { int64_t( 0 ), int64_t( DRILL_CHART_MAX_COLUMN_WIDTH ) } )
231 {
232 proto.mutable_columns( 0 )->mutable_width()->set_value_nm( width );
233 any.PackFrom( proto );
234 PCB_DRILL_CHART restored( m_board.get() );
235 BOOST_REQUIRE( restored.Deserialize( any ) );
236 BOOST_CHECK_EQUAL( restored.Columns().front().m_Width, width );
237 }
238
239 for( int64_t width : { int64_t( -1 ), int64_t( DRILL_CHART_MAX_COLUMN_WIDTH ) + 1,
240 std::numeric_limits<int64_t>::max() } )
241 {
242 proto.mutable_columns( 0 )->mutable_width()->set_value_nm( width );
243 any.PackFrom( proto );
244 PCB_DRILL_CHART restored( m_board.get() );
245 BOOST_REQUIRE( restored.Deserialize( any ) );
246 BOOST_CHECK_EQUAL( restored.Columns().front().m_Width,
247 width < 0 ? 0 : DRILL_CHART_MAX_COLUMN_WIDTH );
248 }
249}
250
251
253{
254 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
255
256 for( PCB_TRACK* track : m_board->Tracks() )
257 {
258 switch( track->Type() )
259 {
260 case PCB_VIA_T:
261 testProtoFromKiCadObject<kiapi::board::types::Via>( static_cast<PCB_VIA*>( track ), m_board.get() );
262 break;
263
264 default:
265 wxFAIL;
266 }
267 }
268
269 for( FOOTPRINT* footprint : m_board->Footprints() )
271}
272
273
275{
276 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
277
278 for( const FOOTPRINT* fp : m_board->Footprints() )
279 {
280 if( fp->EmbeddedFileMap().empty() )
281 continue;
282
283 BOOST_TEST_CONTEXT( wxString::Format( wxS( "Footprint %s embedded files" ), fp->m_Uuid.AsStdString() ) )
284 {
285 google::protobuf::Any any;
286 fp->Serialize( any );
287
288 kiapi::board::types::FootprintInstance proto;
289 BOOST_CHECK( any.UnpackTo( &proto ) );
290
291 BOOST_CHECK_GT( proto.embedded_files().files_size(), 0 );
292
293 const kiapi::common::types::EmbeddedFile& file = proto.embedded_files().files( 0 );
294 BOOST_CHECK( !file.data().empty() );
295
296 std::unique_ptr<FOOTPRINT> roundTripped = std::make_unique<FOOTPRINT>( m_board.get() );
297 BOOST_CHECK( roundTripped->Deserialize( any ) );
298
299 for( const auto& [name, expected] : fp->EmbeddedFileMap() )
300 {
301 const EMBEDDED_FILES::EMBEDDED_FILE* actual = roundTripped->GetEmbeddedFile( name );
302 BOOST_CHECK_MESSAGE( actual, wxString::Format( wxS( "Embedded file '%s' missing after round-trip" ), name ).c_str() );
303 BOOST_CHECK_EQUAL( magic_enum::enum_name( expected->type ), magic_enum::enum_name( actual->type ) );
304 BOOST_CHECK_EQUAL( expected->data_hash, actual->data_hash );
305 BOOST_CHECK_EQUAL( expected->compressedEncodedData, actual->compressedEncodedData );
306 }
307 }
308 }
309}
310
311
318BOOST_FIXTURE_TEST_CASE( CopperThievingZoneRoundTrip, PROTO_TEST_FIXTURE )
319{
320 m_board = std::make_unique<BOARD>();
321
322 ZONE* zone = new ZONE( m_board.get() );
323 zone->SetLayer( F_Cu );
324 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
325 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 5 ), 0 ), -1 );
326 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 5 ), pcbIUScale.mmToIU( 5 ) ), -1 );
327 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 5 ) ), -1 );
329
330 THIEVING_SETTINGS thieving;
332 thieving.element_size = pcbIUScale.mmToIU( 0.75 );
333 thieving.gap = pcbIUScale.mmToIU( 2.0 );
334 thieving.line_width = pcbIUScale.mmToIU( 0.4 );
335 thieving.stagger = true;
336 thieving.orientation = EDA_ANGLE( 15.0, DEGREES_T );
337 zone->SetThievingSettings( thieving );
338
339 m_board->Add( zone );
340
341 google::protobuf::Any any;
342 BOOST_REQUIRE_NO_THROW( zone->Serialize( any ) );
343
344 kiapi::board::types::Zone proto;
345 BOOST_REQUIRE( any.UnpackTo( &proto ) );
346 BOOST_REQUIRE( proto.has_copper_settings() );
347 BOOST_REQUIRE( proto.copper_settings().has_thieving_settings() );
348
349 std::unique_ptr<ZONE> roundTripped = std::make_unique<ZONE>( m_board.get() );
350 BOOST_REQUIRE( roundTripped->Deserialize( any ) );
351
352 BOOST_CHECK( roundTripped->GetFillMode() == ZONE_FILL_MODE::COPPER_THIEVING );
353 // A board with no netinfo list returns GetNetCode() == -1 for an unbound zone.
354 // The invariant we want is "no real net assigned"; netcode > 0 would mean a leak.
355 BOOST_CHECK_LE( roundTripped->GetNetCode(), 0 );
356
357 const THIEVING_SETTINGS& loaded = roundTripped->GetThievingSettings();
358 BOOST_CHECK( loaded.pattern == THIEVING_PATTERN::SQUARES );
359 BOOST_CHECK_EQUAL( loaded.element_size, thieving.element_size );
360 BOOST_CHECK_EQUAL( loaded.gap, thieving.gap );
361 BOOST_CHECK_EQUAL( loaded.line_width, thieving.line_width );
362 BOOST_CHECK_EQUAL( loaded.stagger, true );
363 BOOST_CHECK( loaded.orientation == EDA_ANGLE( 15.0, DEGREES_T ) );
364}
365
366
368{
369 const auto makeGridItem =
370 []()
371 {
372 return std::make_unique<PCB_GRID_ITEM>( nullptr );
373 };
374
375 PCB_GRID_ITEM cartesian( nullptr );
377 cartesian.SetPosition( VECTOR2I( 1000000, -2000000 ) );
378 cartesian.SetOrientationDegrees( 30.0 );
379 cartesian.SetExtent( VECTOR2I( 5000000, 4000000 ) );
380 cartesian.SetSpacing( VECTOR2I( 250000, 500000 ) );
381 cartesian.SetAssignedPriority( 3 );
382 cartesian.SetTickInterval( 5 );
383 cartesian.SetAffectsRouting( false );
384 cartesian.SetLocked( true );
385
387
388 PCB_GRID_ITEM polar( nullptr );
390 polar.SetPosition( VECTOR2I( -750000, 125000 ) );
391 polar.SetRadiusExtent( 8000000 );
392 polar.SetRadiusSpacing( 1000000 );
393 polar.SetPhiExtentDegrees( 270.0 );
394 polar.SetPhiSpacingDegrees( 7.5 );
395 polar.SetAssignedPriority( 1 );
396 polar.SetAffectsCursor( false );
397 polar.SetAffectsPlacement( false );
398
400
401 // The polar half of the grid must survive, not just the shared linear fields.
402 google::protobuf::Any any;
403 polar.Serialize( any );
404
405 std::unique_ptr<PCB_GRID_ITEM> roundTripped = makeGridItem();
406 BOOST_REQUIRE( roundTripped->Deserialize( any ) );
407
408 BOOST_CHECK( roundTripped->GetGridItemType() == PCB_GRID_TYPE::POLAR );
409 BOOST_CHECK_EQUAL( roundTripped->GetRadiusExtent(), 8000000 );
410 BOOST_CHECK_EQUAL( roundTripped->GetRadiusSpacing(), 1000000 );
411 BOOST_CHECK( roundTripped->GetPhiExtent() == EDA_ANGLE( 270.0, DEGREES_T ) );
412 BOOST_CHECK( roundTripped->GetPhiSpacing() == EDA_ANGLE( 7.5, DEGREES_T ) );
413 BOOST_CHECK_EQUAL( roundTripped->GetAffectsCursor(), false );
414 BOOST_CHECK_EQUAL( roundTripped->GetAffectsRouting(), true );
415 BOOST_CHECK_EQUAL( roundTripped->GetAffectsPlacement(), false );
416 BOOST_CHECK_EQUAL( roundTripped->IsLocked(), false );
417}
418
419
const char * name
void testProtoFromKiCadObject(KiCadClass *aInput, Factory &&aCreateOutput)
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
void SetLocked(bool aLocked) override
Definition board_item.h:417
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
void SetExcludedFromSim(bool aExclude=true)
Definition footprint.h:1047
For better understanding of the points that make a dimension:
Mark the center of a circle or arc with a cross shape.
A leader is a dimension-like object pointing to a specific point.
An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the X or ...
A radial dimension indicates either the radius or diameter of an arc or circle.
A drill chart placed on the board, kept in step with the holes.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
std::vector< DRILL_CHART_COLUMN > & Columns()
Turns on drill symbols at the holes, for one layer.
void SetRadiusExtent(int aR)
void SetAffectsRouting(bool aOn)
void SetPosition(const VECTOR2I &aPos) override
void SetRadiusSpacing(int aR)
void SetGridItemType(PCB_GRID_TYPE aType)
void SetExtent(const VECTOR2I &aExtent)
void SetTickInterval(unsigned aInterval)
void SetOrientationDegrees(double aDeg)
void SetSpacing(const VECTOR2I &aSpacing)
void SetPhiExtentDegrees(double aDeg)
void SetAffectsPlacement(bool aOn)
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
void SetPhiSpacingDegrees(double aDeg)
void SetAssignedPriority(unsigned aPriority)
Priority 0 is reserved for the global background grid; user-assigned 0 is bumped to 1.
void SetAffectsCursor(bool aOn)
Object to handle a bitmap image that can be inserted in a PCB.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
bool AppendCorner(const VECTOR2I &aPosition, int aHoleIdx, bool aAllowDuplication=false)
Add a new corner to the zone outline (to the main outline or a hole)
Definition zone.cpp:1441
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition zone.cpp:641
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition zone.cpp:247
void SetFillMode(ZONE_FILL_MODE aFillMode)
Definition zone.cpp:647
void SetThievingSettings(const THIEVING_SETTINGS &aSettings)
Definition zone.h:352
A type-safe container of any type.
Definition ki_any.h:92
constexpr int DRILL_CHART_MAX_COLUMN_WIDTH
A chart column can be no wider than this, and no chart wider than that in total.
@ DEGREES_T
Definition eda_angle.h:31
@ F_Cu
Definition layer_ids.h:60
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
BARCODE class definition.
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
Parameters that drive copper-thieving fill generation.
EDA_ANGLE orientation
THIEVING_PATTERN pattern
BOOST_FIXTURE_TEST_CASE(BoardTypes, PROTO_TEST_FIXTURE)
BOOST_AUTO_TEST_CASE(FootprintExcludeFromSimulationRoundTrip)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_TEST_CONTEXT("Test Clearance")
int actual
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:98
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:95
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DRILL_MAP_T
class PCB_DRILL_MAP, drill symbols drawn at the holes
Definition typeinfo.h:240
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:96
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:81
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:86
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:97
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683