KiCad PCB EDA Suite
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 (C) 2022 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_text.h>
36#include <pcb_textbox.h>
37#include <pcb_bitmap.h>
38#include <fp_text.h>
39#include <fp_textbox.h>
40#include <fp_shape.h>
41#include <zone.h>
42#include <pcb_track.h>
43#include <pcb_marker.h>
44#include <pcb_dimension.h>
45#include <pcb_target.h>
46#include <netinfo.h>
47#include <pcb_group.h>
48
50{
51public:
54 std::shared_ptr<DRC_ITEM> m_drcItem;
56
58 m_board(),
61 m_text( &m_board )
62 {
63 }
64
66 {
67 m_text.SetParentGroup( nullptr );
68 }
69
71 {
72 if( !IsPcbnewType( aType ) )
73 return nullptr;
74
75 if( !IsInstantiableType( aType ) )
76 return nullptr;
77
78 switch( aType )
79 {
80 case PCB_FOOTPRINT_T: return new FOOTPRINT( &m_board );
81 case PCB_PAD_T: return new PAD( &m_footprint );
82 case PCB_SHAPE_T: return new PCB_SHAPE( &m_board );
83 case PCB_TEXT_T: return new PCB_TEXT( &m_board );
84 case PCB_TEXTBOX_T: return new PCB_TEXTBOX( &m_board );
85 case PCB_BITMAP_T: return new PCB_BITMAP( &m_board );
86 case PCB_FP_TEXT_T: return new FP_TEXT( &m_footprint );
87 case PCB_FP_TEXTBOX_T: return new FP_TEXTBOX( &m_footprint );
88 case PCB_FP_SHAPE_T: return new FP_SHAPE( &m_footprint );
90 case PCB_FP_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_footprint, true );
91 case PCB_FP_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_footprint, true );
92 case PCB_FP_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_footprint, true );
94 case PCB_FP_ZONE_T:
95 {
96 FP_ZONE* fpZone = new FP_ZONE( &m_footprint );
97
98 fpZone->AppendCorner( wxPoint( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
99 fpZone->AppendCorner( wxPoint( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
100 fpZone->AppendCorner( wxPoint( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
101 fpZone->AppendCorner( wxPoint( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
102
103 return fpZone;
104 }
105
106 case PCB_TRACE_T: return new PCB_TRACK( &m_board );
107 case PCB_VIA_T: return new PCB_VIA( &m_board );
108 case PCB_ARC_T: return new PCB_ARC( &m_board );
109 case PCB_MARKER_T: return new PCB_MARKER( m_drcItem, wxPoint( 0, 0 ) );
111 case PCB_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_board );
112 case PCB_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_board );
113 case PCB_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_board );
115 case PCB_TARGET_T: return new PCB_TARGET( &m_board );
116 case PCB_ZONE_T:
117 {
118 ZONE* zone = new ZONE( &m_board );
119
120 zone->AppendCorner( wxPoint( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
121 zone->AppendCorner( wxPoint( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
122 zone->AppendCorner( wxPoint( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
123 zone->AppendCorner( wxPoint( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
124
125 return zone;
126 }
127 case PCB_GROUP_T:
128 {
129 PCB_GROUP* group = new PCB_GROUP( &m_board );
130
131 // Group position only makes sense if there's at least one item in the group.
132 group->AddItem( &m_text );
133
134 return group;
135 }
136
137 case PCB_T:
138 case PCB_ITEM_LIST_T:
139 case PCB_NETINFO_T:
140 return nullptr;
141
142 default:
143 BOOST_FAIL( wxString::Format(
144 "Unhandled type: %d "
145 "(if you created a new type you need to handle it in this switch statement)",
146 aType ) );
147 return nullptr;
148 }
149 }
150
151 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
152 {
153 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
154 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(),
155 aOriginalItem->GetBoundingBox().GetTop() );
156 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(),
157 aOriginalItem->GetBoundingBox().GetLeft() );
158 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(),
159 aOriginalItem->GetBoundingBox().GetBottom() );
160 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(),
161 aOriginalItem->GetBoundingBox().GetRight() );
162 }
163};
164
165
166BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
167
168
170{
171 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
172 {
173 KICAD_T type = static_cast<KICAD_T>( i );
174
175 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
176
177 if( item == nullptr )
178 continue;
179
180 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
181 {
182 IterateOverPositionsAndReferences<BOARD_ITEM>(
183 item.get(),
184 []( BOARD_ITEM* aOriginalItem, wxPoint aRef )
185 {
186 FP_SHAPE* originalFpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
187
188 if( originalFpShape != nullptr )
189 originalFpShape->SetDrawCoord();
190
191 // FIXME: Update() has to be called after SetPosition() to update dimension
192 // shapes.
193 PCB_DIMENSION_BASE* originalDimension =
194 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
195
196 if( originalDimension != nullptr )
197 originalDimension->Update();
198
199 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
200 VECTOR2I originalPos = item->GetPosition();
201
202 // Move to a point, then go back.
203 // This has to be an identity transformation.
204
205 item->Move( aRef );
206
207 FP_SHAPE* fpShape = dynamic_cast<FP_SHAPE*>( item.get() );
208
209 if( fpShape != nullptr )
210 fpShape->SetDrawCoord();
211
212 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
213
214 item->Move( -aRef );
215
216 if( fpShape != nullptr )
217 fpShape->SetDrawCoord();
218
219 CompareItems( item.get(), aOriginalItem );
220 } );
221 }
222 }
223}
224
225
227{
228 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
229 {
230 KICAD_T type = static_cast<KICAD_T>( i );
231
232 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
233
234 if( item == nullptr )
235 continue;
236
237 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
238 {
239 // Four same 90 degree rotations are an identity.
240
241 IterateOverPositionsAndReferences<BOARD_ITEM>(
242 item.get(),
243 []( BOARD_ITEM* aOriginalItem, wxPoint aRef )
244 {
245 FP_SHAPE* originalFpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
246
247 if( originalFpShape != nullptr )
248 originalFpShape->SetDrawCoord();
249
250 // FIXME: Update() has to be called after SetPosition() to update dimension
251 // shapes.
252 PCB_DIMENSION_BASE* originalDimension =
253 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
254
255 if( originalDimension != nullptr )
256 originalDimension->Update();
257
258 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
259
260 // Four equivalent 90 degree rotations are an identity.
261
262 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
263 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
264 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
265 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
266
267 FP_SHAPE* fpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
268
269 if( fpShape != nullptr )
270 fpShape->SetDrawCoord();
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 {
292 IterateOverPositionsAndReferences<BOARD_ITEM>(
293 item.get(),
294 []( BOARD_ITEM* aOriginalItem, wxPoint aRef )
295 {
296 FP_SHAPE* originalFpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
297
298 if( originalFpShape != nullptr )
299 originalFpShape->SetDrawCoord();
300
301 // FIXME: Update() has to be called after SetPosition() to update dimension
302 // shapes.
303 PCB_DIMENSION_BASE* originalDimension =
304 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
305
306 if( originalDimension != nullptr )
307 originalDimension->Update();
308
309 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
310
311 // Two equivalent flips are an identity.
312
313 item->Flip( aRef, true );
314 item->Flip( aRef, true );
315
316 FP_SHAPE* fpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
317
318 if( fpShape != nullptr )
319 fpShape->SetDrawCoord();
320
321 CompareItems( item.get(), aOriginalItem );
322 } );
323 }
324 }
325}
326
327
329{
330 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
331 {
332 KICAD_T type = static_cast<KICAD_T>( i );
333
334 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
335
336 if( item == nullptr )
337 continue;
338
339 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
340 {
341 IterateOverPositionsAndReferences<BOARD_ITEM>(
342 item.get(),
343 []( BOARD_ITEM* aOriginalItem, wxPoint aRef )
344 {
345 FP_SHAPE* originalFpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
346
347 if( originalFpShape != nullptr )
348 originalFpShape->SetDrawCoord();
349
350 // FIXME: Update() has to be called after SetPosition() to update dimension
351 // shapes.
352 PCB_DIMENSION_BASE* originalDimension =
353 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
354
355 if( originalDimension != nullptr )
356 originalDimension->Update();
357
358 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
359
360 // Two equivalent flips are an identity.
361
362 item->Flip( aRef, false );
363 item->Flip( aRef, false );
364
365 FP_SHAPE* fpShape = dynamic_cast<FP_SHAPE*>( aOriginalItem );
366
367 if( fpShape != nullptr )
368 fpShape->SetDrawCoord();
369
370 CompareItems( item.get(), aOriginalItem );
371 } );
372 }
373 }
374}
375
376
377BOOST_AUTO_TEST_SUITE_END()
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:71
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
coord_type GetTop() const
Definition: box2.h:194
coord_type GetRight() const
Definition: box2.h:189
coord_type GetLeft() const
Definition: box2.h:193
coord_type GetBottom() const
Definition: box2.h:190
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:251
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:74
A specialization of ZONE for use in footprints.
Definition: zone.h:910
Object to handle a bitmap image that can be inserted in a PCB.
Definition: pcb_bitmap.h:42
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:51
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:57
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:757
@ DRCE_MALFORMED_COURTYARD
Definition: drc_item.h:63
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
BOOST_AUTO_TEST_CASE(Move)
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_FP_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:95
@ 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:110
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:107
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102
@ MAX_STRUCT_TYPE_ID
Definition: typeinfo.h:241
@ PCB_FP_TEXTBOX_T
class FP_TEXTBOX, wrapped text in a footprint
Definition: typeinfo.h:93
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:108
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:115
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:91
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:112
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_FP_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:97
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:113
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition: typeinfo.h:104
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:111
@ PCB_FP_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:99
@ PCB_FP_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:96
@ 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:106
@ PCB_FP_ZONE_T
class ZONE, managed by a footprint
Definition: typeinfo.h:100
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FP_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:98
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:103
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:114
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:101
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:109
constexpr bool IsPcbnewType(const KICAD_T aType)
Definition: typeinfo.h:423
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition: typeinfo.h:311
#define BOOST_TEST_CONTEXT(A)