From 6f28e05a6892ea3e1f06a0ddad78c5e4dbbbf83b Mon Sep 17 00:00:00 2001 From: sbh Date: Tue, 25 Nov 2014 12:13:45 +0300 Subject: [PATCH] BoxFeature is rewritten + small corrections in APIs --- src/ModelAPI/CMakeLists.txt | 2 +- src/ModelAPI/ModelAPI.i | 32 +++---- src/ModelAPI/ModelAPI_AttributeRefAttr.h | 4 +- .../PythonFeaturesPlugin_Box.py | 90 +++++++++++++++---- src/PythonFeaturesPlugin/box_widget.xml | 6 +- src/PythonFeaturesPlugin/examples.py | 30 ++++--- src/PythonFeaturesPlugin/extrusion.py | 2 +- src/PythonFeaturesPlugin/sketch.py | 4 + 8 files changed, 115 insertions(+), 55 deletions(-) diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 5df84da6a..87a43a2b7 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -48,7 +48,7 @@ SET(PROJECT_LIBRARIES Config ) SET(CMAKE_SWIG_FLAGS -threads -Wall) -ADD_DEFINITIONS(-DMODELAPI_EXPORTS -DSWIG_TYPE_TABLE=ModelAPI) +ADD_DEFINITIONS(-DMODELAPI_EXPORT) ADD_LIBRARY(ModelAPI SHARED ${PROJECT_SOURCES} ${PROJECT_HEADERS}) SET_TARGET_PROPERTIES(ModelAPI PROPERTIES LINKER_LANGUAGE CXX) diff --git a/src/ModelAPI/ModelAPI.i b/src/ModelAPI/ModelAPI.i index d1ffd5643..5043bcfe1 100644 --- a/src/ModelAPI/ModelAPI.i +++ b/src/ModelAPI/ModelAPI.i @@ -131,30 +131,22 @@ %template(ObjectList) std::list >; %template(ResultList) std::list >; -template boost::shared_ptr boost_cast(boost::shared_ptr theObject); - -// Feature casts -%template(modelAPI_Feature) shared_ptr_cast; -%template(modelAPI_CompositeFeature) shared_ptr_cast; template std::shared_ptr shared_ptr_cast(std::shared_ptr theObject); %template(modelAPI_CompositeFeature) shared_ptr_cast; +%template(modelAPI_Feature) shared_ptr_cast; +// Result casts %template(modelAPI_ResultConstruction) shared_ptr_cast; %template(modelAPI_ResultBody) shared_ptr_cast; %template(modelAPI_ResultPart) shared_ptr_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; +%template(modelAPI_AttributeDocRef) shared_ptr_cast; +%template(modelAPI_AttributeDouble) shared_ptr_cast; +%template(modelAPI_AttributeInteger) shared_ptr_cast; +%template(modelAPI_AttributeString) shared_ptr_cast; +%template(modelAPI_AttributeReference) shared_ptr_cast; +%template(modelAPI_AttributeRefAttr) shared_ptr_cast; +%template(modelAPI_AttributeBoolean) shared_ptr_cast; +%template(modelAPI_AttributeSelection) shared_ptr_cast; +%template(modelAPI_AttributeSelectionList) shared_ptr_cast; +%template(modelAPI_AttributeRefList) shared_ptr_cast; diff --git a/src/ModelAPI/ModelAPI_AttributeRefAttr.h b/src/ModelAPI/ModelAPI_AttributeRefAttr.h index 5644cc47f..b1e9ad233 100644 --- a/src/ModelAPI/ModelAPI_AttributeRefAttr.h +++ b/src/ModelAPI/ModelAPI_AttributeRefAttr.h @@ -21,10 +21,10 @@ class ModelAPI_AttributeRefAttr : public ModelAPI_Attribute MODELAPI_EXPORT virtual bool isObject() = 0; /// Defines the reference to the attribute - MODELAPI_EXPORT virtual void setAttr(std::shared_ptr theAttr) = 0; + MODELAPI_EXPORT virtual void setAttr(AttributePtr theAttr) = 0; /// Returns attribute referenced from this attribute - MODELAPI_EXPORT virtual std::shared_ptr attr() = 0; + MODELAPI_EXPORT virtual AttributePtr attr() = 0; /// Defines the reference to the object MODELAPI_EXPORT virtual void setObject(ObjectPtr theFeature) = 0; diff --git a/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py b/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py index 05f6460d6..4dbf89abc 100644 --- a/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py +++ b/src/PythonFeaturesPlugin/PythonFeaturesPlugin_Box.py @@ -1,5 +1,7 @@ import ModelAPI -import examples +import sketch +import extrusion +from SketchResult import SketchResult class PythonFeaturesPlugin_Box(ModelAPI.ModelAPI_Feature): @@ -25,29 +27,85 @@ class PythonFeaturesPlugin_Box(ModelAPI.ModelAPI_Feature): def HEIGHT_ID(): return "box_height" + @staticmethod + def WIDTH_REF_ID(): + return "box_ref_width" + + @staticmethod + def LENGTH_REF_ID(): + return "box_ref_length" + + @staticmethod + def HEIGHT_REF_ID(): + return "box_ref_height" + def getKind(self): return PythonFeaturesPlugin_Box.ID() def initAttributes(self): # C++ static methods (in example "type()" of the 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()) + self.data().addAttribute(self.WIDTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) + self.data().addAttribute(self.LENGTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) + self.data().addAttribute(self.HEIGHT_ID(), ModelAPI.ModelAPI_AttributeDouble_type()) + self.data().addAttribute(self.WIDTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type()) + self.data().addAttribute(self.LENGTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type()) + self.data().addAttribute(self.HEIGHT_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type()) + aSession = ModelAPI.ModelAPI_Session.get() + aSession.validators().registerNotObligatory(self.getKind(), self.WIDTH_REF_ID()); + aSession.validators().registerNotObligatory(self.getKind(), self.LENGTH_REF_ID()); + aSession.validators().registerNotObligatory(self.getKind(), self.HEIGHT_REF_ID()); def execute(self): - 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)) - aResultBody = self.document().createBody(self.data()) - aResult = examples.makeBox(aLength, aWidth, aHeight) - #aShape = modelAPI_ResultConstruction(aResult).shape() - # aResultBody.store(aShape) - self.setResult(aResultBody) + aWidth = self.real(self.WIDTH_ID()).value() + aLength = self.real(self.LENGTH_ID()).value() + aHeight = self.real(self.HEIGHT_ID()).value() + aWidthFeature = ModelAPI.modelAPI_Feature(self.reference(self.WIDTH_REF_ID()).value()) + aLengthFeature = ModelAPI.modelAPI_Feature(self.reference(self.LENGTH_REF_ID()).value()) + aHeightFeature = ModelAPI.modelAPI_Feature(self.reference(self.HEIGHT_REF_ID()).value()) + aResult = None + if not all((aWidthFeature, aLengthFeature, aLengthFeature)): + aResult = extrusion.getBody(self.makeBox(aLength, aWidth, aHeight)) + else: + aWidthFeature.real("ConstraintValue").setValue(aWidth) + aLengthFeature.real("ConstraintValue").setValue(aLength) + aHeightFeature.real("extrusion_size").setValue(aHeight) + aResult = extrusion.getBody(aHeightFeature) + self.setResult(aResult) + + def makeBox(self, aWidth, aLength, aHeight): + aSession = ModelAPI.ModelAPI_Session.get() + aPart = aSession.activeDocument() + # Starting the Sketch + aSketch = sketch.addTo(aPart) + sketch.setXOYPlane(aSketch) + # Creating the lines + l1 = sketch.addLine(10, 10, 10, 50, aSketch) + l2 = sketch.addLine(10, 50, 60, 60, aSketch) + l3 = sketch.addLine(60, 60, 50, 10, aSketch) + l4 = sketch.addLine(50, 10, 10, 10, aSketch) + aSketch.execute() + # Creating the constraints + sketch.makeCoincident(sketch.getEndPoint(l1), sketch.getStartPoint(l2), aSketch) + sketch.makeCoincident(sketch.getEndPoint(l2), sketch.getStartPoint(l3), aSketch) + sketch.makeCoincident(sketch.getEndPoint(l3), sketch.getStartPoint(l4), aSketch) + sketch.makeCoincident(sketch.getEndPoint(l4), sketch.getStartPoint(l1), aSketch) + sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3), aSketch) + sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4), aSketch) + sketch.makePerpendicular(sketch.getGeometry(l1), sketch.getGeometry(l4), aSketch) + # Set to 0X and 0Y lines defined length + aWidthFeature = sketch.makeConstantLength(sketch.getGeometry(l4), aWidth, aSketch) + aLengthFeature = sketch.makeConstantLength(sketch.getGeometry(l1), aLength, aSketch) + # Finalisation of the operation + builder = SketchResult(aSketch) + # Creating a feature Extrusion + aHeightFeature = extrusion.addNew(builder, aHeight, aPart) + # Store features... + self.reference(self.WIDTH_REF_ID()).setValue(aWidthFeature) + self.reference(self.LENGTH_REF_ID()).setValue(aLengthFeature) + self.reference(self.HEIGHT_REF_ID()).setValue(aHeightFeature) + return aHeightFeature + # TEST """ diff --git a/src/PythonFeaturesPlugin/box_widget.xml b/src/PythonFeaturesPlugin/box_widget.xml index f35231580..7c1a2c553 100644 --- a/src/PythonFeaturesPlugin/box_widget.xml +++ b/src/PythonFeaturesPlugin/box_widget.xml @@ -1,11 +1,11 @@ - + - + - + diff --git a/src/PythonFeaturesPlugin/examples.py b/src/PythonFeaturesPlugin/examples.py index 3da0d6f57..9f68e9234 100644 --- a/src/PythonFeaturesPlugin/examples.py +++ b/src/PythonFeaturesPlugin/examples.py @@ -23,23 +23,29 @@ def makeBox(aLength, aWidth, aHeight): base.execute() # Creating the constraints - # NOTE : the following lines are currently not working in BR_PYTHON_PLUGIN - # branch - # sketch.makeCoincident(sketch.getEndPoint(l1), sketch.getStartPoint(l2), base) - # sketch.makeCoincident(sketch.getEndPoint(l2), sketch.getStartPoint(l3), base) - # sketch.makeCoincident(sketch.getEndPoint(l3), sketch.getStartPoint(l4), base) - # sketch.makeCoincident(sketch.getEndPoint(l4), sketch.getStartPoint(l1), base) - # - # sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3)) - # sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4)) - # - # sketch.makePerpendicular(sketch.getGeometry(l1), sketch.getGeometry(l4)) + sketch.makeCoincident(sketch.getEndPoint(l1), + sketch.getStartPoint(l2), base) + sketch.makeCoincident(sketch.getEndPoint(l2), + sketch.getStartPoint(l3), base) + sketch.makeCoincident(sketch.getEndPoint(l3), + sketch.getStartPoint(l4), base) + sketch.makeCoincident(sketch.getEndPoint(l4), + sketch.getStartPoint(l1), base) + + sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3), base) + sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4), base) + + sketch.makePerpendicular(sketch.getGeometry(l1), + sketch.getGeometry(l4), base) + # Set to 0X and 0Y lines defined length + sketch.makeConstantLength(sketch.getGeometry(l1), aLength, base) + sketch.makeConstantLength(sketch.getGeometry(l4), aWidth, base) # Finalisation of the operation builder = SketchResult(base) # Creating a feature Extrusion - box = extrusion.addNew(builder, 50, part) + box = extrusion.addNew(builder, aHeight, part) # return base.lastResult() return extrusion.getBody(box) diff --git a/src/PythonFeaturesPlugin/extrusion.py b/src/PythonFeaturesPlugin/extrusion.py index 2ce035dca..9008479a0 100644 --- a/src/PythonFeaturesPlugin/extrusion.py +++ b/src/PythonFeaturesPlugin/extrusion.py @@ -12,4 +12,4 @@ def addNew(builder, length, part, edges=None, reverse=False): def getBody(extrusion): - return extrusion.firstResult() + return modelAPI_ResultBody(extrusion.firstResult()) diff --git a/src/PythonFeaturesPlugin/sketch.py b/src/PythonFeaturesPlugin/sketch.py index f60fbc16e..ca13a707d 100644 --- a/src/PythonFeaturesPlugin/sketch.py +++ b/src/PythonFeaturesPlugin/sketch.py @@ -110,21 +110,25 @@ def makeCoincident(p1, p2, sketch): constraint = sketch.addFeature("SketchConstraintCoincidence") constraint.refattr("ConstraintEntityA").setAttr(p1) constraint.refattr("ConstraintEntityB").setAttr(p2) + return constraint def makeParallel(l1, l2, sketch): constraint = sketch.addFeature("SketchConstraintParallel") constraint.refattr("ConstraintEntityA").setObject(l1) constraint.refattr("ConstraintEntityB").setObject(l2) + return constraint def makePerpendicular(l1, l2, sketch): constraint = sketch.addFeature("SketchConstraintPerpendicular") constraint.refattr("ConstraintEntityA").setObject(l1) constraint.refattr("ConstraintEntityB").setObject(l2) + return constraint def makeConstantLength(line, length, sketch): constraint = sketch.addFeature("SketchConstraintLength") constraint.refattr("ConstraintEntityA").setObject(line) constraint.real("ConstraintValue").setValue(length) + return constraint -- 2.39.2