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
30// TODO(JE) library tables -- this class doesn't need to exist anymore
38
39
40void LIB_SYMBOL_LIBRARY_MANAGER::Sync( const wxString& aForceRefresh,
41 std::function<void( int, int,
42 const wxString& )> aProgressCallback )
43{
44 m_logger->Activate();
45 {
46 getAdapter()->Sync( aForceRefresh, aProgressCallback );
48 m_syncHash = adapter->GetModifyHash();
49 }
50 m_logger->Deactivate();
51}
52
53
54std::unique_ptr<LIB_SYMBOL> LIB_SYMBOL_LIBRARY_MANAGER::CreateSymbol( const NEW_SYMBOL_PROPERTIES& aProps,
55 LIB_SYMBOL* aParent )
56{
57 std::unique_ptr<LIB_SYMBOL> new_symbol = std::make_unique<LIB_SYMBOL>( aProps.name );
58
59 if( !aParent )
60 {
61 new_symbol->GetReferenceField().SetText( aProps.reference );
62 new_symbol->SetUnitCount( aProps.unitCount, true );
63
64 if( aProps.pinNameInside )
65 {
66 new_symbol->SetPinNameOffset( aProps.pinTextPosition );
67
68 if( new_symbol->GetPinNameOffset() == 0 )
69 new_symbol->SetPinNameOffset( 1 );
70 }
71 else
72 {
73 new_symbol->SetPinNameOffset( 0 );
74 }
75
76 ( aProps.powerSymbol ) ? new_symbol->SetGlobalPower() : new_symbol->SetNormal();
77 new_symbol->SetShowPinNumbers( aProps.showPinNumber );
78 new_symbol->SetShowPinNames( aProps.showPinName );
79 new_symbol->LockUnits( !aProps.unitsInterchangeable );
80 new_symbol->SetExcludedFromBOM( !aProps.includeInBom );
81 new_symbol->SetExcludedFromBoard( !aProps.includeOnBoard );
82
83 if( aProps.unitCount < 2 )
84 new_symbol->LockUnits( false );
85
86 if( aProps.alternateBodyStyle )
87 new_symbol->SetBodyStyleCount( 2, false, true );
88 }
89 else
90 {
91 new_symbol->SetParent( aParent );
92
93 for( FIELD_T fieldId : MANDATORY_FIELDS )
94 {
95 SCH_FIELD* field = new_symbol->GetField( fieldId );
96 SCH_FIELD* parentField = aParent->GetField( fieldId );
97
98 *field = *parentField;
99
100 switch( fieldId )
101 {
103 break;
104
105 case FIELD_T::VALUE:
106 if( aParent->IsPower() )
107 field->SetText( aProps.name );
108 break;
109
111 if( !aProps.keepFootprint )
112 field->SetText( wxEmptyString );
113 break;
114
116 if( !aProps.keepDatasheet )
117 field->SetText( wxEmptyString );
118 break;
119
120 default:
121 break;
122 }
123
124 field->SetParent( new_symbol.get() );
125 }
126
127 if( aProps.transferUserFields )
128 {
129 std::vector<SCH_FIELD*> listFields;
130 aParent->GetFields( listFields );
131
132 for( SCH_FIELD* field : listFields )
133 {
134 if( field->GetId() == FIELD_T::USER )
135 {
136 SCH_FIELD* new_field = new SCH_FIELD( *field );
137
138 if( !aProps.keepContentUserFields )
139 new_field->SetText( wxEmptyString );
140
141 new_field->SetParent( new_symbol.get() );
142 new_symbol->AddField( new_field );
143 }
144 }
145 }
146 }
147
148 return new_symbol;
149}
150
151
152bool LIB_SYMBOL_LIBRARY_MANAGER::CreateNewSymbol( const wxString& aLibrary,
153 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.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.
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
void SetText(const wxString &aText) override
The symbol library editor main window.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
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".