From 622e6d07002bdb703c2d7e51a8b65dfcd9fbd377 Mon Sep 17 00:00:00 2001 From: azv Date: Mon, 8 Aug 2016 12:17:11 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) 1. Dump Sketch features. 2. Store getter for each attribute in ModelHighAPI_Interface --- src/ModelHighAPI/ModelHighAPI.i | 28 ++++++++++++++++++ src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 16 ++++++++++- src/ModelHighAPI/ModelHighAPI_Dumper.h | 8 ++++++ src/ModelHighAPI/ModelHighAPI_Interface.cpp | 5 ++++ src/ModelHighAPI/ModelHighAPI_Interface.h | 7 +++++ src/ModelHighAPI/ModelHighAPI_Macro.h | 7 ++++- src/PythonAPI/model/dump/DumpAssistant.py | 10 +++++-- src/SketchAPI/SketchAPI_Arc.cpp | 32 +++++++++++++++++++++ src/SketchAPI/SketchAPI_Arc.h | 4 +++ src/SketchAPI/SketchAPI_Circle.cpp | 27 +++++++++++++++++ src/SketchAPI/SketchAPI_Circle.h | 4 +++ src/SketchAPI/SketchAPI_Line.cpp | 18 ++++++++++++ src/SketchAPI/SketchAPI_Line.h | 4 +++ src/SketchAPI/SketchAPI_Point.cpp | 5 ++-- src/SketchAPI/SketchAPI_SketchEntity.cpp | 10 +++++++ src/SketchAPI/SketchAPI_SketchEntity.h | 3 ++ 16 files changed, 182 insertions(+), 6 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI.i b/src/ModelHighAPI/ModelHighAPI.i index 4f4312e23..39b0589a8 100644 --- a/src/ModelHighAPI/ModelHighAPI.i +++ b/src/ModelHighAPI/ModelHighAPI.i @@ -118,6 +118,34 @@ } } +%typecheck(SWIG_TYPECHECK_POINTER) ModelHighAPI_RefAttr, const ModelHighAPI_RefAttr & { + std::shared_ptr * temp_attribute; + std::shared_ptr * temp_object; + std::shared_ptr * temp_interface; + int newmem = 0; + if ((SWIG_ConvertPtrAndOwn($input, (void **)&temp_attribute, $descriptor(std::shared_ptr *), SWIG_POINTER_EXCEPTION, &newmem)) == 0) { + if (temp_attribute) { + $1 = 1; + } else { + $1 = 0; + } + } else if ((SWIG_ConvertPtrAndOwn($input, (void **)&temp_object, $descriptor(std::shared_ptr *), SWIG_POINTER_EXCEPTION, &newmem)) == 0) { + if (temp_object) { + $1 = 1; + } else { + $1 = 0; + } + } else if ((SWIG_ConvertPtrAndOwn($input, (void **)&temp_interface, $descriptor(std::shared_ptr *), SWIG_POINTER_EXCEPTION, &newmem)) == 0) { + if (temp_interface) { + $1 = 1; + } else { + $1 = 0; + } + } else { + $1 = 0; + } +} + %typemap(in) const ModelHighAPI_Reference & (ModelHighAPI_Reference temp) { std::shared_ptr * temp_object; std::shared_ptr * temp_interface; diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 5bff806d8..bbf3be1d0 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -309,7 +310,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrBool) { - myDumpBuffer << theAttrBool->value(); + myDumpBuffer << (theAttrBool->value() ? "True" : "False"); return *this; } @@ -350,6 +351,19 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity) return *this; } +ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( + const std::shared_ptr& theRefAttr) +{ + if (theRefAttr->isObject()) + myDumpBuffer << name(theRefAttr->object()); + else { + AttributePtr anAttr = theRefAttr->attr(); + FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner()); + myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()"; + } + return *this; +} + ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrSelect) { diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index 675228960..c653e5067 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -25,6 +25,7 @@ class GeomDataAPI_Point2D; class ModelAPI_AttributeBoolean; class ModelAPI_AttributeDouble; class ModelAPI_AttributeInteger; +class ModelAPI_AttributeRefAttr; class ModelAPI_AttributeSelection; class ModelAPI_AttributeString; class ModelAPI_CompositeFeature; @@ -80,6 +81,10 @@ public: /// Dump given feature virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0; + /// Return name of getter for corresponding attribute + virtual std::string attributeGetter(const FeaturePtr& theFeature, + const std::string& theAttrName) const = 0; + /// Save all dumps into specified file MODELHIGHAPI_EXPORT bool exportTo(const std::string& theFileName); @@ -143,6 +148,9 @@ public: MODELHIGHAPI_EXPORT ModelHighAPI_Dumper& operator<<(const EntityPtr& theEntity); + /// Dump AttributeRefAttr + MODELHIGHAPI_EXPORT + ModelHighAPI_Dumper& operator<<(const std::shared_ptr& theRefAttr); /// Dump AttributeSelection MODELHIGHAPI_EXPORT ModelHighAPI_Dumper& operator<<(const std::shared_ptr& theAttrSelect); diff --git a/src/ModelHighAPI/ModelHighAPI_Interface.cpp b/src/ModelHighAPI/ModelHighAPI_Interface.cpp index 228fdcc91..a9af48585 100644 --- a/src/ModelHighAPI/ModelHighAPI_Interface.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Interface.cpp @@ -64,3 +64,8 @@ void ModelHighAPI_Interface::throwException(const std::string & theDescription) { Events_InfoMessage("ModelHighAPI_Interface", theDescription).send(); } + +const std::string& ModelHighAPI_Interface::attributeGetter(const std::string& theAttrName) +{ + return myAttrGetter[theAttrName]; +} diff --git a/src/ModelHighAPI/ModelHighAPI_Interface.h b/src/ModelHighAPI/ModelHighAPI_Interface.h index 143be4e53..009b7e89b 100644 --- a/src/ModelHighAPI/ModelHighAPI_Interface.h +++ b/src/ModelHighAPI/ModelHighAPI_Interface.h @@ -11,6 +11,7 @@ #include "ModelHighAPI.h" #include +#include #include #include #include @@ -59,12 +60,18 @@ public: MODELHIGHAPI_EXPORT void throwException(const std::string & theDescription); + /// Return name of getter for specified attribute + MODELHIGHAPI_EXPORT + const std::string& attributeGetter(const std::string& theAttrName); + /// Dump wrapped feature MODELHIGHAPI_EXPORT virtual void dump(ModelHighAPI_Dumper& theDumper) const {} protected: std::shared_ptr myFeature; + + std::map myAttrGetter; ///< names of attributes and their getters }; //! Pointer on Interface object diff --git a/src/ModelHighAPI/ModelHighAPI_Macro.h b/src/ModelHighAPI/ModelHighAPI_Macro.h index aed82f368..f6bfc6cee 100644 --- a/src/ModelHighAPI/ModelHighAPI_Macro.h +++ b/src/ModelHighAPI/ModelHighAPI_Macro.h @@ -29,6 +29,10 @@ //-------------------------------------------------------------------------------------- #define VAR_NAME(NAME) my##NAME +//-------------------------------------------------------------------------------------- +#define TO_STRING_(NAME) #NAME +#define TO_STRING(NAME) TO_STRING_(NAME) + //-------------------------------------------------------------------------------------- // Used in INTERFACE_N for create variable and getter #define DEFINE_ATTRIBUTE(NAME, TYPE, COMMENT) \ @@ -43,7 +47,8 @@ #define SET_ATTRIBUTE(NAME, TYPE, ATT_NAME) \ VAR_NAME(NAME) = std::dynamic_pointer_cast(feature()->attribute(ATT_NAME)); \ if (!VAR_NAME(NAME)) \ - return false; + return false; \ + myAttrGetter[ATT_NAME] = TO_STRING(NAME); //-------------------------------------------------------------------------------------- #define INTERFACE_COMMON(KIND) \ diff --git a/src/PythonAPI/model/dump/DumpAssistant.py b/src/PythonAPI/model/dump/DumpAssistant.py index 457c7116d..7585d9239 100644 --- a/src/PythonAPI/model/dump/DumpAssistant.py +++ b/src/PythonAPI/model/dump/DumpAssistant.py @@ -24,7 +24,7 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper): ModelHighAPI.ModelHighAPI_Dumper.__init__(self) ModelHighAPI.ModelHighAPI_Dumper.setInstance(self) self.collectFeatures() - + ## Collect feature wrappers, which allow dumping (have method dump) def collectFeatures(self): self.myFeatures = {} @@ -32,7 +32,7 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper): for aName, anObj in inspect.getmembers(sys.modules[aModule], inspect.isclass): if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump: self.myFeatures[anObj.ID()] = anObj - + ## Create wrapper for a given feature and dump it def dumpFeature(self, theFeature, theForce): aFeatureKind = theFeature.getKind() @@ -44,6 +44,12 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper): else: self.name(theFeature) + ## Return getter for specified attribute + def attributeGetter(self, theFeature, theAttrName): + aFeatureKind = theFeature.getKind() + if aFeatureKind in self.myFeatures: + return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName) + return std_string() # Instance of dumper dumper = DumpAssistant \ No newline at end of file diff --git a/src/SketchAPI/SketchAPI_Arc.cpp b/src/SketchAPI/SketchAPI_Arc.cpp index 438c28d9f..696bd9ab5 100644 --- a/src/SketchAPI/SketchAPI_Arc.cpp +++ b/src/SketchAPI/SketchAPI_Arc.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -232,3 +233,34 @@ void SketchAPI_Arc::setAngle(double theAngle) execute(); } + +//================================================================================================== +void SketchAPI_Arc::dump(ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); + if (anExternal->value()) { + // arc is external + theDumper << aBase << " = " << aSketchName << ".addArc(" << anExternal << ")" << std::endl; + } else { + AttributeStringPtr aType = arcType(); + if (aType->value() == SketchPlugin_Arc::ARC_TYPE_CENTER_START_END()) { + // arc given by center and start, end points + theDumper << aBase << " = " << aSketchName << ".addArc(" << center() << ", " + << startPoint() << ", " << endPoint() << ", " << inversed() << ")" << std::endl; + } else if (aType->value() == SketchPlugin_Arc::ARC_TYPE_THREE_POINTS()) { + // arc given by three points + theDumper << aBase << " = " << aSketchName << ".addArc(" << startPoint() << ", " + << endPoint() << ", " << passedPoint() << ")" << std::endl; + } else { + // tangent arc + AttributeRefAttrPtr aTangentPoint = tangentPoint(); + theDumper << aBase << " = " << aSketchName << ".addArc(" + << aTangentPoint << ", " << endPoint() << ", " << inversed() << ")" << std::endl; + } + } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); +} diff --git a/src/SketchAPI/SketchAPI_Arc.h b/src/SketchAPI/SketchAPI_Arc.h index 4cff20044..5fc0e5b0d 100644 --- a/src/SketchAPI/SketchAPI_Arc.h +++ b/src/SketchAPI/SketchAPI_Arc.h @@ -150,6 +150,10 @@ public: /// Set angle. SKETCHAPI_EXPORT void setAngle(double theAngle); + + /// Dump wrapped feature + SKETCHAPI_EXPORT + virtual void dump(ModelHighAPI_Dumper& theDumper) const; }; /// Pointer on Arc object. diff --git a/src/SketchAPI/SketchAPI_Circle.cpp b/src/SketchAPI/SketchAPI_Circle.cpp index d8a07f13b..7ffcfd0cb 100644 --- a/src/SketchAPI/SketchAPI_Circle.cpp +++ b/src/SketchAPI/SketchAPI_Circle.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -235,3 +236,29 @@ void SketchAPI_Circle::setThirdPoint(const std::shared_ptr& thePo execute(); } + +//================================================================================================== +void SketchAPI_Circle::dump(ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); + if (anExternal->value()) { + // circle is external + theDumper << aBase << " = " << aSketchName << ".addCircle(" << anExternal << ")" << std::endl; + } else { + AttributeStringPtr aType = circleType(); + if (aType->value() == SketchPlugin_Circle::CIRCLE_TYPE_CENTER_AND_RADIUS()) { + // circle given by center and radius + theDumper << aBase << " = " << aSketchName << ".addCircle(" + << center() << ", " << radius() << ")" << std::endl; + } else { + // circle given by three points + theDumper << aBase << " = " << aSketchName << ".addCircle(" << firstPoint() << ", " + << secondPoint() << ", " << thirdPoint() << ")" << std::endl; + } + } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); +} diff --git a/src/SketchAPI/SketchAPI_Circle.h b/src/SketchAPI/SketchAPI_Circle.h index b4332c675..76c7f1fac 100644 --- a/src/SketchAPI/SketchAPI_Circle.h +++ b/src/SketchAPI/SketchAPI_Circle.h @@ -137,6 +137,10 @@ public: /// Set third point. SKETCHAPI_EXPORT void setThirdPoint(const std::shared_ptr& thePoint); + + /// Dump wrapped feature + SKETCHAPI_EXPORT + virtual void dump(ModelHighAPI_Dumper& theDumper) const; }; /// Pointer on Circle object. diff --git a/src/SketchAPI/SketchAPI_Line.cpp b/src/SketchAPI/SketchAPI_Line.cpp index d6be6744d..2b868adb6 100644 --- a/src/SketchAPI/SketchAPI_Line.cpp +++ b/src/SketchAPI/SketchAPI_Line.cpp @@ -9,6 +9,7 @@ //-------------------------------------------------------------------------------------- #include //-------------------------------------------------------------------------------------- +#include #include #include //-------------------------------------------------------------------------------------- @@ -127,3 +128,20 @@ void SketchAPI_Line::setEndPoint(const std::shared_ptr & thePoint //-------------------------------------------------------------------------------------- +void SketchAPI_Line::dump(ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); + if (anExternal->value()) { + // line is external + theDumper << aBase << " = " << aSketchName << ".addLine(" << anExternal << ")" << std::endl; + } else { + // segment given by its points + theDumper << aBase << " = " << aSketchName << ".addLine(" + << startPoint() << ", " << endPoint() << ")" << std::endl; + } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); +} diff --git a/src/SketchAPI/SketchAPI_Line.h b/src/SketchAPI/SketchAPI_Line.h index c2f3d98f5..23f904240 100644 --- a/src/SketchAPI/SketchAPI_Line.h +++ b/src/SketchAPI/SketchAPI_Line.h @@ -87,6 +87,10 @@ public: /// Set end point SKETCHAPI_EXPORT void setEndPoint(const std::shared_ptr & thePoint); + + /// Dump wrapped feature + SKETCHAPI_EXPORT + virtual void dump(ModelHighAPI_Dumper& theDumper) const; }; //! Pointer on Line object diff --git a/src/SketchAPI/SketchAPI_Point.cpp b/src/SketchAPI/SketchAPI_Point.cpp index 08df487e4..10719adb5 100644 --- a/src/SketchAPI/SketchAPI_Point.cpp +++ b/src/SketchAPI/SketchAPI_Point.cpp @@ -109,8 +109,9 @@ void SketchAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const theDumper << aBase << " = " << aSketchName << ".addPoint(" << anExternal << ")" << std::endl; } else { // point given by coordinates - std::shared_ptr aPoint = std::dynamic_pointer_cast( - aBase->attribute(SketchPlugin_Point::COORD_ID())); + std::shared_ptr aPoint = coordinates(); theDumper << aBase << " = " << aSketchName << ".addPoint(" << aPoint << ")" << std::endl; } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); } diff --git a/src/SketchAPI/SketchAPI_SketchEntity.cpp b/src/SketchAPI/SketchAPI_SketchEntity.cpp index a017576ab..13b4afeaa 100644 --- a/src/SketchAPI/SketchAPI_SketchEntity.cpp +++ b/src/SketchAPI/SketchAPI_SketchEntity.cpp @@ -7,6 +7,7 @@ //-------------------------------------------------------------------------------------- #include "SketchAPI_SketchEntity.h" //-------------------------------------------------------------------------------------- +#include #include //-------------------------------------------------------------------------------------- SketchAPI_SketchEntity::SketchAPI_SketchEntity( @@ -43,3 +44,12 @@ void SketchAPI_SketchEntity::setAuxiliary(bool theAuxiliary) } //-------------------------------------------------------------------------------------- +void SketchAPI_SketchEntity::dump(ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + AttributeBooleanPtr anAux = aBase->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID()); + if (anAux->value()) { + const std::string& aName = theDumper.name(aBase); + theDumper << aName << ".setAuxiliary(" << anAux << ")" < myAuxiliary; -- 2.30.2