From a7c4c8c7f20291ad71cca774a008561cbfbf0f9e Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 28 Jan 2017 18:47:11 +0100 Subject: [PATCH] Pcbnew: Fix a crash when reloading python scripts, when Kicad uses wxPython. also, refresh the board editor action plugins menu, when reloading python scripts from the footprint wizard selector. --- include/wxPcbStruct.h | 12 ++++++-- .../dialogs/dialog_footprint_wizard_list.cpp | 11 +++----- pcbnew/footprint_wizard_frame.cpp | 24 ++++++++++++++++ pcbnew/footprint_wizard_frame.h | 7 +++++ pcbnew/pcbframe.cpp | 28 +++++++++++++++++-- pcbnew/swig/pcbnew_action_plugins.cpp | 11 +------- pcbnew/swig/python_scripting.h | 6 +++- 7 files changed, 75 insertions(+), 24 deletions(-) diff --git a/include/wxPcbStruct.h b/include/wxPcbStruct.h index 671da135e3..ee85356494 100644 --- a/include/wxPcbStruct.h +++ b/include/wxPcbStruct.h @@ -139,9 +139,8 @@ protected: void OnActionPluginRefresh( wxCommandEvent& aEvent); #endif - - // Has meaning only if DKICAD_SCRIPTING_WXPYTHON option is on - /** + /** Has meaning only if KICAD_SCRIPTING_WXPYTHON option is + * not defined * @return the frame name identifier for the python console frame */ static const wxChar * pythonConsoleNameId() @@ -249,6 +248,13 @@ public: void OnQuit( wxCommandEvent& event ); + /** + * Reload the Python plugins if they are newer than + * the already loaded, and load new plugins if any + * Do nothing if KICAD_SCRIPTING is not defined + */ + void PythonPluginsReload(); + /** * Function GetAutoSaveFilePrefix * diff --git a/pcbnew/dialogs/dialog_footprint_wizard_list.cpp b/pcbnew/dialogs/dialog_footprint_wizard_list.cpp index 00a642eec2..0a342c8893 100644 --- a/pcbnew/dialogs/dialog_footprint_wizard_list.cpp +++ b/pcbnew/dialogs/dialog_footprint_wizard_list.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #if defined(KICAD_SCRIPTING) || defined(KICAD_SCRIPTING_WXPYTHON) #include @@ -150,13 +151,9 @@ void DIALOG_FOOTPRINT_WIZARD_LIST::initLists() void DIALOG_FOOTPRINT_WIZARD_LIST::onUpdatePythonModulesClick( wxCommandEvent& event ) { -#if defined(KICAD_SCRIPTING) || defined(KICAD_SCRIPTING_WXPYTHON) - char cmd[1024]; - snprintf( cmd, sizeof(cmd), - "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) ); - PyLOCK lock; - // ReRun the Python method pcbnew.LoadPlugins (already called when starting Pcbnew) - PyRun_SimpleString( cmd ); +#if defined(KICAD_SCRIPTING) + FOOTPRINT_WIZARD_FRAME* fpw_frame = static_cast( GetParent() ); + fpw_frame->PythonPluginsReload(); initLists(); #endif diff --git a/pcbnew/footprint_wizard_frame.cpp b/pcbnew/footprint_wizard_frame.cpp index 3428c8094a..0c1696addb 100644 --- a/pcbnew/footprint_wizard_frame.cpp +++ b/pcbnew/footprint_wizard_frame.cpp @@ -720,6 +720,30 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateVToolbar() // Currently, there is no vertical toolbar } +#if defined(KICAD_SCRIPTING) +#include +void FOOTPRINT_WIZARD_FRAME::PythonPluginsReload() +{ + // Reload the Python plugins + // Because the board editor has also a plugin python menu, + // call PCB_EDIT_FRAME::PythonPluginsReload() if the board editor + // is running + PCB_EDIT_FRAME* brd_frame = + static_cast( Kiway().Player( FRAME_PCB, false ) ); + + if( brd_frame ) + brd_frame->PythonPluginsReload(); + else + { + char cmd[1024]; + snprintf( cmd, sizeof(cmd), + "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) ); + PyLOCK lock; + // ReRun the Python method pcbnew.LoadPlugins (already called when starting Pcbnew) + PyRun_SimpleString( cmd ); + } +} +#endif // frame to display messages from footprint builder scripts FOOTPRINT_WIZARD_MESSAGES::FOOTPRINT_WIZARD_MESSAGES( FOOTPRINT_WIZARD_FRAME* aParent, wxConfigBase* aCfg ) : diff --git a/pcbnew/footprint_wizard_frame.h b/pcbnew/footprint_wizard_frame.h index 302d74601d..8e6a263fb8 100644 --- a/pcbnew/footprint_wizard_frame.h +++ b/pcbnew/footprint_wizard_frame.h @@ -74,6 +74,13 @@ public: MODULE* GetBuiltFootprint(); + /** + * Reload the Python plugins if they are newer than + * the already loaded, and load new plugins if any + * Do nothing if KICAD_SCRIPTING is not defined + */ + void PythonPluginsReload(); + private: void OnSize( wxSizeEvent& event ) override; diff --git a/pcbnew/pcbframe.cpp b/pcbnew/pcbframe.cpp index 2f1c2e59ee..94c83f6d69 100644 --- a/pcbnew/pcbframe.cpp +++ b/pcbnew/pcbframe.cpp @@ -1,10 +1,10 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr + * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck * Copyright (C) 2013-2016 Wayne Stambaugh - * Copyright (C) 2013-2016 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2013-2017 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -1056,7 +1056,6 @@ void PCB_EDIT_FRAME::ScriptingConsoleEnableDisable( wxCommandEvent& aEvent ) else wxMessageBox( wxT( "Error: unable to create the Python Console" ) ); } - #endif @@ -1159,3 +1158,26 @@ void PCB_EDIT_FRAME::OnFlipPcbView( wxCommandEvent& evt ) view->RecacheAllItems(); Refresh(); } + +void PCB_EDIT_FRAME::PythonPluginsReload() +{ + // Reload plugin list: reload Python plugins if they are newer than + // the already loaded, and load new plugins +#if defined(KICAD_SCRIPTING) + //Reload plugin list: reload Python plugins if they are newer than + // the already loaded, and load new plugins + char cmd[1024]; + + snprintf( cmd, sizeof(cmd), + "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) ); + + PyLOCK lock; + + // ReRun the Python method pcbnew.LoadPlugins (already called when starting Pcbnew) + PyRun_SimpleString( cmd ); + + #if defined(KICAD_SCRIPTING_ACTION_MENU) + RebuildActionPluginMenus(); + #endif +#endif +} diff --git a/pcbnew/swig/pcbnew_action_plugins.cpp b/pcbnew/swig/pcbnew_action_plugins.cpp index 3ca5aa4a21..3e3c1e01d6 100644 --- a/pcbnew/swig/pcbnew_action_plugins.cpp +++ b/pcbnew/swig/pcbnew_action_plugins.cpp @@ -199,16 +199,7 @@ void PCB_EDIT_FRAME::OnActionPlugin( wxCommandEvent& aEvent ) void PCB_EDIT_FRAME::OnActionPluginRefresh( wxCommandEvent& aEvent ) { - char cmd[1024]; - - snprintf( cmd, sizeof(cmd), - "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) ); - - PyLOCK lock; - // ReRun the Python method pcbnew.LoadPlugins (already called when starting Pcbnew) - PyRun_SimpleString( cmd ); - - RebuildActionPluginMenus(); + PythonPluginsReload(); } diff --git a/pcbnew/swig/python_scripting.h b/pcbnew/swig/python_scripting.h index 861d166855..b1517c9479 100644 --- a/pcbnew/swig/python_scripting.h +++ b/pcbnew/swig/python_scripting.h @@ -73,10 +73,14 @@ void pcbnewGetScriptsSearchPaths( wxString& aNames ); void pcbnewGetWizardsBackTrace( wxString& aNames ); #ifdef KICAD_SCRIPTING_WXPYTHON - void RedirectStdio(); wxWindow* CreatePythonShellWindow( wxWindow* parent, const wxString& aFramenameId ); +#endif + +#if 0 && defined (KICAD_SCRIPTING_WXPYTHON) +// This definition of PyLOCK crashed Pcbnew under some conditions (JPC), +// especially reloading plugins class PyLOCK { wxPyBlock_t b;