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
35
37 GENERATOR_TOOL_PNS_PROXY( "pcbnew.Generators" ),
38 m_mgrDialog( nullptr )
39{
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
63 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
64 {
65 // Special case: regenerate generators when their properties change
66
67 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( aItem ) )
68 {
69 BOARD_COMMIT* commit = static_cast<BOARD_COMMIT*>( aCommit );
70
71 generator->EditStart( this, board(), commit );
72 generator->Update( this, board(), commit );
73 generator->EditFinish( this, board(), commit );
74 }
75 } );
76}
77
78
82
83
88
89
91{
92 auto tuningPatternCondition =
93 []( const SELECTION& aSel )
94 {
95 for( EDA_ITEM* item : aSel )
96 {
97 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
98 {
99 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
100 return true;
101 }
102 }
103
104 return false;
105 };
106
107 // Add the generator control menus to relevant other tools
108
109 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
110
111 if( selTool )
112 {
113 TOOL_MENU& toolMenu = selTool->GetToolMenu();
114 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
115
116 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
117 }
118
119 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
120
121 if( routerTool )
122 {
123 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
124 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
125
127 }
128
129 return true;
130}
131
132
134{
135 if( m_mgrDialog )
136 {
137 m_mgrDialog->Destroy();
138 m_mgrDialog = nullptr;
139 }
140}
141
142
144{
145 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
146
147 if( !pcbFrame )
148 return 0;
149
150 if( !m_mgrDialog )
151 {
152 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
153 }
154 else
155 {
156 m_mgrDialog->RebuildModels();
157 }
158
159 m_mgrDialog->Show( true );
160
161 return 0;
162}
163
164
166{
167 wxString generatorType = aEvent.Parameter<wxString>();
168 BOARD_COMMIT commit( this );
169 wxString commitMsg;
170 int commitFlags = 0;
171
172 if( generatorType == wxS( "*" ) )
173 commitMsg = _( "Regenerate All" );
174
175 for( PCB_GENERATOR* generator : board()->Generators() )
176 {
177 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
178 {
179 if( commitMsg.IsEmpty() )
180 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
181
182 generator->EditStart( this, board(), &commit );
183 generator->Update( this, board(), &commit );
184 generator->EditFinish( this, board(), &commit );
185
186 commit.Push( commitMsg, commitFlags );
187 commitFlags |= APPEND_UNDO;
188 }
189 }
190
191 frame()->RefreshCanvas();
192 return 0;
193}
194
195
197{
198 BOARD_COMMIT commit( this );
199 int commitFlags = 0;
200
201 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
202
203 PCB_SELECTION sel = selTool->RequestSelection(
204 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
205 {
206 // Iterate from the back so we don't have to worry about removals.
207 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
208 {
209 BOARD_ITEM* item = aCollector[i];
210
211 if( item->Type() != PCB_GENERATOR_T )
212 aCollector.Remove( item );
213 }
214 } );
215
216 GENERATORS generators;
217
218 for( EDA_ITEM* item : sel )
219 {
220 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
221 generators.push_back( gen );
222 }
223
224#ifdef GENERATOR_ORDER
225 std::sort( generators.begin(), generators.end(),
226 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
227 {
228 return a->GetUpdateOrder() < b->GetUpdateOrder();
229 } );
230#endif
231
232 for( PCB_GENERATOR* gen : generators )
233 {
234 gen->EditStart( this, board(), &commit );
235 gen->Update( this, board(), &commit );
236 gen->EditFinish( this, board(), &commit );
237
238 commit.Push( _( "Regenerate Selected" ), commitFlags );
239 commitFlags |= APPEND_UNDO;
240 }
241
242 frame()->RefreshCanvas();
243 return 0;
244}
245
246
248{
249 BOARD_COMMIT commit( this );
250
251 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
252
253 gen->EditStart( this, board(), &commit );
254 gen->Update( this, board(), &commit );
255 gen->EditFinish( this, board(), &commit );
256
257 commit.Push( gen->GetCommitMessage() );
258
259 frame()->RefreshCanvas();
260 return 0;
261}
262
263
265{
266 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
267
268 wxCHECK( commit, 0 );
269
270 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
271
272 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
273 {
274 gen->EditStart( this, board(), commit );
275 }
276 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
277 {
278 gen->Update( this, board(), commit );
279 }
280 else if( aEvent.IsAction( &PCB_ACTIONS::genFinishEdit ) )
281 {
282 gen->EditFinish( this, board(), commit );
283 }
284 else if( aEvent.IsAction( &PCB_ACTIONS::genCancelEdit ) )
285 {
286 gen->EditCancel( this, board(), commit );
287 }
288 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
289 {
290 gen->Remove( this, board(), commit );
291 }
292
293 return 0;
294}
295
296
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:79
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:167
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: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
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:37
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:219
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:73
#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