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 The 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 {
58
60 }
61
62 return g_LastPinLength;
63}
64
66{
67 if( g_LastPinNameSize == -1 )
68 {
71
73 }
74
75 return g_LastPinNameSize;
76}
77
79{
80 if( g_LastPinNumSize == -1 )
81 {
84
86 }
87
88 return g_LastPinNumSize;
89}
90
91
93 EE_TOOL_BASE<SYMBOL_EDIT_FRAME>( "eeschema.PinEditing" )
94{
95}
96
97
99{
101
102 auto canEdit =
103 [&]( const SELECTION& sel )
104 {
106 wxCHECK( editor, false );
107
108 return editor->IsSymbolEditable() && !editor->IsSymbolAlias();
109 };
110
111 static const std::vector<KICAD_T> pinTypes = { SCH_PIN_T };
112
113 auto singlePinCondition = EE_CONDITIONS::Count( 1 ) && EE_CONDITIONS::OnlyTypes( pinTypes );
114
116
117 selToolMenu.AddSeparator( 250 );
118 selToolMenu.AddItem( EE_ACTIONS::pushPinLength, canEdit && singlePinCondition, 250 );
119 selToolMenu.AddItem( EE_ACTIONS::pushPinNameSize, canEdit && singlePinCondition, 250 );
120 selToolMenu.AddItem( EE_ACTIONS::pushPinNumSize, canEdit && singlePinCondition, 250 );
121
122 return true;
123}
124
125
126bool SYMBOL_EDITOR_PIN_TOOL::EditPinProperties( SCH_PIN* aPin, bool aFocusPinNumber )
127{
128 SCH_PIN original_pin( *aPin );
129 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin, aFocusPinNumber );
130 SCH_COMMIT commit( m_frame );
131 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
132
133 if( aPin->GetEditFlags() == 0 )
134 commit.Modify( parentSymbol );
135
136 if( dlg.ShowModal() == wxID_CANCEL )
137 return false;
138
139 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
140 {
141 // a pin can have a unit id = 0 (common to all units) to unit count
142 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
143 // when using the unit id of a pin as index
144 std::vector<bool> got_unit( parentSymbol->GetUnitCount() + 1 );
145
146 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
147
148 for( SCH_PIN* other : parentSymbol->GetPins() )
149 {
150 if( other == aPin )
151 continue;
152
156 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
157 continue;
158
159 if( other->GetPosition() == original_pin.GetPosition()
160 && other->GetOrientation() == original_pin.GetOrientation()
161 && other->GetType() == original_pin.GetType()
162 && other->IsVisible() == original_pin.IsVisible()
163 && other->GetName() == original_pin.GetName() )
164 {
165 if( aPin->GetBodyStyle() == 0 )
166 {
167 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
168 parentSymbol->RemoveDrawItem( other );
169 }
170
171 if( other->GetBodyStyle() == aPin->GetBodyStyle() )
172 {
173 other->ChangeLength( aPin->GetLength() );
174
175 // Must be done after ChangeLenght(), which can alter the position
176 other->SetPosition( aPin->GetPosition() );
177
178 other->SetShape( aPin->GetShape() );
179 }
180
181 if( aPin->GetUnit() == 0 )
182 {
183 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
184 parentSymbol->RemoveDrawItem( other );
185 }
186
187 other->SetOrientation( aPin->GetOrientation() );
188 other->SetType( aPin->GetType() );
189 other->SetVisible( aPin->IsVisible() );
190 other->SetName( aPin->GetName() );
191 other->SetNameTextSize( aPin->GetNameTextSize() );
192 other->SetNumberTextSize( aPin->GetNumberTextSize() );
193
194 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
195 }
196 }
197 }
198
199 commit.Push( _( "Edit Pin Properties" ) );
200
201 std::vector<MSG_PANEL_ITEM> items;
202 aPin->GetMsgPanelInfo( m_frame, items );
203 m_frame->SetMsgPanel( items );
204
205 // Save the pin properties to use for the next new pin.
209 g_LastPinLength = aPin->GetLength();
210 g_LastPinShape = aPin->GetShape();
211 g_LastPinType = aPin->GetType();
213 g_LastPinCommonUnit = aPin->GetUnit() == 0;
214 g_LastPinVisible = aPin->IsVisible();
215
216 return true;
217}
218
219
221{
222 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
223 bool ask_for_pin = true; // Test for another pin in same position in other units
224
225 std::vector<SCH_PIN*> pins = symbol->GetPins();
226
227 for( SCH_PIN* test : pins )
228 {
229 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
230 continue;
231
232 // test for same body style
233 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
234 continue;
235
236 if( ask_for_pin && m_frame->SynchronizePins() )
237 {
238 wxString msg;
239 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
240 test->GetUnit() );
241
242 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
243 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
244 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
245
246 bool status = dlg.ShowModal() == wxID_OK;
247
248 if( !status )
249 {
250 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
251 delete aPin;
252
253 return false;
254 }
255 else
256 {
257 ask_for_pin = false;
258 }
259 }
260 }
261
262 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
263 {
265 g_LastPinType = aPin->GetType();
266 g_LastPinShape = aPin->GetShape();
267
268 if( m_frame->SynchronizePins() )
269 CreateImagePins( aPin );
270
271 symbol->AddDrawItem( aPin );
272 aPin->ClearFlags( IS_NEW );
273 }
274
275 // Put linked pins in new position, and clear flags
276 for( SCH_PIN* pin : pins )
277 {
278 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
279 continue;
280
281 pin->SetPosition( aPin->GetPosition() );
282 pin->ClearFlags();
283 }
284
286 m_frame->OnModify();
287
288 return true;
289}
290
291
292/*
293 * Create a new pin.
294 */
296{
297 aSymbol->ClearTempFlags();
298
299 SCH_PIN* pin = new SCH_PIN( aSymbol );
300
301 pin->SetFlags( IS_NEW );
302
303 // Flag pins to consider
304 if( m_frame->SynchronizePins() )
305 pin->SetFlags( IS_LINKED );
306
307 pin->SetPosition( aPosition );
308 pin->SetLength( GetLastPinLength() );
309 pin->SetOrientation( g_LastPinOrient );
310 pin->SetType( g_LastPinType );
311 pin->SetShape( g_LastPinShape );
312 pin->SetNameTextSize( GetLastPinNameSize() );
313 pin->SetNumberTextSize( GetLastPinNumSize() );
314 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
315 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
316 pin->SetVisible( g_LastPinVisible );
317
318 if( !EditPinProperties( pin, false ) )
319 {
320 delete pin;
321 pin = nullptr;
322 }
323
324 return pin;
325}
326
327
329{
330 int ii;
331 SCH_PIN* newPin;
332
333 // if "synchronize pins editing" option is off, do not create any similar pin for other
334 // units and/or shapes: each unit is edited regardless other units or body
335 if( !m_frame->SynchronizePins() )
336 return;
337
338 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
339 return;
340
341 // When units are interchangeable, all units are expected to have similar pins
342 // at the same position
343 // to facilitate pin editing, create pins for all other units for the current body style
344 // at the same position as aPin
345
346 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
347 {
348 if( ii == aPin->GetUnit() )
349 continue;
350
351 newPin = static_cast<SCH_PIN*>( aPin->Duplicate() );
352
353 // To avoid mistakes, gives this pin a new pin number because
354 // it does no have the save pin number as the master pin
355 // Because we do not know the actual number, give it a temporary number
356 wxString unknownNum;
357 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
358 newPin->SetNumber( unknownNum );
359
360 newPin->SetUnit( ii );
361
362 try
363 {
364 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
365 symbol->AddDrawItem( newPin );
366 }
367 catch( const boost::bad_pointer& e )
368 {
369 wxFAIL_MSG( wxString::Format( wxT( "Boost pointer exception occurred: %s" ),
370 e.what() ));
371 delete newPin;
372 return;
373 }
374
375 newPin->ClearFlags( IS_NEW );
376 }
377}
378
379
381{
382 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
384 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
385
386 if( !sourcePin )
387 return 0;
388
389 saveCopyInUndoList( symbol, UNDO_REDO::LIBEDIT );
390
391 for( SCH_PIN* pin : symbol->GetPins() )
392 {
393 if( pin == sourcePin )
394 continue;
395
396 if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
397 {
398 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
399 pin->ChangeLength( sourcePin->GetLength() );
400 }
401 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
402 {
403 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
404 }
405 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
406 {
407 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
408 }
409 }
410
412 m_frame->OnModify();
413
414 return 0;
415}
416
417
418// Create a new pin based on the previous pin with an incremented pin number.
420{
421 SCH_COMMIT commit( m_frame );
422 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
423
424 commit.Modify( symbol );
425
426 SCH_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate() );
427 VECTOR2I step;
428
429 pin->ClearFlags();
430 pin->SetFlags( IS_NEW );
431
433 SYMBOL_EDITOR_SETTINGS* cfg = mgr.GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" );
434
435 switch( pin->GetOrientation() )
436 {
437 default:
438 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU( -cfg->m_Repeat.pin_step ); break;
439 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
440 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step) ; break;
441 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU( -cfg->m_Repeat.pin_step ); break;
442 }
443
444 pin->Move( step );
445
446 wxString nextName = pin->GetName();
447 IncrementString( nextName, cfg->m_Repeat.label_delta );
448 pin->SetName( nextName );
449
450 wxString nextNumber = pin->GetNumber();
451 IncrementString( nextNumber, cfg->m_Repeat.label_delta );
452 pin->SetNumber( nextNumber );
453
454 if( m_frame->SynchronizePins() )
455 pin->SetFlags( IS_LINKED );
456
457 if( PlacePin( pin ) )
458 {
459 commit.Push( _( "Repeat Pin" ) );
460 return pin;
461 }
462
463 return nullptr;
464}
465
466
468{
472}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:108
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)
Shows the 'do not show again' checkbox.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
Define a library symbol object.
Definition: lib_symbol.h:84
std::vector< SCH_PIN * > GetPins(int aUnit, int aBodyStyle) const
Return a list of pin object pointers from the draw item list.
Definition: lib_symbol.cpp:851
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
void RemoveDrawItem(SCH_ITEM *aItem)
Remove draw aItem from list.
Definition: lib_symbol.cpp:812
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:837
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:432
const SYMBOL * GetParentSymbol() const
Definition: sch_item.cpp:166
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
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:564
int GetLength() const
Definition: sch_pin.cpp:295
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:1551
void SetNumber(const wxString &aNumber)
Definition: sch_pin.cpp:527
bool IsVisible() const
Definition: sch_pin.cpp:374
const wxString & GetName() const
Definition: sch_pin.cpp:388
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:260
VECTOR2I GetPosition() const override
Definition: sch_pin.cpp:252
int GetNameTextSize() const
Definition: sch_pin.cpp:540
const wxString & GetNumber() const
Definition: sch_pin.h:116
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:274
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:309
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(const wxString &aFilename)
Return a handle to the a given settings by type.
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:168
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:1073
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