-
-
Notifications
You must be signed in to change notification settings - Fork 486
/
lib_symbol.cpp
1921 lines (1494 loc) · 50.6 KB
/
lib_symbol.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) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2008 Wayne Stambaugh <[email protected]>
* Copyright (C) 2022 CERN
* Copyright (C) 2004-2024 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 <font/outline_font.h>
#include <sch_draw_panel.h>
#include <plotters/plotter.h>
#include <sch_screen.h>
#include <template_fieldnames.h>
#include <transform.h>
#include <symbol_library.h>
#include <settings/color_settings.h>
#include <sch_pin.h>
#include <sch_shape.h>
#include <memory>
std::vector<SEARCH_TERM> LIB_SYMBOL::GetSearchTerms()
{
std::vector<SEARCH_TERM> terms;
terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
wxStringTokenizer keywordTokenizer( GetKeyWords(), wxS( " " ), wxTOKEN_STRTOK );
while( keywordTokenizer.HasMoreTokens() )
terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
// TODO(JE) rework this later so we can highlight matches in their column
std::map<wxString, wxString> fields;
GetChooserFields( fields );
for( const auto& [ name, text ] : fields )
terms.emplace_back( SEARCH_TERM( text, 4 ) );
// Also include keywords as one long string, just in case
terms.emplace_back( SEARCH_TERM( GetKeyWords(), 1 ) );
terms.emplace_back( SEARCH_TERM( GetDescription(), 1 ) );
wxString footprint = GetFootprintField().GetText();
if( !footprint.IsEmpty() )
terms.emplace_back( SEARCH_TERM( GetFootprintField().GetText(), 1 ) );
return terms;
}
void LIB_SYMBOL::GetChooserFields( std::map<wxString, wxString>& aColumnMap )
{
for( SCH_ITEM& item : m_drawings[ SCH_FIELD_T ] )
{
SCH_FIELD* field = static_cast<SCH_FIELD*>( &item );
if( field->ShowInChooser() )
aColumnMap[field->GetName()] = field->EDA_TEXT::GetShownText( false );
}
}
bool operator<( const LIB_SYMBOL& aItem1, const LIB_SYMBOL& aItem2 )
{
return aItem1.GetName() < aItem2.GetName();
}
/// http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
struct null_deleter
{
void operator()(void const *) const
{
}
};
LIB_SYMBOL::LIB_SYMBOL( const wxString& aName, LIB_SYMBOL* aParent, SYMBOL_LIB* aLibrary ) :
SYMBOL( LIB_SYMBOL_T ),
m_me( this, null_deleter() )
{
m_lastModDate = 0;
m_unitCount = 1;
m_pinNameOffset = schIUScale.MilsToIU( DEFAULT_PIN_NAME_OFFSET );
m_options = ENTRY_NORMAL;
m_unitsLocked = false;
// Add the MANDATORY_FIELDS in RAM only. These are assumed to be present
// when the field editors are invoked.
m_drawings[SCH_FIELD_T].reserve( MANDATORY_FIELDS );
for( int i = 0; i < MANDATORY_FIELDS; i++ )
m_drawings[SCH_FIELD_T].push_back( new SCH_FIELD( this, i ) );
// Ensure reference and value fields are visible when creating a lib symbol
// whatever the SCH_FIELD Ctor default value is.
GetReferenceField().SetVisible( true );
GetValueField().SetVisible( true );
// Set visibilty to false for these other mandatory fields (at lest for now)
// whatever the SCH_FIELD Ctor default value is.
GetFootprintField().SetVisible( false );
GetDatasheetField().SetVisible( false );
GetDescriptionField().SetVisible( false );
SetName( aName );
if( aParent )
SetParent( aParent );
SetLib( aLibrary );
}
LIB_SYMBOL::LIB_SYMBOL( const LIB_SYMBOL& aSymbol, SYMBOL_LIB* aLibrary ) :
SYMBOL( aSymbol ),
m_me( this, null_deleter() )
{
m_library = aLibrary;
m_name = aSymbol.m_name;
m_fpFilters = wxArrayString( aSymbol.m_fpFilters );
m_unitCount = aSymbol.m_unitCount;
m_unitsLocked = aSymbol.m_unitsLocked;
m_lastModDate = aSymbol.m_lastModDate;
m_options = aSymbol.m_options;
m_libId = aSymbol.m_libId;
m_keyWords = aSymbol.m_keyWords;
aSymbol.CopyUnitDisplayNames( m_unitDisplayNames );
ClearSelected();
for( const SCH_ITEM& oldItem : aSymbol.m_drawings )
{
if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
continue;
try
{
SCH_ITEM* newItem = (SCH_ITEM*) oldItem.Clone();
newItem->ClearSelected();
newItem->SetParent( this );
m_drawings.push_back( newItem );
}
catch( ... )
{
wxFAIL_MSG( "Failed to clone SCH_ITEM." );
return;
}
}
LIB_SYMBOL_SPTR parent = aSymbol.m_parent.lock();
if( parent )
SetParent( parent.get() );
}
const LIB_SYMBOL& LIB_SYMBOL::operator=( const LIB_SYMBOL& aSymbol )
{
if( &aSymbol == this )
return aSymbol;
SYMBOL::operator=( aSymbol );
m_library = aSymbol.m_library;
m_name = aSymbol.m_name;
m_fpFilters = wxArrayString( aSymbol.m_fpFilters );
m_unitCount = aSymbol.m_unitCount;
m_unitsLocked = aSymbol.m_unitsLocked;
m_lastModDate = aSymbol.m_lastModDate;
m_options = aSymbol.m_options;
m_libId = aSymbol.m_libId;
m_keyWords = aSymbol.m_keyWords;
m_unitDisplayNames.clear();
aSymbol.CopyUnitDisplayNames( m_unitDisplayNames );
m_drawings.clear();
for( const SCH_ITEM& oldItem : aSymbol.m_drawings )
{
if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
continue;
SCH_ITEM* newItem = (SCH_ITEM*) oldItem.Clone();
newItem->SetParent( this );
m_drawings.push_back( newItem );
}
m_drawings.sort();
LIB_SYMBOL_SPTR parent = aSymbol.m_parent.lock();
if( parent )
SetParent( parent.get() );
return *this;
}
unsigned LIB_SYMBOL::GetInheritanceDepth() const
{
unsigned depth = 0;
LIB_SYMBOL_SPTR parent = GetParent().lock();
while( parent )
{
depth += 1;
parent = parent->GetParent().lock();
}
return depth;
}
LIB_SYMBOL_SPTR LIB_SYMBOL::GetRootSymbol() const
{
const LIB_SYMBOL_SPTR sp = m_parent.lock();
// Recurse until the parent symbol is empty.
if( sp )
return sp->GetRootSymbol();
return m_me;
}
wxString LIB_SYMBOL::GetUnitReference( int aUnit )
{
return LIB_SYMBOL::LetterSubReference( aUnit, 'A' );
}
bool LIB_SYMBOL::HasUnitDisplayName( int aUnit )
{
return ( m_unitDisplayNames.count( aUnit ) == 1 );
}
wxString LIB_SYMBOL::GetUnitDisplayName( int aUnit )
{
if( HasUnitDisplayName( aUnit ) )
return m_unitDisplayNames[aUnit];
else
return wxString::Format( _( "Unit %s" ), GetUnitReference( aUnit ) );
}
void LIB_SYMBOL::CopyUnitDisplayNames( std::map<int, wxString>& aTarget ) const
{
for( const auto& it : m_unitDisplayNames )
aTarget[it.first] = it.second;
}
void LIB_SYMBOL::SetUnitDisplayName( int aUnit, const wxString& aName )
{
if( aUnit <= GetUnitCount() )
{
if( aName.Length() > 0 )
m_unitDisplayNames[aUnit] = aName;
else
m_unitDisplayNames.erase( aUnit );
}
}
void LIB_SYMBOL::SetName( const wxString& aName )
{
m_name = aName;
m_libId.SetLibItemName( aName );
}
void LIB_SYMBOL::SetParent( LIB_SYMBOL* aParent )
{
if( aParent )
m_parent = aParent->SharedPtr();
else
m_parent.reset();
}
std::unique_ptr< LIB_SYMBOL > LIB_SYMBOL::Flatten() const
{
std::unique_ptr< LIB_SYMBOL > retv;
if( IsAlias() )
{
LIB_SYMBOL_SPTR parent = m_parent.lock();
wxCHECK_MSG( parent, retv,
wxString::Format( "Parent of derived symbol '%s' undefined", m_name ) );
// Copy the parent.
if( parent->IsAlias() )
retv = parent->Flatten();
else
retv = std::make_unique<LIB_SYMBOL>( *parent.get() );
retv->m_name = m_name;
retv->SetLibId( m_libId );
// Now add the inherited part mandatory field (this) information.
for( int i = 0; i < MANDATORY_FIELDS; i++ )
{
wxString tmp = GetFieldById( i )->GetText();
// If the field isn't defined then inherit the parent field value.
if( tmp.IsEmpty() )
retv->GetFieldById( i )->SetText( retv->GetFieldById( i )->GetText() );
else
*retv->GetFieldById( i ) = *GetFieldById( i );
}
// Grab all the rest of derived symbol fields.
for( const SCH_ITEM& item : m_drawings[ SCH_FIELD_T ] )
{
const SCH_FIELD* aliasField = static_cast<const SCH_FIELD*>( &item );
// Mandatory fields were already resolved.
if( aliasField->IsMandatory() )
continue;
SCH_FIELD* newField = new SCH_FIELD( *aliasField );
newField->SetParent( retv.get() );
SCH_FIELD* parentField = retv->FindField( aliasField->GetName() );
if( !parentField ) // Derived symbol field does not exist in parent symbol.
{
retv->AddDrawItem( newField );
}
else // Derived symbol field overrides the parent symbol field.
{
retv->RemoveDrawItem( parentField );
retv->AddDrawItem( newField );
}
}
retv->SetKeyWords( m_keyWords.IsEmpty() ? parent->GetKeyWords() : m_keyWords );
retv->SetFPFilters( m_fpFilters.IsEmpty() ? parent->GetFPFilters() : m_fpFilters );
retv->SetExcludedFromSim( parent->GetExcludedFromSim() );
retv->SetExcludedFromBOM( parent->GetExcludedFromBOM() );
retv->SetExcludedFromBoard( parent->GetExcludedFromBoard() );
retv->UpdateFieldOrdinals();
retv->m_parent.reset();
}
else
{
retv = std::make_unique<LIB_SYMBOL>( *this );
}
return retv;
}
const wxString LIB_SYMBOL::GetLibraryName() const
{
if( m_library )
return m_library->GetName();
return m_libId.GetLibNickname();
}
bool LIB_SYMBOL::IsPower() const
{
std::shared_ptr<LIB_SYMBOL> parent;
if( !m_parent.expired() && ( parent = m_parent.lock() ) )
{
if( parent->IsRoot() )
return parent->m_options == ENTRY_POWER;
else
return parent->IsPower();
}
return m_options == ENTRY_POWER;
}
void LIB_SYMBOL::SetPower()
{
if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
{
if( parent->IsRoot() )
parent->m_options = ENTRY_POWER;
else
parent->SetPower();
}
m_options = ENTRY_POWER;
}
bool LIB_SYMBOL::IsNormal() const
{
if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
{
if( parent->IsRoot() )
return parent->m_options == ENTRY_NORMAL;
else
return parent->IsNormal();
}
return m_options == ENTRY_NORMAL;
}
void LIB_SYMBOL::SetNormal()
{
if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
{
if( parent->IsRoot() )
parent->m_options = ENTRY_NORMAL;
else
parent->SetNormal();
}
m_options = ENTRY_NORMAL;
}
wxString LIB_SYMBOL::LetterSubReference( int aUnit, int aFirstId )
{
// use letters as notation. To allow more than 26 units, the sub ref
// use one letter if letter = A .. Z or a ... z, and 2 letters otherwise
// first letter is expected to be 'A' or 'a' (i.e. 26 letters are available)
int u;
wxString suffix;
do
{
u = ( aUnit - 1 ) % 26;
suffix = wxChar( aFirstId + u ) + suffix;
aUnit = ( aUnit - u ) / 26;
} while( aUnit > 0 );
return suffix;
}
bool LIB_SYMBOL::ResolveTextVar( wxString* token, int aDepth ) const
{
wxString footprint;
for( const SCH_ITEM& item : m_drawings )
{
if( item.Type() == SCH_FIELD_T )
{
const SCH_FIELD& field = static_cast<const SCH_FIELD&>( item );
if( field.GetId() == FOOTPRINT_FIELD )
footprint = field.GetShownText( nullptr, false, aDepth + 1 );
if( token->IsSameAs( field.GetCanonicalName().Upper() )
|| token->IsSameAs( field.GetName(), false ) )
{
*token = field.GetShownText( nullptr, false, aDepth + 1 );
return true;
}
}
}
// Consider missing simulation fields as empty, not un-resolved
if( token->IsSameAs( wxT( "SIM.DEVICE" ) )
|| token->IsSameAs( wxT( "SIM.TYPE" ) )
|| token->IsSameAs( wxT( "SIM.PINS" ) )
|| token->IsSameAs( wxT( "SIM.PARAMS" ) )
|| token->IsSameAs( wxT( "SIM.LIBRARY" ) )
|| token->IsSameAs( wxT( "SIM.NAME" ) ) )
{
*token = wxEmptyString;
return true;
}
if( token->IsSameAs( wxT( "FOOTPRINT_LIBRARY" ) ) )
{
wxArrayString parts = wxSplit( footprint, ':' );
if( parts.Count() > 0 )
*token = parts[ 0 ];
else
*token = wxEmptyString;
return true;
}
else if( token->IsSameAs( wxT( "FOOTPRINT_NAME" ) ) )
{
wxArrayString parts = wxSplit( footprint, ':' );
if( parts.Count() > 1 )
*token = parts[ std::min( 1, (int) parts.size() - 1 ) ];
else
*token = wxEmptyString;
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_LIBRARY" ) ) )
{
*token = m_libId.GetUniStringLibNickname();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_NAME" ) ) )
{
*token = m_libId.GetUniStringLibItemName();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_DESCRIPTION" ) ) )
{
*token = GetDescription();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_KEYWORDS" ) ) )
{
*token = GetKeyWords();
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOM" ) ) )
{
*token = this->GetExcludedFromBOM() ? _( "Excluded from BOM" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOARD" ) ) )
{
*token = this->GetExcludedFromBoard() ? _( "Excluded from board" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_SIM" ) ) )
{
*token = this->GetExcludedFromSim() ? _( "Excluded from simulation" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "DNP" ) ) )
{
*token = this->GetDNP() ? _( "DNP" ) : wxString( "" );
return true;
}
return false;
}
void LIB_SYMBOL::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
{
for( SCH_ITEM& item : m_drawings )
{
// Do not print private items
if( item.IsPrivate() )
continue;
// Do not draw items not attached to the current part
if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
continue;
if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
continue;
if( item.Type() == SCH_PIN_T )
{
item.Print( aSettings, aUnit, aBodyStyle, aOffset, aForceNoFill, aDimmed );
}
else if( item.Type() == SCH_FIELD_T )
{
SCH_FIELD& field = static_cast<SCH_FIELD&>( item );
if( ( field.IsVisible() && aSettings->m_ShowVisibleFields )
|| ( !field.IsVisible() && aSettings->m_ShowHiddenFields ) )
{
item.Print( aSettings, aUnit, aBodyStyle, aOffset, aForceNoFill, aDimmed );
}
}
else if( item.Type() == SCH_SHAPE_T )
{
SCH_SHAPE& shape = static_cast<SCH_SHAPE&>( item );
if( shape.GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR )
aForceNoFill = true;
shape.Print( aSettings, aUnit, aBodyStyle, aOffset, aForceNoFill, aDimmed );
}
else
{
item.Print( aSettings, aUnit, aBodyStyle, aOffset, aForceNoFill, aDimmed );
}
}
}
void LIB_SYMBOL::PrintBackground( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aDimmed )
{
/* draw background for filled items using background option
* Solid lines will be drawn after the background
* Note also, background is not drawn when printing in black and white
*/
if( !GetGRForceBlackPenState() )
{
for( SCH_ITEM& item : m_drawings )
{
// Do not print private items
if( item.IsPrivate() )
continue;
if( item.Type() == SCH_SHAPE_T )
{
SCH_SHAPE& shape = static_cast<SCH_SHAPE&>( item );
// Do not draw items not attached to the current part
if( aUnit && shape.m_unit && ( shape.m_unit != aUnit ) )
continue;
if( aBodyStyle && shape.m_bodyStyle && ( shape.m_bodyStyle != aBodyStyle ) )
continue;
if( shape.GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR )
shape.Print( aSettings, aUnit, aBodyStyle, aOffset, false, aDimmed );
}
}
}
}
void LIB_SYMBOL::Plot( PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed )
{
wxASSERT( aPlotter != nullptr );
SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
COLOR4D color = renderSettings->GetLayerColor( LAYER_DEVICE );
COLOR4D bg = renderSettings->GetBackgroundColor();
if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
bg = COLOR4D::WHITE;
if( aDimmed )
{
color.Desaturate( );
color = color.Mix( bg, 0.5f );
}
aPlotter->SetColor( color );
for( SCH_ITEM& item : m_drawings )
{
// Do not plot private items
if( item.IsPrivate() )
continue;
// LIB_FIELDs are not plotted here, because this plot function is used to plot schematic
// items which have their own SCH_FIELDs
if( item.Type() == SCH_FIELD_T )
continue;
if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
continue;
if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
continue;
item.Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
}
}
void LIB_SYMBOL::PlotFields( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
{
wxASSERT( aPlotter != nullptr );
SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
COLOR4D color = renderSettings->GetLayerColor( LAYER_FIELDS );
COLOR4D bg = renderSettings->GetBackgroundColor();
if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
bg = COLOR4D::WHITE;
if( aDimmed )
{
color.Desaturate( );
color = color.Mix( bg, 0.5f );
}
aPlotter->SetColor( color );
for( SCH_ITEM& item : m_drawings[ SCH_FIELD_T ] )
{
SCH_FIELD& field = static_cast<SCH_FIELD&>( item );
if( !renderSettings->m_ShowHiddenFields && !field.IsVisible() )
continue;
// The reference is a special case: we should change the basic text
// to add '?' and the part id
wxString tmp = field.GetText();
field.SetText( field.GetFullText( aUnit ) );
item.Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
field.SetText( tmp );
}
}
void LIB_SYMBOL::FixupDrawItems()
{
std::vector<SCH_SHAPE*> potential_top_items;
std::vector<SCH_ITEM*> bottom_items;
for( SCH_ITEM& item : m_drawings )
{
if( item.Type() == SCH_SHAPE_T )
{
SCH_SHAPE& shape = static_cast<SCH_SHAPE&>( item );
if( shape.GetFillMode() == FILL_T::FILLED_WITH_COLOR )
potential_top_items.push_back( &shape );
else
bottom_items.push_back( &item );
}
else
{
bottom_items.push_back( &item );
}
}
std::sort( potential_top_items.begin(), potential_top_items.end(),
[]( SCH_ITEM* a, SCH_ITEM* b )
{
return a->GetBoundingBox().GetArea() > b->GetBoundingBox().GetArea();
} );
for( SCH_SHAPE* item : potential_top_items )
{
for( SCH_ITEM* bottom_item : bottom_items )
{
if( item->GetBoundingBox().Contains( bottom_item->GetBoundingBox() ) )
{
item->SetFillMode( FILL_T::FILLED_WITH_BG_BODYCOLOR );
break;
}
}
}
}
void LIB_SYMBOL::RemoveDrawItem( SCH_ITEM* aItem )
{
wxASSERT( aItem != nullptr );
// none of the MANDATORY_FIELDS may be removed in RAM, but they may be
// omitted when saving to disk.
if( aItem->Type() == SCH_FIELD_T )
{
if( static_cast<SCH_FIELD*>( aItem )->IsMandatory() )
return;
}
LIB_ITEMS& items = m_drawings[ aItem->Type() ];
for( LIB_ITEMS::iterator i = items.begin(); i != items.end(); i++ )
{
if( &*i == aItem )
{
items.erase( i );
break;
}
}
}
void LIB_SYMBOL::AddDrawItem( SCH_ITEM* aItem, bool aSort )
{
if( aItem )
{
aItem->SetParent( this );
m_drawings.push_back( aItem );
if( aSort )
m_drawings.sort();
}
}
std::vector<SCH_PIN*> LIB_SYMBOL::GetPins( int aUnit, int aBodyStyle ) const
{
std::vector<SCH_PIN*> pins;
/* Notes:
* when aUnit == 0: no unit filtering
* when aBodyStyle == 0: no body style filtering
* when m_unit == 0, the item is common to all units
* when m_bodyStyle == 0, the item is common to all body styles
*/
LIB_SYMBOL_SPTR parent = m_parent.lock();
const LIB_ITEMS_CONTAINER& drawItems = parent ? parent->m_drawings : m_drawings;
for( const SCH_ITEM& item : drawItems[SCH_PIN_T] )
{
// Unit filtering:
if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
continue;
// De Morgan variant filtering:
if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
continue;
// TODO: get rid of const_cast. (It used to be a C-style cast so was less noticeable.)
pins.push_back( const_cast<SCH_PIN*>( static_cast<const SCH_PIN*>( &item ) ) );
}
return pins;
}
std::vector<SCH_PIN*> LIB_SYMBOL::GetAllLibPins() const
{
return GetPins( 0, 0 );
}
int LIB_SYMBOL::GetPinCount()
{
return (int) GetPins( 0 /* all units */, 1 /* single body style */ ).size();
}
SCH_PIN* LIB_SYMBOL::GetPin( const wxString& aNumber, int aUnit, int aBodyStyle ) const
{
for( SCH_PIN* pin : GetPins( aUnit, aBodyStyle ) )
{
if( aNumber == pin->GetNumber() )
return pin;
}
return nullptr;
}
bool LIB_SYMBOL::PinsConflictWith( const LIB_SYMBOL& aOtherPart, bool aTestNums, bool aTestNames,
bool aTestType, bool aTestOrientation, bool aTestLength ) const
{
std::vector<SCH_PIN*> thisPinList = GetAllLibPins();
for( const SCH_PIN* eachThisPin : thisPinList )
{
wxASSERT( eachThisPin );
std::vector<SCH_PIN*> otherPinList = aOtherPart.GetAllLibPins();
bool foundMatch = false;
for( const SCH_PIN* eachOtherPin : otherPinList )
{
wxASSERT( eachOtherPin );
// Same unit?
if( eachThisPin->GetUnit() != eachOtherPin->GetUnit() )
continue;
// Same body stype?
if( eachThisPin->GetBodyStyle() != eachOtherPin->GetBodyStyle() )
continue;
// Same position?
if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
continue;
// Same number?
if( aTestNums && ( eachThisPin->GetNumber() != eachOtherPin->GetNumber() ) )
continue;
// Same name?
if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ) )
continue;
// Same electrical type?
if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ) )
continue;
// Same orientation?
if( aTestOrientation
&& ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ) )
continue;
// Same length?
if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ) )
continue;
foundMatch = true;
break; // Match found so search is complete.
}
if( !foundMatch )
{
// This means there was not an identical (according to the arguments)
// pin at the same position in the other symbol.
return true;
}
}
// The loop never gave up, so no conflicts were found.
return false;
}
const BOX2I LIB_SYMBOL::GetUnitBoundingBox( int aUnit, int aBodyStyle,
bool aIgnoreHiddenFields ) const
{
BOX2I bBox; // Start with a fresh BOX2I so the Merge algorithm works
for( const SCH_ITEM& item : m_drawings )
{
if( item.m_unit > 0 && m_unitCount > 1 && aUnit > 0 && aUnit != item.m_unit )
continue;
if( item.m_bodyStyle > 0 && aBodyStyle > 0 && aBodyStyle != item.m_bodyStyle )
continue;
if( aIgnoreHiddenFields && item.Type() == SCH_FIELD_T )
{
if( !static_cast<const SCH_FIELD&>( item ).IsVisible() )
continue;
}
bBox.Merge( item.GetBoundingBox() );
}
return bBox;
}
const BOX2I LIB_SYMBOL::GetBodyBoundingBox( int aUnit, int aBodyStyle, bool aIncludePins,
bool aIncludePrivateItems ) const
{
BOX2I bbox;
for( const SCH_ITEM& item : m_drawings )
{
if( item.m_unit > 0 && aUnit > 0 && aUnit != item.m_unit )
continue;
if( item.m_bodyStyle > 0 && aBodyStyle > 0 && aBodyStyle != item.m_bodyStyle )
continue;
if( item.IsPrivate() && !aIncludePrivateItems )
continue;
if( item.Type() == SCH_FIELD_T )
continue;
if( item.Type() == SCH_PIN_T )
{
const SCH_PIN& pin = static_cast<const SCH_PIN&>( item );
if( pin.IsVisible() )
{
// Note: the roots of the pins are always included for symbols that don't have
// a well-defined body.
if( aIncludePins )
bbox.Merge( pin.GetBoundingBox( false, false, false ) );
else
bbox.Merge( pin.GetPinRoot() );
}
}
else
{
bbox.Merge( item.GetBoundingBox() );
}
}
return bbox;
}