Salome HOME
e7409f5c225ccc0d383ce8643ba24750d447278f
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : SketchAPI_Constraint.cpp
3 // Purpose:
4 //
5 // History:
6 // 08/08/16 - Artem ZHIDKOV - Creation of the file
7
8 #include "SketchAPI_Constraint.h"
9
10 #include <ModelHighAPI_Dumper.h>
11 #include <ModelHighAPI_Tools.h>
12
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>
31
32 #include <SketcherPrs_Tools.h>
33
34 SketchAPI_Constraint::SketchAPI_Constraint(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : ModelHighAPI_Interface(theFeature)
37 {
38   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
39   if (aConstraint)
40     initialize();
41 }
42
43 SketchAPI_Constraint::~SketchAPI_Constraint()
44 {
45 }
46
47 bool SketchAPI_Constraint::initialize()
48 {
49   if (!feature()) {
50     throwException("Constraint exception: The feature is NULL.");
51     return false;
52   }
53   return true;
54 }
55
56 static const std::string& constraintTypeToSetter(const std::string& theType)
57 {
58   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
59     static const std::string COINCIDENCE_SETTER("setCoincident");
60     return COINCIDENCE_SETTER;
61   }
62   if (theType == SketchPlugin_ConstraintAngle::ID()) {
63     static const std::string ANGLE_SETTER("setAngle");
64     return ANGLE_SETTER;
65   }
66   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
67     static const std::string COLLINEAR_SETTER("setCollinear");
68     return COLLINEAR_SETTER;
69   }
70   if (theType == SketchPlugin_ConstraintDistance::ID()) {
71     static const std::string DISTANCE_SETTER("setDistance");
72     return DISTANCE_SETTER;
73   }
74   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
75     static const std::string DISTANCE_SETTER("setHorizontalDistance");
76     return DISTANCE_SETTER;
77   }
78   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
79     static const std::string DISTANCE_SETTER("setVerticalDistance");
80     return DISTANCE_SETTER;
81   }
82   if (theType == SketchPlugin_ConstraintEqual::ID()) {
83     static const std::string EQUAL_SETTER("setEqual");
84     return EQUAL_SETTER;
85   }
86   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
87     static const std::string HORIZONTAL_SETTER("setHorizontal");
88     return HORIZONTAL_SETTER;
89   }
90   if (theType == SketchPlugin_ConstraintLength::ID()) {
91     static const std::string LENGTH_SETTER("setLength");
92     return LENGTH_SETTER;
93   }
94   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
95     static const std::string MIDDLE_SETTER("setMiddlePoint");
96     return MIDDLE_SETTER;
97   }
98   if (theType == SketchPlugin_ConstraintParallel::ID()) {
99     static const std::string PARALLEL_SETTER("setParallel");
100     return PARALLEL_SETTER;
101   }
102   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
103     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
104     return PERPENDICULAR_SETTER;
105   }
106   if (theType == SketchPlugin_ConstraintRadius::ID()) {
107     static const std::string RADIUS_SETTER("setRadius");
108     return RADIUS_SETTER;
109   }
110   if (theType == SketchPlugin_ConstraintRigid::ID()) {
111     static const std::string FIXED_SETTER("setFixed");
112     return FIXED_SETTER;
113   }
114   if (theType == SketchPlugin_ConstraintTangent::ID()) {
115     static const std::string TANGENT_SETTER("setTangent");
116     return TANGENT_SETTER;
117   }
118   if (theType == SketchPlugin_ConstraintVertical::ID()) {
119     static const std::string VERTICAL_SETTER("setVertical");
120     return VERTICAL_SETTER;
121   }
122
123   static const std::string DUMMY;
124   return DUMMY;
125 }
126
127 static std::string angleTypeToString(int theAngleType)
128 {
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");
134   default:
135     break;
136   }
137   return std::string();
138 }
139
140 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
141 {
142   FeaturePtr aBase = feature();
143   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
144   if (aSetter.empty())
145     return; // incorrect constraint type
146
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());
151     if (!aFeature)
152       return;
153     AttributeSelectionPtr aAttr =
154       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
155     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
156       return;
157   }
158
159   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
160   std::string aSetterSuffix;
161   if (isAngle)
162     aSetterSuffix = angleTypeToString(aBase->integer(
163                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
164
165   const std::string& aSketchName = theDumper.parentName(aBase);
166   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
167
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;
173       isFirstAttr = false;
174     }
175   }
176
177   AttributeDoublePtr aValueAttr = aBase->real(
178       isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
179   if (aValueAttr && aValueAttr->isInitialized())
180     theDumper << ", " << aValueAttr;
181
182   theDumper << ")" << std::endl;
183 }