KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_board_item.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
25#include <eda_item_test_utils.h>
26#include <core/typeinfo.h>
27#include <drc/drc_item.h>
28
29// Code under test
30#include <board.h>
31#include <board_item.h>
32#include <footprint.h>
33#include <pad.h>
34#include <pcb_shape.h>
35#include <pcb_barcode.h>
36#include <pcb_text.h>
37#include <pcb_textbox.h>
38#include <pcb_table.h>
39#include <pcb_tablecell.h>
40#include <pcb_reference_image.h>
41#include <zone.h>
42#include <pcb_track.h>
43#include <pcb_marker.h>
44#include <pcb_dimension.h>
45#include <pcb_point.h>
46#include <pcb_target.h>
47#include <pcb_group.h>
48#include <pcb_board_outline.h>
49#include <properties/property.h>
51
53{
54public:
57 std::shared_ptr<DRC_ITEM> m_drcItem;
59
67
69 {
70 m_text.SetParentGroup( nullptr );
71 }
72
74 {
75 if( !IsPcbnewType( aType ) )
76 return nullptr;
77
78 if( !IsInstantiableType( aType ) )
79 return nullptr;
80
81 switch( aType )
82 {
83 case PCB_FOOTPRINT_T: return new FOOTPRINT( &m_board );
84 case PCB_PAD_T: return new PAD( &m_footprint );
85 case PCB_FIELD_T: return new PCB_FIELD( &m_footprint, FIELD_T::USER );
86 case PCB_SHAPE_T: return new PCB_SHAPE( &m_board );
87
88 case PCB_BARCODE_T:
89 {
90 PCB_BARCODE* barcode = new PCB_BARCODE( &m_board );
91 barcode->SetText( "XXXX" );
92 barcode->AssembleBarcode();
93 return barcode;
94 }
95
96 case PCB_TEXT_T: return new PCB_TEXT( &m_board );
97 case PCB_TEXTBOX_T: return new PCB_TEXTBOX( &m_board );
98 case PCB_TABLECELL_T: return new PCB_TABLECELL( &m_board );
99
100 case PCB_TABLE_T:
101 {
102 PCB_TABLE* table = new PCB_TABLE( &m_board, pcbIUScale.mmToIU( 0.1 ) );
103
104 table->SetColCount( 2 );
105
106 for( int ii = 0; ii < 4; ++ii )
107 {
108 PCB_TABLECELL* cell = new PCB_TABLECELL( &m_board );
109 cell->SetRectangleHeight( 0 );
110 cell->SetRectangleWidth( 0 );
111 table->InsertCell( ii, cell );
112 }
113
114 return table;
115 }
116
118 case PCB_TRACE_T: return new PCB_TRACK( &m_board );
119 case PCB_VIA_T: return new PCB_VIA( &m_board );
120 case PCB_ARC_T: return new PCB_ARC( &m_board );
121 case PCB_MARKER_T: return new PCB_MARKER( m_drcItem, VECTOR2I( 0, 0 ) );
123 case PCB_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_board );
124 case PCB_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_board );
125 case PCB_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_board );
127 case PCB_TARGET_T: return new PCB_TARGET( &m_board );
128 case PCB_POINT_T: return new PCB_POINT( &m_board );
129
130 case PCB_ZONE_T:
131 {
132 ZONE* zone = new ZONE( &m_board );
133
134 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
135 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
136 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
137 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
138
139 return zone;
140 }
141
142 case PCB_GROUP_T:
143 {
144 PCB_GROUP* group = new PCB_GROUP( &m_board );
145
146 // Group position only makes sense if there's at least one item in the group.
147 group->AddItem( &m_text );
148
149 return group;
150 }
151
152 case PCB_T:
153 case PCB_ITEM_LIST_T:
154 case PCB_NETINFO_T:
155 case PCB_GENERATOR_T:
157 return nullptr;
158
159 default:
160 BOOST_FAIL( wxString::Format( "Unhandled type: %d (if you created a new type you need to handle it in "
161 "this switch statement)",
162 aType ) );
163 return nullptr;
164 }
165 }
166
167 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
168 {
169 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
170 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(), aOriginalItem->GetBoundingBox().GetTop() );
171 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(), aOriginalItem->GetBoundingBox().GetLeft() );
172 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(), aOriginalItem->GetBoundingBox().GetBottom() );
173 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(), aOriginalItem->GetBoundingBox().GetRight() );
174 }
175};
176
177
178BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
179
180
182{
183 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
184 {
185 KICAD_T type = static_cast<KICAD_T>( i );
186
187 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
188
189 if( item == nullptr )
190 continue;
191
192 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
193 {
194 BOOST_CHECK( !ENUM_MAP<KICAD_T>::Instance().ToString( type ).IsEmpty() );
195 }
196 }
197}
198
199
201{
202 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
203 {
204 KICAD_T type = static_cast<KICAD_T>( i );
205
206 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
207
208 if( item == nullptr )
209 continue;
210
211 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
212 {
214 item.get(),
215 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
216 {
217 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
218 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
219 dimension->Update();
220
221 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
222 VECTOR2I originalPos = item->GetPosition();
223
224 // Move to a point, then go back.
225 // This has to be an identity transformation.
226
227 item->Move( aRef );
228 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
229
230 item->Move( -aRef );
231 CompareItems( item.get(), aOriginalItem );
232 } );
233 }
234 }
235}
236
237
239{
240 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
241 {
242 KICAD_T type = static_cast<KICAD_T>( i );
243
244 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
245
246 if( item == nullptr )
247 continue;
248
249 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
250 {
251 // Four same 90 degree rotations are an identity.
252
254 item.get(),
255 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
256 {
257 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
258 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
259 dimension->Update();
260 else if( PCB_BARCODE* barcode = dynamic_cast<PCB_BARCODE*>( aOriginalItem ) )
261 barcode->AssembleBarcode();
262
263 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
264
265 // Four equivalent 90 degree rotations are an identity.
266
267 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
268 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
269 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
270 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
271
272 CompareItems( item.get(), aOriginalItem );
273 } );
274 }
275 }
276}
277
278
279BOOST_AUTO_TEST_CASE( FlipLeftRight )
280{
281 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
282 {
283 KICAD_T type = static_cast<KICAD_T>( i );
284
285 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
286
287 if( item == nullptr )
288 continue;
289
290 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
291 {
293 item.get(),
294 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
295 {
296 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
297 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
298 dimension->Update();
299
300 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
301
302 // Two equivalent flips are an identity.
303
304 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
305 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
306
307 CompareItems( item.get(), aOriginalItem );
308 } );
309 }
310 }
311}
312
313
315{
316 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
317 {
318 KICAD_T type = static_cast<KICAD_T>( i );
319
320 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
321
322 if( item == nullptr )
323 continue;
324
325 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
326 {
328 item.get(),
329 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
330 {
331 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
332 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
333 dimension->Update();
334
335 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
336
337 // Two equivalent flips are an identity.
338
339 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
340 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
341
342 CompareItems( item.get(), aOriginalItem );
343 } );
344 }
345 }
346}
347
348
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
constexpr coord_type GetLeft() const
Definition box2.h:228
constexpr coord_type GetRight() const
Definition box2.h:217
constexpr coord_type GetTop() const
Definition box2.h:229
constexpr coord_type GetBottom() const
Definition box2.h:222
virtual VECTOR2I GetPosition() const
Definition eda_item.h:278
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:120
void SetRectangleHeight(const int &aHeight)
void SetRectangleWidth(const int &aWidth)
static ENUM_MAP< T > & Instance()
Definition property.h:721
void AssembleBarcode()
Assemble the barcode polygon and text polygons into a single polygonal representation.
void SetText(const wxString &aText)
Set the barcode content text to encode.
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 set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:53
A PCB_POINT is a 0-dimensional point that is used to mark a position on a PCB, or more usually a foot...
Definition pcb_point.h:43
Object to handle a bitmap image that can be inserted in a PCB.
BOARD_ITEM * Instantiate(KICAD_T aType)
static void CompareItems(BOARD_ITEM *aItem, BOARD_ITEM *aOriginalItem)
std::shared_ptr< DRC_ITEM > m_drcItem
Handle a list of polygons defining a copper zone.
Definition zone.h:73
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:1183
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:68
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
BARCODE class definition.
Class to handle a set of BOARD_ITEMs.
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(Type)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_CHECK_EQUAL(result, "25.4")
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:78
@ PCB_T
Definition typeinfo.h:82
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:106
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:103
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:242
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:104
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:89
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition typeinfo.h:109
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition typeinfo.h:99
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:107
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:95
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:102
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87
@ PCB_BOARD_OUTLINE_T
class PCB_BOARD_OUTLINE_T, a pcb board outline item
Definition typeinfo.h:112
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:98
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:94
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition typeinfo.h:110
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:113
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:105
constexpr bool IsPcbnewType(const KICAD_T aType)
Definition typeinfo.h:428
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:313
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695