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
27#include <sch_commit.h>
28#include <kidialog.h>
29#include <sch_actions.h>
31#include <increment.h>
34#include <pgm_base.h>
35#include <wx/debug.h>
36
37
41static bool g_LastPinCommonBodyStyle = false;
42static bool g_LastPinCommonUnit = false;
43static bool g_LastPinVisible = true;
44
45// The -1 is a non-valid value to trigger delayed initialization
46static int g_LastPinLength = -1;
47static int g_LastPinNameSize = -1;
48static int g_LastPinNumSize = -1;
49
50static int GetLastPinLength()
51{
52 if( g_LastPinLength == -1 )
53 {
54 if( SYMBOL_EDITOR_SETTINGS* cfg = GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" ) )
55 g_LastPinLength = schIUScale.MilsToIU( cfg->m_Defaults.pin_length );
56 }
57
58 return g_LastPinLength;
59}
60
62{
63 if( g_LastPinNameSize == -1 )
64 {
65 if( SYMBOL_EDITOR_SETTINGS* cfg = GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" ) )
66 g_LastPinNameSize = schIUScale.MilsToIU( cfg->m_Defaults.pin_name_size );
67 }
68
69 return g_LastPinNameSize;
70}
71
73{
74 if( g_LastPinNumSize == -1 )
75 {
76 if( SYMBOL_EDITOR_SETTINGS* cfg = GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" ) )
77 g_LastPinNumSize = schIUScale.MilsToIU( cfg->m_Defaults.pin_num_size );
78 }
79
80 return g_LastPinNumSize;
81}
82
83
85 SCH_TOOL_BASE<SYMBOL_EDIT_FRAME>( "eeschema.PinEditing" )
86{
87}
88
89
91{
93
94 auto canEdit =
95 [&]( const SELECTION& sel )
96 {
98 wxCHECK( editor, false );
99
100 return editor->IsSymbolEditable() && !editor->IsSymbolAlias();
101 };
102
103 static const std::vector<KICAD_T> pinTypes = { SCH_PIN_T };
104
105 auto singlePinCondition = SCH_CONDITIONS::Count( 1 ) && SCH_CONDITIONS::OnlyTypes( pinTypes );
106
108
109 selToolMenu.AddSeparator( 250 );
110 selToolMenu.AddItem( SCH_ACTIONS::pushPinLength, canEdit && singlePinCondition, 250 );
111 selToolMenu.AddItem( SCH_ACTIONS::pushPinNameSize, canEdit && singlePinCondition, 250 );
112 selToolMenu.AddItem( SCH_ACTIONS::pushPinNumSize, canEdit && singlePinCondition, 250 );
113
114 return true;
115}
116
117
118bool SYMBOL_EDITOR_PIN_TOOL::EditPinProperties( SCH_PIN* aPin, bool aFocusPinNumber )
119{
120 SCH_PIN original_pin( *aPin );
121 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin, aFocusPinNumber );
122 SCH_COMMIT commit( m_frame );
123 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
124
125 if( aPin->GetEditFlags() == 0 )
126 commit.Modify( parentSymbol );
127
128 if( dlg.ShowModal() == wxID_CANCEL )
129 return false;
130
131 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
132 {
133 // a pin can have a unit id = 0 (common to all units) to unit count
134 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
135 // when using the unit id of a pin as index
136 std::vector<bool> got_unit( parentSymbol->GetUnitCount() + 1 );
137
138 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
139
140 for( SCH_PIN* other : parentSymbol->GetPins() )
141 {
142 if( other == aPin )
143 continue;
144
148 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
149 continue;
150
151 if( other->GetPosition() == original_pin.GetPosition()
152 && other->GetOrientation() == original_pin.GetOrientation()
153 && other->GetType() == original_pin.GetType()
154 && other->IsVisible() == original_pin.IsVisible()
155 && other->GetName() == original_pin.GetName() )
156 {
157 if( aPin->GetBodyStyle() == 0 )
158 {
159 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
160 parentSymbol->RemoveDrawItem( other );
161 }
162
163 if( other->GetBodyStyle() == aPin->GetBodyStyle() )
164 {
165 other->ChangeLength( aPin->GetLength() );
166
167 // Must be done after ChangeLenght(), which can alter the position
168 other->SetPosition( aPin->GetPosition() );
169
170 other->SetShape( aPin->GetShape() );
171 }
172
173 if( aPin->GetUnit() == 0 )
174 {
175 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
176 parentSymbol->RemoveDrawItem( other );
177 }
178
179 other->SetOrientation( aPin->GetOrientation() );
180 other->SetType( aPin->GetType() );
181 other->SetVisible( aPin->IsVisible() );
182 other->SetName( aPin->GetName() );
183 other->SetNameTextSize( aPin->GetNameTextSize() );
184 other->SetNumberTextSize( aPin->GetNumberTextSize() );
185
186 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
187 }
188 }
189 }
190
191 commit.Push( _( "Edit Pin Properties" ) );
192
193 std::vector<MSG_PANEL_ITEM> items;
194 aPin->GetMsgPanelInfo( m_frame, items );
195 m_frame->SetMsgPanel( items );
196
197 // Save the pin properties to use for the next new pin.
201 g_LastPinLength = aPin->GetLength();
202 g_LastPinShape = aPin->GetShape();
203 g_LastPinType = aPin->GetType();
205 g_LastPinCommonUnit = aPin->GetUnit() == 0;
206 g_LastPinVisible = aPin->IsVisible();
207
208 return true;
209}
210
211
213{
214 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
215 bool ask_for_pin = true; // Test for another pin in same position in other units
216
217 std::vector<SCH_PIN*> pins = symbol->GetPins();
218
219 for( SCH_PIN* test : pins )
220 {
221 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
222 continue;
223
224 // test for same body style
225 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
226 continue;
227
228 if( ask_for_pin && m_frame->SynchronizePins() )
229 {
230 wxString msg;
231 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
232 test->GetUnit() );
233
234 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
235 dlg.SetExtendedMessage( _( "Disable the 'Synchronized Pins Mode' option to avoid this message." ) );
236 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
237 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
238
239 bool status = dlg.ShowModal() == wxID_OK;
240
241 if( !status )
242 {
243 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
244 delete aPin;
245
246 return false;
247 }
248 else
249 {
250 ask_for_pin = false;
251 }
252 }
253 }
254
255 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
256 {
258 g_LastPinType = aPin->GetType();
259 g_LastPinShape = aPin->GetShape();
260
261 if( m_frame->SynchronizePins() )
262 CreateImagePins( aCommit, aPin );
263
264 symbol->AddDrawItem( aPin );
265 aPin->ClearFlags( IS_NEW );
266 }
267
268 // Put linked pins in new position, and clear flags
269 for( SCH_PIN* pin : pins )
270 {
271 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
272 continue;
273
274 pin->SetPosition( aPin->GetPosition() );
275 pin->ClearFlags();
276 }
277
279 m_frame->OnModify();
280
281 return true;
282}
283
284
285/*
286 * Create a new pin.
287 */
289{
290 aSymbol->ClearTempFlags();
291
292 SCH_PIN* pin = new SCH_PIN( aSymbol );
293
294 pin->SetFlags( IS_NEW );
295
296 // Flag pins to consider
297 if( m_frame->SynchronizePins() )
298 pin->SetFlags( IS_LINKED );
299
300 pin->SetPosition( aPosition );
301 pin->SetLength( GetLastPinLength() );
302 pin->SetOrientation( g_LastPinOrient );
303 pin->SetType( g_LastPinType );
304 pin->SetShape( g_LastPinShape );
305 pin->SetNameTextSize( GetLastPinNameSize() );
306 pin->SetNumberTextSize( GetLastPinNumSize() );
307 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
308 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
309 pin->SetVisible( g_LastPinVisible );
310
311 if( !EditPinProperties( pin, false ) )
312 {
313 delete pin;
314 pin = nullptr;
315 }
316
317 return pin;
318}
319
320
322{
323 int ii;
324 SCH_PIN* newPin;
325
326 // if "synchronize pins editing" option is off, do not create any similar pin for other
327 // units and/or shapes: each unit is edited regardless other units or body
328 if( !m_frame->SynchronizePins() )
329 return;
330
331 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
332 return;
333
334 // When units are interchangeable, all units are expected to have similar pins
335 // at the same position
336 // to facilitate pin editing, create pins for all other units for the current body style
337 // at the same position as aPin
338
339 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
340 {
341 if( ii == aPin->GetUnit() )
342 continue;
343
344 // Already called Modify() on parent symbol; no need for Modify() calls on individual items
346 newPin = static_cast<SCH_PIN*>( aPin->Duplicate( true, &dummy ) );
347
348 // To avoid mistakes, gives this pin a new pin number because
349 // it does no have the save pin number as the master pin
350 // Because we do not know the actual number, give it a temporary number
351 wxString unknownNum;
352 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
353 newPin->SetNumber( unknownNum );
354
355 newPin->SetUnit( ii );
356
357 try
358 {
359 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
360 symbol->AddDrawItem( newPin );
361 }
362 catch( const boost::bad_pointer& e )
363 {
364 wxFAIL_MSG( wxString::Format( wxT( "Boost pointer exception occurred: %s" ), e.what() ));
365 delete newPin;
366 return;
367 }
368
369 newPin->ClearFlags( IS_NEW );
370 }
371}
372
373
375{
376 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
378 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
379
380 if( !sourcePin )
381 return 0;
382
383 saveCopyInUndoList( symbol, UNDO_REDO::LIBEDIT );
384
385 for( SCH_PIN* pin : symbol->GetPins() )
386 {
387 if( pin == sourcePin )
388 continue;
389
390 if( aEvent.IsAction( &SCH_ACTIONS::pushPinLength ) )
391 {
392 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
393 pin->ChangeLength( sourcePin->GetLength() );
394 }
395 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNameSize ) )
396 {
397 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
398 }
399 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNumSize ) )
400 {
401 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
402 }
403 }
404
406 m_frame->OnModify();
407
408 return 0;
409}
410
411
412// Create a new pin based on the previous pin with an incremented pin number.
414{
415 SCH_COMMIT commit( m_frame );
416 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
417
418 commit.Modify( symbol );
419
420 SCH_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate( true, &commit ) );
421
422 pin->ClearFlags();
423 pin->SetFlags( IS_NEW );
424
425 if( SYMBOL_EDITOR_SETTINGS* cfg = GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" ) )
426 {
427 VECTOR2I step;
428
429 switch( pin->GetOrientation() )
430 {
431 default:
432 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
433 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
434 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step) ; break;
435 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
436 }
437
438 pin->Move( step );
439
440 wxString nextName = pin->GetName();
441 IncrementString( nextName, cfg->m_Repeat.label_delta );
442 pin->SetName( nextName );
443
444 wxString nextNumber = pin->GetNumber();
445 IncrementString( nextNumber, cfg->m_Repeat.label_delta );
446 pin->SetNumber( nextNumber );
447 }
448
449 if( m_frame->SynchronizePins() )
450 pin->SetFlags( IS_LINKED );
451
452 if( PlacePin( &commit, pin ) )
453 {
454 commit.Push( _( "Repeat Pin" ) );
455 return pin;
456 }
457
458 return nullptr;
459}
460
461
463{
467}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:114
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
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:148
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:144
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:146
bool IsNew() const
Definition: eda_item.h:124
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
Define a library symbol object.
Definition: lib_symbol.h:85
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:799
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:760
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:785
static TOOL_ACTION pushPinLength
Definition: sch_actions.h:248
static TOOL_ACTION pushPinNameSize
Definition: sch_actions.h:249
static TOOL_ACTION pushPinNumSize
Definition: sch_actions.h:250
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:489
SCH_ITEM * Duplicate(bool addToParentGroup, SCH_COMMIT *aCommit=nullptr, bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:137
const SYMBOL * GetParentSymbol() const
Definition: sch_item.cpp:252
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
int GetNumberTextSize() const
Definition: sch_pin.cpp:578
int GetLength() const
Definition: sch_pin.cpp:255
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:1141
void SetNumber(const wxString &aNumber)
Definition: sch_pin.cpp:541
bool IsVisible() const
Definition: sch_pin.cpp:343
const wxString & GetName() const
Definition: sch_pin.cpp:357
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:220
VECTOR2I GetPosition() const override
Definition: sch_pin.cpp:212
int GetNameTextSize() const
Definition: sch_pin.cpp:554
const wxString & GetNumber() const
Definition: sch_pin.h:123
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:234
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:269
SCH_SELECTION & GetSelection()
A foundation class for a tool operating on a schematic or symbol.
Definition: sch_tool_base.h:49
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false, bool aDirtyConnectivity=true)
bool Init() override
Init() is called once upon a registration of the tool.
Definition: sch_tool_base.h:65
SCH_SELECTION_TOOL * m_selectionTool
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:177
void setTransitions() override
< Set up handlers for various events.
int PushPinProperties(const TOOL_EVENT &aEvent)
bool EditPinProperties(SCH_PIN *aPin, bool aFocusPinNumber)
void CreateImagePins(SCH_COMMIT *aCommit, SCH_PIN *aPin)
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)
bool PlacePin(SCH_COMMIT *aCommit, 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.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
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.
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:105
@ PIN_RIGHT
The pin extends rightwards from the connection point.
GRAPHIC_PINSHAPE
Definition: pin_type.h:84
std::vector< FAB_LAYER_COLOR > dummy
constexpr int MilsToIU(int mils) const
Definition: base_units.h:97
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:154