Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
authorazv <azv@opencascade.com>
Tue, 9 Aug 2016 08:29:41 +0000 (11:29 +0300)
committerazv <azv@opencascade.com>
Tue, 9 Aug 2016 08:30:10 +0000 (11:30 +0300)
Dump sketch constraints

src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_RefAttr.cpp
src/ModelHighAPI/ModelHighAPI_RefAttr.h
src/PythonAPI/model/dump/DumpAssistant.py
src/SketchAPI/CMakeLists.txt
src/SketchAPI/SketchAPI.i
src/SketchAPI/SketchAPI_Constraint.cpp [new file with mode: 0644]
src/SketchAPI/SketchAPI_Constraint.h [new file with mode: 0644]
src/SketchAPI/SketchAPI_swig.h

index 4b3138389b9dfd266954f346a476530b257a9ec1..e1ed55d36942c39d43366d8edbed5f73861d87cc 100644 (file)
@@ -118,11 +118,16 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
 {
   bool isOk = true;
+  CompositeFeaturePtr aLastComposite;
   // dump all features
   std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
   std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
   for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
-    dumpFeature(*aFeatIt);
+    // dump feature if and only if it is not a sub-feature of last composite feature
+    // (all subs of composite are dumped in special method)
+    if (!aLastComposite || !aLastComposite->isSub(*aFeatIt))
+      dumpFeature(*aFeatIt);
+
     // iteratively process composite features
     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
     if (!aCompFeat)
@@ -139,8 +144,10 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
       myNames[aSubDoc] = myNames[*aFeatIt];
 
       isOk = process(aSubDoc) && isOk;
-    } else 
+    } else {
       isOk = process(aCompFeat) && isOk;
+      aLastComposite = aCompFeat;
+    }
   }
   return isOk;
 }
