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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <boost/test/unit_test.hpp>
22#include <import_export.h>
27
28#include <api/board/board_types.pb.h>
29
30#include <board.h>
31#include <footprint.h>
32#include <pcb_barcode.h>
33#include <pcb_dimension.h>
34#include <pcb_reference_image.h>
35#include <pcb_track.h>
36#include <zone.h>
37
38
39BOOST_AUTO_TEST_SUITE( ApiProto )
40
49
50
52{
53 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
54
55 int barcodeCount = 0;
56 int referenceImageCount = 0;
57
58 for( PCB_TRACK* track : m_board->Tracks() )
59 {
60 switch( track->Type() )
61 {
62 case PCB_TRACE_T:
64 break;
65
66 case PCB_ARC_T:
68 m_board.get() );
69 break;
70
71 case PCB_VIA_T:
72 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
73 // currently exposed to the API
74 // TODO(JE) enable strict when fixed
76 m_board.get(), false );
77 break;
78
79 default:
80 wxFAIL;
81 }
82 }
83
84 for( FOOTPRINT* footprint : m_board->Footprints() )
86
87 for( ZONE* zone : m_board->Zones() )
89
90 for( BOARD_ITEM* item : m_board->Drawings() )
91 {
92 switch( item->Type() )
93 {
96 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
97 break;
98
101 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
102 break;
103
104 case PCB_DIM_CENTER_T:
106 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
107 break;
108
109 case PCB_DIM_LEADER_T:
111 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
112 break;
113
114 case PCB_DIM_RADIAL_T:
116 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
117 break;
118
119 case PCB_BARCODE_T:
121 static_cast<PCB_BARCODE*>( item ), m_board.get() );
122 ++barcodeCount;
123 break;
124
127 static_cast<PCB_REFERENCE_IMAGE*>( item ), m_board.get() );
128 ++referenceImageCount;
129 break;
130
131 default: break;
132 }
133 // TODO(JE) Shapes
134
135 // TODO(JE) Text
136 }
137
138 BOOST_CHECK_GT( barcodeCount, 0 );
139 BOOST_CHECK_GT( referenceImageCount, 0 );
140}
141
142
144{
145 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
146
147 for( PCB_TRACK* track : m_board->Tracks() )
148 {
149 switch( track->Type() )
150 {
151 case PCB_VIA_T:
152 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
153 // currently exposed to the API
154 // TODO(JE) enable strict when fixed
156 m_board.get(), false );
157 break;
158
159 default:
160 wxFAIL;
161 }
162 }
163
164 for( FOOTPRINT* footprint : m_board->Footprints() )
166}
167
174BOOST_FIXTURE_TEST_CASE( CopperThievingZoneRoundTrip, PROTO_TEST_FIXTURE )
175{
176 m_board = std::make_unique<BOARD>();
177
178 ZONE* zone = new ZONE( m_board.get() );
179 zone->SetLayer( F_Cu );
180 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
181 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 5 ), 0 ), -1 );
182 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 5 ), pcbIUScale.mmToIU( 5 ) ), -1 );
183 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 5 ) ), -1 );
185
186 THIEVING_SETTINGS thieving;
188 thieving.element_size = pcbIUScale.mmToIU( 0.75 );
189 thieving.gap = pcbIUScale.mmToIU( 2.0 );
190 thieving.line_width = pcbIUScale.mmToIU( 0.4 );
191 thieving.stagger = true;
192 thieving.orientation = EDA_ANGLE( 15.0, DEGREES_T );
193 zone->SetThievingSettings( thieving );
194
195 m_board->Add( zone );
196
197 google::protobuf::Any any;
198 BOOST_REQUIRE_NO_THROW( zone->Serialize( any ) );
199
200 kiapi::board::types::Zone proto;
201 BOOST_REQUIRE( any.UnpackTo( &proto ) );
202 BOOST_REQUIRE( proto.has_copper_settings() );
203 BOOST_REQUIRE( proto.copper_settings().has_thieving_settings() );
204
205 std::unique_ptr<ZONE> roundTripped = std::make_unique<ZONE>( m_board.get() );
206 BOOST_REQUIRE( roundTripped->Deserialize( any ) );
207
208 BOOST_CHECK( roundTripped->GetFillMode() == ZONE_FILL_MODE::COPPER_THIEVING );
209 // A board with no netinfo list returns GetNetCode() == -1 for an unbound zone.
210 // The invariant we want is "no real net assigned"; netcode > 0 would mean a leak.
211 BOOST_CHECK_LE( roundTripped->GetNetCode(), 0 );
212
213 const THIEVING_SETTINGS& loaded = roundTripped->GetThievingSettings();
214 BOOST_CHECK( loaded.pattern == THIEVING_PATTERN::SQUARES );
215 BOOST_CHECK_EQUAL( loaded.element_size, thieving.element_size );
216 BOOST_CHECK_EQUAL( loaded.gap, thieving.gap );
217 BOOST_CHECK_EQUAL( loaded.line_width, thieving.line_width );
218 BOOST_CHECK_EQUAL( loaded.stagger, true );
219 BOOST_CHECK( loaded.orientation == EDA_ANGLE( 15.0, DEGREES_T ) );
220}
221
222
void testProtoFromKiCadObject(KiCadClass *aInput, Factory &&aCreateOutput)
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
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.
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:74
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition zone.cpp:603
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition zone.cpp:233
void SetFillMode(ZONE_FILL_MODE aFillMode)
Definition zone.cpp:609
void SetThievingSettings(const THIEVING_SETTINGS &aSettings)
Definition zone.h:356
bool AppendCorner(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:1280
A type-safe container of any type.
Definition ki_any.h:93
@ DEGREES_T
Definition eda_angle.h:31
@ F_Cu
Definition layer_ids.h:64
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_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:103
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:100
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:101
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:86
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:98
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:99
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:102
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687