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->IsSymbolGraphicallyEditable();
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
114 const SCH_PIN& aOriginalPin )
115{
116 // A pin can have a unit id = 0 (common to all units) to unit count, so we need a buffer
117 // size = GetUnitCount()+1 to store a value in a vector when using the unit id as index.
118 std::vector<bool> got_unit( aSymbol->GetUnitCount() + 1 );
119
120 auto markUnit =
121 [&got_unit]( int aUnit )
122 {
123 if( aUnit >= 0 && static_cast<size_t>( aUnit ) < got_unit.size() )
124 got_unit[static_cast<size_t>( aUnit )] = true;
125 };
126
127 auto unitDone =
128 [&got_unit]( int aUnit )
129 {
130 return aUnit >= 0 && static_cast<size_t>( aUnit ) < got_unit.size()
131 && got_unit[static_cast<size_t>( aUnit )];
132 };
133
134 markUnit( aPin->GetUnit() );
135
136 int removed = 0;
137
138 for( SCH_PIN* other : aSymbol->GetPins() )
139 {
140 if( other == aPin )
141 continue;
142
146 if( unitDone( other->GetUnit() ) )
147 continue;
148
149 if( other->GetPosition() != aOriginalPin.GetPosition()
150 || other->GetOrientation() != aOriginalPin.GetOrientation()
151 || other->GetType() != aOriginalPin.GetType()
152 || other->IsVisible() != aOriginalPin.IsVisible()
153 || other->GetName() != aOriginalPin.GetName() )
154 {
155 continue;
156 }
157
158 if( other->GetBodyStyle() == aPin->GetBodyStyle() )
159 {
160 other->ChangeLength( aPin->GetLength() );
161
162 // Must be done after ChangeLength(), which can alter the position
163 other->SetPosition( aPin->GetPosition() );
164
165 other->SetShape( aPin->GetShape() );
166 }
167
168 other->SetOrientation( aPin->GetOrientation() );
169 other->SetType( aPin->GetType() );
170 other->SetVisible( aPin->IsVisible() );
171 other->SetName( aPin->GetName() );
172 other->SetNameTextSize( aPin->GetNameTextSize() );
173 other->SetNumberTextSize( aPin->GetNumberTextSize() );
174
175 markUnit( other->GetUnit() );
176
177 // Unit or body style 0 means aPin applies to all of them, making the matching pin
178 // redundant. Delete last, because RemoveDrawItem() frees the pin.
179 bool coversBodyStyles = aPin->GetBodyStyle() == 0
180 && ( aPin->GetUnit() == 0 || other->GetUnit() == aPin->GetUnit() );
181 bool coversUnits = aPin->GetUnit() == 0
182 && ( aPin->GetBodyStyle() == 0
183 || other->GetBodyStyle() == aPin->GetBodyStyle() );
184
185 if( coversBodyStyles || coversUnits )
186 {
187 aSymbol->RemoveDrawItem( other );
188 removed++;
189 }
190 }
191
192 return removed;
193}
194
195
196bool SYMBOL_EDITOR_PIN_TOOL::EditPinProperties( SCH_PIN* aPin, bool aFocusPinNumber )
197{
198 SCH_PIN original_pin( *aPin );
199 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin, aFocusPinNumber );
200 SCH_COMMIT commit( m_frame );
201 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
202
203 if( aPin->GetEditFlags() == 0 )
204 commit.Modify( parentSymbol, m_frame->GetScreen() );
205
206 if( dlg.ShowModal() == wxID_CANCEL )
207 return false;
208
209 int removedPins = 0;
210
211 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
212 removedPins = SynchronizeOtherUnits( parentSymbol, aPin, original_pin );
213
214 commit.Push( _( "Edit Pin Properties" ) );
215
216 // RemoveDrawItem() leaves the freed pins in the view, where hit testing would hand them to
217 // the next edit. RebuildView() empties the selection container without clearing SELECTED,
218 // so drop the selection through the tool.
219 if( removedPins > 0 )
220 {
222 m_frame->RebuildView();
223 }
224
225 std::vector<MSG_PANEL_ITEM> items;
226 aPin->GetMsgPanelInfo( m_frame, items );
227 m_frame->SetMsgPanel( items );
228
229 // Save the pin properties to use for the next new pin.
233 g_LastPinLength = aPin->GetLength();
234 g_LastPinShape = aPin->GetShape();
235 g_LastPinType = aPin->GetType();
237 g_LastPinCommonUnit = aPin->GetUnit() == 0;
238 g_LastPinVisible = aPin->IsVisible();
239
240 return true;
241}
242
243
245{
246 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
247 bool ask_for_pin = true; // Test for another pin in same position in other units
248
249 std::vector<SCH_PIN*> pins = symbol->GetPins();
250
251 for( SCH_PIN* test : pins )
252 {
253 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
254 continue;
255
256 // test for same body style
257 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
258 continue;
259
260 if( ask_for_pin && m_frame->SynchronizePins() )
261 {
262 wxString msg;
263 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
264 test->GetUnit() );
265
266 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
267 dlg.SetExtendedMessage( _( "Disable the 'Synchronized Pins Mode' option to avoid this message." ) );
268 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
269 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
270
271 bool status = dlg.ShowModal() == wxID_OK;
272
273 if( !status )
274 {
275 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
276 delete aPin;
277
278 return false;
279 }
280 else
281 {
282 ask_for_pin = false;
283 }
284 }
285 }
286
287 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
288 {
290 g_LastPinType = aPin->GetType();
291 g_LastPinShape = aPin->GetShape();
292
293 if( m_frame->SynchronizePins() )
294 CreateImagePins( aCommit, aPin );
295
296 symbol->AddDrawItem( aPin );
297 aPin->ClearFlags( IS_NEW );
298 }
299
300 // Put linked pins in new position, and clear flags
301 for( SCH_PIN* pin : pins )
302 {
303 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
304 continue;
305
306 pin->SetPosition( aPin->GetPosition() );
307 pin->ClearFlags();
308 }
309
310 m_frame->RebuildView();
311 m_frame->OnModify();
312
313 return true;
314}
315
316
317/*
318 * Create a new pin.
319 */
321{
322 aSymbol->ClearTempFlags();
323
324 SCH_PIN* pin = new SCH_PIN( aSymbol );
325
326 pin->SetFlags( IS_NEW );
327
328 // Flag pins to consider
329 if( m_frame->SynchronizePins() )
330 pin->SetFlags( IS_LINKED );
331
332 pin->SetPosition( aPosition );
333 pin->SetLength( GetLastPinLength() );
334 pin->SetOrientation( g_LastPinOrient );
335 pin->SetType( g_LastPinType );
336 pin->SetShape( g_LastPinShape );
337 pin->SetNameTextSize( GetLastPinNameSize() );
338 pin->SetNumberTextSize( GetLastPinNumSize() );
339 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
340 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
341 pin->SetVisible( g_LastPinVisible );
342
343 if( !EditPinProperties( pin, false ) )
344 {
345 delete pin;
346 pin = nullptr;
347 }
348
349 return pin;
350}
351
352
354{
355 int ii;
356 SCH_PIN* newPin;
357
358 // if "synchronize pins editing" option is off, do not create any similar pin for other
359 // units and/or shapes: each unit is edited regardless other units or body
360 if( !m_frame->SynchronizePins() )
361 return;
362
363 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
364 return;
365
366 // When units are interchangeable, all units are expected to have similar pins
367 // at the same position
368 // to facilitate pin editing, create pins for all other units for the current body style
369 // at the same position as aPin
370
371 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
372 {
373 if( ii == aPin->GetUnit() )
374 continue;
375
376 // Already called Modify() on parent symbol; no need for Modify() calls on individual items
378 newPin = static_cast<SCH_PIN*>( aPin->Duplicate( true, &dummy ) );
379
380 // To avoid mistakes, gives this pin a new pin number because
381 // it does no have the save pin number as the master pin
382 // Because we do not know the actual number, give it a temporary number
383 wxString unknownNum;
384 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
385 newPin->SetNumber( unknownNum );
386
387 newPin->SetUnit( ii );
388
389 try
390 {
391 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
392 symbol->AddDrawItem( newPin );
393 }
394 catch( const boost::bad_pointer& e )
395 {
396 wxFAIL_MSG( wxString::Format( wxT( "Boost pointer exception occurred: %s" ), e.what() ));
397 delete newPin;
398 return;
399 }
400
401 newPin->ClearFlags( IS_NEW );
402 }
403}
404
405
407{
408 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
409 SCH_SELECTION& selection = m_selectionTool->GetSelection();
410 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
411
412 if( !sourcePin )
413 return 0;
414
416
417 for( SCH_PIN* pin : symbol->GetPins() )
418 {
419 if( pin == sourcePin )
420 continue;
421
422 if( aEvent.IsAction( &SCH_ACTIONS::pushPinLength ) )
423 {
424 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
425 pin->ChangeLength( sourcePin->GetLength() );
426 }
427 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNameSize ) )
428 {
429 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
430 }
431 else if( aEvent.IsAction( &SCH_ACTIONS::pushPinNumSize ) )
432 {
433 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
434 }
435 }
436
437 m_frame->RebuildView();
438 m_frame->OnModify();
439
440 return 0;
441}
442
443
444// Create a new pin based on the previous pin with an incremented pin number.
446{
447 SCH_COMMIT commit( m_frame );
448 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
449
450 commit.Modify( symbol, m_frame->GetScreen() );
451
452 SCH_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate( true, &commit ) );
453
454 pin->ClearFlags();
455 pin->SetFlags( IS_NEW );
456
458 {
459 VECTOR2I step;
460
461 switch( pin->GetOrientation() )
462 {
463 default:
464 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
465 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
466 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU( cfg->m_Repeat.pin_step) ; break;
467 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU( cfg->m_Repeat.pin_step ); break;
468 }
469
470 pin->Move( step );
471
472 wxString nextName = pin->GetName();
473 IncrementString( nextName, cfg->m_Repeat.label_delta );
474 pin->SetName( nextName );
475
476 wxString nextNumber = pin->GetNumber();
477 IncrementString( nextNumber, cfg->m_Repeat.label_delta );
478 pin->SetNumber( nextNumber );
479 }
480
481 if( m_frame->SynchronizePins() )
482 pin->SetFlags( IS_LINKED );
483
484 if( PlacePin( &commit, pin ) )
485 {
486 commit.Push( _( "Repeat Pin" ) );
487 return pin;
488 }
489
490 return nullptr;
491}
492
493
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
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:170
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:168
bool IsNew() const
Definition eda_item.h:131
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:119
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:170
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:287
int GetBodyStyle() const
Definition sch_item.h:247
int GetUnit() const
Definition sch_item.h:237
virtual void SetUnit(int aUnit)
Definition sch_item.h:236
int GetNumberTextSize() const
Definition sch_pin.cpp:865
int GetLength() const
Definition sch_pin.cpp:397
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:1608
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:825
bool IsVisible() const
Definition sch_pin.cpp:489
const wxString & GetName() const
Definition sch_pin.cpp:503
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:362
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:354
int GetNameTextSize() const
Definition sch_pin.cpp:839
const wxString & GetNumber() const
Definition sch_pin.h:142
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:376
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:411
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:176
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)
static int SynchronizeOtherUnits(LIB_SYMBOL *aSymbol, SCH_PIN *aPin, const SCH_PIN &aOriginalPin)
Propagate the edits made to aPin to the matching pins of the other units.
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:30
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:149
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683