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 (C) 2022-2023 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 <zone.h>
39#include <pcb_track.h>
40#include <pcb_marker.h>
41#include <pcb_dimension.h>
42#include <pcb_target.h>
43#include <pcb_group.h>
44
46{
47public:
50 std::shared_ptr<DRC_ITEM> m_drcItem;
52
54 m_board(),
57 m_text( &m_board )
58 {
59 }
60
62 {
63 m_text.SetParentGroup( nullptr );
64 }
65
67 {
68 if( !IsPcbnewType( aType ) )
69 return nullptr;
70
71 if( !IsInstantiableType( aType ) )
72 return nullptr;
73
74 switch( aType )
75 {
76 case PCB_FOOTPRINT_T: return new FOOTPRINT( &m_board );
77 case PCB_PAD_T: return new PAD( &m_footprint );
79 case PCB_SHAPE_T: return new PCB_SHAPE( &m_board );
80 case PCB_TEXT_T: return new PCB_TEXT( &m_board );
81 case PCB_TEXTBOX_T: return new PCB_TEXTBOX( &m_board );
82 case PCB_BITMAP_T: return new PCB_BITMAP( &m_board );
83 case PCB_TRACE_T: return new PCB_TRACK( &m_board );
84 case PCB_VIA_T: return new PCB_VIA( &m_board );
85 case PCB_ARC_T: return new PCB_ARC( &m_board );
86 case PCB_MARKER_T: return new PCB_MARKER( m_drcItem, VECTOR2I( 0, 0 ) );
88 case PCB_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_board );
89 case PCB_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_board );
90 case PCB_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_board );
92 case PCB_TARGET_T: return new PCB_TARGET( &m_board );
93 case PCB_ZONE_T:
94 {
95 ZONE* zone = new ZONE( &m_board );
96
97 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
98 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
99 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
100 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
101
102 return zone;
103 }
104 case PCB_GROUP_T:
105 {
106 PCB_GROUP* group = new PCB_GROUP( &m_board );
107
108 // Group position only makes sense if there's at least one item in the group.
109 group->AddItem( &m_text );
110
111 return group;
112 }
113
114 case PCB_T:
115 case PCB_ITEM_LIST_T:
116 case PCB_NETINFO_T:
117 return nullptr;
118
119 default:
120 BOOST_FAIL( wxString::Format(
121 "Unhandled type: %d "
122 "(if you created a new type you need to handle it in this switch statement)",
123 aType ) );
124 return nullptr;
125 }
126 }
127
128 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
129 {
130 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
131 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(),
132 aOriginalItem->GetBoundingBox().GetTop() );
133 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(),
134 aOriginalItem->GetBoundingBox().GetLeft() );
135 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(),
136 aOriginalItem->GetBoundingBox().GetBottom() );
137 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(),
138 aOriginalItem->GetBoundingBox().GetRight() );
139 }
140};
141
142
143BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
144
145
147{
148 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
149 {
150 KICAD_T type = static_cast<KICAD_T>( i );
151
152 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
153
154 if( item == nullptr )
155 continue;
156
157 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
158 {
159 IterateOverPositionsAndReferences<BOARD_ITEM>(
160 item.get(),
161 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
162 {
163 // FIXME: Update() has to be called after SetPosition() to update dimension
164 // shapes.
165 PCB_DIMENSION_BASE* originalDimension =
166 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
167
168 if( originalDimension != nullptr )
169 originalDimension->Update();
170
171 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
172 VECTOR2I originalPos = item->GetPosition();
173
174 // Move to a point, then go back.
175 // This has to be an identity transformation.
176
177 item->Move( aRef );
178 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
179
180 item->Move( -aRef );
181 CompareItems( item.get(), aOriginalItem );
182 } );
183 }
184 }
185}
186
187
189{
190 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
191 {
192 KICAD_T type = static_cast<KICAD_T>( i );
193
194 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
195
196 if( item == nullptr )
197 continue;
198
199 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
200 {
201 // Four same 90 degree rotations are an identity.
202
203 IterateOverPositionsAndReferences<BOARD_ITEM>(
204 item.get(),
205 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
206 {
207 // FIXME: Update() has to be called after SetPosition() to update dimension
208 // shapes.
209 PCB_DIMENSION_BASE* originalDimension =
210 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
211
212 if( originalDimension != nullptr )
213 originalDimension->Update();
214
215 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
216
217 // Four equivalent 90 degree rotations are an identity.
218
219 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
220 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
221 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
222 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
223
224 CompareItems( item.get(), aOriginalItem );
225 } );
226 }
227 }
228}
229
230
231BOOST_AUTO_TEST_CASE( FlipLeftRight )
232{
233 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
234 {
235 KICAD_T type = static_cast<KICAD_T>( i );
236
237 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
238
239 if( item == nullptr )
240 continue;
241
242 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
243 {
244 IterateOverPositionsAndReferences<BOARD_ITEM>(
245 item.get(),
246 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
247 {
248 // FIXME: Update() has to be called after SetPosition() to update dimension
249 // shapes.
250 PCB_DIMENSION_BASE* originalDimension =
251 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
252
253 if( originalDimension != nullptr )
254 originalDimension->Update();
255
256 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
257
258 // Two equivalent flips are an identity.
259
260 item->Flip( aRef, true );
261 item->Flip( aRef, true );
262
263 CompareItems( item.get(), aOriginalItem );
264 } );
265 }
266 }
267}
268
269
271{
272 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
273 {
274 KICAD_T type = static_cast<KICAD_T>( i );
275
276 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
277
278 if( item == nullptr )
279 continue;
280
281 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
282 {
283 IterateOverPositionsAndReferences<BOARD_ITEM>(
284 item.get(),
285 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
286 {
287 // FIXME: Update() has to be called after SetPosition() to update dimension
288 // shapes.
289 PCB_DIMENSION_BASE* originalDimension =
290 dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem );
291
292 if( originalDimension != nullptr )
293 originalDimension->Update();
294
295 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate() );
296
297 // Two equivalent flips are an identity.
298
299 item->Flip( aRef, false );
300 item->Flip( aRef, false );
301
302 CompareItems( item.get(), aOriginalItem );
303 } );
304 }
305 }
306}
307
308
309BOOST_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:77
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:90
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
coord_type GetTop() const
Definition: box2.h:195
coord_type GetRight() const
Definition: box2.h:190
coord_type GetLeft() const
Definition: box2.h:194
coord_type GetBottom() const
Definition: box2.h:191
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:239
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:74
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: footprint.h:669
Definition: pad.h:58
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:72
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:805
@ DRCE_MALFORMED_COURTYARD
Definition: drc_item.h:63
Class to handle a set of BOARD_ITEMs.
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_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:102
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:99
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:94
@ MAX_STRUCT_TYPE_ID
Definition: typeinfo.h:240
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:100
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:107
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:92
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:104
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:105
@ 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:96
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:103
@ 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:98
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:95
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:106
@ 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:101
constexpr bool IsPcbnewType(const KICAD_T aType)
Definition: typeinfo.h:427
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition: typeinfo.h:311
#define BOOST_TEST_CONTEXT(A)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588