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#include <symbol_edit_frame.h>
27#include <sch_commit.h>
28#include <confirm.h>
29#include <ee_actions.h>
33#include <pgm_base.h>
34#include <wx/log.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 auto* settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
55 g_LastPinLength = schIUScale.MilsToIU( settings->m_Defaults.pin_length );
56 }
57
58 return g_LastPinLength;
59}
60
62{
63 if( g_LastPinNameSize == -1 )
64 {
65 auto* settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
66 g_LastPinNameSize = schIUScale.MilsToIU( settings->m_Defaults.pin_name_size );
67 }
68
69 return g_LastPinNameSize;
70}
71
73{
74 if( g_LastPinNumSize == -1 )
75 {
76 auto* settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
77 g_LastPinNumSize = schIUScale.MilsToIU( settings->m_Defaults.pin_num_size );
78 }
79
80 return g_LastPinNumSize;
81}
82
83
84extern bool IncrementLabelMember( wxString& name, int aIncrement );
85
86
88 EE_TOOL_BASE<SYMBOL_EDIT_FRAME>( "eeschema.PinEditing" )
89{
90}
91
92
94{
96
97 auto canEdit =
98 [&]( const SELECTION& sel )
99 {
101 wxCHECK( editor, false );
102
103 return editor->IsSymbolEditable() && !editor->IsSymbolAlias();
104 };
105
106 auto singlePinCondition = EE_CONDITIONS::Count( 1 ) && EE_CONDITIONS::OnlyTypes( { LIB_PIN_T } );
107
109
110 selToolMenu.AddSeparator( 250 );
111 selToolMenu.AddItem( EE_ACTIONS::pushPinLength, canEdit && singlePinCondition, 250 );
112 selToolMenu.AddItem( EE_ACTIONS::pushPinNameSize, canEdit && singlePinCondition, 250 );
113 selToolMenu.AddItem( EE_ACTIONS::pushPinNumSize, canEdit && singlePinCondition, 250 );
114
115 return true;
116}
117
118
120{
121 LIB_PIN original_pin( *aPin );
122 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin );
123 SCH_COMMIT commit( m_frame );
124
125 if( aPin->GetEditFlags() == 0 )
126 commit.Modify( aPin->GetParent() );
127
128 if( dlg.ShowModal() == wxID_CANCEL )
129 return false;
130
131 if( !aPin->IsNew() && m_frame->SynchronizePins() && aPin->GetParent() )
132 {
133 LIB_PINS pinList;
134 aPin->GetParent()->GetPins( pinList );
135
136 // a pin can have a unit id = 0 (common to all units) to unit count
137 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
138 // when using the unit id of a pin as index
139 std::vector<bool> got_unit( aPin->GetParent()->GetUnitCount() + 1 );
140
141 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
142
143 for( LIB_PIN* other : pinList )
144 {
145 if( other == aPin )
146 continue;
147
151 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
152 continue;
153
154 if( other->GetPosition() == original_pin.GetPosition()
155 && other->GetOrientation() == original_pin.GetOrientation()
156 && other->GetType() == original_pin.GetType()
157 && other->IsVisible() == original_pin.IsVisible()
158 && other->GetName() == original_pin.GetName() )
159 {
160 if( aPin->GetBodyStyle() == 0 )
161 {
162 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
163 aPin->GetParent()->RemoveDrawItem( other );
164 }
165 else if( other->GetBodyStyle() == aPin->GetBodyStyle() )
166 {
167 other->SetPosition( aPin->GetPosition() );
168 other->ChangeLength( aPin->GetLength() );
169 other->SetShape( aPin->GetShape() );
170 }
171
172 if( aPin->GetUnit() == 0 )
173 {
174 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
175 aPin->GetParent()->RemoveDrawItem( other );
176 }
177
178 other->SetOrientation( aPin->GetOrientation() );
179 other->SetType( aPin->GetType() );
180 other->SetVisible( aPin->IsVisible() );
181 other->SetName( aPin->GetName() );
182 other->SetNameTextSize( aPin->GetNameTextSize() );
183 other->SetNumberTextSize( aPin->GetNumberTextSize() );
184
185 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
186 }
187 }
188 }
189
190 commit.Push( _( "Edit Pin Properties" ) );
191
192 std::vector<MSG_PANEL_ITEM> items;
193 aPin->GetMsgPanelInfo( m_frame, items );
194 m_frame->SetMsgPanel( items );
195
196 // Save the pin properties to use for the next new pin.
200 g_LastPinLength = aPin->GetLength();
201 g_LastPinShape = aPin->GetShape();
202 g_LastPinType = aPin->GetType();
204 g_LastPinCommonUnit = aPin->GetUnit() == 0;
205 g_LastPinVisible = aPin->IsVisible();
206
207 return true;
208}
209
210
212{
213 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
214 bool ask_for_pin = true; // Test for another pin in same position in other units
215
216 std::vector<LIB_PIN*> pins = symbol->GetAllLibPins();
217
218 for( LIB_PIN* test : pins )
219 {
220 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
221 continue;
222
223 // test for same body style
224 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
225 continue;
226
227 if( ask_for_pin && m_frame->SynchronizePins() )
228 {
229 wxString msg;
230 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
231 test->GetUnit() );
232
233 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
234 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
235 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
236
237 bool status = dlg.ShowModal() == wxID_OK;
238
239 if( !status )
240 {
241 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
242 delete aPin;
243
244 return false;
245 }
246 else
247 {
248 ask_for_pin = false;
249 }
250 }
251 }
252
253 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
254 {
256 g_LastPinType = aPin->GetType();
257 g_LastPinShape = aPin->GetShape();
258
259 if( m_frame->SynchronizePins() )
260 CreateImagePins( aPin );
261
262 symbol->AddDrawItem( aPin );
263 aPin->ClearFlags( IS_NEW );
264 }
265
266 // Put linked pins in new position, and clear flags
267 for( LIB_PIN* pin : pins )
268 {
269 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
270 continue;
271
272 pin->MoveTo( aPin->GetPosition() );
273 pin->ClearFlags();
274 }
275
277 m_frame->OnModify();
278
279 return true;
280}
281
282
283/*
284 * Create a new pin.
285 */
287{
288 aSymbol->ClearTempFlags();
289
290 LIB_PIN* pin = new LIB_PIN( aSymbol );
291
292 pin->SetFlags( IS_NEW );
293
294 // Flag pins to consider
295 if( m_frame->SynchronizePins() )
296 pin->SetFlags( IS_LINKED );
297
298 pin->MoveTo( aPosition );
299 pin->SetLength( GetLastPinLength() );
300 pin->SetOrientation( g_LastPinOrient );
301 pin->SetType( g_LastPinType );
302 pin->SetShape( g_LastPinShape );
303 pin->SetNameTextSize( GetLastPinNameSize() );
304 pin->SetNumberTextSize( GetLastPinNumSize() );
305 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
306 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
307 pin->SetVisible( g_LastPinVisible );
308
309 if( !EditPinProperties( pin ) )
310 {
311 delete pin;
312 pin = nullptr;
313 }
314
315 return pin;
316}
317
318
320{
321 int ii;
322 LIB_PIN* newPin;
323
324 // if "synchronize pins editing" option is off, do not create any similar pin for other
325 // units and/or shapes: each unit is edited regardless other units or body
326 if( !m_frame->SynchronizePins() )
327 return;
328
329 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
330 return;
331
332 // When units are interchangeable, all units are expected to have similar pins
333 // at the same position
334 // to facilitate pin editing, create pins for all other units for the current body style
335 // at the same position as aPin
336
337 for( ii = 1; ii <= aPin->GetParent()->GetUnitCount(); ii++ )
338 {
339 if( ii == aPin->GetUnit() )
340 continue;
341
342 newPin = (LIB_PIN*) aPin->Duplicate();
343
344 // To avoid mistakes, gives this pin a new pin number because
345 // it does no have the save pin number as the master pin
346 // Because we do not know the actual number, give it a temporary number
347 wxString unknownNum;
348 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
349 newPin->SetNumber( unknownNum );
350
351 newPin->SetUnit( ii );
352
353 try
354 {
355 aPin->GetParent()->AddDrawItem( newPin );
356 }
357 catch( const boost::bad_pointer& e )
358 {
359 wxLogError( "Cannot add new pin to symbol. Boost pointer error %s occurred.",
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 LIB_PIN* sourcePin = dynamic_cast<LIB_PIN*>( selection.Front() );
375
376 if( !sourcePin )
377 return 0;
378
379 saveCopyInUndoList( symbol, UNDO_REDO::LIBEDIT );
380 std::vector<LIB_PIN*> pins = symbol->GetAllLibPins();
381
382 for( LIB_PIN* pin : pins )
383 {
384 if( pin == sourcePin )
385 continue;
386
387 if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
388 {
389 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
390 pin->ChangeLength( sourcePin->GetLength() );
391 }
392 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
393 {
394 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
395 }
396 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
397 {
398 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
399 }
400 }
401
403 m_frame->OnModify();
404
405 return 0;
406}
407
408
409// Create a new pin based on the previous pin with an incremented pin number.
411{
412 LIB_PIN* pin = (LIB_PIN*) aSourcePin->Duplicate();
413 VECTOR2I step;
414
415 pin->ClearFlags();
416 pin->SetFlags( IS_NEW );
417
418 auto* settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
419
420 switch( pin->GetOrientation() )
421 {
422 case PIN_ORIENTATION::PIN_UP: step.x = schIUScale.MilsToIU(settings->m_Repeat.pin_step); break;
423 case PIN_ORIENTATION::PIN_DOWN: step.x = schIUScale.MilsToIU(settings->m_Repeat.pin_step); break;
424 case PIN_ORIENTATION::PIN_LEFT: step.y = schIUScale.MilsToIU(-settings->m_Repeat.pin_step); break;
425 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU(-settings->m_Repeat.pin_step); break;
426 }
427
428 pin->Offset( step );
429
430 wxString nextName = pin->GetName();
431 IncrementLabelMember( nextName, settings->m_Repeat.label_delta );
432 pin->SetName( nextName );
433
434 wxString nextNumber = pin->GetNumber();
435 IncrementLabelMember( nextNumber, settings->m_Repeat.label_delta );
436 pin->SetNumber( nextNumber );
437
438 if( m_frame->SynchronizePins() )
439 pin->SetFlags( IS_LINKED );
440
441 if( PlacePin( pin ) )
442 return pin;
443
444 return nullptr;
445}
446
447
449{
453}
const char * name
Definition: DXF_plotter.cpp:57
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
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 AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
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:129
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:125
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:127
bool IsNew() const
Definition: eda_item.h:103
static TOOL_ACTION pushPinNameSize
Definition: ee_actions.h:245
static TOOL_ACTION pushPinNumSize
Definition: ee_actions.h:246
static TOOL_ACTION pushPinLength
Definition: ee_actions.h:244
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: confirm.h:47
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:56
int ShowModal() override
Definition: confirm.cpp:100
int GetBodyStyle() const
Definition: lib_item.h:346
int GetUnit() const
Definition: lib_item.h:343
LIB_ITEM * Duplicate() const
Create a copy of this LIB_ITEM (with a new Uuid).
Definition: lib_item.cpp:131
LIB_SYMBOL * GetParent() const
Definition: lib_item.h:209
void SetUnit(int aUnit)
Definition: lib_item.h:342
int GetLength() const
Definition: lib_pin.h:74
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:84
PIN_ORIENTATION GetOrientation() const
Definition: lib_pin.h:68
int GetNumberTextSize() const
Definition: lib_pin.h:135
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_pin.cpp:1187
VECTOR2I GetPosition() const override
Definition: lib_pin.h:232
const wxString & GetNumber() const
Definition: lib_pin.h:117
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:71
bool IsVisible() const
Definition: lib_pin.h:97
void SetNumber(const wxString &aNumber)
Definition: lib_pin.h:119
const wxString & GetName() const
Definition: lib_pin.h:106
int GetNameTextSize() const
Definition: lib_pin.h:128
Define a library symbol object.
Definition: lib_symbol.h:99
void ClearTempFlags()
Clears the status flag all draw objects in this symbol.
void RemoveDrawItem(LIB_ITEM *aItem)
Remove draw aItem from list.
void GetPins(LIB_PINS &aList, int aUnit=0, int aBodyStyle=0) const
Return a list of pin object pointers from the draw item list.
void AddDrawItem(LIB_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
std::vector< LIB_PIN * > GetAllLibPins() const
Return a list of pin pointers for all units / converts.
int GetUnitCount() const override
For items with units, return the number of units.
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:393
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:208
void setTransitions() override
< Set up handlers for various events.
LIB_PIN * CreatePin(const VECTOR2I &aPosition, LIB_SYMBOL *aSymbol)
LIB_PIN * RepeatPin(const LIB_PIN *aSourcePin)
void CreateImagePins(LIB_PIN *aPin)
int PushPinProperties(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
bool EditPinProperties(LIB_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
This file is part of the common library.
#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)
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:61
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:75
GRAPHIC_PINSHAPE
Definition: pin_type.h:56
bool IncrementLabelMember(wxString &name, int aIncrement)
Definition: sch_label.cpp:50
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
static GRAPHIC_PINSHAPE g_LastPinShape
static bool g_LastPinCommonUnit
static bool g_LastPinVisible
bool IncrementLabelMember(wxString &name, int aIncrement)
Definition: sch_label.cpp:50
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
@ LIB_PIN_T
Definition: typeinfo.h:206