1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name : SketchAPI_Constraint.cpp
6 // 08/08/16 - Artem ZHIDKOV - Creation of the file
8 #include "SketchAPI_Constraint.h"
10 #include <ModelHighAPI_Dumper.h>
11 #include <ModelHighAPI_Tools.h>
13 #include <SketchPlugin_Constraint.h>
14 #include <SketchPlugin_ConstraintAngle.h>
15 #include <SketchPlugin_ConstraintCoincidence.h>
16 #include <SketchPlugin_ConstraintCollinear.h>
17 #include <SketchPlugin_ConstraintDistance.h>
18 #include <SketchPlugin_ConstraintEqual.h>
19 #include <SketchPlugin_ConstraintHorizontal.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintMiddle.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintVertical.h>
28 #include <SketchPlugin_SketchEntity.h>
30 #include <SketcherPrs_Tools.h>
32 SketchAPI_Constraint::SketchAPI_Constraint(
33 const std::shared_ptr<ModelAPI_Feature> & theFeature)
34 : ModelHighAPI_Interface(theFeature)
36 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
41 SketchAPI_Constraint::~SketchAPI_Constraint()
45 bool SketchAPI_Constraint::initialize()
48 throwException("Constraint exception: The feature is NULL.");
54 static const std::string& constraintTypeToSetter(const std::string& theType)
56 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
57 static const std::string COINCIDENCE_SETTER("setCoincident");
58 return COINCIDENCE_SETTER;
60 if (theType == SketchPlugin_ConstraintAngle::ID()) {
61 static const std::string ANGLE_SETTER("setAngle");
64 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
65 static const std::string COLLINEAR_SETTER("setCollinear");
66 return COLLINEAR_SETTER;
68 if (theType == SketchPlugin_ConstraintDistance::ID()) {
69 static const std::string DISTANCE_SETTER("setDistance");
70 return DISTANCE_SETTER;
72 if (theType == SketchPlugin_ConstraintEqual::ID()) {
73 static const std::string EQUAL_SETTER("setEqual");
76 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
77 static const std::string HORIZONTAL_SETTER("setHorizontal");
78 return HORIZONTAL_SETTER;
80 if (theType == SketchPlugin_ConstraintLength::ID()) {
81 static const std::string LENGTH_SETTER("setLength");
84 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
85 static const std::string MIDDLE_SETTER("setMiddlePoint");
88 if (theType == SketchPlugin_ConstraintParallel::ID()) {
89 static const std::string PARALLEL_SETTER("setParallel");
90 return PARALLEL_SETTER;
92 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
93 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
94 return PERPENDICULAR_SETTER;
96 if (theType == SketchPlugin_ConstraintRadius::ID()) {
97 static const std::string RADIUS_SETTER("setRadius");
100 if (theType == SketchPlugin_ConstraintRigid::ID()) {
101 static const std::string FIXED_SETTER("setFixed");
104 if (theType == SketchPlugin_ConstraintTangent::ID()) {
105 static const std::string TANGENT_SETTER("setTangent");
106 return TANGENT_SETTER;
108 if (theType == SketchPlugin_ConstraintVertical::ID()) {
109 static const std::string VERTICAL_SETTER("setVertical");
110 return VERTICAL_SETTER;
113 static const std::string DUMMY;
117 static std::string angleTypeToString(int theAngleType)
119 switch (theAngleType) {
120 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
121 return std::string("Complementary");
122 case SketcherPrs_Tools::ANGLE_BACKWARD:
123 return std::string("Backward");
127 return std::string();
130 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
132 FeaturePtr aBase = feature();
133 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
135 return; // incorrect constraint type
137 // do not need to dump "Fixed" constraint for external object
138 if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
139 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
140 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
143 AttributeSelectionPtr aAttr =
144 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
145 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
149 bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
150 std::string aSetterSuffix;
152 aSetterSuffix = angleTypeToString(aBase->integer(
153 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
155 const std::string& aSketchName = theDumper.parentName(aBase);
156 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
158 bool isFirstAttr = true;
159 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
160 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
161 if (aRefAttr && aRefAttr->isInitialized()) {
162 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
167 AttributeDoublePtr aValueAttr = aBase->real(
168 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
169 if (aValueAttr && aValueAttr->isInitialized())
170 theDumper << ", " << aValueAttr;
172 theDumper << ")" << std::endl;