You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

  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. #include "pcb_scripting_tool.h"
  24. #include <action_plugin.h>
  25. #include <kiface_ids.h>
  26. #include <kiway.h>
  27. #include <macros.h>
  28. #include <python_scripting.h>
  29. #include <tools/pcb_actions.h>
  30. #include <pybind11/eval.h>
  31. #include <Python.h>
  32. #include <wx/string.h>
  33. #include <launch_ext.h>
  34. using initfunc = PyObject* (*)(void);
  35. SCRIPTING_TOOL::SCRIPTING_TOOL() :
  36. PCB_TOOL_BASE( "pcbnew.ScriptingTool" )
  37. {}
  38. SCRIPTING_TOOL::~SCRIPTING_TOOL()
  39. {}
  40. void SCRIPTING_TOOL::Reset( RESET_REASON aReason )
  41. {
  42. }
  43. bool SCRIPTING_TOOL::Init()
  44. {
  45. PyLOCK lock;
  46. std::string pymodule( "_pcbnew" );
  47. if( !SCRIPTING::IsModuleLoaded( pymodule ) )
  48. {
  49. KIFACE* kiface = frame()->Kiway().KiFACE( KIWAY::FACE_PCB );
  50. initfunc pcbnew_init = reinterpret_cast<initfunc>( kiface->IfaceOrAddress( KIFACE_SCRIPTING_LEGACY ) );
  51. PyImport_AddModule( pymodule.c_str() );
  52. PyObject* mod = pcbnew_init();
  53. PyObject* sys_mod = PyImport_GetModuleDict();
  54. PyDict_SetItemString( sys_mod, "_pcbnew", mod );
  55. Py_DECREF( mod );
  56. // plugins will be loaded later via ReloadPlugins()
  57. }
  58. return true;
  59. }
  60. void SCRIPTING_TOOL::ReloadPlugins()
  61. {
  62. // Reload Python plugins if they are newer than the already loaded, and load new plugins
  63. // Remove all action plugins so that we don't keep references to old versions
  64. ACTION_PLUGINS::UnloadAll();
  65. PyLOCK lock;
  66. callLoadPlugins();
  67. }
  68. int SCRIPTING_TOOL::reloadPlugins( const TOOL_EVENT& aEvent )
  69. {
  70. // Reload Python plugins if they are newer than the already loaded, and load new plugins
  71. // Remove all action plugins so that we don't keep references to old versions
  72. ACTION_PLUGINS::UnloadAll();
  73. {
  74. PyLOCK lock;
  75. callLoadPlugins();
  76. }
  77. if( !m_isFootprintEditor )
  78. {
  79. // Action plugins can be modified, therefore the plugins menu must be updated:
  80. frame()->ReCreateMenuBar();
  81. // Recreate top toolbar to add action plugin buttons
  82. frame()->ReCreateHToolbar();
  83. // Post a size event to force resizing toolbar by the AUI manager:
  84. frame()->PostSizeEvent();
  85. }
  86. return 0;
  87. }
  88. void SCRIPTING_TOOL::callLoadPlugins()
  89. {
  90. // Load pcbnew inside Python and load all the user plugins and package-based plugins
  91. using namespace pybind11::literals;
  92. auto locals = pybind11::dict(
  93. "sys_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::STOCK ) ),
  94. "user_path"_a = TO_UTF8( SCRIPTING::PyScriptingPath( SCRIPTING::PATH_TYPE::USER ) ),
  95. "third_party_path"_a =
  96. TO_UTF8( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::THIRDPARTY ) ) );
  97. pybind11::exec( R"(
  98. import sys
  99. import pcbnew
  100. pcbnew.LoadPlugins( sys_path, user_path, third_party_path )
  101. )",
  102. pybind11::globals(), locals );
  103. }
  104. void SCRIPTING_TOOL::ShowPluginFolder()
  105. {
  106. wxString pluginpath( SCRIPTING::PyPluginsPath( SCRIPTING::PATH_TYPE::USER ) );
  107. LaunchExternal( pluginpath );
  108. }
  109. int SCRIPTING_TOOL::showPluginFolder( const TOOL_EVENT& aEvent )
  110. {
  111. ShowPluginFolder();
  112. return 0;
  113. }
  114. void SCRIPTING_TOOL::setTransitions()
  115. {
  116. Go( &SCRIPTING_TOOL::reloadPlugins, PCB_ACTIONS::pluginsReload.MakeEvent() );
  117. Go( &SCRIPTING_TOOL::showPluginFolder, PCB_ACTIONS::pluginsShowFolder.MakeEvent() );
  118. }