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>
28#include <board_item.h>
29#include <footprint.h>
30#include <pad.h>
31#include <pcb_shape.h>
32#include <pcb_barcode.h>
33#include <pcb_text.h>
34#include <pcb_textbox.h>
35#include <pcb_drill_chart.h>
36#include <pcb_table.h>
37#include <pcb_tablecell.h>
38#include <pcb_reference_image.h>
39#include <zone.h>
40#include <pcb_track.h>
41#include <pcb_marker.h>
42#include <pcb_dimension.h>
43#include <pcb_point.h>
44#include <pcb_target.h>
45#include <pcb_group.h>
46#include <pcb_grid_item.h>
47#include <pcb_board_outline.h>
48#include <properties/property.h>
50
52{
53public:
56 std::shared_ptr<DRC_ITEM> m_drcItem;
58
66
68 {
69 m_text.SetParentGroup( nullptr );
70 }
71
73 {
74 if( !IsPcbnewType( aType ) )
75 return nullptr;
76
77 if( !IsInstantiableType( aType ) )
78 return nullptr;
79
80 switch( aType )
81 {
82 case PCB_FOOTPRINT_T: return new FOOTPRINT( &m_board );
83 case PCB_PAD_T: return new PAD( &m_footprint );
84 case PCB_FIELD_T: return new PCB_FIELD( &m_footprint, FIELD_T::USER );
85 case PCB_SHAPE_T: return new PCB_SHAPE( &m_board );
86
87 case PCB_BARCODE_T:
88 {
89 PCB_BARCODE* barcode = new PCB_BARCODE( &m_board );
90 barcode->SetText( "XXXX" );
91 barcode->AssembleBarcode();
92 return barcode;
93 }
94
95 case PCB_TEXT_T: return new PCB_TEXT( &m_board );
96 case PCB_TEXTBOX_T: return new PCB_TEXTBOX( &m_board );
97 case PCB_TABLECELL_T: return new PCB_TABLECELL( &m_board );
98
99 case PCB_TABLE_T:
100 {
101 PCB_TABLE* table = new PCB_TABLE( &m_board, pcbIUScale.mmToIU( 0.1 ) );
102
103 const int colWidths[2] = { pcbIUScale.mmToIU( 20.0 ), pcbIUScale.mmToIU( 30.0 ) };
104 const int rowHeights[2] = { pcbIUScale.mmToIU( 5.0 ), pcbIUScale.mmToIU( 7.0 ) };
105
106 table->SetColCount( 2 );
107 table->SetColWidth( 0, colWidths[0] );
108 table->SetColWidth( 1, colWidths[1] );
109 table->SetRowHeight( 0, rowHeights[0] );
110 table->SetRowHeight( 1, rowHeights[1] );
111
112 int y = 0;
113
114 for( int row = 0; row < 2; ++row )
115 {
116 int x = 0;
117
118 for( int col = 0; col < 2; ++col )
119 {
120 PCB_TABLECELL* cell = new PCB_TABLECELL( &m_board );
121 cell->SetRectangleHeight( 0 );
122 cell->SetRectangleWidth( 0 );
123 cell->SetStart( VECTOR2I( x, y ) );
124 cell->SetEnd( VECTOR2I( x + colWidths[col], y + rowHeights[row] ) );
125 table->AddCell( cell );
126
127 x += colWidths[col];
128 }
129
130 y += rowHeights[row];
131 }
132
133 return table;
134 }
135
137 case PCB_TRACE_T: return new PCB_TRACK( &m_board );
138 case PCB_VIA_T: return new PCB_VIA( &m_board );
139 case PCB_ARC_T: return new PCB_ARC( &m_board );
140 case PCB_MARKER_T: return new PCB_MARKER( m_drcItem, VECTOR2I( 0, 0 ) );
142 case PCB_DIM_LEADER_T: return new PCB_DIM_LEADER( &m_board );
143 case PCB_DIM_CENTER_T: return new PCB_DIM_CENTER( &m_board );
144 case PCB_DIM_RADIAL_T: return new PCB_DIM_RADIAL( &m_board );
146 case PCB_TARGET_T: return new PCB_TARGET( &m_board );
147 case PCB_POINT_T: return new PCB_POINT( &m_board );
148 case PCB_GRID_ITEM_T: return new PCB_GRID_ITEM( &m_board );
149
151 {
152 PCB_DRILL_CHART* chart = new PCB_DRILL_CHART( &m_board );
153
154 const int colWidths[2] = { pcbIUScale.mmToIU( 20.0 ), pcbIUScale.mmToIU( 30.0 ) };
155 const int rowHeights[2] = { pcbIUScale.mmToIU( 5.0 ), pcbIUScale.mmToIU( 7.0 ) };
156
157 chart->SetColCount( 2 );
158 chart->SetColWidth( 0, colWidths[0] );
159 chart->SetColWidth( 1, colWidths[1] );
160 chart->SetRowHeight( 0, rowHeights[0] );
161 chart->SetRowHeight( 1, rowHeights[1] );
162
163 int y = 0;
164
165 for( int row = 0; row < 2; ++row )
166 {
167 int x = 0;
168
169 for( int col = 0; col < 2; ++col )
170 {
171 PCB_TABLECELL* cell = new PCB_TABLECELL( &m_board );
172 cell->SetRectangleHeight( 0 );
173 cell->SetRectangleWidth( 0 );
174 cell->SetStart( VECTOR2I( x, y ) );
175 cell->SetEnd( VECTOR2I( x + colWidths[col], y + rowHeights[row] ) );
176 chart->AddCell( cell );
177
178 x += colWidths[col];
179 }
180
181 y += rowHeights[row];
182 }
183
184 return chart;
185 }
186
187 case PCB_ZONE_T:
188 {
189 ZONE* zone = new ZONE( &m_board );
190
191 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
192 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( -100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
193 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 50 ) ), -1 );
194 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( -50 ) ), -1 );
195
196 return zone;
197 }
198
199 case PCB_GROUP_T:
200 {
201 PCB_GROUP* group = new PCB_GROUP( &m_board );
202
203 // Group position only makes sense if there's at least one item in the group.
204 group->AddItem( &m_text );
205
206 return group;
207 }
208
209 case PCB_T:
210 case PCB_ITEM_LIST_T:
211 case PCB_NETINFO_T:
212 case PCB_GENERATOR_T:
213 case PCB_CONSTRAINT_T: // geometry-free; geometric behavior covered by ConstraintSolverItem
215 case PCB_DRILL_MAP_T: // configuration only; its symbols are drawn by the holes
216 return nullptr;
217
218 default:
219 BOOST_FAIL( wxString::Format( "Unhandled type: %d (if you created a new type you need to handle it in "
220 "this switch statement)",
221 aType ) );
222 return nullptr;
223 }
224 }
225
226 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
227 {
228 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
229 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(), aOriginalItem->GetBoundingBox().GetTop() );
230 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(), aOriginalItem->GetBoundingBox().GetLeft() );
231 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(), aOriginalItem->GetBoundingBox().GetBottom() );
232 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(), aOriginalItem->GetBoundingBox().GetRight() );
233 }
234};
235
236
237BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
238
239
241{
242 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
243 {
244 KICAD_T type = static_cast<KICAD_T>( i );
245
246 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
247
248 if( item == nullptr )
249 continue;
250
251 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
252 {
253 BOOST_CHECK( !ENUM_MAP<KICAD_T>::Instance().ToString( type ).IsEmpty() );
254 }
255 }
256}
257
258
260{
261 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
262 {
263 KICAD_T type = static_cast<KICAD_T>( i );
264
265 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
266
267 if( item == nullptr )
268 continue;
269
270 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
271 {
273 item.get(),
274 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
275 {
276 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
277 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
278 dimension->Update();
279
280 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
281 VECTOR2I originalPos = item->GetPosition();
282
283 // Move to a point, then go back.
284 // This has to be an identity transformation.
285
286 item->Move( aRef );
287 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
288
289 item->Move( -aRef );
290 CompareItems( item.get(), aOriginalItem );
291 } );
292 }
293 }
294}
295
296
298{
299 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
300 {
301 KICAD_T type = static_cast<KICAD_T>( i );
302
303 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
304
305 if( item == nullptr )
306 continue;
307
308 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
309 {
310 // Four same 90 degree rotations are an identity.
311
313 item.get(),
314 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
315 {
316 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
317 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
318 dimension->Update();
319 else if( PCB_BARCODE* barcode = dynamic_cast<PCB_BARCODE*>( aOriginalItem ) )
320 barcode->AssembleBarcode();
321
322 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
323
324 // Four equivalent 90 degree rotations are an identity.
325
326 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
327 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
328 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
329 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
330
331 CompareItems( item.get(), aOriginalItem );
332 } );
333 }
334 }
335}
336
337
338BOOST_AUTO_TEST_CASE( FlipLeftRight )
339{
340 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
341 {
342 KICAD_T type = static_cast<KICAD_T>( i );
343
344 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
345
346 if( item == nullptr )
347 continue;
348
349 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
350 {
352 item.get(),
353 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
354 {
355 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
356 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
357 dimension->Update();
358
359 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
360
361 // Two equivalent flips are an identity.
362
363 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
364 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
365
366 CompareItems( item.get(), aOriginalItem );
367 } );
368 }
369 }
370}
371
372
374{
375 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
376 {
377 KICAD_T type = static_cast<KICAD_T>( i );
378
379 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
380
381 if( item == nullptr )
382 continue;
383
384 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
385 {
387 item.get(),
388 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
389 {
390 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
391 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
392 dimension->Update();
393
394 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
395
396 // Two equivalent flips are an identity.
397
398 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
399 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
400
401 CompareItems( item.get(), aOriginalItem );
402 } );
403 }
404 }
405}
406
407
408// Two columns and two rows, built at the origin and then turned. Each cell carries its grid
409// position as text so a test can say where it ended up.
410static PCB_TABLE* makeTable( BOARD& aBoard, const int aColWidths[2], const int aRowHeights[2], double aDegrees )
411{
412 PCB_TABLE* table = new PCB_TABLE( &aBoard, pcbIUScale.mmToIU( 0.1 ) );
413
414 table->SetLayer( F_SilkS );
415 table->SetColCount( 2 );
416
417 for( int ii = 0; ii < 2; ++ii )
418 {
419 table->SetColWidth( ii, aColWidths[ii] );
420 table->SetRowHeight( ii, aRowHeights[ii] );
421 }
422
423 int y = 0;
424
425 for( int row = 0; row < 2; ++row )
426 {
427 int x = 0;
428
429 for( int col = 0; col < 2; ++col )
430 {
431 PCB_TABLECELL* cell = new PCB_TABLECELL( &aBoard );
432 cell->SetStart( VECTOR2I( x, y ) );
433 cell->SetEnd( VECTOR2I( x + aColWidths[col], y + aRowHeights[row] ) );
434 cell->SetText( wxString::Format( wxT( "%c%d" ), 'A' + col, row + 1 ) );
435 table->AddCell( cell );
436
437 x += aColWidths[col];
438 }
439
440 y += aRowHeights[row];
441 }
442
443 table->Normalize();
444 aBoard.Add( table );
445
446 if( aDegrees != 0.0 )
447 table->Rotate( table->GetPosition(), EDA_ANGLE( aDegrees, DEGREES_T ) );
448
449 return table;
450}
451
452
453// Flipping a cell turns its text 180 degrees, and the grid is laid out in the frame the cells
454// read in. A top to bottom flip that renumbers its rows as well undoes that turn and swaps the
455// columns instead.
456BOOST_AUTO_TEST_CASE( TableFlipSwapsTheChosenAxis )
457{
458 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 10.0 ) };
459 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 4.0 ) };
460
461 // Which cell sits in a corner, found by where it ended up rather than by its index.
462 auto textAt = []( PCB_TABLE* aTable, bool aRight, bool aBottom )
463 {
464 BOX2I box;
465
466 for( PCB_TABLECELL* cell : aTable->GetCells() )
467 {
468 box.Merge( cell->GetStart() );
469 box.Merge( cell->GetEnd() );
470 }
471
472 VECTOR2I mid = box.GetCenter();
473
474 for( PCB_TABLECELL* cell : aTable->GetCells() )
475 {
476 VECTOR2I centre = ( cell->GetStart() + cell->GetEnd() ) / 2;
477
478 if( ( centre.x > mid.x ) == aRight && ( centre.y > mid.y ) == aBottom )
479 return cell->GetText();
480 }
481
482 return wxString();
483 };
484
485 BOOST_TEST_CONTEXT( "left/right" )
486 {
487 PCB_TABLE* table = makeTable( m_board, cols, rows, 0.0 );
488
489 table->Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::LEFT_RIGHT );
490
491 BOOST_CHECK_EQUAL( textAt( table, false, false ), wxString( wxT( "B1" ) ) );
492 BOOST_CHECK_EQUAL( textAt( table, true, false ), wxString( wxT( "A1" ) ) );
493 BOOST_CHECK_EQUAL( textAt( table, false, true ), wxString( wxT( "B2" ) ) );
494 BOOST_CHECK_EQUAL( textAt( table, true, true ), wxString( wxT( "A2" ) ) );
495 }
496
497 BOOST_TEST_CONTEXT( "top/bottom" )
498 {
499 PCB_TABLE* table = makeTable( m_board, cols, rows, 0.0 );
500
501 table->Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::TOP_BOTTOM );
502
503 BOOST_CHECK_EQUAL( textAt( table, false, false ), wxString( wxT( "A2" ) ) );
504 BOOST_CHECK_EQUAL( textAt( table, true, false ), wxString( wxT( "B2" ) ) );
505 BOOST_CHECK_EQUAL( textAt( table, false, true ), wxString( wxT( "A1" ) ) );
506 BOOST_CHECK_EQUAL( textAt( table, true, true ), wxString( wxT( "B1" ) ) );
507 }
508}
509
510
511// The table's box is used for hit testing and for working out where a flip should land it, so
512// it has to cover every cell and not just the two on one diagonal.
513BOOST_AUTO_TEST_CASE( TableBoundingBoxCoversEveryCell )
514{
515 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 30.0 ) };
516 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 12.0 ) };
517
518 for( double degrees : { 0.0, 30.0, 45.0, 90.0 } )
519 {
520 BOOST_TEST_CONTEXT( "turned " << degrees )
521 {
522 PCB_TABLE* table = makeTable( m_board, cols, rows, degrees );
523 BOX2I box = table->GetBoundingBox();
524
525 for( PCB_TABLECELL* cell : table->GetCells() )
526 {
527 BOOST_CHECK_MESSAGE(
528 box.Contains( cell->GetBoundingBox() ),
529 "cell ( " << cell->GetBoundingBox().GetLeft() << ", " << cell->GetBoundingBox().GetTop()
530 << " ) to ( " << cell->GetBoundingBox().GetRight() << ", "
531 << cell->GetBoundingBox().GetBottom() << " ) sticks out of the table box ( "
532 << box.GetLeft() << ", " << box.GetTop() << " ) to ( " << box.GetRight() << ", "
533 << box.GetBottom() << " )" );
534 }
535 }
536 }
537}
538
539
540// A turned text box keeps its rectangle square to the board and carries the turn in its text
541// angle. Its box is what clicking and selection are judged against, so it has to cover the
542// corners the box is actually drawn with.
543BOOST_AUTO_TEST_CASE( TextBoxBoundingBoxFollowsItsRotation )
544{
545 for( double degrees : { 0.0, 30.0, 45.0, 90.0 } )
546 {
547 BOOST_TEST_CONTEXT( "turned " << degrees )
548 {
549 PCB_TEXTBOX* box = new PCB_TEXTBOX( &m_board );
550
551 box->SetLayer( F_SilkS );
552 box->SetStart( VECTOR2I( 0, 0 ) );
553 box->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 40.0 ), pcbIUScale.mmToIU( 8.0 ) ) );
554 box->SetTextAngle( EDA_ANGLE( degrees, DEGREES_T ) );
555 m_board.Add( box );
556
557 BOX2I bbox = box->GetBoundingBox();
558
559 for( const VECTOR2I& corner : box->GetCorners() )
560 {
561 BOOST_CHECK_MESSAGE( bbox.Contains( corner ),
562 "corner ( " << corner.x << ", " << corner.y << " ) is outside the box ( "
563 << bbox.GetLeft() << ", " << bbox.GetTop() << " ) to ( "
564 << bbox.GetRight() << ", " << bbox.GetBottom() << " )" );
565 }
566 }
567 }
568}
569
570
571// A flip mirrors an item about a board axis, which tilts it the way PCB_TEXTBOX::Mirror already
572// states: 180 minus the angle across a vertical axis, minus the angle across a horizontal one.
573// A table has to follow that too. Reading direction is free, because a table at one angle is the
574// same picture as one at that angle plus 180 with its cells reordered, so compare the lines.
575BOOST_AUTO_TEST_CASE( TableFlipTiltsTheWayEverythingElseDoes )
576{
577 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 30.0 ) };
578 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 12.0 ) };
579
580 const VECTOR2I point( pcbIUScale.mmToIU( 100.0 ), pcbIUScale.mmToIU( 50.0 ) );
581
582 auto sameLine = []( const EDA_ANGLE& aFirst, const EDA_ANGLE& aSecond )
583 {
584 double apart = std::fmod( std::abs( aFirst.AsDegrees() - aSecond.AsDegrees() ), 180.0 );
585 return apart < 0.01 || apart > 179.99;
586 };
587
588 for( double degrees : { 30.0, 45.0 } )
589 {
591 {
592 BOOST_TEST_CONTEXT( "turned " << degrees
593 << ( dir == FLIP_DIRECTION::LEFT_RIGHT ? " left/right" : " top/bottom" ) )
594 {
595 PCB_TABLE* table = makeTable( m_board, cols, rows, degrees );
596 EDA_ANGLE tilt( degrees, DEGREES_T );
597 EDA_ANGLE reflected = dir == FLIP_DIRECTION::LEFT_RIGHT ? ANGLE_180 - tilt : -tilt;
598
599 std::vector<VECTOR2I> before;
600
601 for( PCB_TABLECELL* cell : table->GetCells() )
602 before.push_back( cell->GetStart() );
603
604 table->Flip( point, dir );
605
606 BOOST_CHECK_MESSAGE( sameLine( table->GetCell( 0, 0 )->GetTextAngle(), reflected ),
607 "table came back at " << table->GetCell( 0, 0 )->GetTextAngle().AsDegrees()
608 << " degrees, expected " << reflected.AsDegrees() );
609
610 // Flipping back about the same point has to undo it exactly.
611 table->Flip( point, dir );
612
613 for( size_t ii = 0; ii < before.size(); ++ii )
614 {
615 BOOST_CHECK_MESSAGE( ( table->GetCells()[ii]->GetStart() - before[ii] ).EuclideanNorm()
616 <= pcbIUScale.mmToIU( 0.001 ),
617 "cell " << ii << " did not come back" );
618 }
619 }
620 }
621 }
622}
623
624
631BOOST_AUTO_TEST_CASE( Issue23234_CustomPadstackFlip )
632{
633 // Create a board with two copper layers so Flip works correctly
634 BOARD board;
635 FOOTPRINT footprint( &board );
636 PAD pad( &footprint );
637
638 // Set up a circular SMD pad on F_Cu (NORMAL padstack mode)
639 pad.SetAttribute( PAD_ATTRIB::SMD );
640 pad.SetPadstackMode( PADSTACK::MODE::NORMAL );
642 pad.SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( 500000, 500000 ) );
643 LSET smd_layers;
644 smd_layers.set( F_Cu );
645 pad.SetLayerSet( smd_layers );
646
647 // Switch padstack to CUSTOM mode (as the dialog does when user selects "Custom")
648 pad.Padstack().SetMode( PADSTACK::MODE::CUSTOM );
649
650 // Now flip the pad (as TransferDataFromWindow does for pads on flipped footprints).
651 // This renames the F_Cu key in m_copperProps to B_Cu.
652 // After this, ALL_LAYERS (= F_Cu) is no longer in m_copperProps.
653 pad.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::TOP_BOTTOM );
654
655 // These calls must NOT throw std::out_of_range.
656 // Before the fix, CopperLayer( ALL_LAYERS ) called m_copperProps.at( F_Cu ) which threw
657 // because F_Cu was not in the map (it had been renamed to B_Cu by FlipLayers).
658 BOOST_CHECK_NO_THROW( pad.GetShape( PADSTACK::ALL_LAYERS ) );
659 BOOST_CHECK_NO_THROW( pad.GetSize( PADSTACK::ALL_LAYERS ) );
660 BOOST_CHECK_NO_THROW( pad.Padstack().EffectiveLayerFor( PADSTACK::ALL_LAYERS ) );
661
662 // Verify the returned shape is sane (the B_Cu props, which were originally F_Cu props)
663 BOOST_CHECK( pad.GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE );
664}
665
666
667// Regression test for issue #24696: a grouped zone left its group after undo/redo of a fill.
668// SwapItemData() (used by undo/redo and commit revert) must not move group membership, which
669// is a structural back-reference rather than swappable item data.
670BOOST_AUTO_TEST_CASE( Issue24696_SwapItemDataKeepsGroupMembership )
671{
672 PCB_GROUP group( &m_board );
673 ZONE live( &m_board );
674 ZONE image( &m_board );
675
676 // Mirror the undo swap: BOARD::Remove() has already stripped the live item's group,
677 // while the undo image still carries the membership.
678 live.SetParentGroup( nullptr );
679 image.SetParentGroup( &group );
680
681 live.SwapItemData( &image );
682
683 BOOST_CHECK( live.GetParentGroup() == nullptr );
684 BOOST_CHECK( image.GetParentGroup() == &group );
685
686 image.SetParentGroup( nullptr );
687}
688
689
690// Partial hardening for the BOARD::RecordDRCExclusions crash family (Sentry KICAD-YT2,
691// KICAD-YTA). A PCB_MARKER may legitimately carry a null RC_ITEM (its ctor and dtor both guard
692// the member), but DRC_EXCLUSION::FromMarker() dereferences it unconditionally, so recording exclusions
693// during a project save or window close faulted on such a marker.
694BOOST_AUTO_TEST_CASE( RecordDRCExclusionsSkipsMarkerWithoutRCItem )
695{
696 BOARD board;
697
698 PCB_MARKER* marker = new PCB_MARKER( nullptr, VECTOR2I( 0, 0 ) );
699 marker->SetExcluded( true );
700 board.Add( marker );
701
702 BOOST_CHECK_NO_THROW( board.RecordDRCExclusions() );
703
704 // The item-less marker has no violation to serialize, so nothing is persisted.
705 BOOST_CHECK( board.GetDesignSettings().m_DrcExclusions.empty() );
706}
707
708
709BOOST_AUTO_TEST_CASE( ResolveItemIdentityCachePurgedOnDestruction )
710{
711 BOARD board;
712
713 FOOTPRINT* footprint = new FOOTPRINT( &board );
714 board.Add( footprint );
715
716 PAD* pad = new PAD( footprint );
717 footprint->Pads().push_back( pad );
718 board.CacheItemById( pad );
719
720 const KIID padId = pad->m_Uuid;
721
723 BOOST_REQUIRE_EQUAL( board.ResolveItem( padId, true ), static_cast<BOARD_ITEM*>( pad ) );
724
725 // Detach the pad from the footprint without FOOTPRINT::Remove()'s surgical eviction, leaving it
726 // parented to the still-board-attached footprint, then free it directly. This stands in for the
727 // producer paths that free a board-parented item without touching the identity cache; only the
728 // ~BOARD_ITEM safety net can then keep ResolveItem() from returning a freed pointer.
729 std::deque<PAD*>& pads = footprint->Pads();
730 pads.erase( std::find( pads.begin(), pads.end(), pad ) );
731 delete pad;
732
733 BOOST_CHECK( board.ResolveItem( padId, true ) == nullptr );
734}
735
736
737// A never-indexed item parented to a freed board must not touch that board on destruction, the
738// crash from the "KiCad master crashes" report (follow-up to ac12a1c820).
739BOOST_AUTO_TEST_CASE( UncachedItemSurvivesBoardDestruction )
740{
741 BOARD* board = new BOARD();
742 PCB_VIA* dummy = new PCB_VIA( board );
743
744 BOOST_REQUIRE( !dummy->IsIndexedInBoard() );
745
746 delete board;
747
748 BOOST_CHECK_NO_THROW( delete dummy );
749}
750
751
752// The indexed counterpart of the case above. ~BOARD must clear the membership flag as it drops
753// the index, or a survivor still believes it is indexed and walks its parent chain into the
754// freed board.
755BOOST_AUTO_TEST_CASE( IndexedItemSurvivesBoardDestruction )
756{
757 BOARD* board = new BOARD();
758 FOOTPRINT* footprint = new FOOTPRINT( board );
759
760 board->Add( footprint );
761
762 PAD* pad = new PAD( footprint );
763 footprint->Pads().push_back( pad );
764 board->CacheItemById( pad );
765
766 BOOST_REQUIRE( pad->IsIndexedInBoard() );
767
768 // Detach without FOOTPRINT::Remove() so the board frees the footprint while the pad lives on,
769 // still parented to it. This is the ownership hand-off the safety net in ~BOARD_ITEM covers.
770 std::deque<PAD*>& pads = footprint->Pads();
771 pads.erase( std::find( pads.begin(), pads.end(), pad ) );
772
773 delete board;
774
775 BOOST_CHECK( !pad->IsIndexedInBoard() );
776 BOOST_CHECK_NO_THROW( delete pad );
777}
778
779
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
BASE_SET & set(size_t pos)
Definition base_set.h:126
std::set< DRC_EXCLUSION, DRC_EXCLUSION_COMPARE > m_DrcExclusions
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
void CacheItemById(BOARD_ITEM *aItem) const
Add an item to the item-by-id cache.
Definition board.cpp:2265
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1497
void RecordDRCExclusions()
Scan existing markers and record data from any that are Excluded.
Definition board.cpp:569
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1299
bool IsItemIndexedById(const BOARD_ITEM *aItem) const
Definition board.h:1694
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:2116
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:653
constexpr const Vec GetCenter() const
Definition box2.h:227
constexpr coord_type GetLeft() const
Definition box2.h:225
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:165
constexpr coord_type GetRight() const
Definition box2.h:214
constexpr coord_type GetTop() const
Definition box2.h:226
constexpr coord_type GetBottom() const
Definition box2.h:219
double AsDegrees() const
Definition eda_angle.h:116
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:270
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
virtual void SetParentGroup(EDA_GROUP *aGroup)
Definition eda_item.h:115
void SetRectangleHeight(const int &aHeight)
void SetRectangleWidth(const int &aWidth)
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:231
static ENUM_MAP< T > & Instance()
Definition property.h:770
std::deque< PAD * > & Pads()
Definition footprint.h:404
Definition kiid.h:46
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition marker_base.h:90
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:170
@ CUSTOM
Shapes can be defined on arbitrary layers.
Definition padstack.h:172
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:179
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 drill chart placed on the board, kept in step with the holes.
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:51
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.
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
void SetColWidth(int aCol, int aWidth)
Definition pcb_table.h:130
std::vector< PCB_TABLECELL * > GetCells() const
Definition pcb_table.h:160
void AddCell(PCB_TABLECELL *aCell)
Definition pcb_table.h:165
void SetColCount(int aCount)
Definition pcb_table.h:120
void SetRowHeight(int aRow, int aHeight)
Definition pcb_table.h:140
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
void SetTextAngle(const EDA_ANGLE &aAngle) override
std::vector< VECTOR2I > GetCorners() const override
Return 4 corners for a rectangle or rotated rectangle (stored as a poly).
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(const 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:1441
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:70
@ DEGREES_T
Definition eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:426
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
@ F_SilkS
Definition layer_ids.h:96
@ F_Cu
Definition layer_ids.h:60
FLIP_DIRECTION
Definition mirror.h:23
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:24
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:25
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:411
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:98
BARCODE class definition.
Class to handle a set of BOARD_ITEMs.
std::vector< FAB_LAYER_COLOR > dummy
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
static PCB_TABLE * makeTable(BOARD &aBoard, const int aColWidths[2], const int aRowHeights[2], double aDegrees)
BOOST_AUTO_TEST_CASE(Type)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
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:70
@ PCB_T
Definition typeinfo.h:74
@ PCB_CONSTRAINT_T
a geometric constraint between board items
Definition typeinfo.h:237
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:98
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:95
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:83
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:243
@ PCB_DRILL_MAP_T
class PCB_DRILL_MAP, drill symbols drawn at the holes
Definition typeinfo.h:240
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:96
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:103
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:81
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition typeinfo.h:101
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition typeinfo.h:91
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:99
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_GRID_ITEM_T
a subgrid placed on a board
Definition typeinfo.h:238
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_BOARD_OUTLINE_T
class PCB_BOARD_OUTLINE_T, a pcb board outline item
Definition typeinfo.h:104
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:86
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition typeinfo.h:102
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:105
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:97
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
constexpr bool IsPcbnewType(const KICAD_T aType)
Definition typeinfo.h:439
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:322
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683