KiCad PCB EDA Suite
Loading...
Searching...
No Matches
picksymbol.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 (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <pgm_base.h>
27#include <symbol_library.h>
31#include <confirm.h>
32#include <sch_tool_utils.h>
33#include <eeschema_id.h>
34#include <general.h>
35#include <kidialog.h>
36#include <kiway.h>
37#include <symbol_viewer_frame.h>
40#include <algorithm>
41#include <sch_symbol.h>
42#include <sch_commit.h>
43#include <sch_edit_frame.h>
44#include <symbol_lib_table.h>
45#include <tool/tool_manager.h>
46#include <tools/sch_actions.h>
47#include <project_sch.h>
48
50
52 std::vector<PICKED_SYMBOL>& aHistoryList,
53 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
54 bool aShowFootprints, const LIB_ID* aHighlight,
55 bool aAllowFields )
56{
57 std::unique_lock<std::mutex> dialogLock( DIALOG_SYMBOL_CHOOSER::g_Mutex, std::defer_lock );
58
59 // One DIALOG_SYMBOL_CHOOSER dialog at a time. User probably can't handle more anyway.
60 if( !dialogLock.try_lock() )
61 return PICKED_SYMBOL();
62
63 bool aCancelled = false;
64
65 DIALOG_SYMBOL_CHOOSER dlg( this, aHighlight, aFilter, aHistoryList, aAlreadyPlaced,
66 aAllowFields, aShowFootprints, aCancelled );
67
68 if( aCancelled || dlg.ShowModal() == wxID_CANCEL )
69 return PICKED_SYMBOL();
70
71 PICKED_SYMBOL sel;
72 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
73
74 if( !id.IsValid() )
75 return PICKED_SYMBOL();
76
77 if( sel.Unit == 0 )
78 sel.Unit = 1;
79
80 sel.Fields = dlg.GetFields();
81 sel.LibId = id;
82
83 if( sel.LibId.IsValid() )
84 {
85 std::erase_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
86 {
87 return i.LibId == sel.LibId;
88 } );
89
90 aHistoryList.insert( aHistoryList.begin(), sel );
91 }
92
93 sel.KeepSymbol = dlg.GetKeepSymbol();
95 return sel;
96}
97
98
99void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
100{
101 SCH_COMMIT commit( m_toolManager );
102 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
103
104 if( !symbol )
105 return;
106
107 const int unitCount = symbol->GetUnitCount();
108 const int currentUnit = aSymbol->GetUnit();
109
110 if( unitCount <= 1 || currentUnit == aUnit )
111 return;
112
113 if( aUnit > unitCount )
114 aUnit = unitCount;
115
116 const SCH_SHEET_PATH& sheetPath = GetCurrentSheet();
117 bool swapWithOther = false;
118 std::optional<SCH_REFERENCE> otherSymbolRef = FindSymbolByRefAndUnit( *aSymbol->Schematic(),
119 aSymbol->GetRef( &sheetPath, false ),
120 aUnit );
121
122 if( otherSymbolRef )
123 {
124 const wxString targetUnitName = symbol->GetUnitDisplayName( aUnit, false );
125 const wxString currUnitName = symbol->GetUnitDisplayName( currentUnit, false );
126 wxString otherSheetName = otherSymbolRef->GetSheetPath().PathHumanReadable( true, true );
127
128 if( otherSheetName.IsEmpty() )
129 otherSheetName = _( "Root" );
130
131 const wxString msg = wxString::Format( _( "Symbol unit '%s' is already placed (on sheet '%s')" ),
132 targetUnitName, otherSheetName );
133
134 KIDIALOG dlg( this, msg, _( "Unit Already Placed" ), wxYES_NO | wxCANCEL | wxICON_WARNING );
135 dlg.SetYesNoLabels( wxString::Format( _( "&Swap '%s' and '%s'" ), targetUnitName, currUnitName ),
136 wxString::Format( _( "&Duplicate '%s'" ), targetUnitName ) );
137 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
138
139 int ret = dlg.ShowModal();
140
141 if( ret == wxID_CANCEL )
142 return;
143
144 if( ret == wxID_YES )
145 swapWithOther = true;
146 }
147
148 if( swapWithOther )
149 {
150 // We were reliably informed this would exist.
151 wxASSERT( otherSymbolRef );
152
153 SCH_SYMBOL* otherSymbol = otherSymbolRef->GetSymbol();
154
155 if( !otherSymbol->GetEditFlags() )
156 commit.Modify( otherSymbol, otherSymbolRef->GetSheetPath().LastScreen() );
157
158 // Give that symbol the unit we used to have
159 otherSymbol->SetUnitSelection( &otherSymbolRef->GetSheetPath(), currentUnit );
160 otherSymbol->SetUnit( currentUnit );
161 }
162
163 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
164 commit.Modify( aSymbol, GetScreen() );
165
166 // Update the unit number.
167 aSymbol->SetUnit( aUnit );
168 aSymbol->SetUnitSelection( &sheetPath, aUnit );
169
170 if( !commit.Empty() )
171 {
172 if( eeconfig()->m_AutoplaceFields.enable )
173 {
174 AUTOPLACE_ALGO fieldsAutoplaced = aSymbol->GetFieldsAutoplaced();
175
176 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
177 aSymbol->AutoplaceFields( GetScreen(), fieldsAutoplaced );
178 }
179
180 if( swapWithOther )
181 commit.Push( _( "Swap Units" ) );
182 else
183 commit.Push( _( "Change Unit" ) );
184 }
185}
186
187
189{
190 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
191 return;
192
193 SCH_COMMIT commit( m_toolManager );
194 wxString msg;
195
196 if( !aSymbol->GetLibSymbolRef()->HasAlternateBodyStyle() )
197 {
198 LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
199
200 msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
201 id.GetLibItemName().wx_str(),
202 id.GetLibNickname().wx_str() );
203 DisplayError( this, msg );
204 return;
205 }
206
207 commit.Modify( aSymbol, GetScreen() );
208
209 aSymbol->SetBodyStyle( aSymbol->GetBodyStyle() + 1 );
210
211 // ensure m_bodyStyle = 1 or 2
212 // 1 = shape 1 = first (base DeMorgan) alternate body style
213 // 2 = shape 2 = second (DeMorgan conversion) alternate body style
214 // > 2 is not currently supported
215 // When m_bodyStyle = val max, return to the first shape
216 if( aSymbol->GetBodyStyle() > BODY_STYLE::DEMORGAN )
217 aSymbol->SetBodyStyle( BODY_STYLE::BASE );
218
219 // If selected make sure all the now-included pins are selected
220 if( aSymbol->IsSelected() )
222
223 commit.Push( _( "Change Body Style" ) );
224}
225
226
227void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
228{
229 if( !aPin )
230 return;
231
232 SCH_COMMIT commit( m_toolManager );
233 commit.Modify( aPin, GetScreen() );
234
235 if( aFunction == aPin->GetName() )
236 aPin->SetAlt( wxEmptyString );
237 else
238 aPin->SetAlt( aFunction );
239
240 commit.Push( _( "Set Pin Function" ) );
241}
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: actions.h:224
bool Empty() const
Definition: commit.h:152
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition: commit.h:107
int ShowModal() override
std::vector< std::pair< FIELD_T, wxString > > GetFields() const
Get a list of fields edited by the user.
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:98
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:148
bool IsSelected() const
Definition: eda_item.h:127
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:50
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
Define a library symbol object.
Definition: lib_symbol.h:85
int GetUnitCount() const override
wxString GetUnitDisplayName(int aUnit, bool aLabel) const override
Return the user-defined display name for aUnit for symbols with units.
Definition: lib_symbol.cpp:287
PICKED_SYMBOL PickSymbolFromLibrary(const SYMBOL_LIBRARY_FILTER *aFilter, std::vector< PICKED_SYMBOL > &aHistoryList, std::vector< PICKED_SYMBOL > &aAlreadyPlaced, bool aShowFootprints, const LIB_ID *aHighlight=nullptr, bool aAllowFields=true)
Call the library viewer to select symbol to import into schematic.
Definition: picksymbol.cpp:51
EESCHEMA_SETTINGS * eeconfig() const
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:489
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SetAltPinFunction(SCH_PIN *aPin, const wxString &aFunction)
Definition: picksymbol.cpp:227
SCH_SHEET_PATH & GetCurrentSheet() const
void FlipBodyStyle(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:188
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:99
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:246
int GetBodyStyle() const
Definition: sch_item.h:248
int GetUnit() const
Definition: sch_item.h:239
virtual void SetUnit(int aUnit)
Definition: sch_item.h:238
AUTOPLACE_ALGO GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition: sch_item.h:597
void SetAlt(const wxString &aAlt)
Set the name of the alternate pin.
Definition: sch_pin.cpp:389
const wxString & GetName() const
Definition: sch_pin.cpp:357
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition: sch_symbol.h:75
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
Automatically orient all the fields in the symbol.
void SetBodyStyle(int aBodyStyle) override
Definition: sch_symbol.cpp:413
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:164
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:702
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:183
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:558
Helper object to filter a list of libraries.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:171
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:169
This file is part of the common library.
#define _(s)
This file is part of the common library.
see class PGM_BASE
AUTOPLACE_ALGO
Definition: sch_item.h:69
@ AUTOPLACE_MANUAL
Definition: sch_item.h:72
@ AUTOPLACE_AUTO
Definition: sch_item.h:71
std::optional< SCH_REFERENCE > FindSymbolByRefAndUnit(const SCHEMATIC &aSchematic, const wxString &aRef, int aUnit)
Find a symbol by reference and unit.
bool PlaceAllUnits
Definition: sch_screen.h:85
bool KeepSymbol
Definition: sch_screen.h:84
LIB_ID LibId
Definition: sch_screen.h:80
std::vector< std::pair< FIELD_T, wxString > > Fields
Definition: sch_screen.h:87
Definition for symbol library class.