From 287d69a9faf9368f8fc352d50ab408892222c69e Mon Sep 17 00:00:00 2001 From: azv Date: Tue, 9 Aug 2016 11:29:41 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) Dump sketch constraints --- src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 18 ++- src/ModelHighAPI/ModelHighAPI_RefAttr.cpp | 6 + src/ModelHighAPI/ModelHighAPI_RefAttr.h | 4 + src/PythonAPI/model/dump/DumpAssistant.py | 5 + src/SketchAPI/CMakeLists.txt | 2 + src/SketchAPI/SketchAPI.i | 2 + src/SketchAPI/SketchAPI_Constraint.cpp | 140 ++++++++++++++++++++++ src/SketchAPI/SketchAPI_Constraint.h | 48 ++++++++ src/SketchAPI/SketchAPI_swig.h | 1 + 9 files changed, 221 insertions(+), 5 deletions(-) create mode 100644 src/SketchAPI/SketchAPI_Constraint.cpp create mode 100644 src/SketchAPI/SketchAPI_Constraint.h diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 4b3138389..e1ed55d36 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -118,11 +118,16 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& theD bool ModelHighAPI_Dumper::process(const std::shared_ptr& theDoc) { bool isOk = true; + CompositeFeaturePtr aLastComposite; // dump all features std::list aFeatures = theDoc->allFeatures(); std::list::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(*aFeatIt); if (!aCompFeat) @@ -139,8 +144,10 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& 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& 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()) << "()"; diff --git a/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp b/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp index 999cc718e..6bb6aeb3b 100644 --- a/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp +++ b/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp @@ -62,3 +62,9 @@ void ModelHighAPI_RefAttr::appendToList( case VT_OBJECT: theAttribute->append(myObject); return; } } + +//-------------------------------------------------------------------------------------- +bool ModelHighAPI_RefAttr::isEmpty() const +{ + return !(myAttribute && myObject); +} diff --git a/src/ModelHighAPI/ModelHighAPI_RefAttr.h b/src/ModelHighAPI/ModelHighAPI_RefAttr.h index bc54e2078..816e5f59a 100644 --- a/src/ModelHighAPI/ModelHighAPI_RefAttr.h +++ b/src/ModelHighAPI/ModelHighAPI_RefAttr.h @@ -50,6 +50,10 @@ public: MODELHIGHAPI_EXPORT virtual void appendToList(const std::shared_ptr & theAttribute) const; + /// Check the object is empty + MODELHIGHAPI_EXPORT + bool isEmpty() const; + private: enum VariantType { VT_ATTRIBUTE, VT_OBJECT } myVariantType; std::shared_ptr myAttribute; diff --git a/src/PythonAPI/model/dump/DumpAssistant.py b/src/PythonAPI/model/dump/DumpAssistant.py index 7585d9239..7aa341b98 100644 --- a/src/PythonAPI/model/dump/DumpAssistant.py +++ b/src/PythonAPI/model/dump/DumpAssistant.py @@ -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): diff --git a/src/SketchAPI/CMakeLists.txt b/src/SketchAPI/CMakeLists.txt index 12918c991..3bd9afe90 100644 --- a/src/SketchAPI/CMakeLists.txt +++ b/src/SketchAPI/CMakeLists.txt @@ -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 diff --git a/src/SketchAPI/SketchAPI.i b/src/SketchAPI/SketchAPI.i index 5c4c63534..397038e0d 100644 --- a/src/SketchAPI/SketchAPI.i +++ b/src/SketchAPI/SketchAPI.i @@ -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) @@ -113,3 +114,4 @@ %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 index 000000000..5bee50db1 --- /dev/null +++ b/src/SketchAPI/SketchAPI_Constraint.cpp @@ -0,0 +1,140 @@ +// Name : SketchAPI_Constraint.cpp +// Purpose: +// +// History: +// 08/08/16 - Artem ZHIDKOV - Creation of the file + +#include "SketchAPI_Constraint.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SketchAPI_Constraint::SketchAPI_Constraint( + const std::shared_ptr & theFeature) +: ModelHighAPI_Interface(theFeature) +{ + ConstraintPtr aConstraint = std::dynamic_pointer_cast(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(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 index 000000000..2f8765251 --- /dev/null +++ b/src/SketchAPI/SketchAPI_Constraint.h @@ -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 +#include +#include + +#include + +/**\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 & 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 diff --git a/src/SketchAPI/SketchAPI_swig.h b/src/SketchAPI/SketchAPI_swig.h index 34c00398f..c1695defa 100644 --- a/src/SketchAPI/SketchAPI_swig.h +++ b/src/SketchAPI/SketchAPI_swig.h @@ -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" -- 2.39.2