]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Constraint.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/HighLevelDump'
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Name   : SketchAPI_Constraint.cpp
2 // Purpose: 
3 //
4 // History:
5 // 08/08/16 - Artem ZHIDKOV - Creation of the file
6
7 #include "SketchAPI_Constraint.h"
8
9 #include <ModelHighAPI_Dumper.h>
10 #include <ModelHighAPI_Tools.h>
11
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
28 #include <SketcherPrs_Tools.h>
29
30 SketchAPI_Constraint::SketchAPI_Constraint(
31     const std::shared_ptr<ModelAPI_Feature> & theFeature)
32 : ModelHighAPI_Interface(theFeature)
33 {
34   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
35   if (aConstraint)
36     initialize();
37 }
38
39 SketchAPI_Constraint::~SketchAPI_Constraint()
40 {
41 }
42
43 bool SketchAPI_Constraint::initialize()
44 {
45   if (!feature()) {
46     throwException("Constraint exception: The feature is NULL.");
47     return false;
48   }
49   return true;
50 }
51
52 static const std::string& constraintTypeToSetter(const std::string& theType)
53 {
54   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
55     static const std::string COINCIDENCE_SETTER("setCoincident");
56     return COINCIDENCE_SETTER;
57   }
58   if (theType == SketchPlugin_ConstraintAngle::ID()) {
59     static const std::string ANGLE_SETTER("setAngle");
60     return ANGLE_SETTER;
61   }
62   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
63     static const std::string COLLINEAR_SETTER("setCollinear");
64     return COLLINEAR_SETTER;
65   }
66   if (theType == SketchPlugin_ConstraintDistance::ID()) {
67     static const std::string DISTANCE_SETTER("setDistance");
68     return DISTANCE_SETTER;
69   }
70   if (theType == SketchPlugin_ConstraintEqual::ID()) {
71     static const std::string EQUAL_SETTER("setEqual");
72     return EQUAL_SETTER;
73   }
74   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
75     static const std::string HORIZONTAL_SETTER("setHorizontal");
76     return HORIZONTAL_SETTER;
77   }
78   if (theType == SketchPlugin_ConstraintLength::ID()) {
79     static const std::string LENGTH_SETTER("setLength");
80     return LENGTH_SETTER;
81   }
82   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
83     static const std::string MIDDLE_SETTER("setMiddlePoint");
84     return MIDDLE_SETTER;
85   }
86   if (theType == SketchPlugin_ConstraintParallel::ID()) {
87     static const std::string PARALLEL_SETTER("setParallel");
88     return PARALLEL_SETTER;
89   }
90   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
91     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
92     return PERPENDICULAR_SETTER;
93   }
94   if (theType == SketchPlugin_ConstraintRadius::ID()) {
95     static const std::string RADIUS_SETTER("setRadius");
96     return RADIUS_SETTER;
97   }
98   if (theType == SketchPlugin_ConstraintRigid::ID()) {
99     static const std::string FIXED_SETTER("setFixed");
100     return FIXED_SETTER;
101   }
102   if (theType == SketchPlugin_ConstraintTangent::ID()) {
103     static const std::string TANGENT_SETTER("setTangent");
104     return TANGENT_SETTER;
105   }
106   if (theType == SketchPlugin_ConstraintVertical::ID()) {
107     static const std::string VERTICAL_SETTER("setVertical");
108     return VERTICAL_SETTER;
109   }
110
111   static const std::string DUMMY;
112   return DUMMY;
113 }
114
115 static std::string angleTypeToString(int theAngleType)
116 {
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");
122   default:
123     break;
124   }
125   return std::string();
126 }
127
128 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
129 {
130   FeaturePtr aBase = feature();
131   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(aBase);
132   if (!aConstraint)
133     return; // dump constraints only
134
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())
142       return;
143   }
144
145   const std::string& aSetter = constraintTypeToSetter(aConstraint->getKind());
146   if (aSetter.empty())
147     return; // incorrect constraint type
148   bool isAngle = aConstraint->getKind() == SketchPlugin_ConstraintAngle::ID();
149   std::string aSetterSuffix;
150   if (isAngle)
151     aSetterSuffix = angleTypeToString(aConstraint->integer(
152                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
153
154   const std::string& aSketchName = theDumper.parentName(aConstraint);
155   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
156
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;
162       isFirstAttr = false;
163     }
164   }
165
166   AttributeDoublePtr aValueAttr = aConstraint->real(
167       isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
168   if (aValueAttr && aValueAttr->isInitialized())
169     theDumper << ", " << aValueAttr;
170
171   theDumper << ")" << std::endl;
172 }