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>
27 #include <SketchPlugin_SketchEntity.h>
29 #include <SketcherPrs_Tools.h>
31 SketchAPI_Constraint::SketchAPI_Constraint(
32 const std::shared_ptr<ModelAPI_Feature> & theFeature)
33 : ModelHighAPI_Interface(theFeature)
35 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
40 SketchAPI_Constraint::~SketchAPI_Constraint()
44 bool SketchAPI_Constraint::initialize()
47 throwException("Constraint exception: The feature is NULL.");
53 static const std::string& constraintTypeToSetter(const std::string& theType)
55 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
56 static const std::string COINCIDENCE_SETTER("setCoincident");
57 return COINCIDENCE_SETTER;
59 if (theType == SketchPlugin_ConstraintAngle::ID()) {
60 static const std::string ANGLE_SETTER("setAngle");
63 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
64 static const std::string COLLINEAR_SETTER("setCollinear");
65 return COLLINEAR_SETTER;
67 if (theType == SketchPlugin_ConstraintDistance::ID()) {
68 static const std::string DISTANCE_SETTER("setDistance");
69 return DISTANCE_SETTER;
71 if (theType == SketchPlugin_ConstraintEqual::ID()) {
72 static const std::string EQUAL_SETTER("setEqual");
75 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
76 static const std::string HORIZONTAL_SETTER("setHorizontal");
77 return HORIZONTAL_SETTER;
79 if (theType == SketchPlugin_ConstraintLength::ID()) {
80 static const std::string LENGTH_SETTER("setLength");
83 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
84 static const std::string MIDDLE_SETTER("setMiddlePoint");
87 if (theType == SketchPlugin_ConstraintParallel::ID()) {
88 static const std::string PARALLEL_SETTER("setParallel");
89 return PARALLEL_SETTER;
91 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
92 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
93 return PERPENDICULAR_SETTER;
95 if (theType == SketchPlugin_ConstraintRadius::ID()) {
96 static const std::string RADIUS_SETTER("setRadius");
99 if (theType == SketchPlugin_ConstraintRigid::ID()) {
100 static const std::string FIXED_SETTER("setFixed");
103 if (theType == SketchPlugin_ConstraintTangent::ID()) {
104 static const std::string TANGENT_SETTER("setTangent");
105 return TANGENT_SETTER;
107 if (theType == SketchPlugin_ConstraintVertical::ID()) {
108 static const std::string VERTICAL_SETTER("setVertical");
109 return VERTICAL_SETTER;
112 static const std::string DUMMY;
116 static std::string angleTypeToString(int theAngleType)
118 switch (theAngleType) {
119 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
120 return std::string("Complementary");
121 case SketcherPrs_Tools::ANGLE_BACKWARD:
122 return std::string("Backward");
126 return std::string();
129 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
131 FeaturePtr aBase = feature();
132 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
134 return; // incorrect constraint type
136 // do not need to dump "Fixed" constraint for external object
137 if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
138 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
139 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
142 AttributeSelectionPtr aAttr = aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
143 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
147 bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
148 std::string aSetterSuffix;
150 aSetterSuffix = angleTypeToString(aBase->integer(
151 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
153 const std::string& aSketchName = theDumper.parentName(aBase);
154 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
156 bool isFirstAttr = true;
157 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
158 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
159 if (aRefAttr && aRefAttr->isInitialized()) {
160 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
165 AttributeDoublePtr aValueAttr = aBase->real(
166 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
167 if (aValueAttr && aValueAttr->isInitialized())
168 theDumper << ", " << aValueAttr;
170 theDumper << ")" << std::endl;