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, 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
25#include "generator_tool.h"
26
27#include <collectors.h>
28#include <tool/tool_manager.h>
30#include <tools/pcb_actions.h>
31#include <router/router_tool.h>
32
33#include <dialog_generators.h>
34#include <properties/property.h>
36
37
39 GENERATOR_TOOL_PNS_PROXY( "pcbnew.Generators" ),
40 m_mgrDialog( nullptr )
41{
43 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
44 {
45 // Special case: propagate lock from generated items to parent generator
46
47 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
48
49 if( item && aProperty->Name() == _HKI( "Locked" ) )
50 {
51 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item->GetParentGroup() ) )
52 {
53 if( aCommit->GetStatus( generator ) != CHT_MODIFY )
54 aCommit->Modify( generator );
55
56 // Must set generator to unlocked first or item->IsLocked() will just
57 // return the parent's locked state.
58 generator->SetLocked( false );
59 generator->SetLocked( item->IsLocked() );
60 }
61 }
62 } );
63
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
84
85
90
91
93{
94 auto tuningPatternCondition =
95 []( const SELECTION& aSel )
96 {
97 for( EDA_ITEM* item : aSel )
98 {
99 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
100 {
101 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
102 return true;
103 }
104 }
105
106 return false;
107 };
108
109 // Add the generator control menus to relevant other tools
110
111 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
112
113 if( selTool )
114 {
115 TOOL_MENU& toolMenu = selTool->GetToolMenu();
116 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
117
118 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
119 }
120
121 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
122
123 if( routerTool )
124 {
125 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
126 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
127
129 }
130
131 return true;
132}
133
134
136{
137 if( m_mgrDialog )
138 {
139 m_mgrDialog->Destroy();
140 m_mgrDialog = nullptr;
141 }
142}
143
144
146{
147 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
148
149 if( !pcbFrame )
150 return 0;
151
152 if( !m_mgrDialog )
153 {
154 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
155 }
156 else
157 {
158 m_mgrDialog->RebuildModels();
159 }
160
161 m_mgrDialog->Show( true );
162
163 return 0;
164}
165
166
168{
169 wxString generatorType = aEvent.Parameter<wxString>();
170 BOARD_COMMIT commit( this );
171 wxString commitMsg;
172 int commitFlags = 0;
173
174 if( generatorType == wxS( "*" ) )
175 commitMsg = _( "Regenerate All" );
176
177 for( PCB_GENERATOR* generator : board()->Generators() )
178 {
179 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
180 {
181 if( commitMsg.IsEmpty() )
182 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
183
184 generator->EditStart( this, board(), &commit );
185 generator->Update( this, board(), &commit );
186 generator->EditFinish( this, board(), &commit );
187
188 commit.Push( commitMsg, commitFlags );
189 commitFlags |= APPEND_UNDO;
190 }
191 }
192
193 frame()->RefreshCanvas();
194 return 0;
195}
196
197
199{
200 BOARD_COMMIT commit( this );
201 int commitFlags = 0;
202
203 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
204
205 PCB_SELECTION sel = selTool->RequestSelection(
206 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
207 {
208 // Iterate from the back so we don't have to worry about removals.
209 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
210 {
211 BOARD_ITEM* item = aCollector[i];
212
213 if( item->Type() != PCB_GENERATOR_T )
214 aCollector.Remove( item );
215 }
216 } );
217
218 GENERATORS generators;
219
220 for( EDA_ITEM* item : sel )
221 {
222 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
223 generators.push_back( gen );
224 }
225
226#ifdef GENERATOR_ORDER
227 std::sort( generators.begin(), generators.end(),
228 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
229 {
230 return a->GetUpdateOrder() < b->GetUpdateOrder();
231 } );
232#endif
233
234 for( PCB_GENERATOR* gen : generators )
235 {
236 gen->EditStart( this, board(), &commit );
237 gen->Update( this, board(), &commit );
238 gen->EditFinish( this, board(), &commit );
239
240 commit.Push( _( "Regenerate Selected" ), commitFlags );
241 commitFlags |= APPEND_UNDO;
242 }
243
244 frame()->RefreshCanvas();
245 return 0;
246}
247
248
250{
251 BOARD_COMMIT commit( this );
252
253 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
254
255 gen->EditStart( this, board(), &commit );
256 gen->Update( this, board(), &commit );
257 gen->EditFinish( this, board(), &commit );
258
259 commit.Push( gen->GetCommitMessage() );
260
261 frame()->RefreshCanvas();
262 return 0;
263}
264
265
267{
268 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
269
270 wxCHECK( commit, 0 );
271
272 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
273
274 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
275 {
276 gen->EditStart( this, board(), commit );
277 }
278 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
279 {
280 gen->Update( this, board(), commit );
281 }
282 else if( aEvent.IsAction( &PCB_ACTIONS::genFinishEdit ) )
283 {
284 gen->EditFinish( this, board(), commit );
285 }
286 else if( aEvent.IsAction( &PCB_ACTIONS::genCancelEdit ) )
287 {
288 gen->EditCancel( this, board(), commit );
289 }
290 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
291 {
292 gen->Remove( this, board(), commit );
293 }
294
295 return 0;
296}
297
298
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:83
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:111
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition commit.h:72
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:106
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:171
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 base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:117
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:207
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)
DIALOG_GENERATORS * m_mgrDialog
int RegenerateAllOfType(const TOOL_EVENT &aEvent)
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:38
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 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
const wxString & Name() const
Definition property.h:220
static PROPERTY_MANAGER & Instance()
void RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Registers a listener for the given type.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
Generic, UI-independent tool event.
Definition tool_event.h:171
COMMIT * Commit() const
Definition tool_event.h:283
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:473
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:43
CONDITIONAL_MENU & GetMenu()
Definition tool_menu.cpp:44
@ CHT_MODIFY
Definition commit.h:44
#define _(s)
#define _HKI(x)
Definition page_info.cpp:44
#define TYPE_HASH(x)
Definition property.h:74
#define APPEND_UNDO
Definition sch_commit.h:41
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:91
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695