KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_editor_pin_tool.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) 2019 CERN
5 * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26
28#include <symbol_edit_frame.h>
29#include <sch_commit.h>
30#include <kidialog.h>
31#include <ee_actions.h>
33#include <increment.h>
36#include <pgm_base.h>
37#include <wx/debug.h>
38
39
43static bool g_LastPinCommonBodyStyle = false;
44static bool g_LastPinCommonUnit = false;
45static bool g_LastPinVisible = true;
46
47// The -1 is a non-valid value to trigger delayed initialization
48static int g_LastPinLength = -1;
49static int g_LastPinNameSize = -1;
50static int g_LastPinNumSize = -1;
51
52static int GetLastPinLength()
53{
54 if( g_LastPinLength == -1 )
55 {
57 g_LastPinLength = schIUScale.MilsToIU( settings->m_Defaults.pin_length );
58 }
59
60 return g_LastPinLength;
61}
62
64{
65 if( g_LastPinNameSize == -1 )
66 {
68 g_LastPinNameSize = schIUScale.MilsToIU( settings->m_Defaults.pin_name_size );
69 }
70
71 return g_LastPinNameSize;
72}
73
75{
76 if( g_LastPinNumSize == -1 )
77 {
79 g_LastPinNumSize = schIUScale.MilsToIU( settings->m_Defaults.pin_num_size );
80 }
81
82 return g_LastPinNumSize;
83}
84
85
87 EE_TOOL_BASE<SYMBOL_EDIT_FRAME>( "eeschema.PinEditing" )
88{
89}
90
91
93{
95
96 auto canEdit =
97 [&]( const SELECTION& sel )
98 {
100 wxCHECK( editor, false );
101
102 return editor->IsSymbolEditable() && !editor->IsSymbolAlias();
103 };
104
105 static const std::vector<KICAD_T> pinTypes = { SCH_PIN_T };
106
107 auto singlePinCondition = EE_CONDITIONS::Count( 1 ) && EE_CONDITIONS::OnlyTypes( pinTypes );
108
110
111 selToolMenu.AddSeparator( 250 );
112 selToolMenu.AddItem( EE_ACTIONS::pushPinLength, canEdit && singlePinCondition, 250 );
113 selToolMenu.AddItem( EE_ACTIONS::pushPinNameSize, canEdit && singlePinCondition, 250 );
114 selToolMenu.AddItem( EE_ACTIONS::pushPinNumSize, canEdit && singlePinCondition, 250 );
115
116 return true;
117}
118
119
120bool SYMBOL_EDITOR_PIN_TOOL::EditPinProperties( SCH_PIN* aPin, bool aFocusPinNumber )
121{
122 SCH_PIN original_pin( *aPin );
123 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin, aFocusPinNumber );
124 SCH_COMMIT commit( m_frame );
125 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
126
127 if( aPin->GetEditFlags() == 0 )
128 commit.Modify( parentSymbol );
129
130 if( dlg.ShowModal() == wxID_CANCEL )
131 return false;
132
133 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
134 {
135 // a pin can have a unit id = 0 (common to all units) to unit count
136 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
137 // when using the unit id of a pin as index
138 std::vector<bool> got_unit( parentSymbol->GetUnitCount() + 1 );
139
140 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
141
142 for( SCH_PIN* other : parentSymbol->GetPins() )
143 {
144 if( other == aPin )
145 continue;
146
150 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
151 continue;
152
153 if( other->GetPosition() == original_pin.GetPosition()
154 && other->GetOrientation() == original_pin.GetOrientation()
155 && other->GetType() == original_pin.GetType()
156 && other->IsVisible() == original_pin.IsVisible()
157 && other->GetName() == original_pin.GetName() )
158 {
159 if( aPin->GetBodyStyle() == 0 )
160 {
161 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
162 parentSymbol->RemoveDrawItem( other );
163 }
164 else if( other->GetBodyStyle() == aPin->GetBodyStyle() )
165 {
166 other->SetPosition( aPin->GetPosition() );
167 other->ChangeLength( aPin->GetLength() );
168 other->SetShape( aPin->GetShape() );
169 }
170
171 if( aPin->GetUnit() == 0 )
172 {
173 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
174 parentSymbol->RemoveDrawItem( other );
175 }
176
177 other->SetOrientation( aPin->GetOrientation() );
178 other->SetType( aPin->GetType() );
179 other->SetVisible( aPin->IsVisible() );
180 other->SetName( aPin->GetName() );
181 other->SetNameTextSize( aPin->GetNameTextSize() );
182 other->SetNumberTextSize( aPin->GetNumberTextSize() );
183
184 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
185 }
186 }
187 }
188
189 commit.Push( _( "Edit Pin Properties" ) );
190
191 std::vector<MSG_PANEL_ITEM> items;
192 aPin->GetMsgPanelInfo( m_frame, items );
193 m_frame->SetMsgPanel( items );
194
195 // Save the pin properties to use for the next new pin.
199 g_LastPinLength = aPin->GetLength();
200 g_LastPinShape = aPin->GetShape();
201 g_LastPinType = aPin->GetType();
203 g_LastPinCommonUnit = aPin->GetUnit() == 0;
204 g_LastPinVisible = aPin->IsVisible();
205
206 return true;
207}
208
209
211{
212 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
213 bool ask_for_pin = true; // Test for another pin in same position in other units
214
215 std::vector<SCH_PIN*> pins = symbol->GetAllLibPins();
216
217 for( SCH_PIN* test : pins )
218 {
219 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
220 continue;
221
222 // test for same body style
223 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
224 continue;
225
226 if( ask_for_pin && m_frame->SynchronizePins() )
227 {
228 wxString msg;
229 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
230 test->GetUnit() );
231
232 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
233 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
234 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
235
236 bool status = dlg.ShowModal() == wxID_OK;
237
238 if( !status )
239 {
240 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
241 delete aPin;
242
243 return false;
244 }
245 else
246 {
247 ask_for_pin = false;
248 }
249 }
250 }
251
252 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
253 {
255 g_LastPinType = aPin->GetType();
256 g_LastPinShape = aPin->GetShape();
257
258 if( m_frame->SynchronizePins() )
259 CreateImagePins( aPin );
260
261 symbol->AddDrawItem( aPin );
262 aPin->ClearFlags( IS_NEW );
263 }
264
265 // Put linked pins in new position, and clear flags
266 for( SCH_PIN* pin : pins )
267 {
268 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
269 continue;
270
271 pin->SetPosition( aPin->GetPosition() );
272 pin->ClearFlags();
273 }
274
276 m_frame->OnModify();
277
278 return true;
279}
280
281
282/*
283 * Create a new pin.
284 */
286{
287 aSymbol->ClearTempFlags();
288
289 SCH_PIN* pin = new SCH_PIN( aSymbol );
290
291 pin->SetFlags( IS_NEW );
292
293 // Flag pins to consider
294 if( m_frame->SynchronizePins() )
295 pin->SetFlags( IS_LINKED );
296
297 pin->SetPosition( aPosition );
298 pin->SetLength( GetLastPinLength() );
299 pin->SetOrientation( g_LastPinOrient );
300 pin->SetType( g_LastPinType );
301 pin->SetShape( g_LastPinShape );
302 pin->SetNameTextSize( GetLastPinNameSize() );
303 pin->SetNumberTextSize( GetLastPinNumSize() );
304 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
305 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
306 pin->SetVisible( g_LastPinVisible );
307
308 if( !EditPinProperties( pin, false ) )
309 {
310 delete pin;
311 pin = nullptr;
312 }
313
314 return pin;
315}
316
317
319{
320 int ii;
321 SCH_PIN* newPin;
322
323 // if "synchronize pins editing" option is off, do not create any similar pin for other
324 // units and/or shapes: each unit is edited regardless other units or body
325 if( !m_frame->SynchronizePins() )
326 return;
327
328 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
329 return;
330
331 // When units are interchangeable, all units are expected to have similar pins
332 // at the same position
333 // to facilitate pin editing, create pins for all other units for the current body style
334 // at the same position as aPin
335
336 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
337 {
338 if( ii == aPin->GetUnit() )
339 continue;
340
341 newPin = static_cast<SCH_PIN*>( aPin->Duplicate() );
342
343 // To avoid mistakes, gives this pin a new pin number because
344 // it does no have the save pin number as the master pin
345 // Because we do not know the actual number, give it a temporary number
346 wxString unknownNum;
347 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
348 newPin->SetNumber( unknownNum );
349
350 newPin->SetUnit( ii );
351
352 try
353 {
354 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
355 symbol->AddDrawItem( newPin );
356 }
357 catch( const boost::bad_pointer& e )
358 {
359 wxFAIL_MSG( wxString::Format( wxT( "Boost pointer exception occurred: %s" ),
360 e.what() ));
361 delete newPin;
362 return;
363 }
364
365 newPin->ClearFlags( IS_NEW );
366 }
367}
368
369
371{
372 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
374 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
375
376 if( !sourcePin )
377 return 0;
378
379 saveCopyInUndoList( symbol, UNDO_REDO::LIBEDIT );
380
381 for( SCH_PIN* pin : symbol->GetAllLibPins() )
382 {
383 if( pin == sourcePin )
384 continue;
385
386 if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
387 {
388 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
389 pin->ChangeLength( sourcePin->GetLength() );
390 }
391 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
392 {
393 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
394 }
395 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
396 {
397 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
398 }
399 }
400
402 m_frame->OnModify();
403
404 return 0;
405}
406
407
408// Create a new pin based on the previous pin with an incremented pin number.
410{
411 SCH_COMMIT commit( m_frame );
412 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
413
414 commit.Modify( symbol );
415
416 SCH_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate() );
417 VECTOR2I step;
418
419 pin->ClearFlags();
420 pin->SetFlags( IS_NEW );
421
423
424 switch( pin->GetOrientation() )
425 {
426 default:
427 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU(-settings->m_Repeat.pin_step); break;
428 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU(settings->m_Repeat.pin_step); break;
429 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU(settings->m_Repeat.pin_step); break;
430 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU(-settings->m_Repeat.pin_step); break;
431 }
432
433 pin->Move( step );
434
435 wxString nextName = pin->GetName();
436 IncrementString( nextName, settings->m_Repeat.label_delta );
437 pin->SetName( nextName );
438
439 wxString nextNumber = pin->GetNumber();
440 IncrementString( nextNumber, settings->m_Repeat.label_delta );
441 pin->SetNumber( nextNumber );
442
443 if( m_frame->SynchronizePins() )
444 pin->SetFlags( IS_LINKED );
445
446 if( PlacePin( pin ) )
447 {
448 commit.Push( _( "Repeat Pin" ) );
449 return pin;
450 }
451
452 return nullptr;
453}
454
455
457{
461}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
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
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
int ShowModal() override
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:133
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:129
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:131
bool IsNew() const
Definition: eda_item.h:107
static TOOL_ACTION pushPinNameSize
Definition: ee_actions.h:263
static TOOL_ACTION pushPinNumSize
Definition: ee_actions.h:264
static TOOL_ACTION pushPinLength
Definition: ee_actions.h:262
EE_SELECTION & GetSelection()
A foundation class for a tool operating on a schematic or symbol.
Definition: ee_tool_base.h:48
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false, bool aDirtyConnectivity=true)
Definition: ee_tool_base.h:143
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:200
bool Init() override
Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:64
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
Define a library symbol object.
Definition: lib_symbol.h:78
std::vector< SCH_PIN * > GetPins(int aUnit=0, int aBodyStyle=0) const
Return a list of pin object pointers from the draw item list.
Definition: lib_symbol.cpp:810
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
std::vector< SCH_PIN * > GetAllLibPins() const
Return a list of pin pointers for all units / converts.
Definition: lib_symbol.cpp:842
void RemoveDrawItem(SCH_ITEM *aItem)
Remove draw aItem from list.
Definition: lib_symbol.cpp:771
int GetUnitCount() const override
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Definition: lib_symbol.cpp:796
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
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:432
const SYMBOL * GetParentSymbol() const
Definition: sch_item.cpp:166
int GetBodyStyle() const
Definition: sch_item.h:232
int GetUnit() const
Definition: sch_item.h:229
virtual void SetUnit(int aUnit)
Definition: sch_item.h:228
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:131
int GetNumberTextSize() const
Definition: sch_pin.cpp:552
int GetLength() const
Definition: sch_pin.cpp:291
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: sch_pin.cpp:1537
void SetNumber(const wxString &aNumber)
Definition: sch_pin.cpp:514
bool IsVisible() const
Definition: sch_pin.cpp:362
const wxString & GetName() const
Definition: sch_pin.cpp:375
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:258
VECTOR2I GetPosition() const override
Definition: sch_pin.cpp:250
int GetNameTextSize() const
Definition: sch_pin.cpp:527
const wxString & GetNumber() const
Definition: sch_pin.h:117
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:271
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:304
static SELECTION_CONDITION Count(int aNumber)
Create a functor that tests if the number of selected items is equal to the value given as parameter.
static SELECTION_CONDITION OnlyTypes(std::vector< KICAD_T > aTypes)
Create a functor that tests if the selected items are only of given types.
EDA_ITEM * Front() const
Definition: selection.h:172
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
void setTransitions() override
< Set up handlers for various events.
int PushPinProperties(const TOOL_EVENT &aEvent)
bool EditPinProperties(SCH_PIN *aPin, bool aFocusPinNumber)
SCH_PIN * RepeatPin(const SCH_PIN *aSourcePin)
bool Init() override
Init() is called once upon a registration of the tool.
SCH_PIN * CreatePin(const VECTOR2I &aPosition, LIB_SYMBOL *aSymbol)
void CreateImagePins(SCH_PIN *aPin)
The symbol library editor main window.
int GetBodyStyle() const
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:82
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
TOOL_MENU & GetToolMenu()
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
#define _(s)
#define IS_PASTED
Modifier on IS_NEW which indicates it came from clipboard.
#define IS_NEW
New item, just created.
#define IS_LINKED
Used in calculation to mark linked items (temporary use)
KICOMMON_API bool IncrementString(wxString &name, int aIncrement)
Generic string incrementer.
Definition: increment.cpp:33
This file is part of the common library.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
@ PT_INPUT
usual pin input: must be connected
PIN_ORIENTATION
The symbol library pin object orientations.
Definition: pin_type.h:78
@ PIN_RIGHT
The pin extends rightwards from the connection point.
GRAPHIC_PINSHAPE
Definition: pin_type.h:57
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
static GRAPHIC_PINSHAPE g_LastPinShape
static bool g_LastPinCommonUnit
static bool g_LastPinVisible
static int GetLastPinNameSize()
static ELECTRICAL_PINTYPE g_LastPinType
static bool g_LastPinCommonBodyStyle
static int GetLastPinLength()
static int g_LastPinLength
static int g_LastPinNameSize
static int g_LastPinNumSize
static int GetLastPinNumSize()
static PIN_ORIENTATION g_LastPinOrient
@ SCH_PIN_T
Definition: typeinfo.h:153