@@ -354,9 +361,10 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity)
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
 {
-  if (theRefAttr->isObject())
-    myDumpBuffer << name(theRefAttr->object());
-  else {
+  if (theRefAttr->isObject()) {
+    FeaturePtr aFeature = ModelAPI_Feature::feature(theRefAttr->object());
+    myDumpBuffer << name(aFeature);
+  } else {
     AttributePtr anAttr = theRefAttr->attr();
     FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
     myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()";
index 999cc718effb225a7dbef99b34f4539dc94cdae4..6bb6aeb3b2597aedc5ccd7e8e52c48dd9582e94e 100644 (file)
@@ -62,3 +62,9 @@ void ModelHighAPI_RefAttr::appendToList(
     case VT_OBJECT: theAttribute->append(myObject); return;
   }
 }
+
+//--------------------------------------------------------------------------------------
+bool ModelHighAPI_RefAttr::isEmpty() const
+{
+  return !(myAttribute && myObject);
+}
index bc54e20789d32777a9d11b4a387ddd3a8235689b..816e5f59a6fa7e0cb5fbd0702ff67fc7b5338255 100644 (file)
@@ -50,6 +50,10 @@ public:
   MODELHIGHAPI_EXPORT
   virtual void appendToList(const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute) const;
 
+  /// Check the object is empty
+  MODELHIGHAPI_EXPORT
+  bool isEmpty() const;
+
 private:
   enum VariantType { VT_ATTRIBUTE, VT_OBJECT } myVariantType;
   std::shared_ptr<ModelAPI_Attribute> myAttribute;
index 7585d92396b2a0c8e49990f420c514066bd0cb1f..7aa341b985b3ca003b44d6bae68a39baed9ca824 100644 (file)
@@ -4,6 +4,7 @@
 import ModelHighAPI
 
 import ModelAPI
+import SketchAPI
 
 import sys
 import inspect
@@ -43,6 +44,10 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
                 self.myFeatures[aFeatureKind](theFeature).dump(self)
             else:
                 self.name(theFeature)
+        else:
+            # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
+            # In case of theFeature is not a constraint, it will not be dumped.
+            self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self)
 
     ## Return getter for specified attribute
     def attributeGetter(self, theFeature, theAttrName):
index 12918c9917f84533de9dfe035e4986663eefdd1f..3bd9afe905d54a3e75e0942fc3fcdaafec89ddc2 100644 (file)
@@ -6,6 +6,7 @@ SET(PROJECT_HEADERS
   SketchAPI.h
   SketchAPI_Arc.h
   SketchAPI_Circle.h
+  SketchAPI_Constraint.h
   SketchAPI_IntersectionPoint.h
   SketchAPI_Line.h
   SketchAPI_Mirror.h
@@ -21,6 +22,7 @@ SET(PROJECT_HEADERS
 SET(PROJECT_SOURCES
   SketchAPI_Arc.cpp
   SketchAPI_Circle.cpp
+  SketchAPI_Constraint.cpp
   SketchAPI_IntersectionPoint.cpp
   SketchAPI_Line.cpp
   SketchAPI_Mirror.cpp
index 5c4c6353479e9e806237e65fecd5dd2e021a582e..397038e0d0639ec07d8e1efd5869ba0a295fb7ad 100644 (file)
@@ -22,6 +22,7 @@
 // shared pointers
 %shared_ptr(SketchAPI_Arc)
 %shared_ptr(SketchAPI_Circle)
+%shared_ptr(SketchAPI_Constraint)
 %shared_ptr(SketchAPI_IntersectionPoint)
 %shared_ptr(SketchAPI_Line)
 %shared_ptr(SketchAPI_Mirror)
 %include "SketchAPI_Rectangle.h"
 %include "SketchAPI_Rotation.h"
 %include "SketchAPI_Sketch.h"
+%include "SketchAPI_Constraint.h"
diff --git a/src/SketchAPI/SketchAPI_Constraint.cpp b/src/SketchAPI/SketchAPI_Constraint.cpp
new file mode 100644 (file)
index 0000000..5bee50d
--- /dev/null
@@ -0,0 +1,140 @@
+// Name   : SketchAPI_Constraint.cpp
+// Purpose: 
+//
+// History:
+// 08/08/16 - Artem ZHIDKOV - Creation of the file
+
+#include "SketchAPI_Constraint.h"
+
+#include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Tools.h>
+
+#include <SketchPlugin_Constraint.h>
+#include <SketchPlugin_ConstraintAngle.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_ConstraintCollinear.h>
+#include <SketchPlugin_ConstraintDistance.h>
+#include <SketchPlugin_ConstraintEqual.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
+#include <SketchPlugin_ConstraintLength.h>
+#include <SketchPlugin_ConstraintMiddle.h>
+#include <SketchPlugin_ConstraintParallel.h>
+#include <SketchPlugin_ConstraintPerpendicular.h>
+#include <SketchPlugin_ConstraintRadius.h>
+#include <SketchPlugin_ConstraintRigid.h>
+#include <SketchPlugin_ConstraintTangent.h>
+#include <SketchPlugin_ConstraintVertical.h>
+
+SketchAPI_Constraint::SketchAPI_Constraint(
+    const std::shared_ptr<ModelAPI_Feature> & theFeature)
+: ModelHighAPI_Interface(theFeature)
+{
+  ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
+  if (aConstraint)
+    initialize();
+}
+
+SketchAPI_Constraint::~SketchAPI_Constraint()
+{
+}
+
+bool SketchAPI_Constraint::initialize()
+{
+  if (!feature()) {
+    throwException("Constraint exception: The feature is NULL.");
+    return false;
+  }
+  return true;
+}
+
+static const std::string& constraintTypeToSetter(const std::string& theType)
+{
+  if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
+    static const std::string COINCIDENCE_SETTER("setCoincident");
+    return COINCIDENCE_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintAngle::ID()) {
+    static const std::string ANGLE_SETTER("setAngle");
+    return ANGLE_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintCollinear::ID()) {
+    static const std::string COLLINEAR_SETTER("setCollinear");
+    return COLLINEAR_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintDistance::ID()) {
+    static const std::string DISTANCE_SETTER("setDistance");
+    return DISTANCE_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintEqual::ID()) {
+    static const std::string EQUAL_SETTER("setEqual");
+    return EQUAL_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
+    static const std::string HORIZONTAL_SETTER("setHorizontal");
+    return HORIZONTAL_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintLength::ID()) {
+    static const std::string LENGTH_SETTER("setLength");
+    return LENGTH_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintMiddle::ID()) {
+    static const std::string MIDDLE_SETTER("setMiddlePoint");
+    return MIDDLE_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintParallel::ID()) {
+    static const std::string PARALLEL_SETTER("setParallel");
+    return PARALLEL_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
+    static const std::string PERPENDICULAR_SETTER("setPerpendicular");
+    return PERPENDICULAR_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintRadius::ID()) {
+    static const std::string RADIUS_SETTER("setRadius");
+    return RADIUS_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintRigid::ID()) {
+    static const std::string FIXED_SETTER("setFixed");
+    return FIXED_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintTangent::ID()) {
+    static const std::string TANGENT_SETTER("setTangent");
+    return TANGENT_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintVertical::ID()) {
+    static const std::string VERTICAL_SETTER("setVertical");
+    return VERTICAL_SETTER;
+  }
+
+  static const std::string DUMMY;
+  return DUMMY;
+}
+
+void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
+{
+  ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(feature());
+  if (!aConstraint)
+    return; // dump constraints only
+
+  const std::string& aSetter = constraintTypeToSetter(aConstraint->getKind());
+  if (aSetter.empty())
+    return; // incorrect constraint type
+
+  const std::string& aSketchName = theDumper.parentName(aConstraint);
+  theDumper << aConstraint << " = " << aSketchName << "." << aSetter << "(";
+
+  bool isFirstAttr = true;
+  for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+    AttributeRefAttrPtr aRefAttr = aConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+    if (aRefAttr && aRefAttr->isInitialized()) {
+      theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
+      isFirstAttr = false;
+    }
+  }
+
+  AttributeDoublePtr aValueAttr = aConstraint->real(SketchPlugin_Constraint::VALUE());
+  if (aValueAttr && aValueAttr->isInitialized())
+    theDumper << ", " << aValueAttr;
+
+  theDumper << ")" << std::endl;
+}
diff --git a/src/SketchAPI/SketchAPI_Constraint.h b/src/SketchAPI/SketchAPI_Constraint.h
new file mode 100644 (file)
index 0000000..2f87652
--- /dev/null
@@ -0,0 +1,48 @@
+// Name   : SketchAPI_Constraint.h
+// Purpose: 
+//
+// History:
+// 08/08/16 - Artem ZHIDKOV - Creation of the file
+
+#ifndef SRC_SKETCHAPI_SKETCHAPI_CONSTRAINT_H_
+#define SRC_SKETCHAPI_SKETCHAPI_CONSTRAINT_H_
+
+#include "SketchAPI.h"
+
+#include <ModelHighAPI_Interface.h>
+#include <ModelHighAPI_Macro.h>
+#include <ModelHighAPI_RefAttr.h>
+
+#include <SketchPlugin_Constraint.h>
+
+/**\class SketchAPI_Constraint
+ * \ingroup CPPHighAPI
+ * \brief Interface for Constraint feature
+ */
+class SketchAPI_Constraint : public ModelHighAPI_Interface
+{
+public:
+  /// Constructor without values
+  SKETCHAPI_EXPORT
+  explicit SketchAPI_Constraint(const std::shared_ptr<ModelAPI_Feature> & theFeature);
+
+  /// Destructor
+  SKETCHAPI_EXPORT
+  virtual ~SketchAPI_Constraint();
+
+  static std::string ID()
+  {
+    static const std::string DUMMY;
+    return DUMMY;
+  }
+  virtual std::string getID() { return ID(); }
+
+  SKETCHAPI_EXPORT
+  bool initialize();
+
+  /// Dump wrapped feature
+  SKETCHAPI_EXPORT
+  virtual void dump(ModelHighAPI_Dumper& theDumper) const;
+};
+
+#endif
index 34c00398f46f3324a79ad3393943103a86420efa..c1695defaf3fa42a2b05fe3e70f43dd676fe5ccf 100644 (file)
@@ -12,6 +12,7 @@
   #include "SketchAPI.h"
   #include "SketchAPI_Arc.h"
   #include "SketchAPI_Circle.h"
+  #include "SketchAPI_Constraint.h"
   #include "SketchAPI_IntersectionPoint.h"
   #include "SketchAPI_Line.h"
   #include "SketchAPI_Mirror.h"