From 1b8ae7d688c8a698f81f58242b5f6370984a2c22 Mon Sep 17 00:00:00 2001 From: Sergey BELASH Date: Mon, 17 Nov 2014 15:33:28 +0300 Subject: [PATCH] Python API and example plugin are corrected to perform basic actions without crashes --- .../ConstructionPlugin_Plugin.h | 1 - src/ExchangePlugin/ExchangePlugin_Plugin.h | 1 - src/FeaturesPlugin/FeaturesPlugin_Plugin.h | 1 - src/ModelAPI/CMakeLists.txt | 5 ++- src/ModelAPI/ModelAPI.i | 45 ++++++++++++++----- src/PartSetPlugin/PartSetPlugin_Plugin.h | 1 - .../PythonFeaturesPlugin.py | 2 +- .../PythonFeaturesPlugin_Box.py | 32 +++++++------ src/SketchPlugin/SketchPlugin_Plugin.h | 1 - 9 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/ConstructionPlugin/ConstructionPlugin_Plugin.h b/src/ConstructionPlugin/ConstructionPlugin_Plugin.h index 143b786e5..79f48abe5 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Plugin.h +++ b/src/ConstructionPlugin/ConstructionPlugin_Plugin.h @@ -16,7 +16,6 @@ class CONSTRUCTIONPLUGIN_EXPORT ConstructionPlugin_Plugin : public ModelAPI_Plug virtual FeaturePtr createFeature(std::string theFeatureID); public: - /// Is needed for python wrapping by swig ConstructionPlugin_Plugin(); }; diff --git a/src/ExchangePlugin/ExchangePlugin_Plugin.h b/src/ExchangePlugin/ExchangePlugin_Plugin.h index 5cb5f20f0..d614028f1 100644 --- a/src/ExchangePlugin/ExchangePlugin_Plugin.h +++ b/src/ExchangePlugin/ExchangePlugin_Plugin.h @@ -16,7 +16,6 @@ class EXCHANGEPLUGIN_EXPORT ExchangePlugin_Plugin : public ModelAPI_Plugin virtual FeaturePtr createFeature(std::string theFeatureID); public: - /// Is needed for python wrapping by swig ExchangePlugin_Plugin(); }; diff --git a/src/FeaturesPlugin/FeaturesPlugin_Plugin.h b/src/FeaturesPlugin/FeaturesPlugin_Plugin.h index 560a2242a..e56ac83ea 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Plugin.h +++ b/src/FeaturesPlugin/FeaturesPlugin_Plugin.h @@ -16,7 +16,6 @@ class FEATURESPLUGIN_EXPORT FeaturesPlugin_Plugin : public ModelAPI_Plugin virtual FeaturePtr createFeature(std::string theFeatureID); public: - /// Is needed for python wrapping by swig FeaturesPlugin_Plugin(); }; diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 8dff4fb3f..54c0b472d 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -47,7 +47,9 @@ SET(PROJECT_LIBRARIES Config ) -ADD_DEFINITIONS(-DMODELAPI_EXPORTS) +SET(CMAKE_SWIG_FLAGS "-Wall") +ADD_DEFINITIONS(-DMODELAPI_EXPORTS -DSWIG_TYPE_TABLE=ModelAPI) + ADD_LIBRARY(ModelAPI SHARED ${PROJECT_SOURCES} ${PROJECT_HEADERS}) SET_TARGET_PROPERTIES(ModelAPI PROPERTIES LINKER_LANGUAGE CXX) TARGET_LINK_LIBRARIES(ModelAPI ${PROJECT_LIBRARIES}) @@ -59,7 +61,6 @@ INCLUDE_DIRECTORIES( ../GeomAlgoAPI ) -SET(CMAKE_SWIG_FLAGS "-Wall") SET_SOURCE_FILES_PROPERTIES(ModelAPI.i PROPERTIES CPLUSPLUS ON) # "-includeall" is not needed: it starts to follow the standard inludes (like "string") without success diff --git a/src/ModelAPI/ModelAPI.i b/src/ModelAPI/ModelAPI.i index 711c77aac..4d0ebb256 100644 --- a/src/ModelAPI/ModelAPI.i +++ b/src/ModelAPI/ModelAPI.i @@ -1,5 +1,13 @@ /* ModelAPI.i */ %module(directors="1") ModelAPI +%feature("director:except") { + if ($error != NULL) { + PyErr_Print(); + std::cerr << std::endl; + throw Swig::DirectorMethodException(); + } +} + %{ #include "GeomAPI_Interface.h" #include "GeomAPI_Shape.h" @@ -47,19 +55,18 @@ // directors %feature("director") ModelAPI_Plugin; -// %feature("director") ModelAPI_Object; -// %feature("director") ModelAPI_Feature; -// %feature("director") ModelAPI_CompositeFeature; +%feature("director") ModelAPI_Object; +%feature("director") ModelAPI_Feature; // boost pointers -%include +%include "boost_shared_ptr.i" // For ModelAPI_ResultConstruction.shape() %shared_ptr(GeomAPI_Interface) %shared_ptr(GeomAPI_Shape) %shared_ptr(ModelAPI_Document) %shared_ptr(ModelAPI_Session) +%shared_ptr(ModelAPI_Plugin) %shared_ptr(ModelAPI_Object) -// %shared_ptr(ModelAPI_Plugin) %shared_ptr(ModelAPI_Feature) %shared_ptr(ModelAPI_CompositeFeature) %shared_ptr(ModelAPI_Data) @@ -79,14 +86,17 @@ %shared_ptr(ModelAPI_ResultBody) %shared_ptr(ModelAPI_ResultPart) +// Don't create default constructors +// %nodefaultctor ModelAPI_Plugin; +// %nodefaultctor ModelAPI_Session; + // all supported interfaces %include "GeomAPI_Interface.h" %include "GeomAPI_Shape.h" %include "ModelAPI_Document.h" %include "ModelAPI_Session.h" -%include "ModelAPI_Object.h" -// %nodefaultctor ModelAPI_Plugin; %include "ModelAPI_Plugin.h" +%include "ModelAPI_Object.h" %include "ModelAPI_Feature.h" %include "ModelAPI_CompositeFeature.h" %include "ModelAPI_Data.h" @@ -111,9 +121,24 @@ %template(ResultList) std::list >; template boost::shared_ptr boost_cast(boost::shared_ptr theObject); + +// Feature casts +%template(modelAPI_Feature) boost_cast; %template(modelAPI_CompositeFeature) boost_cast; -%template(modelAPI_ResultConstruction) boost_cast; -%template(modelAPI_ResultBody) boost_cast; -%template(modelAPI_ResultPart) boost_cast; +// Result casts +%template(modelAPI_ResultConstruction) boost_cast; +%template(modelAPI_ResultBody) boost_cast; +%template(modelAPI_ResultPart) boost_cast; +// Attribute casts +%template(modelAPI_AttributeDocRef) boost_cast; +%template(modelAPI_AttributeDouble) boost_cast; +%template(modelAPI_AttributeInteger) boost_cast; +%template(modelAPI_AttributeString) boost_cast; +%template(modelAPI_AttributeReference) boost_cast; +%template(modelAPI_AttributeRefAttr) boost_cast; +%template(modelAPI_AttributeBoolean) boost_cast; +%template(modelAPI_AttributeSelection) boost_cast; +%template(modelAPI_AttributeSelectionList) boost_cast; +%template(modelAPI_AttributeRefList) boost_cast; diff --git a/src/PartSetPlugin/PartSetPlugin_Plugin.h b/src/PartSetPlugin/PartSetPlugin_Plugin.h index e8e9e702b..5d91f7d46 100644 --- a/src/PartSetPlugin/PartSetPlugin_Plugin.h +++ b/src/PartSetPlugin/PartSetPlugin_Plugin.h @@ -16,7 +16,6 @@ class PARTSETPLUGIN_EXPORT PartSetPlugin_Plugin : public ModelAPI_Plugin virtual FeaturePtr createFeature(std::string theFeatureID); public: - /// Is needed for python wrapping by swig PartSetPlugin_Plugin(); }; diff --git a/src/PythonFeaturesPlugin/PythonFeaturesPlugin.py b/src/PythonFeaturesPlugin/PythonFeaturesPlugin.py index dc4219d11..4795eaf03 100644 --- a/src/PythonFeaturesPlugin/PythonFeaturesPlugin.py +++ b/src/PythonFeaturesPlugin/PythonFeaturesPlugin.py @@ -9,7 +9,7 @@ class PythonFeaturesPlugin(ModelAPI.ModelAPI_Plugin): def createFeature(self, theFeatureID): if theFeatureID == PythonFeaturesPlugin_Box.ID(): - return PythonFeaturesPlugin_Box() + return PythonFeaturesPlugin_Box().__disown__() else: raise StandardError("No such feature %s"%theFeatureID) diff --git a/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py b/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py index e67278b22..b08f98b41 100644 --- a/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py +++ b/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py @@ -1,9 +1,9 @@ -from ModelAPI import * +import ModelAPI -class PythonFeaturesPlugin_Box(ModelAPI_Feature): +class PythonFeaturesPlugin_Box(ModelAPI.ModelAPI_Feature): "Feature to create a box by drawing a sketch and extruding it" def __init__(self): - pass + ModelAPI.ModelAPI_Feature.__init__(self) @staticmethod def ID(): @@ -21,20 +21,24 @@ class PythonFeaturesPlugin_Box(ModelAPI_Feature): def HEIGHT_ID(): return "box_height" + def getKind(self): + return PythonFeaturesPlugin_Box.ID() + def initAttributes(self): - self.data().addAttribute(PythonFeaturesPlugin_Box.WIDTH_ID(), ModelAPI_AttributeDouble.type()) - self.data().addAttribute(PythonFeaturesPlugin_Box.LENGTH_ID(), ModelAPI_AttributeDouble.type()) - self.data().addAttribute(PythonFeaturesPlugin_Box.HEIGHT_ID(), ModelAPI_AttributeDouble.type()) + # C++ static methods (in example Type() of ModelAPI_AttributeDouble + # should be called like this: moduleName.ClassName_StaticMethod() + self.data().addAttribute(PythonFeaturesPlugin_Box.WIDTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) + self.data().addAttribute(PythonFeaturesPlugin_Box.LENGTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) + self.data().addAttribute(PythonFeaturesPlugin_Box.HEIGHT_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) def execute(self): - aWidth = self.attribute(PythonFeaturesPlugin_Box.WIDTH_ID()).value() - aLength = self.attribute(PythonFeaturesPlugin_Box.LENGTH_ID()).value() - aHeight = self.attribute(PythonFeaturesPlugin_Box.HEIGHT_ID()).value() - - aResult = document().createBody(data()) - #aResult.store(UserPackage.makeBox(aLength, aWidth, aHeight) - - #self.setResult(aResult) + aWidth = self.real(PythonFeaturesPlugin_Box.WIDTH_ID()).value() + aLength = self.real(PythonFeaturesPlugin_Box.LENGTH_ID()).value() + aHeight = self.real(PythonFeaturesPlugin_Box.HEIGHT_ID()).value() + print ("Box W:{0} L:{1} H:{2}".format(aWidth, aLength, aHeight)) + # aResult = document().createBody(data()) + # aResult.store(UserPackage.makeBox(aLength, aWidth, aHeight) + # self.setResult(aResult) diff --git a/src/SketchPlugin/SketchPlugin_Plugin.h b/src/SketchPlugin/SketchPlugin_Plugin.h index bb81ba781..3b480410b 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.h +++ b/src/SketchPlugin/SketchPlugin_Plugin.h @@ -16,7 +16,6 @@ class SKETCHPLUGIN_EXPORT SketchPlugin_Plugin : public ModelAPI_Plugin virtual FeaturePtr createFeature(std::string theFeatureID); public: - /// Is needed for python wrapping by swig SketchPlugin_Plugin(); }; -- 2.39.2