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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <eda_item_test_utils.h>
22#include <core/typeinfo.h>
23#include <drc/drc_item.h>
24
25// Code under test
26#include <board.h>
27#include <board_item.h>
28#include <footprint.h>
29#include <pad.h>
30#include <pcb_shape.h>
31#include <pcb_barcode.h>
32#include <pcb_text.h>
33#include <pcb_textbox.h>
34#include <pcb_table.h>
35#include <pcb_tablecell.h>
36#include <pcb_reference_image.h>
37#include <zone.h>
38#include <pcb_track.h>
39#include <pcb_marker.h>
40#include <pcb_dimension.h>
41#include <pcb_point.h>
42#include <pcb_target.h>
43#include <pcb_group.h>
44#include <pcb_board_outline.h>
45#include <properties/property.h>
47
49{
50public:
53 std::shared_ptr<DRC_ITEM> m_drcItem;
55
63
65 {
66 m_text.SetParentGroup( nullptr );
67 }
68
70 {
71 if( !IsPcbnewType( aType ) )
72 return nullptr;
73
74 if( !IsInstantiableType( aType ) )
75 return nullptr;
76
77 switch( aType )
78 {
79 case PCB_FOOTPRINT_T: return new FOOTPRINT( &m_board );
80 case PCB_PAD_T: return new PAD( &m_footprint );
81 case PCB_FIELD_T: return new PCB_FIELD( &m_footprint, FIELD_T::USER );
82 case PCB_SHAPE_T: return new PCB_SHAPE( &m_board );
83
84 case PCB_BARCODE_T:
85 {
86 PCB_BARCODE* barcode = new PCB_BARCODE( &m_board );
87 barcode->SetText( "XXXX" );
88 barcode->AssembleBarcode();
89 return barcode;
90 }
91
92 case PCB_TEXT_T: return new PCB_TEXT( &m_board );
93 case PCB_TEXTBOX_T: return new PCB_TEXTBOX( &m_board );
94 case PCB_TABLECELL_T: return new PCB_TABLECELL( &m_board );
95
96 case PCB_TABLE_T:
97 {
98 PCB_TABLE* table = new PCB_TABLE( &m_board, pcbIUScale.mmToIU( 0.1 ) );
99
100 table->SetColCount( 2 );
101
102 for( int ii = 0; ii < 4; ++ii )
103 {
104 PCB_TABLECELL* cell = new PCB_TABLECELL( &m_board );
105 cell->SetRectangleHeight( 0 );
106 cell->SetRectangleWidth( 0 );
107 table->InsertCell( ii, cell );
108 }
109
110 return table;
111 }
112
114 case PCB_TRACE_T: return new PCB_TRACK( &m_board );
115 case PCB_VIA_T: return new PCB_VIA( &m_board );
116 case PCB_ARC_T: return new PCB_ARC( &m_board );
117 case PCB_MARKER_T: return new PCB_MARKER( m_drcItem, VECTOR2I( 0, 0 ) );
119 case PCB_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_board );
120 case PCB_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_board );
121 case PCB_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_board );
123 case PCB_TARGET_T: return new PCB_TARGET( &m_board );
124 case PCB_POINT_T: return new PCB_POINT( &m_board );
125
126 case PCB_ZONE_T:
127 {
128 ZONE* zone = new ZONE( &m_board );
129
130 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
131 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
132 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
133 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
134
135 return zone;
136 }
137
138 case PCB_GROUP_T:
139 {
140 PCB_GROUP* group = new PCB_GROUP( &m_board );
141
142 // Group position only makes sense if there's at least one item in the group.
143 group->AddItem( &m_text );
144
145 return group;
146 }
147
148 case PCB_T:
149 case PCB_ITEM_LIST_T:
150 case PCB_NETINFO_T:
151 case PCB_GENERATOR_T:
153 return nullptr;
154
155 default:
156 BOOST_FAIL( wxString::Format( "Unhandled type: %d (if you created a new type you need to handle it in "
157 "this switch statement)",
158 aType ) );
159 return nullptr;
160 }
161 }
162
163 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
164 {
165 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
166 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(), aOriginalItem->GetBoundingBox().GetTop() );
167 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(), aOriginalItem->GetBoundingBox().GetLeft() );
168 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(), aOriginalItem->GetBoundingBox().GetBottom() );
169 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(), aOriginalItem->GetBoundingBox().GetRight() );
170 }
171};
172
173
174BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
175
176
178{
179 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
180 {
181 KICAD_T type = static_cast<KICAD_T>( i );
182
183 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
184
185 if( item == nullptr )
186 continue;
187
188 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
189 {
190 BOOST_CHECK( !ENUM_MAP<KICAD_T>::Instance().ToString( type ).IsEmpty() );
191 }
192 }
193}
194
195
197{
198 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
199 {
200 KICAD_T type = static_cast<KICAD_T>( i );
201
202 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
203
204 if( item == nullptr )
205 continue;
206
207 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
208 {
210 item.get(),
211 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
212 {
213 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
214 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
215 dimension->Update();
216
217 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
218 VECTOR2I originalPos = item->GetPosition();
219
220 // Move to a point, then go back.
221 // This has to be an identity transformation.
222
223 item->Move( aRef );
224 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
225
226 item->Move( -aRef );
227 CompareItems( item.get(), aOriginalItem );
228 } );
229 }
230 }
231}
232
233
235{
236 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
237 {
238 KICAD_T type = static_cast<KICAD_T>( i );
239
240 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
241
242 if( item == nullptr )
243 continue;
244
245 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
246 {
247 // Four same 90 degree rotations are an identity.
248
250 item.get(),
251 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
252 {
253 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
254 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
255 dimension->Update();
256 else if( PCB_BARCODE* barcode = dynamic_cast<PCB_BARCODE*>( aOriginalItem ) )
257 barcode->AssembleBarcode();
258
259 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
260
261 // Four equivalent 90 degree rotations are an identity.
262
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 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
267
268 CompareItems( item.get(), aOriginalItem );
269 } );
270 }
271 }
272}
273
274
275BOOST_AUTO_TEST_CASE( FlipLeftRight )
276{
277 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
278 {
279 KICAD_T type = static_cast<KICAD_T>( i );
280
281 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
282
283 if( item == nullptr )
284 continue;
285
286 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
287 {
289 item.get(),
290 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
291 {
292 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
293 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
294 dimension->Update();
295
296 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
297
298 // Two equivalent flips are an identity.
299
300 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
301 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
302
303 CompareItems( item.get(), aOriginalItem );
304 } );
305 }
306 }
307}
308
309
311{
312 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
313 {
314 KICAD_T type = static_cast<KICAD_T>( i );
315
316 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
317
318 if( item == nullptr )
319 continue;
320
321 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
322 {
324 item.get(),
325 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
326 {
327 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
328 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
329 dimension->Update();
330
331 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
332
333 // Two equivalent flips are an identity.
334
335 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
336 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
337
338 CompareItems( item.get(), aOriginalItem );
339 } );
340 }
341 }
342}
343
344
351BOOST_AUTO_TEST_CASE( Issue23234_CustomPadstackFlip )
352{
353 // Create a board with two copper layers so Flip works correctly
354 BOARD board;
355 FOOTPRINT footprint( &board );
356 PAD pad( &footprint );
357
358 // Set up a circular SMD pad on F_Cu (NORMAL padstack mode)
359 pad.SetAttribute( PAD_ATTRIB::SMD );
361 pad.SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( 500000, 500000 ) );
362 LSET smd_layers;
363 smd_layers.set( F_Cu );
364 pad.SetLayerSet( smd_layers );
365
366 // Switch padstack to CUSTOM mode (as the dialog does when user selects "Custom")
367 pad.Padstack().SetMode( PADSTACK::MODE::CUSTOM );
368
369 // Now flip the pad (as TransferDataFromWindow does for pads on flipped footprints).
370 // This renames the F_Cu key in m_copperProps to B_Cu.
371 // After this, ALL_LAYERS (= F_Cu) is no longer in m_copperProps.
372 pad.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::TOP_BOTTOM );
373
374 // These calls must NOT throw std::out_of_range.
375 // Before the fix, CopperLayer( ALL_LAYERS ) called m_copperProps.at( F_Cu ) which threw
376 // because F_Cu was not in the map (it had been renamed to B_Cu by FlipLayers).
377 BOOST_CHECK_NO_THROW( pad.GetShape( PADSTACK::ALL_LAYERS ) );
378 BOOST_CHECK_NO_THROW( pad.GetSize( PADSTACK::ALL_LAYERS ) );
379 BOOST_CHECK_NO_THROW( pad.Padstack().EffectiveLayerFor( PADSTACK::ALL_LAYERS ) );
380
381 // Verify the returned shape is sane (the B_Cu props, which were originally F_Cu props)
382 BOOST_CHECK( pad.GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE );
383}
384
385
386// Regression test for issue #24696: a grouped zone left its group after undo/redo of a fill.
387// SwapItemData() (used by undo/redo and commit revert) must not move group membership, which
388// is a structural back-reference rather than swappable item data.
389BOOST_AUTO_TEST_CASE( Issue24696_SwapItemDataKeepsGroupMembership )
390{
391 PCB_GROUP group( &m_board );
392 ZONE live( &m_board );
393 ZONE image( &m_board );
394
395 // Mirror the undo swap: BOARD::Remove() has already stripped the live item's group,
396 // while the undo image still carries the membership.
397 live.SetParentGroup( nullptr );
398 image.SetParentGroup( &group );
399
400 live.SwapItemData( &image );
401
402 BOOST_CHECK( live.GetParentGroup() == nullptr );
403 BOOST_CHECK( image.GetParentGroup() == &group );
404
405 image.SetParentGroup( nullptr );
406}
407
408
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BASE_SET & set(size_t pos)
Definition base_set.h:116
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
constexpr coord_type GetLeft() const
Definition box2.h:224
constexpr coord_type GetRight() const
Definition box2.h:213
constexpr coord_type GetTop() const
Definition box2.h:225
constexpr coord_type GetBottom() const
Definition box2.h:218
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:135
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
virtual void SetParentGroup(EDA_GROUP *aGroup)
Definition eda_item.h:113
void SetRectangleHeight(const int &aHeight)
void SetRectangleWidth(const int &aWidth)
static ENUM_MAP< T > & Instance()
Definition property.h:721
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
@ CUSTOM
Shapes can be defined on arbitrary layers.
Definition padstack.h:173
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:61
void AssembleBarcode() const
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:49
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:39
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:70
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:1367
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:64
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
@ F_Cu
Definition layer_ids.h:60
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:25
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:99
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()
std::vector< std::vector< std::string > > table
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:71
@ PCB_T
Definition typeinfo.h:75
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:81
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:99
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:96
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:84
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:90
@ 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:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:104
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:86
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:101
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:85
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:82
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition typeinfo.h:102
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:83
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition typeinfo.h:92
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:94
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:100
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:88
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:95
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:80
@ PCB_BOARD_OUTLINE_T
class PCB_BOARD_OUTLINE_T, a pcb board outline item
Definition typeinfo.h:105
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:91
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:87
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition typeinfo.h:103
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:106
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:98
constexpr bool IsPcbnewType(const KICAD_T aType)
Definition typeinfo.h:433
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:316
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683