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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <sch_commit.h>
24#include <kidialog.h>
25#include <sch_actions.h>
27#include <increment.h>
30#include <pgm_base.h>
31#include <wx/debug.h>
32
33
37static bool g_LastPinCommonBodyStyle = false;
38static bool g_LastPinCommonUnit = false;
39static bool g_LastPinVisible = true;
40
41// The -1 is a non-valid value to trigger delayed initialization
42static int g_LastPinLength = -1;
43static int g_LastPinNameSize = -1;
44static int g_LastPinNumSize = -1;
45
46static int GetLastPinLength()
47{
48 if( g_LastPinLength == -1 )
49 {
51 g_LastPinLength = schIUScale.MilsToIU( cfg->m_Defaults.pin_length );
52 }
53
54 return g_LastPinLength;
55}
56
58{
59 if( g_LastPinNameSize == -1 )
60 {
62 g_LastPinNameSize = schIUScale.MilsToIU( cfg->m_Defaults.pin_name_size );
63 }
64
65 return g_LastPinNameSize;
66}
67
69{
70 if( g_LastPinNumSize == -1 )
71 {
73 g_LastPinNumSize = schIUScale.MilsToIU( cfg->m_Defaults.pin_num_size );
74 }
75
76 return g_LastPinNumSize;
77}
78
79
84
85
87{
89
90 auto canEdit =
91 [this]( const SELECTION& sel )
92 {
94
95 return editor && editor->IsSymbolEditable() && !editor->IsSymbolAlias();
96 };
97
98 static const std::vector<KICAD_T> pinTypes = { SCH_PIN_T };
99
100 auto singlePinCondition = SCH_CONDITIONS::Count( 1 ) && SCH_CONDITIONS::OnlyTypes( pinTypes );
101
102 CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
103
104 selToolMenu.AddSeparator( 250 );
105 selToolMenu.AddItem( SCH_ACTIONS::pushPinLength, canEdit && singlePinCondition, 250 );
106 selToolMenu.AddItem( SCH_ACTIONS::pushPinNameSize, canEdit && singlePinCondition, 250 );
107 selToolMenu.AddItem( SCH_ACTIONS::pushPinNumSize, canEdit && singlePinCondition, 250 );
108
109 return true;
110}
111
112
113bool SYMBOL_EDITOR_PIN_TOOL::EditPinProperties( SCH_PIN* aPin, bool aFocusPinNumber )
114{
115 SCH_PIN original_pin( *aPin );
116 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin, aFocusPinNumber );
117 SCH_COMMIT commit( m_frame );
118 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
119
120 if( aPin->GetEditFlags() == 0 )
121 commit.Modify( parentSymbol, m_frame->GetScreen() );
122
123 if( dlg.ShowModal() == wxID_CANCEL )
124 return false;
125
126 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
127 {
128 // a pin can have a unit id = 0 (common to all units) to unit count
129 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
130 // when using the unit id of a pin as index
131 std::vector<bool> got_unit( parentSymbol->GetUnitCount() + 1 );
132
133 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
134
135 for( SCH_PIN* other : parentSymbol->GetPins() )
136 {
137 if( other == aPin )
138 continue;
139
143 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
144 continue;
145
146 if( other->GetPosition() == original_pin.GetPosition()
147 && other->GetOrientation() == original_pin.GetOrientation()
148 && other->GetType() == original_pin.GetType()
149 && other->IsVisible() == original_pin.IsVisible()
150 && other->GetName() == original_pin.GetName() )
151 {
152 if( aPin->GetBodyStyle() == 0 )
153 {
154 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
155 parentSymbol->RemoveDrawItem( other );
156 }
157
158 if( other->GetBodyStyle() == aPin->GetBodyStyle() )
159 {
160 other->ChangeLength( aPin->GetLength() );
161
162 // Must be done after ChangeLenght(), which can alter the position
163 other->SetPosition( aPin->GetPosition() );
164
165 other->SetShape( aPin->GetShape() );
166 }
167
168 if( aPin->GetUnit() == 0 )
169 {
170 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
171 parentSymbol->RemoveDrawItem( other );
172 }
173
174 other->SetOrientation( aPin->GetOrientation() );
175 other->SetType( aPin->GetType() );
176 other->SetVisible( aPin->IsVisible() );
177 other->SetName( aPin->GetName() );
178 other->SetNameTextSize( aPin->GetNameTextSize() );
179 other->SetNumberTextSize( aPin->GetNumberTextSize() );
180
181 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
182 }
183 }
184 }
185
186 commit.Push( _( "Edit Pin Properties" ) );
187
188 std::vector<MSG_PANEL_ITEM> items;
189 aPin->GetMsgPanelInfo( m_frame, items );
190 m_frame->SetMsgPanel( items );
191
192 // Save the pin properties to use for the next new pin.
196 g_LastPinLength = aPin->GetLength();
197 g_LastPinShape = aPin->GetShape();
198 g_LastPinType = aPin->GetType();
200 g_LastPinCommonUnit = aPin->GetUnit() == 0;
201 g_LastPinVisible = aPin->IsVisible();
202
203 return true;
204}
205
206
208{
209 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
210 bool ask_for_pin = true; // Test for another pin in same position in other units
211
212 std::vector<SCH_PIN*> pins = symbol->GetPins();
213
214 for( SCH_PIN* test : pins )
215 {
216 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
217 continue;
218
219 // test for same body style
220 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
221 continue;
222
223 if( ask_for_pin && m_frame->SynchronizePins() )
224 {
225 wxString msg;
226 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
227 test->GetUnit() );
228
229 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
230 dlg.SetExtendedMessage( _( "Disable the 'Synchronized Pins Mode' option to avoid this message." ) );
231 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
232 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
233
234 bool status = dlg.ShowModal() == wxID_OK;
235
236 if( !status )
237 {
238 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
239 delete aPin;
240
241 return false;
242 }
243 else
244 {
245 ask_for_pin = false;
246 }
247 }
248 }
249
250 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
251 {
253 g_LastPinType = aPin->GetType();
254 g_LastPinShape = aPin->GetShape();
255
256 if( m_frame->SynchronizePins() )
257 CreateImagePins( aCommit, aPin );
258
259 symbol->AddDrawItem( aPin );
260 aPin->ClearFlags( IS_NEW );
261 }
262
263 // Put linked pins in new position, and clear flags
264 for( SCH_PIN* pin : pins )
265 {
266 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
267 continue;
268
269 pin->SetPosition( aPin->GetPosition() );
270 pin->ClearFlags();
271 }
272
273 m_frame->RebuildView();
274 m_frame->OnModify();
275
276 return true;
277}
278
279
280/*
281 * Create a new pin.
282 */
284{
285 aSymbol->ClearTempFlags();
286
287 SCH_PIN* pin = new SCH_PIN( aSymbol );
288
289 pin->SetFlags( IS_NEW );
290
291 // Flag pins to consider
292 if( m_frame->SynchronizePins() )
293 pin->SetFlags( IS_LINKED );
294
295 pin->SetPosition( aPosition );
296 pin->SetLength( GetLastPinLength() );
297 pin->SetOrientation( g_LastPinOrient );
298 pin->SetType( g_LastPinType );
299 pin->SetShape( g_LastPinShape );
300 pin->SetNameTextSize( GetLastPinNameSize() );
301 pin->SetNumberTextSize( GetLastPinNumSize() );
302 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
303 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
304 pin->SetVisible( g_LastPinVisible );
305
306 if( !EditPinProperties( pin, false ) )
307 {
308 delete pin;
309 pin = nullptr;
310 }
311
312 return pin;
313}
314
315
317{
318 int ii;
319 SCH_PIN* newPin;
320
321 // if "synchronize pins editing" option is off, do not create any similar pin for other
322 // units and/or shapes: each unit is edited regardless other units or body
323 if( !m_frame->SynchronizePins() )
324 return;
325
326 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
327 return;
328
329 // When units are interchangeable, all units are expected to have similar pins
330 // at the same position
331 // to facilitate pin editing, create pins for all other units for the current body style
332 // at the same position as aPin
333
334 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
335 {
336 if( ii == aPin->GetUnit() )
337 continue;
338
339 // Already called Modify() on parent symbol; no need for Modify() calls on individual items
341 newPin = static_cast<SCH_PIN*>( aPin->Duplicate( true, &dummy ) );
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" ), e.what() ));
360 delete newPin;
361 return;
362 }
363
364 newPin->ClearFlags( IS_NEW );
365 }
366}
367
368
370{
371 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
372 SCH_SELECTION& selection = m_selectionTool->GetSelection();
373 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
374
375 if( !sourcePin )
376 return 0;
377
379
380 for( SCH_PIN* pin : symbol->GetPins() )
381 {
382 if( pin == sourcePin )
383 continue;
384
385 if( aEvent.IsAction( &SCH_ACTIONS::pushPinLength ) )
386 {
387 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
388 pin->ChangeLength( sourcePin->GetLength() );
389 }
390 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNameSize ) )
391 {
392 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
393 }
394 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNumSize ) )
395 {
396 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
397 }
398 }
399
400 m_frame->RebuildView();
401 m_frame->OnModify();
402
403 return 0;
404}
405
406
407// Create a new pin based on the previous pin with an incremented pin number.
409{
410 SCH_COMMIT commit( m_frame );
411 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
412
413 commit.Modify( symbol, m_frame->GetScreen() );
414
415 SCH_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate( true, &commit ) );
416
417 pin->ClearFlags();
418 pin->SetFlags( IS_NEW );
419
421 {
422 VECTOR2I step;
423
424 switch( pin->GetOrientation() )
425 {
426 default:
427 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
428 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
429 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step) ; break;
430 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
431 }
432
433 pin->Move( step );
434
435 wxString nextName = pin->GetName();
436 IncrementString( nextName, cfg->m_Repeat.label_delta );
437 pin->SetName( nextName );
438
439 wxString nextNumber = pin->GetNumber();
440 IncrementString( nextNumber, cfg->m_Repeat.label_delta );
441 pin->SetNumber( nextNumber );
442 }
443
444 if( m_frame->SynchronizePins() )
445 pin->SetFlags( IS_LINKED );
446
447 if( PlacePin( &commit, pin ) )
448 {
449 commit.Push( _( "Repeat Pin" ) );
450 return pin;
451 }
452
453 return nullptr;
454}
455
456
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
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:102
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
EDA_ITEM_FLAGS GetEditFlags() const
Definition eda_item.h:158
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:154
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:156
bool IsNew() const
Definition eda_item.h:129
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:38
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:51
int ShowModal() override
Definition kidialog.cpp:89
Define a library symbol object.
Definition lib_symbol.h:79
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
void RemoveDrawItem(SCH_ITEM *aItem)
Remove draw aItem from list.
std::vector< SCH_PIN * > GetPins() const override
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.
static TOOL_ACTION pushPinLength
static TOOL_ACTION pushPinNameSize
static TOOL_ACTION pushPinNumSize
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
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:160
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:274
int GetBodyStyle() const
Definition sch_item.h:242
int GetUnit() const
Definition sch_item.h:233
virtual void SetUnit(int aUnit)
Definition sch_item.h:232
int GetNumberTextSize() const
Definition sch_pin.cpp:774
int GetLength() const
Definition sch_pin.cpp:388
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:1468
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:734
bool IsVisible() const
Definition sch_pin.cpp:480
const wxString & GetName() const
Definition sch_pin.cpp:494
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:353
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:345
int GetNameTextSize() const
Definition sch_pin.cpp:748
const wxString & GetNumber() const
Definition sch_pin.h:127
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:367
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:402
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.
SCH_TOOL_BASE(const std::string &aName)
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:173
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.
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.
void Go(int(SYMBOL_EDIT_FRAME::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
#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:29
see class PGM_BASE
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
GRAPHIC_PINSHAPE
Definition pin_type.h:80
T * GetAppSettings(const char *aFilename)
std::vector< FAB_LAYER_COLOR > dummy
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
KIBIS_PIN * pin
@ SCH_PIN_T
Definition typeinfo.h:150
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683