2021-03-19 23:06:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-10-06 02:46:53 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.TXT for contributors.
|
2021-03-19 23:06:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pcb_scripting_tool.h"
|
|
|
|
|
|
|
|
#include <action_plugin.h>
|
2024-01-14 04:14:02 +00:00
|
|
|
#include <api/api_plugin_manager.h>
|
2021-03-19 23:06:29 +00:00
|
|
|
#include <kiface_ids.h>
|
|
|
|
#include <kiway.h>
|
2021-04-05 19:30:27 +00:00
|
|
|
#include <macros.h>
|
2024-01-14 04:14:02 +00:00
|
|
|
#include <pgm_base.h>
|
2021-03-19 23:06:29 +00:00
|
|
|
#include <python_scripting.h>
|
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
|
|
|
|
#include <pybind11/eval.h>
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
#include <wx/string.h>
|
2021-06-15 16:14:19 +00:00
|
|
|
#include <launch_ext.h>
|
2021-03-19 23:06:29 +00:00
|
|
|
|
|
|
|
using initfunc = PyObject* (*)(void);
|
|
|
|
|
|
|
|
SCRIPTING_TOOL::SCRIPTING_TOOL() :
|
|
|
|
PCB_TOOL_BASE( "pcbnew.ScriptingTool" )
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
SCRIPTING_TOOL::~SCRIPTING_TOOL()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void SCRIPTING_TOOL::Reset( RESET_REASON aReason )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCRIPTING_TOOL::Init()
|
|
|
|
{
|
|
|
|
PyLOCK lock;
|
2021-06-07 20:55:28 +00:00
|
|
|
std::string pymodule( "_pcbnew" );
|
2021-03-19 23:06:29 +00:00
|
|
|
|
2021-06-07 20:55:28 +00:00
|
|
|
if( !SCRIPTING::IsModuleLoaded( pymodule ) )
|
2021-03-19 23:06:29 +00:00
|
|
|
{
|
|
|
|
KIFACE* kiface = frame()->Kiway().KiFACE( KIWAY::FACE_PCB );
|
|
|
|
initfunc pcbnew_init = reinterpret_cast<initfunc>( kiface->IfaceOrAddress( KIFACE_SCRIPTING_LEGACY ) );
|
2021-06-07 20:55:28 +00:00
|
|
|
PyImport_AddModule( pymodule.c_str() );
|
2021-03-19 23:06:29 +00:00
|
|
|
PyObject* mod = pcbnew_init();
|
|
|
|
PyObject* sys_mod = PyImport_GetModuleDict();
|
|
|
|
PyDict_SetItemString( sys_mod, "_pcbnew", mod );
|
|
|
|
Py_DECREF( mod );
|
|
|
|
|
2022-02-17 06:28:00 +00:00
|
|
|
// plugins will be loaded later via ReloadPlugins()
|
2021-12-02 19:50:09 +00:00
|
|
|
}
|
2021-03-19 23:06:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void SCRIPTING_TOOL::ReloadPlugins()
|
|
|
|
{
|
|
|
|
// Reload Python plugins if they are newer than the already loaded, and load new plugins
|
|
|
|
// Remove all action plugins so that we don't keep references to old versions
|
|
|
|
ACTION_PLUGINS::UnloadAll();
|
|
|
|
|
2023-01-17 20:55:39 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
PyLOCK lock;
|
|
|
|
callLoadPlugins();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{}
|
2021-08-29 23:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-19 23:06:29 +00:00
|
|
|
int SCRIPTING_TOOL::reloadPlugins( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2021-08-29 23:33:08 +00:00
|
|
|
// Reload Python plugins if they are newer than the already loaded, and load new plugins
|
|
|
|
// Remove all action plugins so that we don't keep references to old versions
|
|
|
|
ACTION_PLUGINS::UnloadAll();
|
2021-03-19 23:06:29 +00:00
|
|
|
|
2023-01-17 20:55:39 +00:00
|
|
|
try
|
2021-03-19 23:06:29 +00:00
|
|
|
{
|
2020-10-26 10:54:36 +00:00
|
|
|
PyLOCK lock;
|
2021-05-29 10:56:25 +00:00
|
|
|
callLoadPlugins();
|
2021-03-19 23:06:29 +00:00
|
|
|
}
|
2023-01-17 20:55:39 +00:00
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-19 23:06:29 +00:00
|
|
|
|
2024-01-14 04:14:02 +00:00
|
|
|
// TODO move this elsewhere when SWIG plugins are removed
|
|
|
|
Pgm().GetPluginManager().ReloadPlugins();
|
|
|
|
|
2021-03-19 23:06:29 +00:00
|
|
|
if( !m_isFootprintEditor )
|
|
|
|
{
|
|
|
|
// Action plugins can be modified, therefore the plugins menu must be updated:
|
|
|
|
frame()->ReCreateMenuBar();
|
|
|
|
// Recreate top toolbar to add action plugin buttons
|
|
|
|
frame()->ReCreateHToolbar();
|
2021-12-03 08:30:24 +00:00
|
|
|
// Post a size event to force resizing toolbar by the AUI manager:
|
|
|
|
frame()->PostSizeEvent();
|
2021-03-19 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-29 10:56:25 +00:00
|
|
|
void SCRIPTING_TOOL::callLoadPlugins()
|
|
|
|
{
|
|
|
|
// Load pcbnew inside Python and load all the user plugins and package-based plugins
|
|
|
|
using namespace pybind11::literals;
|
|
|
|
|
2020-10-26 10:54:36 +00:00
|
|
|
auto locals = pybind11::dict(
|
|
|
|
"sys_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::STOCK ) ),
|
|
|
|
"user_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::USER ) ),
|
|
|
|
"third_party_path"_a =
|
|
|
|
TO_UTF8( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::THIRDPARTY ) ) );
|
2021-05-29 10:56:25 +00:00
|
|
|
|
|
|
|
pybind11::exec( R"(
|
|
|
|
import sys
|
|
|
|
import pcbnew
|
|
|
|
pcbnew.LoadPlugins( sys_path, user_path, third_party_path )
|
2020-10-26 10:54:36 +00:00
|
|
|
)",
|
|
|
|
pybind11::globals(), locals );
|
2021-05-29 10:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void SCRIPTING_TOOL::ShowPluginFolder()
|
2021-03-19 23:06:29 +00:00
|
|
|
{
|
2021-05-29 10:56:25 +00:00
|
|
|
wxString pluginpath( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::USER ) );
|
2021-06-15 16:14:19 +00:00
|
|
|
LaunchExternal( pluginpath );
|
2021-08-29 23:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:06:29 +00:00
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
int SCRIPTING_TOOL::showPluginFolder( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
ShowPluginFolder();
|
2021-03-19 23:06:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCRIPTING_TOOL::setTransitions()
|
|
|
|
{
|
|
|
|
Go( &SCRIPTING_TOOL::reloadPlugins, PCB_ACTIONS::pluginsReload.MakeEvent() );
|
|
|
|
Go( &SCRIPTING_TOOL::showPluginFolder, PCB_ACTIONS::pluginsShowFolder.MakeEvent() );
|
|
|
|
}
|