forked from KiCad/kicad-source-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
footprint.cpp
2590 lines (2034 loc) · 76.1 KB
/
footprint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2015 Wayne Stambaugh <[email protected]>
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <core/mirror.h>
#include <confirm.h>
#include <refdes_utils.h>
#include <bitmaps.h>
#include <unordered_set>
#include <string_utils.h>
#include <pcb_edit_frame.h>
#include <board.h>
#include <board_design_settings.h>
#include <fp_shape.h>
#include <macros.h>
#include <pad.h>
#include <pcb_marker.h>
#include <pcb_group.h>
#include <pcb_track.h>
#include <pcb_dimension.h>
#include <footprint.h>
#include <zone.h>
#include <view/view.h>
#include <geometry/shape_null.h>
#include <i18n_utility.h>
#include <drc/drc_item.h>
#include <geometry/shape_segment.h>
#include <convert_shape_list_to_polygon.h>
#include <geometry/convex_hull.h>
#include "fp_textbox.h"
#include "convert_basic_shapes_to_polygon.h"
FOOTPRINT::FOOTPRINT( BOARD* parent ) :
BOARD_ITEM_CONTAINER((BOARD_ITEM*) parent, PCB_FOOTPRINT_T ),
m_boundingBoxCacheTimeStamp( 0 ),
m_visibleBBoxCacheTimeStamp( 0 ),
m_textExcludedBBoxCacheTimeStamp( 0 ),
m_hullCacheTimeStamp( 0 ),
m_initial_comments( nullptr )
{
m_attributes = 0;
m_layer = F_Cu;
m_orient = ANGLE_0;
m_fpStatus = FP_PADS_are_LOCKED;
m_arflag = 0;
m_link = 0;
m_lastEditTime = 0;
m_localClearance = 0;
m_localSolderMaskMargin = 0;
m_localSolderPasteMargin = 0;
m_localSolderPasteMarginRatio = 0.0;
m_zoneConnection = ZONE_CONNECTION::INHERITED;
// These are special and mandatory text fields
m_reference = new FP_TEXT( this, FP_TEXT::TEXT_is_REFERENCE );
m_value = new FP_TEXT( this, FP_TEXT::TEXT_is_VALUE );
m_3D_Drawings.clear();
}
FOOTPRINT::FOOTPRINT( const FOOTPRINT& aFootprint ) :
BOARD_ITEM_CONTAINER( aFootprint )
{
m_pos = aFootprint.m_pos;
m_fpid = aFootprint.m_fpid;
m_attributes = aFootprint.m_attributes;
m_fpStatus = aFootprint.m_fpStatus;
m_orient = aFootprint.m_orient;
m_lastEditTime = aFootprint.m_lastEditTime;
m_link = aFootprint.m_link;
m_path = aFootprint.m_path;
m_cachedBoundingBox = aFootprint.m_cachedBoundingBox;
m_boundingBoxCacheTimeStamp = aFootprint.m_boundingBoxCacheTimeStamp;
m_cachedVisibleBBox = aFootprint.m_cachedVisibleBBox;
m_visibleBBoxCacheTimeStamp = aFootprint.m_visibleBBoxCacheTimeStamp;
m_cachedTextExcludedBBox = aFootprint.m_cachedTextExcludedBBox;
m_textExcludedBBoxCacheTimeStamp = aFootprint.m_textExcludedBBoxCacheTimeStamp;
m_cachedHull = aFootprint.m_cachedHull;
m_hullCacheTimeStamp = aFootprint.m_hullCacheTimeStamp;
m_localClearance = aFootprint.m_localClearance;
m_localSolderMaskMargin = aFootprint.m_localSolderMaskMargin;
m_localSolderPasteMargin = aFootprint.m_localSolderPasteMargin;
m_localSolderPasteMarginRatio = aFootprint.m_localSolderPasteMarginRatio;
m_zoneConnection = aFootprint.m_zoneConnection;
std::map<BOARD_ITEM*, BOARD_ITEM*> ptrMap;
// Copy reference and value.
m_reference = new FP_TEXT( *aFootprint.m_reference );
m_reference->SetParent( this );
ptrMap[ aFootprint.m_reference ] = m_reference;
m_value = new FP_TEXT( *aFootprint.m_value );
m_value->SetParent( this );
ptrMap[ aFootprint.m_value ] = m_value;
// Copy pads
for( PAD* pad : aFootprint.Pads() )
{
PAD* newPad = static_cast<PAD*>( pad->Clone() );
ptrMap[ pad ] = newPad;
Add( newPad, ADD_MODE::APPEND ); // Append to ensure indexes are identical
}
// Copy zones
for( FP_ZONE* zone : aFootprint.Zones() )
{
FP_ZONE* newZone = static_cast<FP_ZONE*>( zone->Clone() );
ptrMap[ zone ] = newZone;
Add( newZone, ADD_MODE::APPEND ); // Append to ensure indexes are identical
// Ensure the net info is OK and especially uses the net info list
// living in the current board
// Needed when copying a fp from fp editor that has its own board
// Must be NETINFO_LIST::ORPHANED_ITEM for a keepout that has no net.
newZone->SetNetCode( -1 );
}
// Copy drawings
for( BOARD_ITEM* item : aFootprint.GraphicalItems() )
{
BOARD_ITEM* newItem = static_cast<BOARD_ITEM*>( item->Clone() );
ptrMap[ item ] = newItem;
Add( newItem, ADD_MODE::APPEND ); // Append to ensure indexes are identical
}
// Copy groups
for( PCB_GROUP* group : aFootprint.Groups() )
{
PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( group->Clone() );
ptrMap[ group ] = newGroup;
Add( newGroup, ADD_MODE::APPEND ); // Append to ensure indexes are identical
}
// Rebuild groups
for( PCB_GROUP* group : aFootprint.Groups() )
{
PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( ptrMap[ group ] );
newGroup->GetItems().clear();
for( BOARD_ITEM* member : group->GetItems() )
{
if( ptrMap.count( member ) )
newGroup->AddItem( ptrMap[ member ] );
}
}
// Copy auxiliary data
m_3D_Drawings = aFootprint.m_3D_Drawings;
m_doc = aFootprint.m_doc;
m_keywords = aFootprint.m_keywords;
m_properties = aFootprint.m_properties;
m_privateLayers = aFootprint.m_privateLayers;
m_arflag = 0;
m_initial_comments = aFootprint.m_initial_comments ?
new wxArrayString( *aFootprint.m_initial_comments ) : nullptr;
}
FOOTPRINT::FOOTPRINT( FOOTPRINT&& aFootprint ) :
BOARD_ITEM_CONTAINER( aFootprint )
{
*this = std::move( aFootprint );
}
FOOTPRINT::~FOOTPRINT()
{
// Clean up the owned elements
delete m_reference;
delete m_value;
delete m_initial_comments;
for( PAD* p : m_pads )
delete p;
m_pads.clear();
for( FP_ZONE* zone : m_fp_zones )
delete zone;
m_fp_zones.clear();
for( PCB_GROUP* group : m_fp_groups )
delete group;
m_fp_groups.clear();
for( BOARD_ITEM* d : m_drawings )
delete d;
m_drawings.clear();
}
bool FOOTPRINT::FixUuids()
{
// replace null UUIDs if any by a valid uuid
std::vector< BOARD_ITEM* > item_list;
item_list.push_back( m_reference );
item_list.push_back( m_value );
for( PAD* pad : m_pads )
item_list.push_back( pad );
for( BOARD_ITEM* gr_item : m_drawings )
item_list.push_back( gr_item );
// Note: one cannot fix null UUIDs inside the group, but it should not happen
// because null uuids can be found in old footprints, therefore without group
for( PCB_GROUP* group : m_fp_groups )
item_list.push_back( group );
// Probably notneeded, because old fp do not have zones. But just in case.
for( FP_ZONE* zone : m_fp_zones )
item_list.push_back( zone );
bool changed = false;
for( BOARD_ITEM* item : item_list )
{
if( item->m_Uuid == niluuid )
{
const_cast<KIID&>( item->m_Uuid ) = KIID();
changed = true;
}
}
return changed;
}
FOOTPRINT& FOOTPRINT::operator=( FOOTPRINT&& aOther )
{
BOARD_ITEM::operator=( aOther );
m_pos = aOther.m_pos;
m_fpid = aOther.m_fpid;
m_attributes = aOther.m_attributes;
m_fpStatus = aOther.m_fpStatus;
m_orient = aOther.m_orient;
m_lastEditTime = aOther.m_lastEditTime;
m_link = aOther.m_link;
m_path = aOther.m_path;
m_cachedBoundingBox = aOther.m_cachedBoundingBox;
m_boundingBoxCacheTimeStamp = aOther.m_boundingBoxCacheTimeStamp;
m_cachedVisibleBBox = aOther.m_cachedVisibleBBox;
m_visibleBBoxCacheTimeStamp = aOther.m_visibleBBoxCacheTimeStamp;
m_cachedTextExcludedBBox = aOther.m_cachedTextExcludedBBox;
m_textExcludedBBoxCacheTimeStamp = aOther.m_textExcludedBBoxCacheTimeStamp;
m_cachedHull = aOther.m_cachedHull;
m_hullCacheTimeStamp = aOther.m_hullCacheTimeStamp;
m_localClearance = aOther.m_localClearance;
m_localSolderMaskMargin = aOther.m_localSolderMaskMargin;
m_localSolderPasteMargin = aOther.m_localSolderPasteMargin;
m_localSolderPasteMarginRatio = aOther.m_localSolderPasteMarginRatio;
m_zoneConnection = aOther.m_zoneConnection;
// Move reference and value
m_reference = aOther.m_reference;
m_reference->SetParent( this );
m_value = aOther.m_value;
m_value->SetParent( this );
// Move the pads
m_pads.clear();
for( PAD* pad : aOther.Pads() )
Add( pad );
aOther.Pads().clear();
// Move the zones
m_fp_zones.clear();
for( FP_ZONE* item : aOther.Zones() )
{
Add( item );
// Ensure the net info is OK and especially uses the net info list
// living in the current board
// Needed when copying a fp from fp editor that has its own board
// Must be NETINFO_LIST::ORPHANED_ITEM for a keepout that has no net.
item->SetNetCode( -1 );
}
aOther.Zones().clear();
// Move the drawings
m_drawings.clear();
for( BOARD_ITEM* item : aOther.GraphicalItems() )
Add( item );
aOther.GraphicalItems().clear();
// Move the groups
m_fp_groups.clear();
for( PCB_GROUP* group : aOther.Groups() )
Add( group );
aOther.Groups().clear();
// Copy auxiliary data
m_3D_Drawings = aOther.m_3D_Drawings;
m_doc = aOther.m_doc;
m_keywords = aOther.m_keywords;
m_properties = aOther.m_properties;
m_privateLayers = aOther.m_privateLayers;
m_initial_comments = aOther.m_initial_comments;
// Clear the other item's containers since this is a move
aOther.Pads().clear();
aOther.Zones().clear();
aOther.GraphicalItems().clear();
aOther.m_value = nullptr;
aOther.m_reference = nullptr;
aOther.m_initial_comments = nullptr;
return *this;
}
FOOTPRINT& FOOTPRINT::operator=( const FOOTPRINT& aOther )
{
BOARD_ITEM::operator=( aOther );
m_pos = aOther.m_pos;
m_fpid = aOther.m_fpid;
m_attributes = aOther.m_attributes;
m_fpStatus = aOther.m_fpStatus;
m_orient = aOther.m_orient;
m_lastEditTime = aOther.m_lastEditTime;
m_link = aOther.m_link;
m_path = aOther.m_path;
m_cachedBoundingBox = aOther.m_cachedBoundingBox;
m_boundingBoxCacheTimeStamp = aOther.m_boundingBoxCacheTimeStamp;
m_cachedVisibleBBox = aOther.m_cachedVisibleBBox;
m_visibleBBoxCacheTimeStamp = aOther.m_visibleBBoxCacheTimeStamp;
m_cachedTextExcludedBBox = aOther.m_cachedTextExcludedBBox;
m_textExcludedBBoxCacheTimeStamp = aOther.m_textExcludedBBoxCacheTimeStamp;
m_cachedHull = aOther.m_cachedHull;
m_hullCacheTimeStamp = aOther.m_hullCacheTimeStamp;
m_localClearance = aOther.m_localClearance;
m_localSolderMaskMargin = aOther.m_localSolderMaskMargin;
m_localSolderPasteMargin = aOther.m_localSolderPasteMargin;
m_localSolderPasteMarginRatio = aOther.m_localSolderPasteMarginRatio;
m_zoneConnection = aOther.m_zoneConnection;
// Copy reference and value
*m_reference = *aOther.m_reference;
m_reference->SetParent( this );
*m_value = *aOther.m_value;
m_value->SetParent( this );
std::map<BOARD_ITEM*, BOARD_ITEM*> ptrMap;
// Copy pads
m_pads.clear();
for( PAD* pad : aOther.Pads() )
{
PAD* newPad = new PAD( *pad );
ptrMap[ pad ] = newPad;
Add( newPad );
}
// Copy zones
m_fp_zones.clear();
for( FP_ZONE* zone : aOther.Zones() )
{
FP_ZONE* newZone = static_cast<FP_ZONE*>( zone->Clone() );
ptrMap[ zone ] = newZone;
Add( newZone );
// Ensure the net info is OK and especially uses the net info list
// living in the current board
// Needed when copying a fp from fp editor that has its own board
// Must be NETINFO_LIST::ORPHANED_ITEM for a keepout that has no net.
newZone->SetNetCode( -1 );
}
// Copy drawings
m_drawings.clear();
for( BOARD_ITEM* item : aOther.GraphicalItems() )
{
BOARD_ITEM* newItem = static_cast<BOARD_ITEM*>( item->Clone() );
ptrMap[ item ] = newItem;
Add( newItem );
}
// Copy groups
m_fp_groups.clear();
for( PCB_GROUP* group : aOther.Groups() )
{
PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( group->Clone() );
newGroup->GetItems().clear();
for( BOARD_ITEM* member : group->GetItems() )
newGroup->AddItem( ptrMap[ member ] );
Add( newGroup );
}
// Copy auxiliary data
m_3D_Drawings = aOther.m_3D_Drawings;
m_doc = aOther.m_doc;
m_keywords = aOther.m_keywords;
m_properties = aOther.m_properties;
m_privateLayers = aOther.m_privateLayers;
m_initial_comments = aOther.m_initial_comments ?
new wxArrayString( *aOther.m_initial_comments ) : nullptr;
return *this;
}
void FOOTPRINT::GetContextualTextVars( wxArrayString* aVars ) const
{
aVars->push_back( wxT( "REFERENCE" ) );
aVars->push_back( wxT( "VALUE" ) );
aVars->push_back( wxT( "LAYER" ) );
}
bool FOOTPRINT::ResolveTextVar( wxString* token, int aDepth ) const
{
if( token->IsSameAs( wxT( "REFERENCE" ) ) )
{
*token = m_reference->GetShownText( aDepth + 1 );
return true;
}
else if( token->IsSameAs( wxT( "VALUE" ) ) )
{
*token = m_value->GetShownText( aDepth + 1 );
return true;
}
else if( token->IsSameAs( wxT( "LAYER" ) ) )
{
*token = GetLayerName();
return true;
}
else if( m_properties.count( *token ) )
{
*token = m_properties.at( *token );
return true;
}
return false;
}
void FOOTPRINT::ClearAllNets()
{
// Force the ORPHANED dummy net info for all pads.
// ORPHANED dummy net does not depend on a board
for( PAD* pad : m_pads )
pad->SetNetCode( NETINFO_LIST::ORPHANED );
}
void FOOTPRINT::Add( BOARD_ITEM* aBoardItem, ADD_MODE aMode, bool aSkipConnectivity )
{
switch( aBoardItem->Type() )
{
case PCB_FP_TEXT_T:
// Only user text can be added this way.
wxASSERT( static_cast<FP_TEXT*>( aBoardItem )->GetType() == FP_TEXT::TEXT_is_DIVERS );
KI_FALLTHROUGH;
case PCB_FP_DIM_ALIGNED_T:
case PCB_FP_DIM_LEADER_T:
case PCB_FP_DIM_CENTER_T:
case PCB_FP_DIM_RADIAL_T:
case PCB_FP_DIM_ORTHOGONAL_T:
case PCB_FP_SHAPE_T:
case PCB_FP_TEXTBOX_T:
if( aMode == ADD_MODE::APPEND )
m_drawings.push_back( aBoardItem );
else
m_drawings.push_front( aBoardItem );
break;
case PCB_PAD_T:
if( aMode == ADD_MODE::APPEND )
m_pads.push_back( static_cast<PAD*>( aBoardItem ) );
else
m_pads.push_front( static_cast<PAD*>( aBoardItem ) );
break;
case PCB_FP_ZONE_T:
if( aMode == ADD_MODE::APPEND )
m_fp_zones.push_back( static_cast<FP_ZONE*>( aBoardItem ) );
else
m_fp_zones.insert( m_fp_zones.begin(), static_cast<FP_ZONE*>( aBoardItem ) );
break;
case PCB_GROUP_T:
if( aMode == ADD_MODE::APPEND )
m_fp_groups.push_back( static_cast<PCB_GROUP*>( aBoardItem ) );
else
m_fp_groups.insert( m_fp_groups.begin(), static_cast<PCB_GROUP*>( aBoardItem ) );
break;
default:
{
wxString msg;
msg.Printf( wxT( "FOOTPRINT::Add() needs work: BOARD_ITEM type (%d) not handled" ),
aBoardItem->Type() );
wxFAIL_MSG( msg );
return;
}
}
aBoardItem->ClearEditFlags();
aBoardItem->SetParent( this );
}
void FOOTPRINT::Remove( BOARD_ITEM* aBoardItem, REMOVE_MODE aMode )
{
switch( aBoardItem->Type() )
{
case PCB_FP_TEXT_T:
// Only user text can be removed this way.
wxCHECK_RET( static_cast<FP_TEXT*>( aBoardItem )->GetType() == FP_TEXT::TEXT_is_DIVERS,
wxT( "Please report this bug: Invalid remove operation on required text" ) );
KI_FALLTHROUGH;
case PCB_FP_DIM_ALIGNED_T:
case PCB_FP_DIM_CENTER_T:
case PCB_FP_DIM_ORTHOGONAL_T:
case PCB_FP_DIM_RADIAL_T:
case PCB_FP_DIM_LEADER_T:
case PCB_FP_SHAPE_T:
case PCB_FP_TEXTBOX_T:
for( auto it = m_drawings.begin(); it != m_drawings.end(); ++it )
{
if( *it == aBoardItem )
{
m_drawings.erase( it );
break;
}
}
break;
case PCB_PAD_T:
for( auto it = m_pads.begin(); it != m_pads.end(); ++it )
{
if( *it == static_cast<PAD*>( aBoardItem ) )
{
m_pads.erase( it );
break;
}
}
break;
case PCB_FP_ZONE_T:
for( auto it = m_fp_zones.begin(); it != m_fp_zones.end(); ++it )
{
if( *it == static_cast<FP_ZONE*>( aBoardItem ) )
{
m_fp_zones.erase( it );
break;
}
}
break;
case PCB_GROUP_T:
for( auto it = m_fp_groups.begin(); it != m_fp_groups.end(); ++it )
{
if( *it == static_cast<PCB_GROUP*>( aBoardItem ) )
{
m_fp_groups.erase( it );
break;
}
}
break;
default:
{
wxString msg;
msg.Printf( wxT( "FOOTPRINT::Remove() needs work: BOARD_ITEM type (%d) not handled" ),
aBoardItem->Type() );
wxFAIL_MSG( msg );
}
}
aBoardItem->SetFlags( STRUCT_DELETED );
PCB_GROUP* parentGroup = aBoardItem->GetParentGroup();
if( parentGroup && !( parentGroup->GetFlags() & STRUCT_DELETED ) )
parentGroup->RemoveItem( aBoardItem );
}
double FOOTPRINT::GetArea( int aPadding ) const
{
EDA_RECT bbox = GetBoundingBox( false, false );
double w = std::abs( static_cast<double>( bbox.GetWidth() ) ) + aPadding;
double h = std::abs( static_cast<double>( bbox.GetHeight() ) ) + aPadding;
return w * h;
}
int FOOTPRINT::GetLikelyAttribute() const
{
int smd_count = 0;
int tht_count = 0;
for( PAD* pad : m_pads )
{
switch( pad->GetProperty() )
{
case PAD_PROP::FIDUCIAL_GLBL:
case PAD_PROP::FIDUCIAL_LOCAL:
continue;
case PAD_PROP::HEATSINK:
case PAD_PROP::CASTELLATED:
continue;
case PAD_PROP::NONE:
case PAD_PROP::BGA:
case PAD_PROP::TESTPOINT:
break;
}
switch( pad->GetAttribute() )
{
case PAD_ATTRIB::PTH:
tht_count++;
break;
case PAD_ATTRIB::SMD:
smd_count++;
break;
default:
break;
}
}
if( tht_count > 0 )
return FP_THROUGH_HOLE;
if( smd_count > 0 )
return FP_SMD;
return 0;
}
wxString FOOTPRINT::GetTypeName() const
{
if( ( m_attributes & FP_SMD ) == FP_SMD )
return _( "SMD" );
if( ( m_attributes & FP_THROUGH_HOLE ) == FP_THROUGH_HOLE )
return _( "Through hole" );
return _( "Other" );
}
EDA_RECT FOOTPRINT::GetFpPadsLocalBbox() const
{
EDA_RECT area;
// We want the bounding box of the footprint pads at rot 0, not flipped
// Create such a image:
FOOTPRINT dummy( *this );
dummy.SetPosition( VECTOR2I( 0, 0 ) );
dummy.SetOrientation( ANGLE_0 );
if( dummy.IsFlipped() )
dummy.Flip( VECTOR2I( 0, 0 ), false );
for( PAD* pad : dummy.Pads() )
area.Merge( pad->GetBoundingBox() );
return area;
}
const EDA_RECT FOOTPRINT::GetBoundingBox() const
{
return GetBoundingBox( true, true );
}
const EDA_RECT FOOTPRINT::GetBoundingBox( bool aIncludeText, bool aIncludeInvisibleText ) const
{
const BOARD* board = GetBoard();
bool isFPEdit = board && board->IsFootprintHolder();
if( board )
{
if( aIncludeText && aIncludeInvisibleText )
{
if( m_boundingBoxCacheTimeStamp >= board->GetTimeStamp() )
return m_cachedBoundingBox;
}
else if( aIncludeText )
{
if( m_visibleBBoxCacheTimeStamp >= board->GetTimeStamp() )
return m_cachedVisibleBBox;
}
else
{
if( m_textExcludedBBoxCacheTimeStamp >= board->GetTimeStamp() )
return m_cachedTextExcludedBBox;
}
}
EDA_RECT area;
area.SetOrigin( m_pos );
area.SetEnd( m_pos );
area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
for( BOARD_ITEM* item : m_drawings )
{
if( !isFPEdit && m_privateLayers.test( item->GetLayer() ) )
continue;
if( item->Type() != PCB_FP_TEXT_T )
area.Merge( item->GetBoundingBox() );
}
for( PAD* pad : m_pads )
area.Merge( pad->GetBoundingBox() );
for( FP_ZONE* zone : m_fp_zones )
area.Merge( zone->GetBoundingBox() );
bool noDrawItems = ( m_drawings.empty() && m_pads.empty() && m_fp_zones.empty() );
// Groups do not contribute to the rect, only their members
if( aIncludeText || noDrawItems )
{
for( BOARD_ITEM* item : m_drawings )
{
if( !isFPEdit && m_privateLayers.test( item->GetLayer() ) )
continue;
// Only FP_TEXT items are independently selectable; FP_TEXTBOX items go in with
// other graphic items above.
if( item->Type() == PCB_FP_TEXT_T )
area.Merge( item->GetBoundingBox() );
}
// This can be further optimized when aIncludeInvisibleText is true, but currently
// leaving this as is until it's determined there is a noticeable speed hit.
bool valueLayerIsVisible = true;
bool refLayerIsVisible = true;
if( board )
{
// The first "&&" conditional handles the user turning layers off as well as layers
// not being present in the current PCB stackup. Values, references, and all
// footprint text can also be turned off via the GAL meta-layers, so the 2nd and
// 3rd "&&" conditionals handle that.
valueLayerIsVisible = board->IsLayerVisible( m_value->GetLayer() )
&& board->IsElementVisible( LAYER_MOD_VALUES )
&& board->IsElementVisible( LAYER_MOD_TEXT );
refLayerIsVisible = board->IsLayerVisible( m_reference->GetLayer() )
&& board->IsElementVisible( LAYER_MOD_REFERENCES )
&& board->IsElementVisible( LAYER_MOD_TEXT );
}
if( ( m_value->IsVisible() && valueLayerIsVisible )
|| aIncludeInvisibleText
|| noDrawItems )
{
area.Merge( m_value->GetBoundingBox() );
}
if( ( m_reference->IsVisible() && refLayerIsVisible )
|| aIncludeInvisibleText
|| noDrawItems )
{
area.Merge( m_reference->GetBoundingBox() );
}
}
if( board )
{
if( ( aIncludeText && aIncludeInvisibleText ) || noDrawItems )
{
m_boundingBoxCacheTimeStamp = board->GetTimeStamp();
m_cachedBoundingBox = area;
}
else if( aIncludeText )
{
m_visibleBBoxCacheTimeStamp = board->GetTimeStamp();
m_cachedVisibleBBox = area;
}
else
{
m_textExcludedBBoxCacheTimeStamp = board->GetTimeStamp();
m_cachedTextExcludedBBox = area;
}
}
return area;
}
SHAPE_POLY_SET FOOTPRINT::GetBoundingHull() const
{
const BOARD* board = GetBoard();
bool isFPEdit = board && board->IsFootprintHolder();
if( board )
{
if( m_hullCacheTimeStamp >= board->GetTimeStamp() )
return m_cachedHull;
}
SHAPE_POLY_SET rawPolys;
SHAPE_POLY_SET hull;
for( BOARD_ITEM* item : m_drawings )
{
if( !isFPEdit && m_privateLayers.test( item->GetLayer() ) )
continue;
if( item->Type() != PCB_FP_TEXT_T )
{
item->TransformShapeWithClearanceToPolygon( rawPolys, UNDEFINED_LAYER, 0, ARC_LOW_DEF,
ERROR_OUTSIDE );
}
// We intentionally exclude footprint text from the bounding hull.
}
for( PAD* pad : m_pads )
{
pad->TransformShapeWithClearanceToPolygon( rawPolys, UNDEFINED_LAYER, 0, ARC_LOW_DEF,
ERROR_OUTSIDE );
// In case hole is larger than pad
pad->TransformHoleWithClearanceToPolygon( rawPolys, 0, ARC_LOW_DEF, ERROR_OUTSIDE );
}
for( FP_ZONE* zone : m_fp_zones )
{
for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
{
const SHAPE_POLY_SET& layerPoly = *zone->GetFilledPolysList( layer );
for( int ii = 0; ii < layerPoly.OutlineCount(); ii++ )
{
const SHAPE_LINE_CHAIN& poly = layerPoly.COutline( ii );
rawPolys.AddOutline( poly );
}
}
}
// If there are some graphic items, build the actual hull.
// However if no items, create a minimal polygon (can happen if a footprint
// is created with no item: it contains only 2 texts.
if( rawPolys.OutlineCount() == 0 )
{
// generate a small dummy rectangular outline around the anchor
const int halfsize = Millimeter2iu( 1.0 );
rawPolys.NewOutline();
// add a square:
rawPolys.Append( GetPosition().x - halfsize, GetPosition().y - halfsize );
rawPolys.Append( GetPosition().x + halfsize, GetPosition().y - halfsize );
rawPolys.Append( GetPosition().x + halfsize, GetPosition().y + halfsize );
rawPolys.Append( GetPosition().x - halfsize, GetPosition().y + halfsize );
}
std::vector<VECTOR2I> convex_hull;
BuildConvexHull( convex_hull, rawPolys );
m_cachedHull.RemoveAllContours();
m_cachedHull.NewOutline();
for( const VECTOR2I& pt : convex_hull )
m_cachedHull.Append( pt );
if( board )
m_hullCacheTimeStamp = board->GetTimeStamp();
return m_cachedHull;
}
void FOOTPRINT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
wxString msg, msg2;
aList.emplace_back( m_reference->GetShownText(), m_value->GetShownText() );
if( aFrame->IsType( FRAME_FOOTPRINT_VIEWER )
|| aFrame->IsType( FRAME_FOOTPRINT_VIEWER_MODAL )
|| aFrame->IsType( FRAME_FOOTPRINT_EDITOR ) )
{
wxDateTime date( static_cast<time_t>( m_lastEditTime ) );
// Date format: see http://www.cplusplus.com/reference/ctime/strftime
if( m_lastEditTime && date.IsValid() )
msg = date.Format( wxT( "%b %d, %Y" ) ); // Abbreviated_month_name Day, Year
else
msg = _( "Unknown" );
aList.emplace_back( _( "Last Change" ), msg );
}
else if( aFrame->IsType( FRAME_PCB_EDITOR ) )
{
aList.emplace_back( _( "Board Side" ), IsFlipped() ? _( "Back (Flipped)" ) : _( "Front" ) );
}
auto addToken = []( wxString* aStr, const wxString& aAttr )
{
if( !aStr->IsEmpty() )
*aStr += wxT( ", " );
*aStr += aAttr;
};
wxString status;
wxString attrs;
if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
addToken( &status, _( "Locked" ) );
if( m_fpStatus & FP_is_PLACED )
addToken( &status, _( "autoplaced" ) );
if( m_attributes & FP_BOARD_ONLY )
addToken( &attrs, _( "not in schematic" ) );
if( m_attributes & FP_EXCLUDE_FROM_POS_FILES )
addToken( &attrs, _( "exclude from pos files" ) );
if( m_attributes & FP_EXCLUDE_FROM_BOM )
addToken( &attrs, _( "exclude from BOM" ) );
aList.emplace_back( _( "Status: " ) + status, _( "Attributes:" ) + wxS( " " ) + attrs );
aList.emplace_back( _( "Rotation" ), wxString::Format( wxT( "%.4g" ),
GetOrientation().AsDegrees() ) );
msg.Printf( _( "Footprint: %s" ), m_fpid.GetUniStringLibId() );
msg2.Printf( _( "3D-Shape: %s" ), m_3D_Drawings.empty() ? _( "<none>" )