Commit f4223506 authored by Miguel Angel Ajo's avatar Miguel Angel Ajo

pcbnew_scripting_helpers.cpp/h to handle generic scripting tools

CMakeLists.txt options to build scripting or scripting modules
parent 970e0333
...@@ -47,6 +47,16 @@ option(KICAD_TESTING_VERSION ...@@ -47,6 +47,16 @@ option(KICAD_TESTING_VERSION
"set this option to ON to build the stable version of KICAD. mainly used to set version ID (default OFF)" "set this option to ON to build the stable version of KICAD. mainly used to set version ID (default OFF)"
) )
option(KICAD_SCRIPTING
"set this option ON to build the scripting support inside kicad binaries"
)
option(KICAD_SCRIPTING_MODULES
"set this option ON to build kicad modules that can be used from scripting languages"
)
#Set version option (stable or testing) #Set version option (stable or testing)
if (KICAD_STABLE_VERSION ) if (KICAD_STABLE_VERSION )
......
add_definitions(-DPCBNEW) add_definitions(-DPCBNEW)
FIND_PACKAGE(SWIG REQUIRED) if (KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(SWIG REQUIRED)
FIND_PACKAGE(PythonLibs) INCLUDE(${SWIG_USE_FILE})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
add_definitions(-DPCBNEW -DKICAD_SCRIPTING)
endif()
### ###
# Includes # Includes
...@@ -223,79 +226,78 @@ set(PCBNEW_COMMON_SRCS ...@@ -223,79 +226,78 @@ set(PCBNEW_COMMON_SRCS
## ##
# Scripting sources # Scripting sources
## ##
set(PCBNEW_SCRIPTING_PYTHON_HELPERS
scripting/wx_python_helpers.cpp
scripting/python_scripting.cpp
)
set(PCBNEW_SCRIPTING_SRCS if (KICAD_SCRIPTING)
# kicad_wrap.cxx
pcbnew_wrap.cxx set(PCBNEW_SCRIPTING_PYTHON_HELPERS
${PCBNEW_SCRIPTING_PYTHON_HELPERS} scripting/wx_python_helpers.cpp
) scripting/pcbnew_scripting_helpers.cpp
scripting/python_scripting.cpp
)
set(PCBNEW_SCRIPTING_SRCS
pcbnew_wrap.cxx
${PCBNEW_SCRIPTING_PYTHON_HELPERS}
)
endif()
## ##
# Scripting build # Scripting build
## ##
if (KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES)
set(SWIG_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR}/../.. -I${CMAKE_CURRENT_SOURCE_DIR} -I${CMAKE_CURRENT_SOURCE_DIR}/../include -DDEBUG)
# collect CFLAGS , and pass them to swig later
get_directory_property( DirDefs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_DEFINITIONS )
foreach( d ${DirDefs} )
SET(SWIG_FLAGS ${SWIG_FLAGS} -D${d} )
endforeach()
# check if we have IO_MGR and KICAD_PLUGIN available
if ( USE_NEW_PCBNEW_LOAD OR USE_NEW_PCBNEW_SAVE )
SET(SWIG_FLAGS ${SWIG_FLAGS} -DBUILD_WITH_PLUGIN)
endif()
set(SWIG_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR}/../.. -I${CMAKE_CURRENT_SOURCE_DIR} -I${CMAKE_CURRENT_SOURCE_DIR}/../include -DDEBUG)
# collect CFLAGS , and pass them to swig later
get_directory_property( DirDefs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_DEFINITIONS )
foreach( d ${DirDefs} )
SET(SWIG_FLAGS ${SWIG_FLAGS} -D${d} )
endforeach()
# check if we have IO_MGR and KICAD_PLUGIN available
if ( USE_NEW_PCBNEW_LOAD OR USE_NEW_PCBNEW_SAVE )
SET(SWIG_FLAGS ${SWIG_FLAGS} -DBUILD_WITH_PLUGIN)
endif() endif()
if (KICAD_SCRIPTING)
foreach( d ${SWIG_FLAGS} )
message(STATUS "Swig options:" ${d}) SET(SWIG_OPTS -python -c++ -outdir ${CMAKE_CURRENT_BINARY_DIR} ${SWIG_FLAGS} )
endforeach()
add_custom_command(
SET(SWIG_OPTS -python -c++ -outdir ${CMAKE_CURRENT_BINARY_DIR} ${SWIG_FLAGS} ) OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx
COMMAND ${SWIG_EXECUTABLE} ${SWIG_OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx scripting/pcbnew.i
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
# we checked the posibility to build kicad thinks in a separate module, but it )
# will be easier in just one "pcbnew" for now
# endif(KICAD_SCRIPTING)
#add_custom_command(
# OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/kicad_wrap.cxx
# COMMAND ${SWIG_EXECUTABLE} ${SWIG_OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/kicad_wrap.cxx scripting/kicad.i
# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
#)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx
COMMAND ${SWIG_EXECUTABLE} ${SWIG_OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx scripting/pcbnew.i
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
### ###
# _pcbnew DLL/DSO file creation # _pcbnew DLL/DSO file creation
### ###
SET(CMAKE_SWIG_FLAGS ${SWIG_FLAGS}) if (KICAD_SCRIPTING_MODULES)
SET_SOURCE_FILES_PROPERTIES(scripting/pcbnew.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(pcbnew python scripting/pcbnew.i ${PCBNEW_SCRIPTING_PYTHON_HELPERS} ${PCBNEW_SRCS}) SET(CMAKE_SWIG_FLAGS ${SWIG_FLAGS})
SWIG_LINK_LIBRARIES(pcbnew SET_SOURCE_FILES_PROPERTIES(scripting/pcbnew.i PROPERTIES CPLUSPLUS ON)
3d-viewer SWIG_ADD_MODULE(pcbnew python scripting/pcbnew.i ${PCBNEW_SCRIPTING_PYTHON_HELPERS} ${PCBNEW_SRCS})
pcbcommon SWIG_LINK_LIBRARIES(pcbnew
common 3d-viewer
bitmaps pcbcommon
polygon common
kbool bitmaps
${wxWidgets_LIBRARIES} polygon
${OPENGL_LIBRARIES} kbool
${GDI_PLUS_LIBRARIES} ${wxWidgets_LIBRARIES}
${PYTHON_LIBRARIES}) ${OPENGL_LIBRARIES}
${GDI_PLUS_LIBRARIES}
${PYTHON_LIBRARIES})
endif (KICAD_SCRIPTING_MODULES)
### ###
# Windows resource file # Windows resource file
...@@ -390,6 +392,14 @@ install(TARGETS pcbnew ...@@ -390,6 +392,14 @@ install(TARGETS pcbnew
DESTINATION ${KICAD_BIN} DESTINATION ${KICAD_BIN}
COMPONENT binary) COMPONENT binary)
if (KICAD_SCRIPTING_MODULES)
install(FILES ${CMAKE_BINARY_DIR}/pcbnew/pcbnew.py
DESTINATION share/python)
install(FILES ${CMAKE_BINARY_DIR}/pcbnew/_pcbnew.so
DESTINATION share/python)
endif(KICAD_SCRIPTING_MODULES)
# The specctra test fails to build properly using MS Visual Studio. # The specctra test fails to build properly using MS Visual Studio.
if(NOT MSVC) if(NOT MSVC)
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
#include <dialogs/dialog_scripting.h> #include <dialogs/dialog_scripting.h>
#include <scripting/python_scripting.h> #include <scripting/python_scripting.h>
#include <scripting/pcbnew_scripting_helpers.h>
// Colors for layers and items // Colors for layers and items
COLORS_DESIGN_SETTINGS g_ColorsSettings; COLORS_DESIGN_SETTINGS g_ColorsSettings;
...@@ -113,8 +114,11 @@ bool EDA_APP::OnInit() ...@@ -113,8 +114,11 @@ bool EDA_APP::OnInit()
PCB_EDIT_FRAME* frame = NULL; PCB_EDIT_FRAME* frame = NULL;
int i=0; int i=0;
#ifdef KICAD_SCRIPTING
pcbnewInitPythonScripting(); pcbnewInitPythonScripting();
#endif
InitEDA_Appl( wxT( "Pcbnew" ), APP_PCBNEW_T ); InitEDA_Appl( wxT( "Pcbnew" ), APP_PCBNEW_T );
...@@ -150,6 +154,11 @@ Changing extension to .brd." ), GetChars( fn.GetFullPath() ) ); ...@@ -150,6 +154,11 @@ Changing extension to .brd." ), GetChars( fn.GetFullPath() ) );
ReadHotkeyConfig( wxT( "PcbFrame" ), g_Board_Editor_Hokeys_Descr ); ReadHotkeyConfig( wxT( "PcbFrame" ), g_Board_Editor_Hokeys_Descr );
frame = new PCB_EDIT_FRAME( NULL, wxT( "Pcbnew" ), wxPoint( 0, 0 ), wxSize( 600, 400 ) ); frame = new PCB_EDIT_FRAME( NULL, wxT( "Pcbnew" ), wxPoint( 0, 0 ), wxSize( 600, 400 ) );
#ifdef KICAD_SCRIPTING
ScriptingSetPcbEditFrame(frame); /* give the scripting helpers access to our frame */
#endif
frame->UpdateTitle(); frame->UpdateTitle();
SetTopWindow( frame ); SetTopWindow( frame );
...@@ -202,7 +211,7 @@ Changing extension to .brd." ), GetChars( fn.GetFullPath() ) ); ...@@ -202,7 +211,7 @@ Changing extension to .brd." ), GetChars( fn.GetFullPath() ) );
*/ */
frame->SetFocus(); frame->SetFocus();
frame->GetCanvas()->SetFocus(); frame->GetCanvas()->SetFocus();
pythonSetPcbEditFrame(frame);
DIALOG_SCRIPTING* sw = new DIALOG_SCRIPTING(frame); DIALOG_SCRIPTING* sw = new DIALOG_SCRIPTING(frame);
sw->Show(true); sw->Show(true);
return true; return true;
......
...@@ -25,17 +25,23 @@ ...@@ -25,17 +25,23 @@
#include <common.h> #include <common.h>
#include <wx_python_helpers.h> #include <wx_python_helpers.h>
#include <cstddef> #include <cstddef>
#include <vector> #include <vector>
using namespace std; using namespace std;
#include <class_title_block.h>
#include <class_colors_design_settings.h>
%} %}
/* all the wx wrappers for wxString, wxPoint, wxRect, wxChar .. */
%include <wx.i>
%include <dlist.h> %include <dlist.h>
%include <base_struct.h> %include <base_struct.h>
%include <common.h> %include <common.h>
%include <class_title_block.h>
%include <class_colors_design_settings.h>
/* all the wx wrappers for wxString, wxPoint, wxRect, wxChar .. */
%include <wx.i>
/* /*
namespace std namespace std
......
%module pcbnew %module pcbnew
%include "kicad.i" %include "kicad.i"
%{ // this is what it must be included in the wrapper .cxx code to compile
#include <wx_python_helpers.h>
%{
#include <wx_python_helpers.h>
#include <class_board_item.h> #include <class_board_item.h>
#include <class_board.h> #include <class_board.h>
#include <class_module.h> #include <class_module.h>
#include <class_track.h> #include <class_track.h>
#include <class_pad.h> #include <class_pad.h>
#include <class_netinfo.h> #include <class_netinfo.h>
#include <class_pcb_text.h> #include <class_pcb_text.h>
#include <class_dimension.h> #include <class_dimension.h>
#include <dlist.h> #include <dlist.h>
#include <class_zone_settings.h>
#include <class_netclass.h>
#include <class_netinfo.h>
#include <layers_id_colors_and_visibility.h>
#include <pcbnew_scripting_helpers.h>
BOARD *GetBoard(); /* get current editor board */
BOARD *GetBoard();
%} %}
#ifdef BUILD_WITH_PLUGIN #ifdef BUILD_WITH_PLUGIN
...@@ -27,7 +30,6 @@ ...@@ -27,7 +30,6 @@
%} %}
#endif #endif
%include <class_board_item.h> %include <class_board_item.h>
%include <class_board.h> %include <class_board.h>
%include <class_module.h> %include <class_module.h>
...@@ -37,12 +39,32 @@ ...@@ -37,12 +39,32 @@
%include <class_pcb_text.h> %include <class_pcb_text.h>
%include <class_dimension.h> %include <class_dimension.h>
%include <dlist.h> %include <dlist.h>
%include <class_zone_settings.h>
%include <class_netclass.h>
%include <class_netinfo.h>
%include <layers_id_colors_and_visibility.h>
/* the IO_ERROR exception handler, not working yet... */
%exception
{
try {
$function
}
catch (IO_ERROR e) {
PyErr_SetString(PyExc_IOError,"IO error");
return NULL;
}
}
%include <pcbnew_scripting_helpers.h>
#ifdef BUILD_WITH_PLUGIN #ifdef BUILD_WITH_PLUGIN
%include <io_mgr.h> %include <io_mgr.h>
%include <kicad_plugin.h> %include <kicad_plugin.h>
#endif #endif
/* this is to help python with the * accessor of DLIST templates */
%rename(Get) operator BOARD_ITEM*; %rename(Get) operator BOARD_ITEM*;
%rename(Get) operator TRACK*; %rename(Get) operator TRACK*;
%rename(Get) operator D_PAD*; %rename(Get) operator D_PAD*;
...@@ -51,11 +73,16 @@ ...@@ -51,11 +73,16 @@
BOARD *GetBoard(); BOARD *GetBoard();
// se must translate C++ templates to scripting languages // we must translate C++ templates to scripting languages
%template(BOARD_ITEM_List) DLIST<BOARD_ITEM>; %template(BOARD_ITEM_List) DLIST<BOARD_ITEM>;
%template(MODULE_List) DLIST<MODULE>; %template(MODULE_List) DLIST<MODULE>;
%template(TRACK_List) DLIST<TRACK>; %template(TRACK_List) DLIST<TRACK>;
%template(PAD_List) DLIST<D_PAD>; %template(PAD_List) DLIST<D_PAD>;
/* TODO: -the std::* compilatio is broken with some swig + gcc combinations
* see kicad.i for more information.
* %template(MARKER_Vector) std::vector<MARKER_PCB*>;
* %template(ZONE_CONTAINER_Vector) std::vector<ZONE_CONTAINER*>;
*/
#include <pcbnew_scripting_helpers.h>
#include <pcbnew.h>
#include <pcbnew_id.h>
#include <build_version.h>
#include <class_board.h>
#include <kicad_string.h>
#include <io_mgr.h>
static PCB_EDIT_FRAME *PcbEditFrame=NULL;
BOARD *GetBoard()
{
if (PcbEditFrame) return PcbEditFrame->GetBoard();
else return NULL;
}
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPCBEdaFrame)
{
PcbEditFrame = aPCBEdaFrame;
}
BOARD* LoadBoard(wxString aFileName)
{
#ifdef USE_NEW_PCBNEW_LOAD
try{
return IO_MGR::Load(IO_MGR::KICAD,aFileName);
} catch (IO_ERROR)
{
return NULL;
}
#else
fprintf(stderr,"Warning, LoadBoard not implemented without USE_NEW_PCBNEW_LOAD\n");
return NULL;
#endif
}
bool SaveBoard(wxString aFileName, BOARD* aBoard)
{
#ifdef USE_NEW_PCBNEW_LOAD
aBoard->m_Status_Pcb &= ~CONNEXION_OK;
aBoard->SynchronizeNetsAndNetClasses();
aBoard->SetCurrentNetClass( aBoard->m_NetClasses.GetDefault()->GetName() );
wxString header = wxString::Format(
wxT( "PCBNEW-BOARD Version %d date %s\n\n# Created by Pcbnew%s\n\n" ),
BOARD_FILE_VERSION, DateAndTime().GetData(),
GetBuildVersion().GetData() );
PROPERTIES props;
props["header"] = header;
try
{
IO_MGR::Save( IO_MGR::KICAD, aFileName, aBoard, &props );
return true;
}
catch (IO_ERROR)
{
return false;
}
#else
fprintf(stderr,"Warning, SaveBoard not implemented without USE_NEW_PCBNEW_LOAD\n");
return false;
#endif
}
#ifndef __PCBNEW_SCRIPTING_HELPERS_H
#define __PCBNEW_SCRIPTING_HELPERS_H
#include <wxPcbStruct.h>
/* we could be including all these methods as static in a class, but
* we want plain pcbnew.<method_name> access from python */
#ifndef SWIG
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPCBEdaFrame);
#endif
BOARD* LoadBoard(wxString aFileName);
bool SaveBoard(wxString aFileName, BOARD* aBoard);
#endif
\ No newline at end of file
...@@ -18,7 +18,8 @@ static int SwigNumModules = 0; ...@@ -18,7 +18,8 @@ static int SwigNumModules = 0;
/* Add a name + initfuction to our SwigImportInittab */ /* Add a name + initfuction to our SwigImportInittab */
static void swigAddModule(const char *name, void (*initfunc)()) { static void swigAddModule(const char *name, void (*initfunc)())
{
SwigImportInittab[SwigNumModules].name = (char *)name; SwigImportInittab[SwigNumModules].name = (char *)name;
SwigImportInittab[SwigNumModules].initfunc = initfunc; SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigNumModules++; SwigNumModules++;
...@@ -28,7 +29,8 @@ static void swigAddModule(const char *name, void (*initfunc)()) { ...@@ -28,7 +29,8 @@ static void swigAddModule(const char *name, void (*initfunc)()) {
/* Add the builting python modules */ /* Add the builting python modules */
static void swigAddBuiltin() { static void swigAddBuiltin()
{
int i = 0; int i = 0;
while (PyImport_Inittab[i].name) { while (PyImport_Inittab[i].name) {
swigAddModule(PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc); swigAddModule(PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc);
...@@ -38,26 +40,18 @@ static void swigAddBuiltin() { ...@@ -38,26 +40,18 @@ static void swigAddBuiltin() {
} }
static void swigAddModules() static void swigAddModules()
{ {
//swigAddModule("_kicad",init_kicad);
swigAddModule("_pcbnew",init_pcbnew); swigAddModule("_pcbnew",init_pcbnew);
// finally it seems better to include all in just one module
// but in case we needed to include any other modules,
// it must be done like this:
// swigAddModule("_kicad",init_kicad);
} }
static void swigSwitchPythonBuiltin() static void swigSwitchPythonBuiltin()
{ {
PyImport_Inittab = SwigImportInittab; PyImport_Inittab = SwigImportInittab;
}
static PCB_EDIT_FRAME *PcbEditFrame=NULL;
BOARD *GetBoard()
{
if (PcbEditFrame) return PcbEditFrame->GetBoard();
else return NULL;
}
void pythonSetPcbEditFrame(PCB_EDIT_FRAME *aPCBEdaFrame)
{
PcbEditFrame = aPCBEdaFrame;
} }
...@@ -83,8 +77,9 @@ void pcbnewInitPythonScripting() ...@@ -83,8 +77,9 @@ void pcbnewInitPythonScripting()
of kicad here */ of kicad here */
PyRun_SimpleString("import sys\n" PyRun_SimpleString("import sys\n"
"sys.path.append(\".\")\n" "sys.path.append(\".\")\n"
"import pcbnew\n"); "import pcbnew\n");
} }
\ No newline at end of file
#ifndef __PYTHON_SCRIPTING_H #ifndef __PYTHON_SCRIPTING_H
#define __PYTHON_SCRIPTING_H #define __PYTHON_SCRIPTING_H
#include <wxPcbStruct.h>
#include <Python.h> #include <Python.h>
void pythonSetPcbEditFrame(PCB_EDIT_FRAME *aPCBEdaFrame);
void pcbnewInitPythonScripting(void); void pcbnewInitPythonScripting(void);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment