]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
authorazv <azv@opencascade.com>
Mon, 8 Aug 2016 09:17:11 +0000 (12:17 +0300)
committerazv <azv@opencascade.com>
Mon, 8 Aug 2016 09:17:11 +0000 (12:17 +0300)
1. Dump Sketch features.
2. Store getter for each attribute in ModelHighAPI_Interface

16 files changed:
src/ModelHighAPI/ModelHighAPI.i
src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/ModelHighAPI/ModelHighAPI_Interface.cpp
src/ModelHighAPI/ModelHighAPI_Interface.h
src/ModelHighAPI/ModelHighAPI_Macro.h
src/PythonAPI/model/dump/DumpAssistant.py
src/SketchAPI/SketchAPI_Arc.cpp
src/SketchAPI/SketchAPI_Arc.h
src/SketchAPI/SketchAPI_Circle.cpp
src/SketchAPI/SketchAPI_Circle.h
src/SketchAPI/SketchAPI_Line.cpp
src/SketchAPI/SketchAPI_Line.h
src/SketchAPI/SketchAPI_Point.cpp
src/SketchAPI/SketchAPI_SketchEntity.cpp
src/SketchAPI/SketchAPI_SketchEntity.h

index 4f4312e23b6ed62d3cea28ce5b5ec28ac09578c1..39b0589a87e0baa7071d524a8acd8c6ef7f5fa5e 100644 (file)
   }
 }
 
+%typecheck(SWIG_TYPECHECK_POINTER) ModelHighAPI_RefAttr, const ModelHighAPI_RefAttr & {
+  std::shared_ptr<ModelAPI_Attribute> * temp_attribute;
+  std::shared_ptr<ModelAPI_Object> * temp_object;
+  std::shared_ptr<ModelHighAPI_Interface> * temp_interface;
+  int newmem = 0;
+  if ((SWIG_ConvertPtrAndOwn($input, (void **)&temp_attribute, $descriptor(std::shared_ptr<ModelAPI_Attribute> *), 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<ModelAPI_Object> *), 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<ModelHighAPI_Interface> *), 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<ModelAPI_Object> * temp_object;
   std::shared_ptr<ModelHighAPI_Interface> * temp_interface;
index 5bff806d8a46cfc4b4a1afb0ba6328f8bc9d48c9..bbf3be1d0b188845e951282ebeeee317539c8107 100644 (file)
@@ -17,6 +17,7 @@
 #include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_CompositeFeature.h>
@@ -309,7 +310,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
     const std::shared_ptr<ModelAPI_AttributeBoolean>& 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<ModelAPI_AttributeRefAttr>& 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<ModelAPI_AttributeSelection>& theAttrSelect)
 {
index 6752289603b775d893cca6f04514d6f4b353383d..c653e50679cc51841ea97f463836550da4c254ef 100644 (file)
@@ -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<ModelAPI_AttributeRefAttr>& theRefAttr);
   /// Dump AttributeSelection
   MODELHIGHAPI_EXPORT
   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
index 228fdcc913bed9a8ee4b03b5eec242a4d49fe5b9..a9af48585c157315d9e037715c63a41162e4f4ba 100644 (file)
@@ -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];
+}
index 143be4e53d12069a44b0bb98fedc6a9876f9ac9b..009b7e89b08333ebb053d40f650f6c9cc6413f86 100644 (file)
@@ -11,6 +11,7 @@
 #include "ModelHighAPI.h"
 
 #include <list>
+#include <map>
 #include <memory>
 #include <string>
 #include <iostream>
@@ -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<ModelAPI_Feature> myFeature;
+
+  std::map<std::string, std::string> myAttrGetter; ///< names of attributes and their getters
 };
 
 //! Pointer on Interface object
index aed82f368a6ec258be3e296e50bbfc21ea65200e..f6bfc6cee8272650c71c1930d19c73dd69dcb97a 100644 (file)
 //--------------------------------------------------------------------------------------
 #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<TYPE>(feature()->attribute(ATT_NAME)); \
   if (!VAR_NAME(NAME)) \
-    return false;
+    return false; \
+  myAttrGetter[ATT_NAME] = TO_STRING(NAME);
 
 //--------------------------------------------------------------------------------------
 #define INTERFACE_COMMON(KIND) \
