KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_symbol_library_manager.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 modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation, either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License along
17* with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <lib_logger.h>
22#include <symbol_edit_frame.h>
23#include <symbol_lib_table.h>
24#include <template_fieldnames.h>
25#include <lib_symbol.h>
26#include <sch_field.h>
27
28
36
37
38void LIB_SYMBOL_LIBRARY_MANAGER::Sync( const wxString& aForceRefresh,
39 std::function<void( int, int,
40 const wxString& )> aProgressCallback )
41{
42 m_logger->Activate();
43 {
44 getAdapter()->Sync( aForceRefresh, aProgressCallback );
46 }
47 m_logger->Deactivate();
48}
49
50
51std::unique_ptr<LIB_SYMBOL> LIB_SYMBOL_LIBRARY_MANAGER::CreateSymbol( const NEW_SYMBOL_PROPERTIES& aProps,
52 LIB_SYMBOL* aParent )
53{
54 std::unique_ptr<LIB_SYMBOL> new_symbol = std::make_unique<LIB_SYMBOL>( aProps.name );
55
56 if( !aParent )
57 {
58 new_symbol->GetReferenceField().SetText( aProps.reference );
59 new_symbol->SetUnitCount( aProps.unitCount, true );
60
61 if( aProps.pinNameInside )
62 {
63 new_symbol->SetPinNameOffset( aProps.pinTextPosition );
64
65 if( new_symbol->GetPinNameOffset() == 0 )
66 new_symbol->SetPinNameOffset( 1 );
67 }
68 else
69 {
70 new_symbol->SetPinNameOffset( 0 );
71 }
72
73 ( aProps.powerSymbol ) ? new_symbol->SetGlobalPower() : new_symbol->SetNormal();
74 new_symbol->SetShowPinNumbers( aProps.showPinNumber );
75 new_symbol->SetShowPinNames( aProps.showPinName );
76 new_symbol->LockUnits( !aProps.unitsInterchangeable );
77 new_symbol->SetExcludedFromBOM( !aProps.includeInBom );
78 new_symbol->SetExcludedFromBoard( !aProps.includeOnBoard );
79
80 if( aProps.unitCount < 2 )
81 new_symbol->LockUnits( false );
82
83 if( aProps.alternateBodyStyle )
84 new_symbol->SetBodyStyleCount( 2, false, true );
85 }
86 else
87 {
88 new_symbol->SetParent( aParent );
89
90 for( FIELD_T fieldId : MANDATORY_FIELDS )
91 {
92 SCH_FIELD* field = new_symbol->GetField( fieldId );
93 SCH_FIELD* parentField = aParent->GetField( fieldId );
94
95 *field = *parentField;
96
97 switch( fieldId )
98 {
100 break;
101
102 case FIELD_T::VALUE:
103 if( aParent->IsPower() )
104 field->SetText( aProps.name );
105 break;
106
108 if( !aProps.keepFootprint )
109 field->SetText( wxEmptyString );
110 break;
111
113 if( !aProps.keepDatasheet )
114 field->SetText( wxEmptyString );
115 break;
116
117 default:
118 break;
119 }
120
121 field->SetParent( new_symbol.get() );
122 }
123
124 if( aProps.transferUserFields )
125 {
126 std::vector<SCH_FIELD*> listFields;
127 aParent->GetFields( listFields );
128
129 for( SCH_FIELD* field : listFields )
130 {
131 if( field->GetId() == FIELD_T::USER )
132 {
133 SCH_FIELD* new_field = new SCH_FIELD( *field );
134
135 if( !aProps.keepContentUserFields )
136 new_field->SetText( wxEmptyString );
137
138 new_field->SetParent( new_symbol.get() );
139 new_symbol->AddField( new_field );
140 }
141 }
142 }
143 }
144
145 return new_symbol;
146}
147
148
149bool LIB_SYMBOL_LIBRARY_MANAGER::CreateNewSymbol( const wxString& aLibrary,
150 const NEW_SYMBOL_PROPERTIES& aProps )
151{
152 LIB_SYMBOL* parent = nullptr;
153
154 if( !aProps.parentSymbolName.IsEmpty() )
155 parent = GetSymbol( aProps.parentSymbolName, aLibrary );
156
157 std::unique_ptr<LIB_SYMBOL> new_symbol = CreateSymbol( aProps, parent );
158
159 if( !UpdateSymbol( new_symbol.get(), aLibrary ) )
160 return false;
161
162 return true;
163}
164
165
167{
168 static_cast<SYMBOL_EDIT_FRAME&>( m_frame ).SyncLibraries( false );
169}
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
bool CreateNewSymbol(const wxString &aLibrary, const NEW_SYMBOL_PROPERTIES &aProps)
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
static std::unique_ptr< LIB_SYMBOL > CreateSymbol(const NEW_SYMBOL_PROPERTIES &aProps, LIB_SYMBOL *aParent)
void Sync(const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
Updates the SYMBOL_LIBRARY_MANAGER data to synchronize with Symbol Library Table.
int m_syncHash
Symbol lib table hash value from last synchronization.
LIB_SYMBOL_LIBRARY_MANAGER(SYMBOL_EDIT_FRAME &aFrame)
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Define a library symbol object.
Definition lib_symbol.h:85
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool IsPower() const override
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
void SetText(const wxString &aText) override
The symbol library editor main window.
LIB_SYMBOL * GetSymbol(const wxString &aSymbolName, const wxString &aLibrary) const
Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there is no w...
SYMBOL_LIB_TABLE * symTable() const
Return the current symbol library table.
SYMBOL_LIBRARY_MANAGER(SCH_BASE_FRAME &aFrame)
SCH_BASE_FRAME & m_frame
Parent frame.
bool UpdateSymbol(LIB_SYMBOL *aSymbol, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol.
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibs)
void Sync(const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
#define MANDATORY_FIELDS
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".