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>
26#include <tool/tool_manager.h>
28#include <tools/pcb_actions.h>
29#include <router/router_tool.h>
30
31#include <dialog_generators.h>
32#include <properties/property.h>
34
35
38 m_mgrDialog( nullptr )
39{
42 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
43 {
44 // Special case: propagate lock from generated items to parent generator
45
46 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
47
48 if( item && aProperty->Name() == _HKI( "Locked" ) )
49 {
50 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item->GetParentGroup() ) )
51 {
52 if( aCommit->GetStatus( generator ) != CHT_MODIFY )
53 aCommit->Modify( generator );
54
55 // Must set generator to unlocked first or item->IsLocked() will just
56 // return the parent's locked state.
57 generator->SetLocked( false );
58 generator->SetLocked( item->IsLocked() );
59 }
60 }
61 } );
62
65 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
66 {
67 // Special case: regenerate generators when their properties change
68
69 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( aItem ) )
70 {
71 BOARD_COMMIT* commit = static_cast<BOARD_COMMIT*>( aCommit );
72
73 generator->EditStart( this, board(), commit );
74 generator->Update( this, board(), commit );
75 generator->EditFinish( this, board(), commit );
76 }
77 } );
78}
79
80
82{
83 // Unregister before the rest of the object tears down so a property
84 // change firing mid-destruction cannot dispatch into a half-destroyed
85 // listener body.
86 m_generatorListener.reset();
87 m_boardItemListener.reset();
88}
89
90
92{
94
95 if( aReason != RUN )
97}
98
99
101{
102 auto tuningPatternCondition =
103 []( const SELECTION& aSel )
104 {
105 for( EDA_ITEM* item : aSel )
106 {
107 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
108 {
109 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
110 return true;
111 }
112 }
113
114 return false;
115 };
116
117 // Add the generator control menus to relevant other tools
118
119 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
120
121 if( selTool )
122 {
123 TOOL_MENU& toolMenu = selTool->GetToolMenu();
124 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
125
126 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
127 }
128
129 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
130
131 if( routerTool )
132 {
133 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
134 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
135
137 }
138
139 return true;
140}
141
142
144{
145 if( m_mgrDialog )
146 {
147 m_mgrDialog->Destroy();
148 m_mgrDialog = nullptr;
149 }
150}
151
152
154{
155 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
156
157 if( !pcbFrame )
158 return 0;
159
160 if( !m_mgrDialog )
161 {
162 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
163 }
164 else
165 {
166 m_mgrDialog->RebuildModels();
167 }
168
169 m_mgrDialog->Show( true );
170
171 return 0;
172}
173
174
176{
177 wxString generatorType = aEvent.Parameter<wxString>();
178 BOARD_COMMIT commit( this );
179 wxString commitMsg;
180 int commitFlags = 0;
181
182 if( generatorType == wxS( "*" ) )
183 commitMsg = _( "Regenerate All" );
184
185 for( PCB_GENERATOR* generator : board()->Generators() )
186 {
187 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
188 {
189 if( commitMsg.IsEmpty() )
190 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
191
192 generator->EditStart( this, board(), &commit );
193 generator->Update( this, board(), &commit );
194 generator->EditFinish( this, board(), &commit );
195
196 commit.Push( commitMsg, commitFlags );
197 commitFlags |= APPEND_UNDO;
198 }
199 }
200
201 frame()->RefreshCanvas();
202 return 0;
203}
204
205
207{
208 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
209
210 PCB_SELECTION sel = selTool->RequestSelection(
211 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
212 {
213 // Iterate from the back so we don't have to worry about removals.
214 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
215 {
216 BOARD_ITEM* item = aCollector[i];
217
218 if( item->Type() != PCB_VIA_T && item->Type() != PCB_TRACE_T )
219 aCollector.Remove( item );
220 }
221 } );
222
223 std::vector<BOARD_ITEM*> items;
224
225 for( EDA_ITEM* item : sel )
226 {
227 if( item->IsBOARD_ITEM() )
228 items.push_back( static_cast<BOARD_ITEM*>( item ) );
229 }
230
231 std::vector<BOARD_ITEM*> members;
232 wxString error;
233 PCB_VIA_STACK* stack = PCB_VIA_STACK::CreateFromItems( items, board(), &members, &error );
234
235 if( !stack )
236 {
237 if( error.IsEmpty() )
238 {
239 error = _( "A microvia stack needs two or more microvias on the same net, each spanning "
240 "one layer step, with no gap between them." );
241 }
242
243 frame()->ShowInfoBarWarning( error );
244 return 0;
245 }
246
248
249 BOARD_COMMIT commit( this );
250
251 commit.Add( stack );
252
253 for( BOARD_ITEM* item : members )
254 {
255 if( EDA_GROUP* existingGroup = item->GetParentGroup() )
256 commit.Modify( existingGroup->AsEdaItem(), nullptr, RECURSE_MODE::NO_RECURSE );
257
258 commit.Modify( item );
259 stack->AddItem( item );
260 }
261
262 commit.Push( _( "Create Microvia Stack" ) );
263
264 selTool->AddItemToSel( stack );
266 frame()->RefreshCanvas();
267
268 return 0;
269}
270
271
273{
274 BOARD_COMMIT commit( this );
275 int commitFlags = 0;
276
277 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
278
279 PCB_SELECTION sel = selTool->RequestSelection(
280 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
281 {
282 // Iterate from the back so we don't have to worry about removals.
283 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
284 {
285 BOARD_ITEM* item = aCollector[i];
286
287 if( item->Type() != PCB_GENERATOR_T )
288 aCollector.Remove( item );
289 }
290 } );
291
292 GENERATORS generators;
293
294 for( EDA_ITEM* item : sel )
295 {
296 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
297 generators.push_back( gen );
298 }
299
300#ifdef GENERATOR_ORDER
301 std::sort( generators.begin(), generators.end(),
302 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
303 {
304 return a->GetUpdateOrder() < b->GetUpdateOrder();
305 } );
306#endif
307
308 for( PCB_GENERATOR* gen : generators )
309 {
310 gen->EditStart( this, board(), &commit );
311 gen->Update( this, board(), &commit );
312 gen->EditFinish( this, board(), &commit );
313
314 commit.Push( _( "Regenerate Selected" ), commitFlags );
315 commitFlags |= APPEND_UNDO;
316 }
317
318 frame()->RefreshCanvas();
319 return 0;
320}
321
322
324{
325 BOARD_COMMIT commit( this );
326
327 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
328
329 gen->EditStart( this, board(), &commit );
330 gen->Update( this, board(), &commit );
331 gen->EditFinish( this, board(), &commit );
332
333 commit.Push( gen->GetCommitMessage() );
334
335 frame()->RefreshCanvas();
336 return 0;
337}
338
339
341{
342 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
343
344 wxCHECK( commit, 0 );
345
346 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
347
348 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
349 {
350 gen->EditStart( this, board(), commit );
351 }
352 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
353 {
354 gen->Update( this, board(), commit );
355 }
356 else if( aEvent.IsAction( &PCB_ACTIONS::genFinishEdit ) )
357 {
358 gen->EditFinish( this, board(), commit );
359 }
360 else if( aEvent.IsAction( &PCB_ACTIONS::genCancelEdit ) )
361 {
362 gen->EditCancel( this, board(), commit );
363 }
364 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
365 {
366 gen->Remove( this, board(), commit );
367 }
368
369 return 0;
370}
371
372
374{
375 std::set<KIID> selected;
376
377 for( EDA_ITEM* item : m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->GetSelection() )
378 {
379 if( dynamic_cast<PCB_TUNING_PATTERN*>( item ) )
380 selected.insert( item->m_Uuid );
381 }
382
383 // Interactive tools share the generator router, so wait until they exit before editing copper
384 if( !frame()->ToolStackIsEmpty() )
385 {
386 m_selectedTuningPatterns.insert( selected.begin(), selected.end() );
387 return 0;
388 }
389
390 std::vector<PCB_TUNING_PATTERN*> emptyPatterns;
391
392 for( const KIID& id : m_selectedTuningPatterns )
393 {
394 if( selected.contains( id ) )
395 continue;
396
397 auto* pattern = dynamic_cast<PCB_TUNING_PATTERN*>( board()->ResolveItem( id, true ) );
398
399 if( pattern && !pattern->IsLocked() && !pattern->HasFlag( IN_EDIT ) && !pattern->HasMeanders() )
400 emptyPatterns.push_back( pattern );
401 }
402
403 m_selectedTuningPatterns = std::move( selected );
404
405 BOARD_COMMIT commit( this );
406 int commitFlags = 0;
407
408 for( PCB_TUNING_PATTERN* pattern : emptyPatterns )
409 {
410 pattern->Remove( this, board(), &commit );
411 commit.Push( _( "Remove Empty Tuning Pattern" ), commitFlags );
412 commitFlags |= APPEND_UNDO;
413 }
414
415 return 0;
416}
417
418
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
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:85
bool IsLocked() const override
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:2142
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:235
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 ClearedEvent
Definition actions.h:348
static const TOOL_EVENT SelectedEvent
Definition actions.h:346
static const TOOL_EVENT SelectedItemsModified
Selected item had a property changed (except movement)
Definition actions.h:353
static const TOOL_EVENT UnselectedEvent
Definition actions.h:347
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)
int RegenerateItem(const TOOL_EVENT &aEvent)
int MakeViaStackFromSelection(const TOOL_EVENT &aEvent)
Wrap selected microvias and traces into a via stack.
DIALOG_GENERATORS * m_mgrDialog
int RemoveDeselectedEmptyPatterns(const TOOL_EVENT &aEvent)
Remove tuning patterns that were deselected without producing any meanders.
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.
std::set< KIID > m_selectedTuningPatterns
Tuning patterns selected as of the last selection event.
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
Definition kiid.h:46
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.
PCB_SELECTION & GetSelection()
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, wxString *aError=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:484
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 IN_EDIT
Item currently edited.
#define GENERATOR_TOOL_NAME
#define _HKI(x)
Definition page_info.cpp:40
std::deque< PCB_GENERATOR * > GENERATORS
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
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:708