]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Constraint.cpp
Salome HOME
Issue #2741: Undo of distance argument change produces crash
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Constraint.h"
22
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 #include <SketchPlugin_Constraint.h>
27 #include <SketchPlugin_ConstraintAngle.h>
28 #include <SketchPlugin_ConstraintCoincidence.h>
29 #include <SketchPlugin_ConstraintCollinear.h>
30 #include <SketchPlugin_ConstraintDistance.h>
31 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
32 #include <SketchPlugin_ConstraintDistanceVertical.h>
33 #include <SketchPlugin_ConstraintEqual.h>
34 #include <SketchPlugin_ConstraintHorizontal.h>
35 #include <SketchPlugin_ConstraintLength.h>
36 #include <SketchPlugin_ConstraintMiddle.h>
37 #include <SketchPlugin_ConstraintParallel.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
39 #include <SketchPlugin_ConstraintRadius.h>
40 #include <SketchPlugin_ConstraintRigid.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketchPlugin_SketchEntity.h>
44
45 #include <SketcherPrs_Tools.h>
46
47 SketchAPI_Constraint::SketchAPI_Constraint(
48     const std::shared_ptr<ModelAPI_Feature> & theFeature)
49 : ModelHighAPI_Interface(theFeature)
50 {
51   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
52   if (aConstraint)
53     initialize();
54 }
55
56 SketchAPI_Constraint::~SketchAPI_Constraint()
57 {
58 }
59
60 bool SketchAPI_Constraint::initialize()
61 {
62   if (!feature()) {
63     throwException("Constraint exception: The feature is NULL.");
64     return false;
65   }
66   return true;
67 }
68
69 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
70 {
71   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
72 }
73
74 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
75 {
76   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
77 }
78
79 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
80 {
81   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
82 }
83
84 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
85 {
86   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
87 }
88
89 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
90 {
91   fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
92 }
93
94 static const std::string& constraintTypeToSetter(const std::string& theType)
95 {
96   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
97     static const std::string COINCIDENCE_SETTER("setCoincident");
98     return COINCIDENCE_SETTER;
99   }
100   if (theType == SketchPlugin_ConstraintAngle::ID()) {
101     static const std::string ANGLE_SETTER("setAngle");
102     return ANGLE_SETTER;
103   }
104   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
105     static const std::string COLLINEAR_SETTER("setCollinear");
106     return COLLINEAR_SETTER;
107   }
108   if (theType == SketchPlugin_ConstraintDistance::ID()) {
109     static const std::string DISTANCE_SETTER("setDistance");
110     return DISTANCE_SETTER;
111   }
112   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
113     static const std::string DISTANCE_SETTER("setHorizontalDistance");
114     return DISTANCE_SETTER;
115   }
116   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
117     static const std::string DISTANCE_SETTER("setVerticalDistance");
118     return DISTANCE_SETTER;
119   }
120   if (theType == SketchPlugin_ConstraintEqual::ID()) {
121     static const std::string EQUAL_SETTER("setEqual");
122     return EQUAL_SETTER;
123   }
124   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
125     static const std::string HORIZONTAL_SETTER("setHorizontal");
126     return HORIZONTAL_SETTER;
127   }
128   if (theType == SketchPlugin_ConstraintLength::ID()) {
129     static const std::string LENGTH_SETTER("setLength");
130     return LENGTH_SETTER;
131   }
132   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
133     static const std::string MIDDLE_SETTER("setMiddlePoint");
134     return MIDDLE_SETTER;
135   }
136   if (theType == SketchPlugin_ConstraintParallel::ID()) {
137     static const std::string PARALLEL_SETTER("setParallel");
138     return PARALLEL_SETTER;
139   }
140   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
141     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
142     return PERPENDICULAR_SETTER;
143   }
144   if (theType == SketchPlugin_ConstraintRadius::ID()) {
145     static const std::string RADIUS_SETTER("setRadius");
146     return RADIUS_SETTER;
147   }
148   if (theType == SketchPlugin_ConstraintRigid::ID()) {
149     static const std::string FIXED_SETTER("setFixed");
150     return FIXED_SETTER;
151   }
152   if (theType == SketchPlugin_ConstraintTangent::ID()) {
153     static const std::string TANGENT_SETTER("setTangent");
154     return TANGENT_SETTER;
155   }
156   if (theType == SketchPlugin_ConstraintVertical::ID()) {
157     static const std::string VERTICAL_SETTER("setVertical");
158     return VERTICAL_SETTER;
159   }
160
161   static const std::string DUMMY;
162   return DUMMY;
163 }
164
165 static std::string angleTypeToString(int theAngleType)
166 {
167   switch (theAngleType) {
168   case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
169     return std::string("Complementary");
170   case SketcherPrs_Tools::ANGLE_BACKWARD:
171     return std::string("Backward");
172   default:
173     break;
174   }
175   return std::string();
176 }
177
178 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
179 {
180   FeaturePtr aBase = feature();
181   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
182   if (aSetter.empty())
183     return; // incorrect constraint type
184
185   // do not need to dump "Fixed" constraint for external object
186   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
187     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
188     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
189     if (!aFeature)
190       return;
191     AttributeSelectionPtr aAttr =
192       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
193     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
194       return;
195   }
196
197   // Check all attributes are already dumped. If not, store the constraint as postponed.
198   bool areAttributesDumped = true;
199   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
200     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
201     if (aRefAttr && aRefAttr->isInitialized())
202       areAttributesDumped = theDumper.isDumped(aRefAttr);
203   }
204   if (!areAttributesDumped) {
205     theDumper.postpone(aBase);
206     return;
207   }
208
209   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
210   std::string aSetterSuffix;
211   if (isAngle)
212     aSetterSuffix = angleTypeToString(aBase->integer(
213                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
214
215   const std::string& aSketchName = theDumper.parentName(aBase);
216   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
217
218   bool isFirstAttr = true;
219   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
220     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
221     if (aRefAttr && aRefAttr->isInitialized()) {
222       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
223       isFirstAttr = false;
224     }
225   }
226
227   AttributeDoublePtr aValueAttr;
228   if (isAngle)
229     aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
230   else if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
231            aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
232     aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
233   else
234     aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
235   if (aValueAttr && aValueAttr->isInitialized())
236     theDumper << ", " << aValueAttr;
237
238   if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
239     AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
240     theDumper << ", " << isSigned->value();
241   }
242
243   theDumper << ")" << std::endl;
244 }