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_ConstraintDistanceHorizontal.h>
19 #include <SketchPlugin_ConstraintDistanceVertical.h>
20 #include <SketchPlugin_ConstraintEqual.h>
21 #include <SketchPlugin_ConstraintHorizontal.h>
22 #include <SketchPlugin_ConstraintLength.h>
23 #include <SketchPlugin_ConstraintMiddle.h>
24 #include <SketchPlugin_ConstraintParallel.h>
25 #include <SketchPlugin_ConstraintPerpendicular.h>
26 #include <SketchPlugin_ConstraintRadius.h>
27 #include <SketchPlugin_ConstraintRigid.h>
28 #include <SketchPlugin_ConstraintTangent.h>
29 #include <SketchPlugin_ConstraintVertical.h>
30 #include <SketchPlugin_SketchEntity.h>
32 #include <SketcherPrs_Tools.h>
34 SketchAPI_Constraint::SketchAPI_Constraint(
35 const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : ModelHighAPI_Interface(theFeature)
38 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
43 SketchAPI_Constraint::~SketchAPI_Constraint()
47 bool SketchAPI_Constraint::initialize()
50 throwException("Constraint exception: The feature is NULL.");
56 static const std::string& constraintTypeToSetter(const std::string& theType)
58 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
59 static const std::string COINCIDENCE_SETTER("setCoincident");
60 return COINCIDENCE_SETTER;
62 if (theType == SketchPlugin_ConstraintAngle::ID()) {
63 static const std::string ANGLE_SETTER("setAngle");
66 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
67 static const std::string COLLINEAR_SETTER("setCollinear");
68 return COLLINEAR_SETTER;
70 if (theType == SketchPlugin_ConstraintDistance::ID()) {
71 static const std::string DISTANCE_SETTER("setDistance");
72 return DISTANCE_SETTER;
74 if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
75 static const std::string DISTANCE_SETTER("setHorizontalDistance");
76 return DISTANCE_SETTER;
78 if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
79 static const std::string DISTANCE_SETTER("setVerticalDistance");
80 return DISTANCE_SETTER;
82 if (theType == SketchPlugin_ConstraintEqual::ID()) {
83 static const std::string EQUAL_SETTER("setEqual");
86 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
87 static const std::string HORIZONTAL_SETTER("setHorizontal");
88 return HORIZONTAL_SETTER;
90 if (theType == SketchPlugin_ConstraintLength::ID()) {
91 static const std::string LENGTH_SETTER("setLength");
94 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
95 static const std::string MIDDLE_SETTER("setMiddlePoint");
98 if (theType == SketchPlugin_ConstraintParallel::ID()) {
99 static const std::string PARALLEL_SETTER("setParallel");
100 return PARALLEL_SETTER;
102 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
103 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
104 return PERPENDICULAR_SETTER;
106 if (theType == SketchPlugin_ConstraintRadius::ID()) {
107 static const std::string RADIUS_SETTER("setRadius");
108 return RADIUS_SETTER;
110 if (theType == SketchPlugin_ConstraintRigid::ID()) {
111 static const std::string FIXED_SETTER("setFixed");
114 if (theType == SketchPlugin_ConstraintTangent::ID()) {
115 static const std::string TANGENT_SETTER("setTangent");
116 return TANGENT_SETTER;
118 if (theType == SketchPlugin_ConstraintVertical::ID()) {
119 static const std::string VERTICAL_SETTER("setVertical");
120 return VERTICAL_SETTER;
123 static const std::string DUMMY;
127 static std::string angleTypeToString(int theAngleType)
129 switch (theAngleType) {
130 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
131 return std::string("Complementary");
132 case SketcherPrs_Tools::ANGLE_BACKWARD:
133 return std::string("Backward");
137 return std::string();
140 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
142 FeaturePtr aBase = feature();
143 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
145 return; // incorrect constraint type
147 // do not need to dump "Fixed" constraint for external object
148 if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
149 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
150 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
153 AttributeSelectionPtr aAttr =
154 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
155 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
159 bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
160 std::string aSetterSuffix;
162 aSetterSuffix = angleTypeToString(aBase->integer(
163 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
165 const std::string& aSketchName = theDumper.parentName(aBase);
166 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
168 bool isFirstAttr = true;
169 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
170 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
171 if (aRefAttr && aRefAttr->isInitialized()) {
172 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
177 AttributeDoublePtr aValueAttr = aBase->real(
178 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
179 if (aValueAttr && aValueAttr->isInitialized())
180 theDumper << ", " << aValueAttr;
182 theDumper << ")" << std::endl;