Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchAPI_Constraint.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_ConstraintCoincidence.h>
27 #include <SketchPlugin_ConstraintCollinear.h>
28 #include <SketchPlugin_ConstraintDistance.h>
29 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
30 #include <SketchPlugin_ConstraintDistanceVertical.h>
31 #include <SketchPlugin_ConstraintEqual.h>
32 #include <SketchPlugin_ConstraintHorizontal.h>
33 #include <SketchPlugin_ConstraintLength.h>
34 #include <SketchPlugin_ConstraintMiddle.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
39 #include <SketchPlugin_ConstraintTangent.h>
40 #include <SketchPlugin_ConstraintVertical.h>
41 #include <SketchPlugin_SketchEntity.h>
42
43 #include <SketcherPrs_Tools.h>
44
45 SketchAPI_Constraint::SketchAPI_Constraint(
46     const std::shared_ptr<ModelAPI_Feature> & theFeature)
47 : ModelHighAPI_Interface(theFeature)
48 {
49   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
50   if (aConstraint)
51     initialize();
52 }
53
54 SketchAPI_Constraint::~SketchAPI_Constraint()
55 {
56 }
57
58 bool SketchAPI_Constraint::initialize()
59 {
60   if (!feature()) {
61     throwException("Constraint exception: The feature is NULL.");
62     return false;
63   }
64   return true;
65 }
66
67 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
68 {
69   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
70 }
71
72 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
73 {
74   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
75 }
76
77 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
78 {
79   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
80 }
81
82 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
83 {
84   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
85 }
86
87 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
88 {
89   fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
90 }
91
92 static const std::string& constraintTypeToSetter(const std::string& theType)
93 {
94   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
95     static const std::string COINCIDENCE_SETTER("setCoincident");
96     return COINCIDENCE_SETTER;
97   }
98   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
99     static const std::string COLLINEAR_SETTER("setCollinear");
100     return COLLINEAR_SETTER;
101   }
102   if (theType == SketchPlugin_ConstraintDistance::ID()) {
103     static const std::string DISTANCE_SETTER("setDistance");
104     return DISTANCE_SETTER;
105   }
106   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
107     static const std::string DISTANCE_SETTER("setHorizontalDistance");
108     return DISTANCE_SETTER;
109   }
110   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
111     static const std::string DISTANCE_SETTER("setVerticalDistance");
112     return DISTANCE_SETTER;
113   }
114   if (theType == SketchPlugin_ConstraintEqual::ID()) {
115     static const std::string EQUAL_SETTER("setEqual");
116     return EQUAL_SETTER;
117   }
118   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
119     static const std::string HORIZONTAL_SETTER("setHorizontal");
120     return HORIZONTAL_SETTER;
121   }
122   if (theType == SketchPlugin_ConstraintLength::ID()) {
123     static const std::string LENGTH_SETTER("setLength");
124     return LENGTH_SETTER;
125   }
126   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
127     static const std::string MIDDLE_SETTER("setMiddlePoint");
128     return MIDDLE_SETTER;
129   }
130   if (theType == SketchPlugin_ConstraintParallel::ID()) {
131     static const std::string PARALLEL_SETTER("setParallel");
132     return PARALLEL_SETTER;
133   }
134   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
135     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
136     return PERPENDICULAR_SETTER;
137   }
138   if (theType == SketchPlugin_ConstraintRadius::ID()) {
139     static const std::string RADIUS_SETTER("setRadius");
140     return RADIUS_SETTER;
141   }
142   if (theType == SketchPlugin_ConstraintRigid::ID()) {
143     static const std::string FIXED_SETTER("setFixed");
144     return FIXED_SETTER;
145   }
146   if (theType == SketchPlugin_ConstraintTangent::ID()) {
147     static const std::string TANGENT_SETTER("setTangent");
148     return TANGENT_SETTER;
149   }
150   if (theType == SketchPlugin_ConstraintVertical::ID()) {
151     static const std::string VERTICAL_SETTER("setVertical");
152     return VERTICAL_SETTER;
153   }
154
155   static const std::string DUMMY;
156   return DUMMY;
157 }
158
159 static std::string angleTypeToString(int theAngleType)
160 {
161   switch (theAngleType) {
162   case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
163     return std::string("Complementary");
164   case SketcherPrs_Tools::ANGLE_BACKWARD:
165     return std::string("Backward");
166   default:
167     break;
168   }
169   return std::string();
170 }
171
172 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
173 {
174   bool areAttributesDumped = true;
175   FeaturePtr aBase = feature();
176   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
177     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
178     if (aRefAttr && aRefAttr->isInitialized())
179       areAttributesDumped = theDumper.isDumped(aRefAttr);
180   }
181   if (!areAttributesDumped)
182     theDumper.postpone(aBase);
183   return areAttributesDumped;
184 }
185
186 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
187 {
188   FeaturePtr aBase = feature();
189   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
190   if (aSetter.empty())
191     return; // incorrect constraint type
192
193   // do not need to dump "Fixed" constraint for external object
194   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
195     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
196     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
197     if (!aFeature)
198       return;
199     AttributeSelectionPtr aAttr =
200       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
201     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
202       return;
203   }
204
205   // postpone constraint until all its attributes be dumped
206   if (!areAllAttributesDumped(theDumper))
207     return;
208
209   const std::string& aSketchName = theDumper.parentName(aBase);
210   theDumper << aBase << " = " << aSketchName << "." << aSetter << "(";
211
212   bool isFirstAttr = true;
213   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
214     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
215     if (aRefAttr && aRefAttr->isInitialized()) {
216       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
217       isFirstAttr = false;
218     }
219   }
220
221   AttributeDoublePtr aValueAttr;
222   if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
223       aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
224     aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
225   else
226     aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
227   if (aValueAttr && aValueAttr->isInitialized())
228     theDumper << ", " << aValueAttr;
229
230   if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
231     AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
232     theDumper << ", " << isSigned->value();
233   }
234
235   theDumper << ")" << std::endl;
236 }