index 457c7116deca9855d8443df8ca42dccf8fc869d1..7585d92396b2a0c8e49990f420c514066bd0cb1f 100644 (file)
@@ -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
index 438c28d9ff094c2e7208ce93e499adfd444228ce..696bd9ab51571257375a0d9d4ef1ac1dbf47ec46 100644 (file)
@@ -9,6 +9,7 @@
 #include <GeomAPI_Pnt2d.h>
 
 #include <ModelHighAPI_Double.h>
+#include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Selection.h>
 #include <ModelHighAPI_Tools.h>
 
@@ -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);
+}
index 4cff2004402bbd5ce9f7a7c8e59b296cc59f200d..5fc0e5b0d7a9d066f0438e6362b0c6f89c1a86dc 100644 (file)
@@ -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.
index d8a07f13b787bcf4e9948b1b901221b2e00558ab..7ffcfd0cb80ecc4c5d8dfc29a7e88ae526d03184 100644 (file)
@@ -9,6 +9,7 @@
 #include <GeomAPI_Pnt2d.h>
 
 #include <ModelHighAPI_Double.h>
+#include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Selection.h>
 #include <ModelHighAPI_Tools.h>
 
@@ -235,3 +236,29 @@ void SketchAPI_Circle::setThirdPoint(const std::shared_ptr<GeomAPI_Pnt2d>& 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);
+}
index b4332c67553898e49db83fed180a4f378381739e..76c7f1fac50101c274614bbdf36bf23f2b0be253 100644 (file)
@@ -137,6 +137,10 @@ public:
   /// Set third point.
   SKETCHAPI_EXPORT
   void setThirdPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
+
+  /// Dump wrapped feature
+  SKETCHAPI_EXPORT
+  virtual void dump(ModelHighAPI_Dumper& theDumper) const;
 };
 
 /// Pointer on Circle object.
index d6be6744de7bd96ae6d6bbc202520c2f5949a114..2b868adb6f28e053f103cfd5afdf715f63c2f346 100644 (file)
@@ -9,6 +9,7 @@
 //--------------------------------------------------------------------------------------
 #include <GeomAPI_Pnt2d.h>
 //--------------------------------------------------------------------------------------
+#include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Selection.h>
 #include <ModelHighAPI_Tools.h>
 //--------------------------------------------------------------------------------------
@@ -127,3 +128,20 @@ void SketchAPI_Line::setEndPoint(const std::shared_ptr<GeomAPI_Pnt2d> & 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);
+}
index c2f3d98f5dc866d4cae2c8b0f70cd734e3986555..23f9042400ca9378001c450ac8b85d4cead7a331 100644 (file)
@@ -87,6 +87,10 @@ public:
   /// Set end point
   SKETCHAPI_EXPORT
   void setEndPoint(const std::shared_ptr<GeomAPI_Pnt2d> & thePoint);
+
+  /// Dump wrapped feature
+  SKETCHAPI_EXPORT
+  virtual void dump(ModelHighAPI_Dumper& theDumper) const;
 };
 
 //! Pointer on Line object
index 08df487e4af1bf2f4d13ed80a322a78d18d6ee2f..10719adb55b7ac6071f3a05c3eb3df6240cd51af 100644 (file)
@@ -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<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aBase->attribute(SketchPlugin_Point::COORD_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aPoint = coordinates();
     theDumper << aBase << " = " << aSketchName << ".addPoint(" << aPoint << ")" << std::endl;
   }
+  // dump "auxiliary" flag if necessary
+  SketchAPI_SketchEntity::dump(theDumper);
 }
index a017576abdf6308d998230b3acb665d45e90ef27..13b4afeaabea606efb9ecf4c2ae283222cb13a59 100644 (file)
@@ -7,6 +7,7 @@
 //--------------------------------------------------------------------------------------
 #include "SketchAPI_SketchEntity.h"
 //--------------------------------------------------------------------------------------
+#include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Tools.h>
 //--------------------------------------------------------------------------------------
 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 << ")" <<std::endl;
+  }
+}
index 5ac166aa381e3fab27e9dc1b694d4144be37a4dd..583eaafa084ad138237a4832c9343fbf9d4e3d5d 100644 (file)
@@ -37,6 +37,9 @@ public:
   SKETCHAPI_EXPORT
   void setAuxiliary(bool theAuxiliary);
 
+  /// Dump wrapped feature
+  virtual void dump(ModelHighAPI_Dumper& theDumper) const;
+
 protected:
   std::shared_ptr<ModelAPI_AttributeBoolean> myAuxiliary;