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 {
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 {
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 {
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( { SCH_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 SCH_PIN original_pin( *aPin );
122 DIALOG_PIN_PROPERTIES dlg( m_frame, aPin );
123 SCH_COMMIT commit( m_frame );
124 LIB_SYMBOL* parentSymbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
125
126 if( aPin->GetEditFlags() == 0 )
127 commit.Modify( parentSymbol );
128
129 if( dlg.ShowModal() == wxID_CANCEL )
130 return false;
131
132 if( !aPin->IsNew() && m_frame->SynchronizePins() && parentSymbol )
133 {
134 // a pin can have a unit id = 0 (common to all units) to unit count
135 // So we need a buffer size = GetUnitCount()+1 to store a value in a vector
136 // when using the unit id of a pin as index
137 std::vector<bool> got_unit( parentSymbol->GetUnitCount() + 1 );
138
139 got_unit[static_cast<size_t>(aPin->GetUnit())] = true;
140
141 for( SCH_PIN* other : parentSymbol->GetPins() )
142 {
143 if( other == aPin )
144 continue;
145
149 if( got_unit[static_cast<size_t>( other->GetUnit() )] )
150 continue;
151
152 if( other->GetPosition() == original_pin.GetPosition()
153 && other->GetOrientation() == original_pin.GetOrientation()
154 && other->GetType() == original_pin.GetType()
155 && other->IsVisible() == original_pin.IsVisible()
156 && other->GetName() == original_pin.GetName() )
157 {
158 if( aPin->GetBodyStyle() == 0 )
159 {
160 if( !aPin->GetUnit() || other->GetUnit() == aPin->GetUnit() )
161 parentSymbol->RemoveDrawItem( other );
162 }
163 else if( other->GetBodyStyle() == aPin->GetBodyStyle() )
164 {
165 other->SetPosition( aPin->GetPosition() );
166 other->ChangeLength( aPin->GetLength() );
167 other->SetShape( aPin->GetShape() );
168 }
169
170 if( aPin->GetUnit() == 0 )
171 {
172 if( !aPin->GetBodyStyle() || other->GetBodyStyle() == aPin->GetBodyStyle() )
173 parentSymbol->RemoveDrawItem( other );
174 }
175
176 other->SetOrientation( aPin->GetOrientation() );
177 other->SetType( aPin->GetType() );
178 other->SetVisible( aPin->IsVisible() );
179 other->SetName( aPin->GetName() );
180 other->SetNameTextSize( aPin->GetNameTextSize() );
181 other->SetNumberTextSize( aPin->GetNumberTextSize() );
182
183 got_unit[static_cast<size_t>( other->GetUnit() )] = true;
184 }
185 }
186 }
187
188 commit.Push( _( "Edit Pin Properties" ) );
189
190 std::vector<MSG_PANEL_ITEM> items;
191 aPin->GetMsgPanelInfo( m_frame, items );
192 m_frame->SetMsgPanel( items );
193
194 // Save the pin properties to use for the next new pin.
198 g_LastPinLength = aPin->GetLength();
199 g_LastPinShape = aPin->GetShape();
200 g_LastPinType = aPin->GetType();
202 g_LastPinCommonUnit = aPin->GetUnit() == 0;
203 g_LastPinVisible = aPin->IsVisible();
204
205 return true;
206}
207
208
210{
211 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
212 bool ask_for_pin = true; // Test for another pin in same position in other units
213
214 std::vector<SCH_PIN*> pins = symbol->GetAllLibPins();
215
216 for( SCH_PIN* test : pins )
217 {
218 if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
219 continue;
220
221 // test for same body style
222 if( test->GetBodyStyle() && test->GetBodyStyle() != aPin->GetBodyStyle() )
223 continue;
224
225 if( ask_for_pin && m_frame->SynchronizePins() )
226 {
227 wxString msg;
228 msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
229 test->GetUnit() );
230
231 KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
232 dlg.SetOKLabel( _( "Place Pin Anyway" ) );
233 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
234
235 bool status = dlg.ShowModal() == wxID_OK;
236
237 if( !status )
238 {
239 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
240 delete aPin;
241
242 return false;
243 }
244 else
245 {
246 ask_for_pin = false;
247 }
248 }
249 }
250
251 if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
252 {
254 g_LastPinType = aPin->GetType();
255 g_LastPinShape = aPin->GetShape();
256
257 if( m_frame->SynchronizePins() )
258 CreateImagePins( aPin );
259
260 symbol->AddDrawItem( aPin );
261 aPin->ClearFlags( IS_NEW );
262 }
263
264 // Put linked pins in new position, and clear flags
265 for( SCH_PIN* pin : pins )
266 {
267 if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
268 continue;
269
270 pin->SetPosition( aPin->GetPosition() );
271 pin->ClearFlags();
272 }
273
275 m_frame->OnModify();
276
277 return true;
278}
279
280
281/*
282 * Create a new pin.
283 */
285{
286 aSymbol->ClearTempFlags();
287
288 SCH_PIN* pin = new SCH_PIN( aSymbol );
289
290 pin->SetFlags( IS_NEW );
291
292 // Flag pins to consider
293 if( m_frame->SynchronizePins() )
294 pin->SetFlags( IS_LINKED );
295
296 pin->SetPosition( aPosition );
297 pin->SetLength( GetLastPinLength() );
298 pin->SetOrientation( g_LastPinOrient );
299 pin->SetType( g_LastPinType );
300 pin->SetShape( g_LastPinShape );
301 pin->SetNameTextSize( GetLastPinNameSize() );
302 pin->SetNumberTextSize( GetLastPinNumSize() );
303 pin->SetBodyStyle( g_LastPinCommonBodyStyle ? 0 : m_frame->GetBodyStyle() );
304 pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
305 pin->SetVisible( g_LastPinVisible );
306
307 if( !EditPinProperties( pin ) )
308 {
309 delete pin;
310 pin = nullptr;
311 }
312
313 return pin;
314}
315
316
318{
319 int ii;
320 SCH_PIN* newPin;
321
322 // if "synchronize pins editing" option is off, do not create any similar pin for other
323 // units and/or shapes: each unit is edited regardless other units or body
324 if( !m_frame->SynchronizePins() )
325 return;
326
327 if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
328 return;
329
330 // When units are interchangeable, all units are expected to have similar pins
331 // at the same position
332 // to facilitate pin editing, create pins for all other units for the current body style
333 // at the same position as aPin
334
335 for( ii = 1; ii <= aPin->GetParentSymbol()->GetUnitCount(); ii++ )
336 {
337 if( ii == aPin->GetUnit() )
338 continue;
339
340 newPin = static_cast<SCH_PIN*>( aPin->Duplicate() );
341
342 // To avoid mistakes, gives this pin a new pin number because
343 // it does no have the save pin number as the master pin
344 // Because we do not know the actual number, give it a temporary number
345 wxString unknownNum;
346 unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
347 newPin->SetNumber( unknownNum );
348
349 newPin->SetUnit( ii );
350
351 try
352 {
353 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( aPin->GetParentSymbol() );
354 symbol->AddDrawItem( newPin );
355 }
356 catch( const boost::bad_pointer& e )
357 {
358 wxLogError( "Cannot add new pin to symbol. Boost pointer error %s occurred.",
359 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();
373 SCH_PIN* sourcePin = dynamic_cast<SCH_PIN*>( selection.Front() );
374
375 if( !sourcePin )
376 return 0;
377
378 saveCopyInUndoList( symbol, UNDO_REDO::LIBEDIT );
379
380 for( SCH_PIN* pin : symbol->GetAllLibPins() )
381 {
382 if( pin == sourcePin )
383 continue;
384
385 if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
386 {
387 if( !pin->GetBodyStyle() || pin->GetBodyStyle() == m_frame->GetBodyStyle() )
388 pin->ChangeLength( sourcePin->GetLength() );
389 }
390 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
391 {
392 pin->SetNameTextSize( sourcePin->GetNameTextSize() );
393 }
394 else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
395 {
396 pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
397 }
398 }
399
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_PIN* pin = static_cast<SCH_PIN*>( aSourcePin->Duplicate() );
411 VECTOR2I step;
412
413 pin->ClearFlags();
414 pin->SetFlags( IS_NEW );
415
417
418 switch( pin->GetOrientation() )
419 {
420 default:
421 case PIN_ORIENTATION::PIN_RIGHT: step.y = schIUScale.MilsToIU(-settings->m_Repeat.pin_step); break;
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 }
426
427 pin->Move( step );
428
429 wxString nextName = pin->GetName();
430 IncrementLabelMember( nextName, settings->m_Repeat.label_delta );
431 pin->SetName( nextName );
432
433 wxString nextNumber = pin->GetNumber();
434 IncrementLabelMember( nextNumber, settings->m_Repeat.label_delta );
435 pin->SetNumber( nextNumber );
436
437 if( m_frame->SynchronizePins() )
438 pin->SetFlags( IS_LINKED );
439
440 if( PlacePin( pin ) )
441 return pin;
442
443 return nullptr;
444}
445
446
448{
452}
const char * name
Definition: DXF_plotter.cpp:57
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
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:132
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:128
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:130
bool IsNew() const
Definition: eda_item.h:106
static TOOL_ACTION pushPinNameSize
Definition: ee_actions.h:248
static TOOL_ACTION pushPinNumSize
Definition: ee_actions.h:249
static TOOL_ACTION pushPinLength
Definition: ee_actions.h:247
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
Define a library symbol object.
Definition: lib_symbol.h:77
std::vector< SCH_PIN * > GetPins(int aUnit=0, int aBodyStyle=0) const
Return a list of pin object pointers from the draw item list.
Definition: lib_symbol.cpp:982
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
std::vector< SCH_PIN * > GetAllLibPins() const
Return a list of pin pointers for all units / converts.
void RemoveDrawItem(SCH_ITEM *aItem)
Remove draw aItem from list.
Definition: lib_symbol.cpp:944
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:969
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
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:405
const SYMBOL * GetParentSymbol() const
Definition: sch_item.cpp:155
int GetBodyStyle() const
Definition: sch_item.h:240
int GetUnit() const
Definition: sch_item.h:237
virtual void SetUnit(int aUnit)
Definition: sch_item.h:236
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:120
int GetNumberTextSize() const
Definition: sch_pin.cpp:497
int GetLength() const
Definition: sch_pin.cpp:276
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:1534
void SetNumber(const wxString &aNumber)
Definition: sch_pin.cpp:465
bool IsVisible() const
Definition: sch_pin.cpp:338
const wxString & GetName() const
Definition: sch_pin.cpp:351
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:243
VECTOR2I GetPosition() const override
Definition: sch_pin.cpp:235
int GetNameTextSize() const
Definition: sch_pin.cpp:475
const wxString & GetNumber() const
Definition: sch_pin.h:111
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:256
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:289
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
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
void setTransitions() override
< Set up handlers for various events.
bool EditPinProperties(SCH_PIN *aPin)
int PushPinProperties(const TOOL_EVENT &aEvent)
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: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)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
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:77
GRAPHIC_PINSHAPE
Definition: pin_type.h:57
bool IncrementLabelMember(wxString &name, int aIncrement)
Definition: sch_label.cpp:51
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
static GRAPHIC_PINSHAPE g_LastPinShape
static bool g_LastPinCommonUnit
static bool g_LastPinVisible
bool IncrementLabelMember(wxString &name, int aIncrement)
Definition: sch_label.cpp:51
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