KiCad PCB EDA Suite
pcb_scripting_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) 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 3
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/gpl-3.0.html
19  * or you may search the http://www.gnu.org website for the version 3 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 
24 #include "pcb_scripting_tool.h"
25 
26 #include <action_plugin.h>
27 #include <gestfich.h>
28 #include <kiface_ids.h>
29 #include <kiway.h>
30 #include <macros.h>
31 #include <python_scripting.h>
32 #include <tools/pcb_actions.h>
33 
34 #include <pybind11/eval.h>
35 
36 #include <Python.h>
37 #include <wx/string.h>
38 #include <launch_ext.h>
39 
40 using initfunc = PyObject* (*)(void);
41 
43  PCB_TOOL_BASE( "pcbnew.ScriptingTool" )
44 {}
45 
46 
48 {}
49 
50 
52 {
53 }
54 
55 
57 {
58  PyLOCK lock;
59  std::string pymodule( "_pcbnew" );
60 
61  if( !SCRIPTING::IsModuleLoaded( pymodule ) )
62  {
64  initfunc pcbnew_init = reinterpret_cast<initfunc>( kiface->IfaceOrAddress( KIFACE_SCRIPTING_LEGACY ) );
65  PyImport_AddModule( pymodule.c_str() );
66  PyObject* mod = pcbnew_init();
67  PyObject* sys_mod = PyImport_GetModuleDict();
68  PyDict_SetItemString( sys_mod, "_pcbnew", mod );
69  Py_DECREF( mod );
70  }
71 
73 
74  return true;
75 }
76 
77 
79 {
80  if( !m_isFootprintEditor )
81  // Reload Python plugins if they are newer than the already loaded, and load new plugins
82  // Remove all action plugins so that we don't keep references to old versions
84 
85  {
86  PyLOCK lock;
88  }
89 
90  if( !m_isFootprintEditor )
91  {
92  // Action plugins can be modified, therefore the plugins menu must be updated:
94  // Recreate top toolbar to add action plugin buttons
96  }
97 
98  return 0;
99 }
100 
101 
103 {
104  // Load pcbnew inside Python and load all the user plugins and package-based plugins
105  using namespace pybind11::literals;
106 
107  auto locals = pybind11::dict(
108  "sys_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::STOCK ) ),
109  "user_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::USER ) ),
110  "third_party_path"_a =
111  TO_UTF8( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::THIRDPARTY ) ) );
112 
113  pybind11::exec( R"(
114 import sys
115 import pcbnew
116 pcbnew.LoadPlugins( sys_path, user_path, third_party_path )
117  )",
118  pybind11::globals(), locals );
119 }
120 
121 
123 {
124  wxString pluginpath( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::USER ) );
125  LaunchExternal( pluginpath );
126 
127  return 0;
128 }
129 
130 
132 {
133 
136 }
int reloadPlugins(const TOOL_EVENT &aEvent)
< Reload Python plugins and reset toolbar (if in pcbnew)
BITMAP2CMP_SETTINGS kiface
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
void Reset(RESET_REASON aReason) override
Basic initialization.
static TOOL_ACTION pluginsReload
Scripting Actions.
Definition: pcb_actions.h:314
int showPluginFolder(const TOOL_EVENT &aEvent)
Bind handlers to corresponding TOOL_ACTIONs.
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).
static void UnloadAll()
Unload (deregister) all action plugins.
This file contains miscellaneous commonly used macros and functions.
PCB_BASE_EDIT_FRAME * frame() const
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void callLoadPlugins()
Open the user's plugin folder in the system browser.
Generic, UI-independent tool event.
Definition: tool_event.h:152
pcbnew DSO
Definition: kiway.h:269
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:196
virtual void ReCreateMenuBar() override
Recreates the menu bar.
bool m_isFootprintEditor
static TOOL_ACTION pluginsShowFolder
Definition: pcb_actions.h:315
bool Init() override
Init() is called once upon a registration of the tool.
Class PCBNEW_ACTION_PLUGINS.
virtual void ReCreateHToolbar()=0
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:148
~SCRIPTING_TOOL()
React to model/view changes.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
PyObject *(*)(void) initfunc