]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Constraint.cpp
Salome HOME
[GITHUB #2] problem in export dialog - difference in STEP vs BREP
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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 #include <SketchAPI_SketchEntity.h>
25 #include <SketchAPI_Point.h>
26
27 #include <SketchPlugin_Constraint.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 #include <SketchPlugin_Point.h>
45
46 #include <SketcherPrs_Tools.h>
47
48 #include <limits>
49
50 SketchAPI_Constraint::SketchAPI_Constraint(
51     const std::shared_ptr<ModelAPI_Feature> & theFeature)
52 : ModelHighAPI_Interface(theFeature)
53 {
54   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
55   if (aConstraint)
56     initialize();
57 }
58
59 SketchAPI_Constraint::~SketchAPI_Constraint()
60 {
61 }
62
63 bool SketchAPI_Constraint::initialize()
64 {
65   if (!feature()) {
66     throwException("Constraint exception: The feature is NULL.");
67     return false;
68   }
69   return true;
70 }
71
72 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
73 {
74   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
75 }
76
77 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
78 {
79   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
80 }
81
82 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
83 {
84   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
85 }
86
87 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
88 {
89   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
90 }
91
92 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
93 {
94   fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
95 }
96
97 double SketchAPI_Constraint::value() const
98 {
99   AttributeDoublePtr aValueAttr = feature()->real(SketchPlugin_Constraint::VALUE());
100   return aValueAttr->isInitialized() ? aValueAttr->value()
101                                      : std::numeric_limits<double>::quiet_NaN();
102 }
103
104 static const std::string& constraintTypeToSetter(const std::string& theType)
105 {
106   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
107     static const std::string COINCIDENCE_SETTER("setCoincident");
108     return COINCIDENCE_SETTER;
109   }
110   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
111     static const std::string COLLINEAR_SETTER("setCollinear");
112     return COLLINEAR_SETTER;
113   }
114   if (theType == SketchPlugin_ConstraintDistance::ID()) {
115     static const std::string DISTANCE_SETTER("setDistance");
116     return DISTANCE_SETTER;
117   }
118   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
119     static const std::string DISTANCE_SETTER("setHorizontalDistance");
120     return DISTANCE_SETTER;
121   }
122   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
123     static const std::string DISTANCE_SETTER("setVerticalDistance");
124     return DISTANCE_SETTER;
125   }
126   if (theType == SketchPlugin_ConstraintEqual::ID()) {
127     static const std::string EQUAL_SETTER("setEqual");
128     return EQUAL_SETTER;
129   }
130   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
131     static const std::string HORIZONTAL_SETTER("setHorizontal");
132     return HORIZONTAL_SETTER;
133   }
134   if (theType == SketchPlugin_ConstraintLength::ID()) {
135     static const std::string LENGTH_SETTER("setLength");
136     return LENGTH_SETTER;
137   }
138   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
139     static const std::string MIDDLE_SETTER("setMiddlePoint");
140     return MIDDLE_SETTER;
141   }
142   if (theType == SketchPlugin_ConstraintParallel::ID()) {
143     static const std::string PARALLEL_SETTER("setParallel");
144     return PARALLEL_SETTER;
145   }
146   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
147     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
148     return PERPENDICULAR_SETTER;
149   }
150   if (theType == SketchPlugin_ConstraintRadius::ID()) {
151     static const std::string RADIUS_SETTER("setRadius");
152     return RADIUS_SETTER;
153   }
154   if (theType == SketchPlugin_ConstraintRigid::ID()) {
155     static const std::string FIXED_SETTER("setFixed");
156     return FIXED_SETTER;
157   }
158   if (theType == SketchPlugin_ConstraintTangent::ID()) {
159     static const std::string TANGENT_SETTER("setTangent");
160     return TANGENT_SETTER;
161   }
162   if (theType == SketchPlugin_ConstraintVertical::ID()) {
163     static const std::string VERTICAL_SETTER("setVertical");
164     return VERTICAL_SETTER;
165   }
166
167   static const std::string DUMMY;
168   return DUMMY;
169 }
170
171 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
172 {
173   bool areAttributesDumped = true;
174   FeaturePtr aBase = feature();
175   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
176     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
177     if (aRefAttr && aRefAttr->isInitialized())
178       areAttributesDumped = theDumper.isDumped(aRefAttr);
179   }
180   if (!areAttributesDumped)
181     theDumper.postpone(aBase);
182   return areAttributesDumped;
183 }
184
185 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
186 {
187   FeaturePtr aBase = feature();
188   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
189   if (aSetter.empty())
190     return; // incorrect constraint type
191
192   // do not need to dump "Fixed" constraint for external object
193   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
194     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
195     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
196     if (!aFeature)
197       return;
198     AttributeSelectionPtr aAttr =
199       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
200     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
201       return;
202   }
203
204   const std::string& aSketchName = theDumper.parentName(aBase);
205
206   // Dump middle constraint by object
207   if (SketchPlugin_ConstraintMiddle::ID() == aBase->getKind() &&
208     aBase->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE())->value() == SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE())
209   {
210     FeaturePtr aFeature;
211     AttributeRefAttrPtr aPointRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(1));
212     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(0));
213
214     if (!theDumper.isDumped(aRefAttr))
215     {
216       theDumper.postpone(aBase);
217       return;
218     }
219     aFeature = ModelAPI_Feature::feature(aPointRefAttr->object());
220
221     theDumper.name(aFeature, false, true, true); // mark point as dumped
222
223     theDumper << theDumper.name(aFeature) << " = " << aSketchName << "." << aSetter << "(";
224     if (aRefAttr && aRefAttr->isInitialized()) {
225       theDumper << aRefAttr;
226     }
227     theDumper << ")" << std::endl;
228   }
229   else
230   {
231     // postpone constraint until all its attributes be dumped
232     if (!areAllAttributesDumped(theDumper))
233       return;
234
235     theDumper << aSketchName << "." << aSetter << "(";
236     bool isFirstAttr = true;
237     for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
238       AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
239       if (aRefAttr && aRefAttr->isInitialized()) {
240         theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
241         isFirstAttr = false;
242       }
243     }
244
245     AttributeDoublePtr aValueAttr;
246     if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
247       aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
248       aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
249     else
250       aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
251     if (aValueAttr && aValueAttr->isInitialized())
252       theDumper << ", " << aValueAttr;
253
254     if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
255       AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
256       theDumper << ", " << isSigned->value();
257     }
258     theDumper << ")" << std::endl;
259   }
260
261 }