1 // Name : SketchAPI_Constraint.cpp
5 // 08/08/16 - Artem ZHIDKOV - Creation of the file
7 #include "SketchAPI_Constraint.h"
9 #include <ModelHighAPI_Dumper.h>
10 #include <ModelHighAPI_Tools.h>
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_ConstraintAngle.h>
14 #include <SketchPlugin_ConstraintCoincidence.h>
15 #include <SketchPlugin_ConstraintCollinear.h>
16 #include <SketchPlugin_ConstraintDistance.h>
17 #include <SketchPlugin_ConstraintEqual.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
28 #include <SketcherPrs_Tools.h>
30 SketchAPI_Constraint::SketchAPI_Constraint(
31 const std::shared_ptr<ModelAPI_Feature> & theFeature)
32 : ModelHighAPI_Interface(theFeature)
34 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
39 SketchAPI_Constraint::~SketchAPI_Constraint()
43 bool SketchAPI_Constraint::initialize()
46 throwException("Constraint exception: The feature is NULL.");
52 static const std::string& constraintTypeToSetter(const std::string& theType)
54 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
55 static const std::string COINCIDENCE_SETTER("setCoincident");
56 return COINCIDENCE_SETTER;
58 if (theType == SketchPlugin_ConstraintAngle::ID()) {
59 static const std::string ANGLE_SETTER("setAngle");
62 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
63 static const std::string COLLINEAR_SETTER("setCollinear");
64 return COLLINEAR_SETTER;
66 if (theType == SketchPlugin_ConstraintDistance::ID()) {
67 static const std::string DISTANCE_SETTER("setDistance");
68 return DISTANCE_SETTER;
70 if (theType == SketchPlugin_ConstraintEqual::ID()) {
71 static const std::string EQUAL_SETTER("setEqual");
74 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
75 static const std::string HORIZONTAL_SETTER("setHorizontal");
76 return HORIZONTAL_SETTER;
78 if (theType == SketchPlugin_ConstraintLength::ID()) {
79 static const std::string LENGTH_SETTER("setLength");
82 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
83 static const std::string MIDDLE_SETTER("setMiddlePoint");
86 if (theType == SketchPlugin_ConstraintParallel::ID()) {
87 static const std::string PARALLEL_SETTER("setParallel");
88 return PARALLEL_SETTER;
90 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
91 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
92 return PERPENDICULAR_SETTER;
94 if (theType == SketchPlugin_ConstraintRadius::ID()) {
95 static const std::string RADIUS_SETTER("setRadius");
98 if (theType == SketchPlugin_ConstraintRigid::ID()) {
99 static const std::string FIXED_SETTER("setFixed");
102 if (theType == SketchPlugin_ConstraintTangent::ID()) {
103 static const std::string TANGENT_SETTER("setTangent");
104 return TANGENT_SETTER;
106 if (theType == SketchPlugin_ConstraintVertical::ID()) {
107 static const std::string VERTICAL_SETTER("setVertical");
108 return VERTICAL_SETTER;
111 static const std::string DUMMY;
115 static std::string angleTypeToString(int theAngleType)
117 switch (theAngleType) {
118 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
119 return std::string("Complementary");
120 case SketcherPrs_Tools::ANGLE_BACKWARD:
121 return std::string("Backward");
125 return std::string();
128 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
130 FeaturePtr aBase = feature();
131 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(aBase);
133 return; // dump constraints only
135 // do not need to dump "Fixed" constraint for external object
136 if (aConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
137 AttributeRefAttrPtr aRefAttr = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
138 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
139 std::dynamic_pointer_cast<SketchPlugin_Feature>(
140 ModelAPI_Feature::feature(aRefAttr->object()));
141 if (!aSketchFeature || aSketchFeature->isExternal())
145 const std::string& aSetter = constraintTypeToSetter(aConstraint->getKind());
147 return; // incorrect constraint type
148 bool isAngle = aConstraint->getKind() == SketchPlugin_ConstraintAngle::ID();
149 std::string aSetterSuffix;
151 aSetterSuffix = angleTypeToString(aConstraint->integer(
152 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
154 const std::string& aSketchName = theDumper.parentName(aConstraint);
155 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
157 bool isFirstAttr = true;
158 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
159 AttributeRefAttrPtr aRefAttr = aConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
160 if (aRefAttr && aRefAttr->isInitialized()) {
161 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
166 AttributeDoublePtr aValueAttr = aConstraint->real(
167 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
168 if (aValueAttr && aValueAttr->isInitialized())
169 theDumper << ", " << aValueAttr;
171 theDumper << ")" << std::endl;