KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew_action_plugins.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) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <pgm_base.h>
27#include <pcbnew_settings.h>
28#include <bitmaps.h>
29#include <board.h>
30#include <board_commit.h>
32#include <footprint.h>
33#include <pcb_track.h>
34#include <zone.h>
35#include <menus_helpers.h>
36#include <pcbnew_settings.h>
37#include <tool/action_menu.h>
38#include <tool/action_toolbar.h>
39#include <tool/tool_manager.h>
40#include <tools/pcb_actions.h>
42#include <pcb_painter.h>
43#include <wx/msgdlg.h>
44#include "../../scripting/python_scripting.h"
45
47{
48 PyLOCK lock;
49
50 m_PyAction = aAction;
51 Py_XINCREF( aAction );
52}
53
54
56{
57 PyLOCK lock;
58
59 Py_XDECREF( m_PyAction );
60}
61
62
63PyObject* PYTHON_ACTION_PLUGIN::CallMethod( const char* aMethod, PyObject* aArglist )
64{
65 PyLOCK lock;
66
67 PyErr_Clear();
68
69 // pFunc is a new reference to the desired method
70 PyObject* pFunc = PyObject_GetAttrString( m_PyAction, aMethod );
71
72 if( pFunc && PyCallable_Check( pFunc ) )
73 {
74 PyObject* result = PyObject_CallObject( pFunc, aArglist );
75
76 if( PyErr_Occurred() )
77 {
78 wxMessageBox( PyErrStringWithTraceback(),
79 _( "Exception on python action plugin code" ),
80 wxICON_ERROR | wxOK );
81 }
82
83 if( result )
84 {
85 Py_XDECREF( pFunc );
86 return result;
87 }
88 }
89 else
90 {
91 wxString msg = wxString::Format( _( "Method '%s' not found, or not callable" ), aMethod );
92 wxMessageBox( msg, _( "Unknown Method" ), wxICON_ERROR | wxOK );
93 }
94
95 if( pFunc )
96 {
97 Py_XDECREF( pFunc );
98 }
99
100 return nullptr;
101}
102
103
104wxString PYTHON_ACTION_PLUGIN::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
105{
106 wxString ret;
107 PyLOCK lock;
108
109 PyObject* result = CallMethod( aMethod, aArglist );
110
111 ret = PyStringToWx( result );
112 Py_XDECREF( result );
113
114 return ret;
115}
116
117
119{
120 PyLOCK lock;
121
122 return CallRetStrMethod( "GetCategoryName" );
123}
124
125
127{
128 PyLOCK lock;
129
130 return CallRetStrMethod( "GetName" );
131}
132
133
135{
136 PyLOCK lock;
137
138 return CallRetStrMethod( "GetDescription" );
139}
140
141
143{
144 PyLOCK lock;
145
146 PyObject* result = CallMethod( "GetShowToolbarButton");
147
148 return PyObject_IsTrue(result);
149}
150
151
153{
154 PyLOCK lock;
155
156 PyObject* arglist = Py_BuildValue( "(i)", static_cast<int>( aDark ) );
157
158 wxString result = CallRetStrMethod( "GetIconFileName", arglist );
159
160 Py_DECREF( arglist );
161
162 return result;
163}
164
165
167{
168 PyLOCK lock;
169
170 return CallRetStrMethod( "GetPluginPath" );
171}
172
173
175{
176 PyLOCK lock;
177
178 CallMethod( "Run" );
179}
180
181
183{
184 return (void*) m_PyAction;
185}
186
187
188void PYTHON_ACTION_PLUGINS::register_action( PyObject* aPyAction )
189{
190 PYTHON_ACTION_PLUGIN* fw = new PYTHON_ACTION_PLUGIN( aPyAction );
191
192 fw->register_action();
193}
194
195
197{
198 // deregister also destroys the previously created "PYTHON_ACTION_PLUGIN object"
199 ACTION_PLUGINS::deregister_object( (void*) aPyAction );
200}
201
202
203void PCB_EDIT_FRAME::OnActionPluginMenu( wxCommandEvent& aEvent )
204{
205 ACTION_PLUGIN* actionPlugin = ACTION_PLUGINS::GetActionByMenu( aEvent.GetId() );
206
207 if( actionPlugin )
208 RunActionPlugin( actionPlugin );
209}
210
211
212void PCB_EDIT_FRAME::OnActionPluginButton( wxCommandEvent& aEvent )
213{
214 ACTION_PLUGIN* actionPlugin = ACTION_PLUGINS::GetActionByButton( aEvent.GetId() );
215
216 if( actionPlugin )
217 RunActionPlugin( actionPlugin );
218}
219
221{
222
223 PICKED_ITEMS_LIST itemsList;
224 BOARD* currentPcb = GetBoard();
225 bool fromEmpty = false;
226
227 // Append tracks:
228 for( PCB_TRACK* item : currentPcb->Tracks() )
229 {
230 ITEM_PICKER picker( nullptr, item, UNDO_REDO::CHANGED );
231 itemsList.PushItem( picker );
232 }
233
234 // Append footprints:
235 for( FOOTPRINT* item : currentPcb->Footprints() )
236 {
237 ITEM_PICKER picker( nullptr, item, UNDO_REDO::CHANGED );
238 itemsList.PushItem( picker );
239 }
240
241 // Append drawings
242 for( BOARD_ITEM* item : currentPcb->Drawings() )
243 {
244 ITEM_PICKER picker( nullptr, item, UNDO_REDO::CHANGED );
245 itemsList.PushItem( picker );
246 }
247
248 // Append zones outlines
249 for( ZONE* zone : currentPcb->Zones() )
250 {
251 ITEM_PICKER picker( nullptr, zone, UNDO_REDO::CHANGED );
252 itemsList.PushItem( picker );
253 }
254
255 if( itemsList.GetCount() > 0 )
256 SaveCopyInUndoList( itemsList, UNDO_REDO::CHANGED );
257 else
258 fromEmpty = true;
259
260 itemsList.ClearItemsList();
261
262 BOARD_COMMIT commit( this );
263
264 // Execute plugin itself...
266 aActionPlugin->Run();
268
269 // Get back the undo buffer to fix some modifications
270 PICKED_ITEMS_LIST* oldBuffer = nullptr;
271
272 if( fromEmpty )
273 {
274 oldBuffer = new PICKED_ITEMS_LIST();
275 }
276 else
277 {
278 oldBuffer = PopCommandFromUndoList();
279 wxASSERT( oldBuffer );
280 }
281
282 // Try do discover what was modified
283 PICKED_ITEMS_LIST deletedItemsList;
284
285 // The list of existing items after running the action script
286 const std::set<BOARD_ITEM*> currItemList = currentPcb->GetItemSet();
287
288 // Found deleted items
289 for( unsigned int i = 0; i < oldBuffer->GetCount(); i++ )
290 {
291 BOARD_ITEM* item = (BOARD_ITEM*) oldBuffer->GetPickedItem( i );
292 ITEM_PICKER picker( nullptr, item, UNDO_REDO::DELETED );
293
294 wxASSERT( item );
295
296 if( currItemList.find( item ) == currItemList.end() )
297 {
298 deletedItemsList.PushItem( picker );
299 commit.Removed( item );
300 }
301 }
302
303 // Mark deleted elements in undolist
304
305 for( unsigned int i = 0; i < deletedItemsList.GetCount(); i++ )
306 {
307 oldBuffer->PushItem( deletedItemsList.GetItemWrapper( i ) );
308 }
309
310 // Find new footprints
311 for( FOOTPRINT* item : currentPcb->Footprints() )
312 {
313 if( !oldBuffer->ContainsItem( item ) )
314 {
315 ITEM_PICKER picker( nullptr, item, UNDO_REDO::NEWITEM );
316 oldBuffer->PushItem( picker );
317 commit.Added( item );
318 }
319 }
320
321 for( PCB_TRACK* item : currentPcb->Tracks() )
322 {
323 if( !oldBuffer->ContainsItem( item ) )
324 {
325 ITEM_PICKER picker( nullptr, item, UNDO_REDO::NEWITEM );
326 oldBuffer->PushItem( picker );
327 commit.Added( item );
328 }
329 }
330
331 for( BOARD_ITEM* item : currentPcb->Drawings() )
332 {
333 if( !oldBuffer->ContainsItem( item ) )
334 {
335 ITEM_PICKER picker( nullptr, item, UNDO_REDO::NEWITEM );
336 oldBuffer->PushItem( picker );
337 commit.Added( item );
338 }
339 }
340
341 for( ZONE* zone : currentPcb->Zones() )
342 {
343 if( !oldBuffer->ContainsItem( zone ) )
344 {
345 ITEM_PICKER picker( nullptr, zone, UNDO_REDO::NEWITEM );
346 oldBuffer->PushItem( picker );
347 commit.Added( zone );
348 }
349 }
350
351 if( oldBuffer->GetCount() )
352 {
353 OnModify();
354 PushCommandToUndoList( oldBuffer );
355 }
356 else
357 {
358 delete oldBuffer;
359 }
360
361 // Apply changes, UndoList already handled
362 commit.Push( _( "Apply action script" ), SKIP_UNDO | SKIP_SET_DIRTY );
363
365}
366
367
369{
370 // The list of existing items after running the action script
371 const std::set<BOARD_ITEM*> items = GetBoard()->GetItemSet();
372
373 // Sync selection with items selection state
374 SELECTION& selection = GetCurrentSelection();
376 EDA_ITEMS to_add;
377 EDA_ITEMS to_remove;
378
379 for( BOARD_ITEM* item : items )
380 {
381 if( item->IsSelected() && !selection.Contains( item ) )
382 {
383 item->ClearSelected(); // temporarily
384 to_add.push_back( item );
385 }
386 }
387
388 for( EDA_ITEM* item : selection.GetItems() )
389 {
390 if( !item->IsSelected() )
391 to_remove.push_back( static_cast<BOARD_ITEM*>( item ) );
392 }
393
394 if( !to_add.empty() )
395 selTool->AddItemsToSel( &to_add );
396
397 if( !to_remove.empty() )
398 selTool->RemoveItemsFromSel( &to_remove );
399
401
402 PCB_DRAW_PANEL_GAL* canvas = GetCanvas();
403
404 canvas->GetView()->Clear();
405 canvas->GetView()->InitPreview();
407 canvas->DisplayBoard( m_pcb );
408
409 // allow tools to re-add their view items (selection previews, grids, etc.)
410 if( m_toolManager )
412
413 // reload the drawing-sheet
415
416 canvas->SyncLayersVisibility( m_pcb );
417
418 canvas->Refresh();
419}
420
421
423{
424 if( !actionMenu ) // Should not occur.
425 return;
426
427 for( int ii = 0; ii < ACTION_PLUGINS::GetActionsCount(); ii++ )
428 {
429 wxMenuItem* item;
431 const wxBitmap& bitmap = ap->iconBitmap.IsOk() ? ap->iconBitmap :
432 KiBitmap( BITMAPS::puzzle_piece );
433
434 item = AddMenuItem( actionMenu, wxID_ANY, ap->GetName(), ap->GetDescription(), bitmap );
435
436 Connect( item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
437 wxCommandEventHandler( PCB_EDIT_FRAME::OnActionPluginMenu ) );
438
439 ACTION_PLUGINS::SetActionMenu( ii, item->GetId() );
440 }
441}
442
443
445{
446 bool need_separator = true;
447 const std::vector<ACTION_PLUGIN*>& orderedPlugins = GetOrderedActionPlugins();
448
449 for( ACTION_PLUGIN* ap : orderedPlugins )
450 {
451 if( GetActionPluginButtonVisible( ap->GetPluginPath(), ap->GetShowToolbarButton() ) )
452 {
453 if( need_separator )
454 {
456 need_separator = false;
457 }
458
459 // Add button
460 wxBitmap bitmap;
461
462 if ( ap->iconBitmap.IsOk() )
463 bitmap = KiScaledBitmap( ap->iconBitmap, this );
464 else
465 bitmap = KiScaledBitmap( BITMAPS::puzzle_piece, this );
466
467 wxAuiToolBarItem* button = m_mainToolBar->AddTool( wxID_ANY, wxEmptyString,
468 bitmap, ap->GetName() );
469
470 Connect( button->GetId(), wxEVT_COMMAND_MENU_SELECTED,
471 wxCommandEventHandler( PCB_EDIT_FRAME::OnActionPluginButton ) );
472
473 // Link action plugin to button
474 ACTION_PLUGINS::SetActionButton( ap, button->GetId() );
475 }
476 }
477}
478
479
480std::vector<ACTION_PLUGIN*> PCB_EDIT_FRAME::GetOrderedActionPlugins()
481{
482 PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
483
484 std::vector<ACTION_PLUGIN*> plugins;
485 std::vector<ACTION_PLUGIN*> orderedPlugins;
486
487 for( int i = 0; i < ACTION_PLUGINS::GetActionsCount(); i++ )
488 plugins.push_back( ACTION_PLUGINS::GetAction( i ) );
489
490 // First add plugins that have entries in settings
491 for( const auto& pair : cfg->m_VisibleActionPlugins )
492 {
493 auto loc = std::find_if( plugins.begin(), plugins.end(),
494 [pair] ( ACTION_PLUGIN* plugin )
495 {
496 return plugin->GetPluginPath() == pair.first;
497 } );
498
499 if( loc != plugins.end() )
500 {
501 orderedPlugins.push_back( *loc );
502 plugins.erase( loc );
503 }
504 }
505
506 // Now append new plugins that have not been configured yet
507 for( auto remaining_plugin : plugins )
508 orderedPlugins.push_back( remaining_plugin );
509
510 return orderedPlugins;
511}
512
513
514bool PCB_EDIT_FRAME::GetActionPluginButtonVisible( const wxString& aPluginPath,
515 bool aPluginDefault )
516{
517 PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
518
519 for( const auto& entry : cfg->m_VisibleActionPlugins )
520 {
521 if( entry.first == aPluginPath )
522 return entry.second;
523 }
524
525 // Plugin is not in settings, return default.
526 return aPluginDefault;
527}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:106
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Create and insert a menu item with an icon into aMenu.
Definition: bitmap.cpp:266
wxBitmap KiScaledBitmap(BITMAPS aBitmap, wxWindow *aWindow, int aHeight, bool aQuantized)
Construct a wxBitmap from a memory record, scaling it if device DPI demands it.
Definition: bitmap.cpp:157
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
static bool deregister_object(void *aObject)
Deregister an object which builds a action.
static ACTION_PLUGIN * GetActionByMenu(int aMenu)
Find action plugin associated to a menu ID.
static int GetActionsCount()
static ACTION_PLUGIN * GetAction(const wxString &aName)
static void SetActionButton(ACTION_PLUGIN *aAction, int idButton)
Associate a button id to an action plugin.
static void SetActionMenu(int aIndex, int idMenu)
Associate a menu id to an action plugin.
static void SetActionRunning(bool aRunning)
static ACTION_PLUGIN * GetActionByButton(int aButton)
Find action plugin associated to a button ID.
This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:39
wxBitmap iconBitmap
void register_action()
It's the standard method of a "ACTION_PLUGIN" to register itself into the ACTION_PLUGINS singleton ma...
virtual wxString GetDescription()=0
virtual wxString GetName()=0
virtual void Run()=0
This method the the action.
void AddScaledSeparator(wxWindow *aWindow)
Add a separator that introduces space on either side to not squash the tools when scaled.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
const VECTOR2I & GetGridOrigin()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:71
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:270
const PAGE_INFO & GetPageSettings() const
Definition: board.h:638
ZONES & Zones()
Definition: board.h:318
const std::set< BOARD_ITEM * > GetItemSet()
Definition: board.cpp:2408
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition: board.cpp:168
FOOTPRINTS & Footprints()
Definition: board.h:312
TRACKS & Tracks()
Definition: board.h:309
DRAWINGS & Drawings()
Definition: board.h:315
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:728
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition: commit.h:85
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:97
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Add a command to undo in the undo list.
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
Return the last command to undo and remove it from list, nothing is deleted.
ACTION_TOOLBAR * m_mainToolBar
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
void SetGridOrigin(const VECTOR2D &aGridOrigin)
Set the origin point for the grid.
void Clear()
Remove all items from the view.
Definition: view.cpp:1116
void InitPreview()
Definition: view.cpp:1645
ACTION_PLUGIN_SETTINGS_LIST m_VisibleActionPlugins
void SaveCopyInUndoList(EDA_ITEM *aItemToCopy, UNDO_REDO aTypeCommand) override
Create a new entry in undo list of commands.
Definition: undo_redo.cpp:282
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
BOARD * GetBoard() const
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void DisplayBoard(BOARD *aBoard, PROGRESS_REPORTER *aReporter=nullptr)
Add all items from the current board to the VIEW, so they can be displayed by GAL.
void SyncLayersVisibility(const BOARD *aBoard)
Update "visibility" property of each layer of a given BOARD.
void OnModify() override
Must be called after a board change to set the modified flag.
static std::vector< ACTION_PLUGIN * > GetOrderedActionPlugins()
Return ordered list of plugins in sequence in which they should appear on toolbar or in settings.
void SetPageSettings(const PAGE_INFO &aPageSettings) override
void OnActionPluginButton(wxCommandEvent &aEvent)
Launched by the button when an action is called.
void RunActionPlugin(ACTION_PLUGIN *aActionPlugin)
Execute action plugin's Run() method and updates undo buffer.
void buildActionPluginMenus(ACTION_MENU *aActionMenu)
Fill action menu with all registered action plugins.
void RebuildAndRefresh()
Rebuilds board connectivity, refreshes canvas.
static bool GetActionPluginButtonVisible(const wxString &aPluginPath, bool aPluginDefault)
Return true if button visibility action plugin setting was set to true or it is unset and plugin defa...
SELECTION & GetCurrentSelection() override
Get the current selection from the canvas area.
void OnActionPluginMenu(wxCommandEvent &aEvent)
Launched by the menu when an action is called.
void AddActionPluginTools()
Append action plugin buttons to main toolbar.
The selection tool: currently supports:
A holder to handle information on schematic or board items.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
bool ContainsItem(const EDA_ITEM *aItem) const
unsigned GetCount() const
void ClearItemsList()
Delete only the list of pickers NOT the picked data itself.
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
static void deregister_action(PyObject *aPyAction)
static void register_action(PyObject *aPyAction)
bool GetShowToolbarButton() override
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=nullptr)
wxString GetCategoryName() override
wxString GetName() override
wxString GetDescription() override
wxString GetPluginPath() override
PYTHON_ACTION_PLUGIN(PyObject *action)
void Run() override
This method the the action.
wxString GetIconFileName(bool aDark) override
void * GetObject() override
This method gets the pointer to the object from where this action constructs.
int AddItemsToSel(const TOOL_EVENT &aEvent)
int RemoveItemsFromSel(const TOOL_EVENT &aEvent)
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:120
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:84
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:170
@ REDRAW
Full drawing refresh.
Definition: tool_base.h:83
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:506
Macros and inline functions to create menus items in menubars or popup menus.
Class PCBNEW_ACTION_PLUGINS.
see class PGM_BASE
#define SKIP_SET_DIRTY
#define SKIP_UNDO
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587