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 case PCB_3D_BODY_T: // payload only; no 2D geometry of its own
217 return nullptr;
218
219 default:
220 BOOST_FAIL( wxString::Format( "Unhandled type: %d (if you created a new type you need to handle it in "
221 "this switch statement)",
222 aType ) );
223 return nullptr;
224 }
225 }
226
227 static void CompareItems( BOARD_ITEM* aItem, BOARD_ITEM* aOriginalItem )
228 {
229 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
230 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetTop(), aOriginalItem->GetBoundingBox().GetTop() );
231 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetLeft(), aOriginalItem->GetBoundingBox().GetLeft() );
232 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetBottom(), aOriginalItem->GetBoundingBox().GetBottom() );
233 BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetRight(), aOriginalItem->GetBoundingBox().GetRight() );
234 }
235};
236
237
238BOOST_FIXTURE_TEST_SUITE( PcbItem, TEST_BOARD_ITEM_FIXTURE )
239
240
242{
243 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
244 {
245 KICAD_T type = static_cast<KICAD_T>( i );
246
247 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
248
249 if( item == nullptr )
250 continue;
251
252 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
253 {
254 BOOST_CHECK( !ENUM_MAP<KICAD_T>::Instance().ToString( type ).IsEmpty() );
255 }
256 }
257}
258
259
261{
262 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
263 {
264 KICAD_T type = static_cast<KICAD_T>( i );
265
266 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
267
268 if( item == nullptr )
269 continue;
270
271 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
272 {
274 item.get(),
275 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
276 {
277 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
278 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
279 dimension->Update();
280
281 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
282 VECTOR2I originalPos = item->GetPosition();
283
284 // Move to a point, then go back.
285 // This has to be an identity transformation.
286
287 item->Move( aRef );
288 BOOST_CHECK_EQUAL( item->GetPosition(), originalPos + aRef );
289
290 item->Move( -aRef );
291 CompareItems( item.get(), aOriginalItem );
292 } );
293 }
294 }
295}
296
297
299{
300 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
301 {
302 KICAD_T type = static_cast<KICAD_T>( i );
303
304 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
305
306 if( item == nullptr )
307 continue;
308
309 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
310 {
311 // Four same 90 degree rotations are an identity.
312
314 item.get(),
315 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
316 {
317 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
318 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
319 dimension->Update();
320 else if( PCB_BARCODE* barcode = dynamic_cast<PCB_BARCODE*>( aOriginalItem ) )
321 barcode->AssembleBarcode();
322
323 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
324
325 // Four equivalent 90 degree rotations are an identity.
326
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 item->Rotate( aRef, EDA_ANGLE( 90.0, DEGREES_T ) );
331
332 CompareItems( item.get(), aOriginalItem );
333 } );
334 }
335 }
336}
337
338
339BOOST_AUTO_TEST_CASE( FlipLeftRight )
340{
341 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
342 {
343 KICAD_T type = static_cast<KICAD_T>( i );
344
345 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
346
347 if( item == nullptr )
348 continue;
349
350 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
351 {
353 item.get(),
354 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
355 {
356 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
357 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
358 dimension->Update();
359
360 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
361
362 // Two equivalent flips are an identity.
363
364 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
365 item->Flip( aRef, FLIP_DIRECTION::LEFT_RIGHT );
366
367 CompareItems( item.get(), aOriginalItem );
368 } );
369 }
370 }
371}
372
373
375{
376 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
377 {
378 KICAD_T type = static_cast<KICAD_T>( i );
379
380 auto item = std::unique_ptr<BOARD_ITEM>( Instantiate( type ) );
381
382 if( item == nullptr )
383 continue;
384
385 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
386 {
388 item.get(),
389 []( BOARD_ITEM* aOriginalItem, VECTOR2I aRef )
390 {
391 // FIXME: Update() has to be called after SetPosition() to update dimension shapes.
392 if( PCB_DIMENSION_BASE* dimension = dynamic_cast<PCB_DIMENSION_BASE*>( aOriginalItem ) )
393 dimension->Update();
394
395 auto item = std::unique_ptr<BOARD_ITEM>( aOriginalItem->Duplicate( IGNORE_PARENT_GROUP ) );
396
397 // Two equivalent flips are an identity.
398
399 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
400 item->Flip( aRef, FLIP_DIRECTION::TOP_BOTTOM );
401
402 CompareItems( item.get(), aOriginalItem );
403 } );
404 }
405 }
406}
407
408
409BOOST_AUTO_TEST_CASE( BoardlessItemsUseStandardLayerMetadata )
410{
411 PCB_TRACK track( nullptr );
412 track.SetLayer( F_Cu );
413 track.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::LEFT_RIGHT );
414 BOOST_CHECK_EQUAL( track.GetLayer(), B_Cu );
415
416 PCB_ARC arc( nullptr );
417 arc.SetLayer( F_SilkS );
420
421 PCB_TARGET target( nullptr );
422 target.SetLayer( F_Fab );
423 target.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::LEFT_RIGHT );
424 BOOST_CHECK_EQUAL( target.GetLayer(), B_Fab );
425
426 PCB_VIA via( nullptr );
427 via.SetViaType( VIATYPE::BLIND );
428 via.SetLayerPair( F_Cu, In1_Cu );
429 via.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::LEFT_RIGHT );
430
431 PCB_LAYER_ID topLayer;
432 PCB_LAYER_ID bottomLayer;
433 via.LayerPair( &topLayer, &bottomLayer );
434
435 BOOST_CHECK_EQUAL( topLayer, In1_Cu );
436 BOOST_CHECK_EQUAL( bottomLayer, B_Cu );
437 BOOST_CHECK_EQUAL( via.LayerMaskDescribe(), wxT( "In1.Cu - B.Cu" ) );
438
439 PCB_TEXTBOX textbox( nullptr );
440 textbox.SetText( wxT( "${UNKNOWN}" ) );
441 textbox.SetStart( VECTOR2I( 0, 0 ) );
442 textbox.SetEnd( VECTOR2I( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 5 ) ) );
443 BOOST_CHECK_NO_THROW( textbox.GetShownText( FOR_CANVAS ) );
444
445 FOOTPRINT footprint( nullptr );
446 PCB_TABLE table( &footprint );
447 PCB_TABLECELL cell( &table );
448 cell.SetText( wxT( "${UNKNOWN}" ) );
449 BOOST_CHECK_NO_THROW( cell.GetUnwrappedShownText( FOR_CANVAS ) );
450
451 ZONE zone( &footprint );
452 std::vector<ZONE*> sameNetZones;
453 std::vector<ZONE*> otherNetZones;
454 zone.GetInteractingZones( F_Cu, &sameNetZones, &otherNetZones );
455 BOOST_CHECK( sameNetZones.empty() );
456 BOOST_CHECK( otherNetZones.empty() );
457}
458
459
460// Two columns and two rows, built at the origin and then turned. Each cell carries its grid
461// position as text so a test can say where it ended up.
462static PCB_TABLE* makeTable( BOARD& aBoard, const int aColWidths[2], const int aRowHeights[2], double aDegrees )
463{
464 PCB_TABLE* table = new PCB_TABLE( &aBoard, pcbIUScale.mmToIU( 0.1 ) );
465
466 table->SetLayer( F_SilkS );
467 table->SetColCount( 2 );
468
469 for( int ii = 0; ii < 2; ++ii )
470 {
471 table->SetColWidth( ii, aColWidths[ii] );
472 table->SetRowHeight( ii, aRowHeights[ii] );
473 }
474
475 int y = 0;
476
477 for( int row = 0; row < 2; ++row )
478 {
479 int x = 0;
480
481 for( int col = 0; col < 2; ++col )
482 {
483 PCB_TABLECELL* cell = new PCB_TABLECELL( &aBoard );
484 cell->SetStart( VECTOR2I( x, y ) );
485 cell->SetEnd( VECTOR2I( x + aColWidths[col], y + aRowHeights[row] ) );
486 cell->SetText( wxString::Format( wxT( "%c%d" ), 'A' + col, row + 1 ) );
487 table->AddCell( cell );
488
489 x += aColWidths[col];
490 }
491
492 y += aRowHeights[row];
493 }
494
495 table->Normalize();
496 aBoard.Add( table );
497
498 if( aDegrees != 0.0 )
499 table->Rotate( table->GetPosition(), EDA_ANGLE( aDegrees, DEGREES_T ) );
500
501 return table;
502}
503
504
505// Flipping a cell turns its text 180 degrees, and the grid is laid out in the frame the cells
506// read in. A top to bottom flip that renumbers its rows as well undoes that turn and swaps the
507// columns instead.
508BOOST_AUTO_TEST_CASE( TableFlipSwapsTheChosenAxis )
509{
510 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 10.0 ) };
511 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 4.0 ) };
512
513 // Which cell sits in a corner, found by where it ended up rather than by its index.
514 auto textAt = []( PCB_TABLE* aTable, bool aRight, bool aBottom )
515 {
516 BOX2I box;
517
518 for( PCB_TABLECELL* cell : aTable->GetCells() )
519 {
520 box.Merge( cell->GetStart() );
521 box.Merge( cell->GetEnd() );
522 }
523
524 VECTOR2I mid = box.GetCenter();
525
526 for( PCB_TABLECELL* cell : aTable->GetCells() )
527 {
528 VECTOR2I centre = ( cell->GetStart() + cell->GetEnd() ) / 2;
529
530 if( ( centre.x > mid.x ) == aRight && ( centre.y > mid.y ) == aBottom )
531 return cell->GetText();
532 }
533
534 return wxString();
535 };
536
537 BOOST_TEST_CONTEXT( "left/right" )
538 {
539 PCB_TABLE* table = makeTable( m_board, cols, rows, 0.0 );
540
541 table->Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::LEFT_RIGHT );
542
543 BOOST_CHECK_EQUAL( textAt( table, false, false ), wxString( wxT( "B1" ) ) );
544 BOOST_CHECK_EQUAL( textAt( table, true, false ), wxString( wxT( "A1" ) ) );
545 BOOST_CHECK_EQUAL( textAt( table, false, true ), wxString( wxT( "B2" ) ) );
546 BOOST_CHECK_EQUAL( textAt( table, true, true ), wxString( wxT( "A2" ) ) );
547 }
548
549 BOOST_TEST_CONTEXT( "top/bottom" )
550 {
551 PCB_TABLE* table = makeTable( m_board, cols, rows, 0.0 );
552
553 table->Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::TOP_BOTTOM );
554
555 BOOST_CHECK_EQUAL( textAt( table, false, false ), wxString( wxT( "A2" ) ) );
556 BOOST_CHECK_EQUAL( textAt( table, true, false ), wxString( wxT( "B2" ) ) );
557 BOOST_CHECK_EQUAL( textAt( table, false, true ), wxString( wxT( "A1" ) ) );
558 BOOST_CHECK_EQUAL( textAt( table, true, true ), wxString( wxT( "B1" ) ) );
559 }
560}
561
562
563// The table's box is used for hit testing and for working out where a flip should land it, so
564// it has to cover every cell and not just the two on one diagonal.
565BOOST_AUTO_TEST_CASE( TableBoundingBoxCoversEveryCell )
566{
567 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 30.0 ) };
568 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 12.0 ) };
569
570 for( double degrees : { 0.0, 30.0, 45.0, 90.0 } )
571 {
572 BOOST_TEST_CONTEXT( "turned " << degrees )
573 {
574 PCB_TABLE* table = makeTable( m_board, cols, rows, degrees );
575 BOX2I box = table->GetBoundingBox();
576
577 for( PCB_TABLECELL* cell : table->GetCells() )
578 {
579 BOOST_CHECK_MESSAGE(
580 box.Contains( cell->GetBoundingBox() ),
581 "cell ( " << cell->GetBoundingBox().GetLeft() << ", " << cell->GetBoundingBox().GetTop()
582 << " ) to ( " << cell->GetBoundingBox().GetRight() << ", "
583 << cell->GetBoundingBox().GetBottom() << " ) sticks out of the table box ( "
584 << box.GetLeft() << ", " << box.GetTop() << " ) to ( " << box.GetRight() << ", "
585 << box.GetBottom() << " )" );
586 }
587 }
588 }
589}
590
591
592// A turned text box keeps its rectangle square to the board and carries the turn in its text
593// angle. Its box is what clicking and selection are judged against, so it has to cover the
594// corners the box is actually drawn with.
595BOOST_AUTO_TEST_CASE( TextBoxBoundingBoxFollowsItsRotation )
596{
597 for( double degrees : { 0.0, 30.0, 45.0, 90.0 } )
598 {
599 BOOST_TEST_CONTEXT( "turned " << degrees )
600 {
601 PCB_TEXTBOX* box = new PCB_TEXTBOX( &m_board );
602
603 box->SetLayer( F_SilkS );
604 box->SetStart( VECTOR2I( 0, 0 ) );
605 box->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 40.0 ), pcbIUScale.mmToIU( 8.0 ) ) );
606 box->SetTextAngle( EDA_ANGLE( degrees, DEGREES_T ) );
607 m_board.Add( box );
608
609 BOX2I bbox = box->GetBoundingBox();
610
611 for( const VECTOR2I& corner : box->GetCorners() )
612 {
613 BOOST_CHECK_MESSAGE( bbox.Contains( corner ),
614 "corner ( " << corner.x << ", " << corner.y << " ) is outside the box ( "
615 << bbox.GetLeft() << ", " << bbox.GetTop() << " ) to ( "
616 << bbox.GetRight() << ", " << bbox.GetBottom() << " )" );
617 }
618 }
619 }
620}
621
622
623// A flip mirrors an item about a board axis, which tilts it the way PCB_TEXTBOX::Mirror already
624// states: 180 minus the angle across a vertical axis, minus the angle across a horizontal one.
625// A table has to follow that too. Reading direction is free, because a table at one angle is the
626// same picture as one at that angle plus 180 with its cells reordered, so compare the lines.
627BOOST_AUTO_TEST_CASE( TableFlipTiltsTheWayEverythingElseDoes )
628{
629 const int cols[2] = { pcbIUScale.mmToIU( 10.0 ), pcbIUScale.mmToIU( 30.0 ) };
630 const int rows[2] = { pcbIUScale.mmToIU( 4.0 ), pcbIUScale.mmToIU( 12.0 ) };
631
632 const VECTOR2I point( pcbIUScale.mmToIU( 100.0 ), pcbIUScale.mmToIU( 50.0 ) );
633
634 auto sameLine = []( const EDA_ANGLE& aFirst, const EDA_ANGLE& aSecond )
635 {
636 double apart = std::fmod( std::abs( aFirst.AsDegrees() - aSecond.AsDegrees() ), 180.0 );
637 return apart < 0.01 || apart > 179.99;
638 };
639
640 for( double degrees : { 30.0, 45.0 } )
641 {
643 {
644 BOOST_TEST_CONTEXT( "turned " << degrees
645 << ( dir == FLIP_DIRECTION::LEFT_RIGHT ? " left/right" : " top/bottom" ) )
646 {
647 PCB_TABLE* table = makeTable( m_board, cols, rows, degrees );
648 EDA_ANGLE tilt( degrees, DEGREES_T );
649 EDA_ANGLE reflected = dir == FLIP_DIRECTION::LEFT_RIGHT ? ANGLE_180 - tilt : -tilt;
650
651 std::vector<VECTOR2I> before;
652
653 for( PCB_TABLECELL* cell : table->GetCells() )
654 before.push_back( cell->GetStart() );
655
656 table->Flip( point, dir );
657
658 BOOST_CHECK_MESSAGE( sameLine( table->GetCell( 0, 0 )->GetTextAngle(), reflected ),
659 "table came back at " << table->GetCell( 0, 0 )->GetTextAngle().AsDegrees()
660 << " degrees, expected " << reflected.AsDegrees() );
661
662 // Flipping back about the same point has to undo it exactly.
663 table->Flip( point, dir );
664
665 for( size_t ii = 0; ii < before.size(); ++ii )
666 {
667 BOOST_CHECK_MESSAGE( ( table->GetCells()[ii]->GetStart() - before[ii] ).EuclideanNorm()
668 <= pcbIUScale.mmToIU( 0.001 ),
669 "cell " << ii << " did not come back" );
670 }
671 }
672 }
673 }
674}
675
676
683BOOST_AUTO_TEST_CASE( Issue23234_CustomPadstackFlip )
684{
685 // Create a board with two copper layers so Flip works correctly
686 BOARD board;
687 FOOTPRINT footprint( &board );
688 PAD pad( &footprint );
689
690 // Set up a circular SMD pad on F_Cu (NORMAL padstack mode)
691 pad.SetAttribute( PAD_ATTRIB::SMD );
692 pad.SetPadstackMode( PADSTACK::MODE::NORMAL );
694 pad.SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( 500000, 500000 ) );
695 LSET smd_layers;
696 smd_layers.set( F_Cu );
697 pad.SetLayerSet( smd_layers );
698
699 // Switch padstack to CUSTOM mode (as the dialog does when user selects "Custom")
700 pad.Padstack().SetMode( PADSTACK::MODE::CUSTOM );
701
702 // Now flip the pad (as TransferDataFromWindow does for pads on flipped footprints).
703 // This renames the F_Cu key in m_copperProps to B_Cu.
704 // After this, ALL_LAYERS (= F_Cu) is no longer in m_copperProps.
705 pad.Flip( VECTOR2I( 0, 0 ), FLIP_DIRECTION::TOP_BOTTOM );
706
707 // These calls must NOT throw std::out_of_range.
708 // Before the fix, CopperLayer( ALL_LAYERS ) called m_copperProps.at( F_Cu ) which threw
709 // because F_Cu was not in the map (it had been renamed to B_Cu by FlipLayers).
710 BOOST_CHECK_NO_THROW( pad.GetShape( PADSTACK::ALL_LAYERS ) );
711 BOOST_CHECK_NO_THROW( pad.GetSize( PADSTACK::ALL_LAYERS ) );
712 BOOST_CHECK_NO_THROW( pad.Padstack().EffectiveLayerFor( PADSTACK::ALL_LAYERS ) );
713
714 // Verify the returned shape is sane (the B_Cu props, which were originally F_Cu props)
715 BOOST_CHECK( pad.GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE );
716}
717
718
719// Regression test for issue #24696: a grouped zone left its group after undo/redo of a fill.
720// SwapItemData() (used by undo/redo and commit revert) must not move group membership, which
721// is a structural back-reference rather than swappable item data.
722BOOST_AUTO_TEST_CASE( Issue24696_SwapItemDataKeepsGroupMembership )
723{
724 PCB_GROUP group( &m_board );
725 ZONE live( &m_board );
726 ZONE image( &m_board );
727
728 // Mirror the undo swap: BOARD::Remove() has already stripped the live item's group,
729 // while the undo image still carries the membership.
730 live.SetParentGroup( nullptr );
731 image.SetParentGroup( &group );
732
733 live.SwapItemData( &image );
734
735 BOOST_CHECK( live.GetParentGroup() == nullptr );
736 BOOST_CHECK( image.GetParentGroup() == &group );
737
738 image.SetParentGroup( nullptr );
739}
740
741
742// Partial hardening for the BOARD::RecordDRCExclusions crash family (Sentry KICAD-YT2,
743// KICAD-YTA). A PCB_MARKER may legitimately carry a null RC_ITEM (its ctor and dtor both guard
744// the member), but DRC_EXCLUSION::FromMarker() dereferences it unconditionally, so recording exclusions
745// during a project save or window close faulted on such a marker.
746BOOST_AUTO_TEST_CASE( RecordDRCExclusionsSkipsMarkerWithoutRCItem )
747{
748 BOARD board;
749
750 PCB_MARKER* marker = new PCB_MARKER( nullptr, VECTOR2I( 0, 0 ) );
751 marker->SetExcluded( true );
752 board.Add( marker );
753
754 BOOST_CHECK_NO_THROW( board.RecordDRCExclusions() );
755
756 // The item-less marker has no violation to serialize, so nothing is persisted.
757 BOOST_CHECK( board.GetDesignSettings().m_DrcExclusions.empty() );
758}
759
760
761// Squaring the width in int overflowed for any line wider than about 46 um
762BOOST_AUTO_TEST_CASE( LinearShapeCoverageAreaIsWidthSquared )
763{
764 PCB_SHAPE segment( nullptr, SHAPE_T::SEGMENT );
765 segment.SetEnd( VECTOR2I( pcbIUScale.mmToIU( 10 ), 0 ) );
766 segment.SetWidth( pcbIUScale.mmToIU( 0.2 ) );
767
768 const double width = pcbIUScale.mmToIU( 0.2 );
769 BOOST_TEST( segment.GetCoverageArea( 0 ) == width * width );
770}
771
772
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
BASE_SET & set(size_t pos)
Definition base_set.h:126
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
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:85
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:383
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1520
void RecordDRCExclusions()
Scan existing markers and record data from any that are Excluded.
Definition board.cpp:569
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1290
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:594
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:115
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:236
static ENUM_MAP< T > & Instance()
Definition property.h:792
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition marker_base.h:91
@ 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 Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
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 SetWidth(int aWidth) override
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
double GetCoverageArea(int aTextMargin) const override
Return the board area this item covers, used to rank candidates under the cursor so a click on a smal...
void SetStart(const VECTOR2I &aStart) override
wxString GetUnwrappedShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const
GetShownText() before it is line-broken to the cell's width, i.e.
void SetColWidth(int aCol, int aWidth)
Definition pcb_table.h:135
std::vector< PCB_TABLECELL * > GetCells() const
Definition pcb_table.h:165
void AddCell(PCB_TABLECELL *aCell)
Definition pcb_table.h:170
void SetColCount(int aCount)
Definition pcb_table.h:125
void SetRowHeight(int aRow, int aHeight)
Definition pcb_table.h:145
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
void SetTextAngle(const EDA_ANGLE &aAngle) override
wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Return the string actually shown after processing of the base text.
std::vector< VECTOR2I > GetCorners() const override
Return 4 corners for a rectangle or rotated rectangle (stored as a poly).
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
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:1496
void GetInteractingZones(PCB_LAYER_ID aLayer, std::vector< ZONE * > *aSameNetCollidingZones, std::vector< ZONE * > *aOtherNetIntersectingZones) const
Some intersecting zones, despite being on the same layer with the same net, cannot be merged due to o...
Definition zone.cpp:1758
@ FOR_CANVAS
Definition common.h:92
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:81
@ DEGREES_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:452
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
@ SEGMENT
Definition eda_shape.h:56
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ F_Fab
Definition layer_ids.h:115
@ F_SilkS
Definition layer_ids.h:96
@ In1_Cu
Definition layer_ids.h:62
@ B_SilkS
Definition layer_ids.h:97
@ F_Cu
Definition layer_ids.h:60
@ B_Fab
Definition layer_ids.h:114
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:437
@ 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.
@ 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_AUTO_TEST_SUITE_END()
BOOST_TEST(netlist.find("R_G1 ARM_OUT1 DIE_B R='0.001 / ((SW_STATE)") !=std::string::npos)
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:244
@ 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_3D_BODY_T
class PCB_3D_BODY, a 3D body (extruded or model) on a footprint
Definition typeinfo.h:241
@ 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:448
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:331
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708