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 <project_sch.h>
23#include <symbol_edit_frame.h>
24#include <template_fieldnames.h>
25#include <lib_symbol.h>
26#include <sch_field.h>
28
29
35
42
43
44void LIB_SYMBOL_LIBRARY_MANAGER::Sync( const wxString& aForceRefresh,
45 std::function<void( int, int, const wxString& )> aProgressCallback )
46{
47 m_logger->Activate();
48 {
49 getAdapter()->Sync( aForceRefresh, std::move( aProgressCallback ) );
50 }
51 m_logger->Deactivate();
52}
53
54
55std::unique_ptr<LIB_SYMBOL> LIB_SYMBOL_LIBRARY_MANAGER::CreateSymbol( const NEW_SYMBOL_PROPERTIES& aProps,
56 LIB_SYMBOL* aParent )
57{
58 std::unique_ptr<LIB_SYMBOL> new_symbol = std::make_unique<LIB_SYMBOL>( aProps.name );
59
60 if( !aParent )
61 {
62 new_symbol->GetReferenceField().SetText( aProps.reference );
63 new_symbol->SetUnitCount( aProps.unitCount, true );
64
65 if( aProps.pinNameInside )
66 {
67 new_symbol->SetPinNameOffset( aProps.pinTextPosition );
68
69 if( new_symbol->GetPinNameOffset() == 0 )
70 new_symbol->SetPinNameOffset( 1 );
71 }
72 else
73 {
74 new_symbol->SetPinNameOffset( 0 );
75 }
76
77 ( aProps.powerSymbol ) ? new_symbol->SetGlobalPower() : new_symbol->SetNormal();
78 new_symbol->SetShowPinNumbers( aProps.showPinNumber );
79 new_symbol->SetShowPinNames( aProps.showPinName );
80 new_symbol->LockUnits( !aProps.unitsInterchangeable );
81 new_symbol->SetExcludedFromBOM( !aProps.includeInBom );
82 new_symbol->SetExcludedFromBoard( !aProps.includeOnBoard );
83
84 if( aProps.unitCount < 2 )
85 new_symbol->LockUnits( false );
86
87 if( aProps.alternateBodyStyle )
88 new_symbol->SetBodyStyleCount( 2, false, true );
89 }
90 else
91 {
92 new_symbol->SetParent( aParent );
93
94 for( FIELD_T fieldId : MANDATORY_FIELDS )
95 {
96 SCH_FIELD* field = new_symbol->GetField( fieldId );
97 SCH_FIELD* parentField = aParent->GetField( fieldId );
98
99 *field = *parentField;
100
101 switch( fieldId )
102 {
104 break;
105
106 case FIELD_T::VALUE:
107 if( aParent->IsPower() )
108 field->SetText( aProps.name );
109 break;
110
112 if( !aProps.keepFootprint )
113 field->SetText( wxEmptyString );
114 break;
115
117 if( !aProps.keepDatasheet )
118 field->SetText( wxEmptyString );
119 break;
120
121 default:
122 break;
123 }
124
125 field->SetParent( new_symbol.get() );
126 }
127
128 if( aProps.transferUserFields )
129 {
130 std::vector<SCH_FIELD*> listFields;
131 aParent->GetFields( listFields );
132
133 for( SCH_FIELD* field : listFields )
134 {
135 if( field->GetId() == FIELD_T::USER )
136 {
137 SCH_FIELD* new_field = new SCH_FIELD( *field );
138
139 if( !aProps.keepContentUserFields )
140 new_field->SetText( wxEmptyString );
141
142 new_field->SetParent( new_symbol.get() );
143 new_symbol->AddField( new_field );
144 }
145 }
146 }
147 }
148
149 return new_symbol;
150}
151
152
153bool LIB_SYMBOL_LIBRARY_MANAGER::CreateNewSymbol( const wxString& aLibrary, const NEW_SYMBOL_PROPERTIES& aProps )
154{
155 LIB_SYMBOL* parent = nullptr;
156
157 if( !aProps.parentSymbolName.IsEmpty() )
158 parent = GetSymbol( aProps.parentSymbolName, aLibrary );
159
160 std::unique_ptr<LIB_SYMBOL> new_symbol = CreateSymbol( aProps, parent );
161
162 if( !UpdateSymbol( new_symbol.get(), aLibrary ) )
163 return false;
164
165 return true;
166}
167
168
170{
171 static_cast<SYMBOL_EDIT_FRAME&>( m_frame ).SyncLibraries( false );
172}
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
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.
LIB_SYMBOL_LIBRARY_MANAGER(SYMBOL_EDIT_FRAME &aFrame)
Symbol library management helper that is specific to the symbol library editor frame.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Define a library symbol object.
Definition lib_symbol.h:83
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_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".