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 (C) 2004-2023 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 <eeschema_id.h>
34#include <general.h>
35#include <kiway.h>
36#include <symbol_viewer_frame.h>
39#include <sch_symbol.h>
40#include <sch_commit.h>
41#include <sch_edit_frame.h>
42#include <symbol_lib_table.h>
43#include <tool/tool_manager.h>
44#include <tools/ee_actions.h>
45#include <project_sch.h>
46
48
50 std::vector<PICKED_SYMBOL>& aHistoryList,
51 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
52 bool aShowFootprints, const LIB_ID* aHighlight,
53 bool aAllowFields )
54{
55 std::unique_lock<std::mutex> dialogLock( DIALOG_SYMBOL_CHOOSER::g_Mutex, std::defer_lock );
56
57 // One DIALOG_SYMBOL_CHOOSER dialog at a time. User probably can't handle more anyway.
58 if( !dialogLock.try_lock() )
59 return PICKED_SYMBOL();
60
61 DIALOG_SYMBOL_CHOOSER dlg( this, aHighlight, aFilter, aHistoryList, aAlreadyPlaced,
62 aAllowFields, aShowFootprints );
63
64 if( dlg.ShowModal() == wxID_CANCEL )
65 return PICKED_SYMBOL();
66
67 PICKED_SYMBOL sel;
68 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
69
70 if( !id.IsValid() )
71 return PICKED_SYMBOL();
72
73 if( sel.Unit == 0 )
74 sel.Unit = 1;
75
76 sel.Fields = dlg.GetFields();
77 sel.LibId = id;
78
79 if( sel.LibId.IsValid() )
80 {
81 alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
82 {
83 return i.LibId == sel.LibId;
84 } );
85
86 aHistoryList.insert( aHistoryList.begin(), sel );
87 }
88
89 return sel;
90}
91
92
93void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
94{
95 SCH_COMMIT commit( m_toolManager );
96 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
97
98 if( !symbol )
99 return;
100
101 int unitCount = symbol->GetUnitCount();
102
103 if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
104 return;
105
106 if( aUnit > unitCount )
107 aUnit = unitCount;
108
109 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
110 commit.Modify( aSymbol, GetScreen() );
111
112 /* Update the unit number. */
113 aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
114 aSymbol->SetUnit( aUnit );
115
116 if( !commit.Empty() )
117 {
118 if( eeconfig()->m_AutoplaceFields.enable )
119 aSymbol->AutoAutoplaceFields( GetScreen() );
120
121 commit.Push( _( "Change Unit" ) );
122 }
123}
124
125
127{
128 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
129 return;
130
131 SCH_COMMIT commit( m_toolManager );
132 wxString msg;
133
134 if( !aSymbol->GetLibSymbolRef()->HasAlternateBodyStyle() )
135 {
136 LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
137
138 msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
139 id.GetLibItemName().wx_str(),
140 id.GetLibNickname().wx_str() );
141 DisplayError( this, msg );
142 return;
143 }
144
145 commit.Modify( aSymbol, GetScreen() );
146
147 aSymbol->SetBodyStyle( aSymbol->GetBodyStyle() + 1 );
148
149 // ensure m_bodyStyle = 1 or 2
150 // 1 = shape 1 = first (base DeMorgan) alternate body style
151 // 2 = shape 2 = second (DeMorgan conversion) alternate body style
152 // > 2 is not currently supported
153 // When m_bodyStyle = val max, return to the first shape
154 if( aSymbol->GetBodyStyle() > BODY_STYLE::DEMORGAN )
155 aSymbol->SetBodyStyle( BODY_STYLE::BASE );
156
157 // If selected make sure all the now-included pins are selected
158 if( aSymbol->IsSelected() )
160
161 commit.Push( _( "Change Body Style" ) );
162}
163
164
165void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
166{
167 if( !aPin )
168 return;
169
170 SCH_COMMIT commit( m_toolManager );
171 commit.Modify( aPin, GetScreen() );
172
173 if( aFunction == aPin->GetName() )
174 aPin->SetAlt( wxEmptyString );
175 else
176 aPin->SetAlt( aFunction );
177
178 commit.Push( _( "Set Pin Function" ) );
179}
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
bool Empty() const
Returns status of an item.
Definition: commit.h:144
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:88
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:132
bool IsSelected() const
Definition: eda_item.h:109
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
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:77
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:49
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
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:405
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:165
SCH_SHEET_PATH & GetCurrentSheet() const
void FlipBodyStyle(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:126
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:93
int GetBodyStyle() const
Definition: sch_item.h:240
virtual void SetUnit(int aUnit)
Definition: sch_item.h:236
void AutoAutoplaceFields(SCH_SCREEN *aScreen)
Autoplace fields only if correct to do so automatically.
Definition: sch_item.h:568
void SetAlt(const wxString &aAlt)
Definition: sch_pin.h:135
const wxString & GetName() const
Definition: sch_pin.cpp:351
Schematic symbol object.
Definition: sch_symbol.h:105
void SetBodyStyle(int aBodyStyle) override
Definition: sch_symbol.cpp:427
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:194
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:857
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:213
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:161
This file is part of the common library.
#define _(s)
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
LIB_ID LibId
Definition: sch_screen.h:80
std::vector< std::pair< int, wxString > > Fields
Definition: sch_screen.h:84
Definition for symbol library class.