KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generator_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) 2023 Alex Shvartzkop <[email protected]>
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
21#include "generator_tool.h"
22
23#include <collectors.h>
25#include <tool/tool_manager.h>
27#include <tools/pcb_actions.h>
28#include <router/router_tool.h>
29
30#include <dialog_generators.h>
31#include <properties/property.h>
33
34
36 GENERATOR_TOOL_PNS_PROXY( "pcbnew.Generators" ),
37 m_mgrDialog( nullptr )
38{
41 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
42 {
43 // Special case: propagate lock from generated items to parent generator
44
45 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
46
47 if( item && aProperty->Name() == _HKI( "Locked" ) )
48 {
49 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item->GetParentGroup() ) )
50 {
51 if( aCommit->GetStatus( generator ) != CHT_MODIFY )
52 aCommit->Modify( generator );
53
54 // Must set generator to unlocked first or item->IsLocked() will just
55 // return the parent's locked state.
56 generator->SetLocked( false );
57 generator->SetLocked( item->IsLocked() );
58 }
59 }
60 } );
61
64 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
65 {
66 // Special case: regenerate generators when their properties change
67
68 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( aItem ) )
69 {
70 BOARD_COMMIT* commit = static_cast<BOARD_COMMIT*>( aCommit );
71
72 generator->EditStart( this, board(), commit );
73 generator->Update( this, board(), commit );
74 generator->EditFinish( this, board(), commit );
75 }
76 } );
77}
78
79
81{
82 // Unregister before the rest of the object tears down so a property
83 // change firing mid-destruction cannot dispatch into a half-destroyed
84 // listener body.
85 m_generatorListener.reset();
86 m_boardItemListener.reset();
87}
88
89
94
95
97{
98 auto tuningPatternCondition =
99 []( const SELECTION& aSel )
100 {
101 for( EDA_ITEM* item : aSel )
102 {
103 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
104 {
105 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
106 return true;
107 }
108 }
109
110 return false;
111 };
112
113 // Add the generator control menus to relevant other tools
114
115 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
116
117 if( selTool )
118 {
119 TOOL_MENU& toolMenu = selTool->GetToolMenu();
120 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
121
122 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
123 }
124
125 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
126
127 if( routerTool )
128 {
129 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
130 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
131
133 }
134
135 return true;
136}
137
138
140{
141 if( m_mgrDialog )
142 {
143 m_mgrDialog->Destroy();
144 m_mgrDialog = nullptr;
145 }
146}
147
148
150{
151 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
152
153 if( !pcbFrame )
154 return 0;
155
156 if( !m_mgrDialog )
157 {
158 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
159 }
160 else
161 {
162 m_mgrDialog->RebuildModels();
163 }
164
165 m_mgrDialog->Show( true );
166
167 return 0;
168}
169
170
172{
173 wxString generatorType = aEvent.Parameter<wxString>();
174 BOARD_COMMIT commit( this );
175 wxString commitMsg;
176 int commitFlags = 0;
177
178 if( generatorType == wxS( "*" ) )
179 commitMsg = _( "Regenerate All" );
180
181 for( PCB_GENERATOR* generator : board()->Generators() )
182 {
183 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
184 {
185 if( commitMsg.IsEmpty() )
186 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
187
188 generator->EditStart( this, board(), &commit );
189 generator->Update( this, board(), &commit );
190 generator->EditFinish( this, board(), &commit );
191
192 commit.Push( commitMsg, commitFlags );
193 commitFlags |= APPEND_UNDO;
194 }
195 }
196
197 frame()->RefreshCanvas();
198 return 0;
199}
200
201
203{
204 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
205
206 PCB_SELECTION sel = selTool->RequestSelection(
207 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
208 {
209 // Iterate from the back so we don't have to worry about removals.
210 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
211 {
212 BOARD_ITEM* item = aCollector[i];
213
214 if( item->Type() != PCB_VIA_T && item->Type() != PCB_TRACE_T )
215 aCollector.Remove( item );
216 }
217 } );
218
219 std::vector<BOARD_ITEM*> items;
220
221 for( EDA_ITEM* item : sel )
222 {
223 if( item->IsBOARD_ITEM() )
224 items.push_back( static_cast<BOARD_ITEM*>( item ) );
225 }
226
227 std::vector<BOARD_ITEM*> members;
228 PCB_VIA_STACK* stack = PCB_VIA_STACK::CreateFromItems( items, board(), &members );
229
230 if( !stack )
231 {
232 frame()->ShowInfoBarWarning( _( "Selection does not form a contiguous microvia stack on one net." ) );
233 return 0;
234 }
235
237
238 BOARD_COMMIT commit( this );
239
240 commit.Add( stack );
241
242 for( BOARD_ITEM* item : members )
243 {
244 if( EDA_GROUP* existingGroup = item->GetParentGroup() )
245 commit.Modify( existingGroup->AsEdaItem(), nullptr, RECURSE_MODE::NO_RECURSE );
246
247 commit.Modify( item );
248 stack->AddItem( item );
249 }
250
251 commit.Push( _( "Create Microvia Stack" ) );
252
253 selTool->AddItemToSel( stack );
255 frame()->RefreshCanvas();
256
257 return 0;
258}
259
260
262{
263 BOARD_COMMIT commit( this );
264 int commitFlags = 0;
265
266 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
267
268 PCB_SELECTION sel = selTool->RequestSelection(
269 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
270 {
271 // Iterate from the back so we don't have to worry about removals.
272 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
273 {
274 BOARD_ITEM* item = aCollector[i];
275
276 if( item->Type() != PCB_GENERATOR_T )
277 aCollector.Remove( item );
278 }
279 } );
280
281 GENERATORS generators;
282
283 for( EDA_ITEM* item : sel )
284 {
285 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
286 generators.push_back( gen );
287 }
288
289#ifdef GENERATOR_ORDER
290 std::sort( generators.begin(), generators.end(),
291 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
292 {
293 return a->GetUpdateOrder() < b->GetUpdateOrder();
294 } );
295#endif
296
297 for( PCB_GENERATOR* gen : generators )
298 {
299 gen->EditStart( this, board(), &commit );
300 gen->Update( this, board(), &commit );
301 gen->EditFinish( this, board(), &commit );
302
303 commit.Push( _( "Regenerate Selected" ), commitFlags );
304 commitFlags |= APPEND_UNDO;
305 }
306
307 frame()->RefreshCanvas();
308 return 0;
309}
310
311
313{
314 BOARD_COMMIT commit( this );
315
316 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
317
318 gen->EditStart( this, board(), &commit );
319 gen->Update( this, board(), &commit );
320 gen->EditFinish( this, board(), &commit );
321
322 commit.Push( gen->GetCommitMessage() );
323
324 frame()->RefreshCanvas();
325 return 0;
326}
327
328
330{
331 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
332
333 wxCHECK( commit, 0 );
334
335 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
336
337 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
338 {
339 gen->EditStart( this, board(), commit );
340 }
341 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
342 {
343 gen->Update( this, board(), commit );
344 }
345 else if( aEvent.IsAction( &PCB_ACTIONS::genFinishEdit ) )
346 {
347 gen->EditFinish( this, board(), commit );
348 }
349 else if( aEvent.IsAction( &PCB_ACTIONS::genCancelEdit ) )
350 {
351 gen->EditCancel( this, board(), commit );
352 }
353 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
354 {
355 gen->Remove( this, board(), commit );
356 }
357
358 return 0;
359}
360
361
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
bool IsLocked() const override
int GetCount() const
Return the number of objects in the list.
Definition collector.h:79
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:107
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition commit.h:68
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
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:232
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.
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:43
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:58
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition actions.h:350
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
GENERATOR_TOOL_PNS_PROXY(const std::string &aToolName)
int ShowGeneratorsManager(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void setTransitions() override
< Set up handlers for various events.
int RegenerateSelected(const TOOL_EVENT &aEvent)
int GenEditAction(const TOOL_EVENT &aEvent)
Wrap selected microvias and traces into a via stack.
int RegenerateItem(const TOOL_EVENT &aEvent)
int MakeViaStackFromSelection(const TOOL_EVENT &aEvent)
DIALOG_GENERATORS * m_mgrDialog
PROPERTY_LISTENER_SUBSCRIPTION m_boardItemListener
RAII PROPERTY_MANAGER listener subscriptions; auto-unregister on destruction so this tool composes cl...
int RegenerateAllOfType(const TOOL_EVENT &aEvent)
PROPERTY_LISTENER_SUBSCRIPTION m_generatorListener
bool Init() override
Init() is called once upon a registration of the tool.
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
static TOOL_ACTION genFinishEdit
static TOOL_ACTION genStartEdit
static TOOL_ACTION genRemove
static TOOL_ACTION genCancelEdit
static TOOL_ACTION generatorsShowManager
static TOOL_ACTION genUpdateEdit
static TOOL_ACTION regenerateAll
static TOOL_ACTION regenerateAllTuning
Generator tool.
static TOOL_ACTION makeViaStack
static TOOL_ACTION regenerateSelected
The main frame for Pcbnew.
virtual void Remove(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual void EditCancel(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual wxString GetCommitMessage() const =0
virtual void EditStart(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual bool Update(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual void EditFinish(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
The selection tool: currently supports:
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter)
Return the current selection, filtered according to aClientFilter.
T * frame() const
BOARD * board() const
A microvia stack: several single hop microvias, plus connecting traces for staggered stacks,...
static PCB_VIA_STACK * CreateFromItems(const std::vector< BOARD_ITEM * > &aItems, BOARD *aBoard, std::vector< BOARD_ITEM * > *aMembers=nullptr)
Build a stack from existing board items (microvias plus optional connecting traces).
const wxString & Name() const
Definition property.h:221
static PROPERTY_MANAGER & Instance()
class PROPERTY_LISTENER_SUBSCRIPTION RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Register a listener for the given type and return a move-only subscription that auto-unregisters in i...
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
int AddItemToSel(const TOOL_EVENT &aEvent)
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
COMMIT * Commit() const
Definition tool_event.h:279
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
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()
Manage a CONDITIONAL_MENU and some number of CONTEXT_MENUs as sub-menus.
Definition tool_menu.h:39
CONDITIONAL_MENU & GetMenu()
Definition tool_menu.cpp:40
@ CHT_MODIFY
Definition commit.h:40
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
#define _HKI(x)
Definition page_info.cpp:40
std::deque< PCB_GENERATOR * > GENERATORS
#define TYPE_HASH(x)
Definition property.h:74
#define APPEND_UNDO
Definition sch_commit.h:39
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:83
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683