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>
30#include <core/kicad_algo.h>
32#include <confirm.h>
33#include <ee_tool_utils.h>
34#include <eeschema_id.h>
35#include <general.h>
36#include <kidialog.h>
37#include <kiway.h>
38#include <symbol_viewer_frame.h>
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/ee_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 DIALOG_SYMBOL_CHOOSER dlg( this, aHighlight, aFilter, aHistoryList, aAlreadyPlaced,
64 aAllowFields, aShowFootprints );
65
66 if( dlg.ShowModal() == wxID_CANCEL )
67 return PICKED_SYMBOL();
68
69 PICKED_SYMBOL sel;
70 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
71
72 if( !id.IsValid() )
73 return PICKED_SYMBOL();
74
75 if( sel.Unit == 0 )
76 sel.Unit = 1;
77
78 sel.Fields = dlg.GetFields();
79 sel.LibId = id;
80
81 if( sel.LibId.IsValid() )
82 {
83 alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
84 {
85 return i.LibId == sel.LibId;
86 } );
87
88 aHistoryList.insert( aHistoryList.begin(), sel );
89 }
90
91 return sel;
92}
93
94
95void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
96{
97 SCH_COMMIT commit( m_toolManager );
98 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
99
100 if( !symbol )
101 return;
102
103 const int unitCount = symbol->GetUnitCount();
104 const int currentUnit = aSymbol->GetUnit();
105
106 if( unitCount <= 1 || currentUnit == aUnit )
107 return;
108
109 if( aUnit > unitCount )
110 aUnit = unitCount;
111
112 const SCH_SHEET_PATH& sheetPath = GetCurrentSheet();
113 bool swapWithOther = false;
114 std::optional<SCH_REFERENCE> otherSymbolRef = FindSymbolByRefAndUnit(
115 *aSymbol->Schematic(), aSymbol->GetRef( &sheetPath, false ), aUnit );
116
117 if( otherSymbolRef )
118 {
119 const wxString targetUnitName = symbol->GetUnitDisplayName( aUnit );
120 const wxString currUnitName = symbol->GetUnitDisplayName( currentUnit );
121 wxString otherSheetName = otherSymbolRef->GetSheetPath().PathHumanReadable( true, true );
122 if( otherSheetName.IsEmpty() )
123 otherSheetName = _( "Root" );
124
125 const wxString msg =
126 wxString::Format( _( "Symbol unit '%s' is already placed (on sheet '%s')" ),
127 targetUnitName, otherSheetName );
128
129 KIDIALOG dlg( this, msg, _( "Unit Already Placed" ), wxYES_NO | wxCANCEL | wxICON_WARNING );
130 dlg.SetYesNoLabels(
131 wxString::Format( _( "&Swap '%s' and '%s'" ), targetUnitName, currUnitName ),
132 wxString::Format( _( "&Duplicate '%s'" ), targetUnitName ) );
133 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
134
135 int ret = dlg.ShowModal();
136
137 if( ret == wxID_CANCEL )
138 return;
139
140 if( ret == wxID_YES )
141 swapWithOther = true;
142 }
143
144 if( swapWithOther )
145 {
146 // We were reliably informed this would exist.
147 wxASSERT( otherSymbolRef );
148
149 SCH_SYMBOL* otherSymbol = otherSymbolRef->GetSymbol();
150
151 if( !otherSymbol->GetEditFlags() )
152 commit.Modify( otherSymbol, otherSymbolRef->GetSheetPath().LastScreen() );
153
154 // Give that symbol the unit we used to have
155 otherSymbol->SetUnitSelection( &otherSymbolRef->GetSheetPath(), currentUnit );
156 otherSymbol->SetUnit( currentUnit );
157 }
158
159 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
160 commit.Modify( aSymbol, GetScreen() );
161
162 // Update the unit number.
163 aSymbol->SetUnit( aUnit );
164 aSymbol->SetUnitSelection( &sheetPath, aUnit );
165
166 if( !commit.Empty() )
167 {
168 if( eeconfig()->m_AutoplaceFields.enable )
169 {
170 AUTOPLACE_ALGO fieldsAutoplaced = aSymbol->GetFieldsAutoplaced();
171
172 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
173 aSymbol->AutoplaceFields( GetScreen(), fieldsAutoplaced );
174 }
175
176 commit.Push( _( "Change Unit" ) );
177 }
178}
179
180
182{
183 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
184 return;
185
186 SCH_COMMIT commit( m_toolManager );
187 wxString msg;
188
189 if( !aSymbol->GetLibSymbolRef()->HasAlternateBodyStyle() )
190 {
191 LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
192
193 msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
194 id.GetLibItemName().wx_str(),
195 id.GetLibNickname().wx_str() );
196 DisplayError( this, msg );
197 return;
198 }
199
200 commit.Modify( aSymbol, GetScreen() );
201
202 aSymbol->SetBodyStyle( aSymbol->GetBodyStyle() + 1 );
203
204 // ensure m_bodyStyle = 1 or 2
205 // 1 = shape 1 = first (base DeMorgan) alternate body style
206 // 2 = shape 2 = second (DeMorgan conversion) alternate body style
207 // > 2 is not currently supported
208 // When m_bodyStyle = val max, return to the first shape
209 if( aSymbol->GetBodyStyle() > BODY_STYLE::DEMORGAN )
210 aSymbol->SetBodyStyle( BODY_STYLE::BASE );
211
212 // If selected make sure all the now-included pins are selected
213 if( aSymbol->IsSelected() )
215
216 commit.Push( _( "Change Body Style" ) );
217}
218
219
220void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
221{
222 if( !aPin )
223 return;
224
225 SCH_COMMIT commit( m_toolManager );
226 commit.Modify( aPin, GetScreen() );
227
228 if( aFunction == aPin->GetName() )
229 aPin->SetAlt( wxEmptyString );
230 else
231 aPin->SetAlt( aFunction );
232
233 commit.Push( _( "Set Pin Function" ) );
234}
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:108
bool Empty() const
Definition: commit.h:150
int ShowModal() override
std::vector< std::pair< int, 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:89
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:133
bool IsSelected() const
Definition: eda_item.h:110
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
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:84
wxString GetUnitDisplayName(int aUnit) override
Return the user-defined display name for aUnit for symbols with units.
Definition: lib_symbol.cpp:289
int GetUnitCount() const override
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:432
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:220
SCH_SHEET_PATH & GetCurrentSheet() const
void FlipBodyStyle(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:181
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:95
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:150
int GetBodyStyle() const
Definition: sch_item.h:233
int GetUnit() const
Definition: sch_item.h:230
virtual void SetUnit(int aUnit)
Definition: sch_item.h:229
AUTOPLACE_ALGO GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition: sch_item.h:549
void SetAlt(const wxString &aAlt)
Definition: sch_pin.h:146
const wxString & GetName() const
Definition: sch_pin.cpp:388
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:77
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:412
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:166
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:848
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:185
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:704
Helper object to filter a list of libraries.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:167
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, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
This file is part of the common library.
#define _(s)
std::optional< SCH_REFERENCE > FindSymbolByRefAndUnit(const SCHEMATIC &aSchematic, const wxString &aRef, int aUnit)
Find a symbol by reference and unit.
This file is part of the common library.
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:174
see class PGM_BASE
AUTOPLACE_ALGO
Definition: sch_item.h:68
@ AUTOPLACE_MANUAL
Definition: sch_item.h:71
@ AUTOPLACE_AUTO
Definition: sch_item.h:70
LIB_ID LibId
Definition: sch_screen.h:79
std::vector< std::pair< int, wxString > > Fields
Definition: sch_screen.h:83
Definition for symbol library class.