Salome HOME
1e063c61e9b7b498b983203fa5ec1b5618f4c1f5
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2023  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 #include <limits>
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 double SketchAPI_Constraint::value() const
95 {
96   AttributeDoublePtr aValueAttr = feature()->real(SketchPlugin_Constraint::VALUE());
97   return aValueAttr->isInitialized() ? aValueAttr->value()
98                                      : std::numeric_limits<double>::quiet_NaN();
99 }
100
101 static const std::string& constraintTypeToSetter(const std::string& theType)
102 {
103   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
104     static const std::string COINCIDENCE_SETTER("setCoincident");
105     return COINCIDENCE_SETTER;
106   }
107   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
108     static const std::string COLLINEAR_SETTER("setCollinear");
109     return COLLINEAR_SETTER;
110   }
111   if (theType == SketchPlugin_ConstraintDistance::ID()) {
112     static const std::string DISTANCE_SETTER("setDistance");
113     return DISTANCE_SETTER;
114   }
115   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
116     static const std::string DISTANCE_SETTER("setHorizontalDistance");
117     return DISTANCE_SETTER;
118   }
119   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
120     static const std::string DISTANCE_SETTER("setVerticalDistance");
121     return DISTANCE_SETTER;
122   }
123   if (theType == SketchPlugin_ConstraintEqual::ID()) {
124     static const std::string EQUAL_SETTER("setEqual");
125     return EQUAL_SETTER;
126   }
127   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
128     static const std::string HORIZONTAL_SETTER("setHorizontal");
129     return HORIZONTAL_SETTER;
130   }
131   if (theType == SketchPlugin_ConstraintLength::ID()) {
132     static const std::string LENGTH_SETTER("setLength");
133     return LENGTH_SETTER;
134   }
135   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
136     static const std::string MIDDLE_SETTER("setMiddlePoint");
137     return MIDDLE_SETTER;
138   }
139   if (theType == SketchPlugin_ConstraintParallel::ID()) {
140     static const std::string PARALLEL_SETTER("setParallel");
141     return PARALLEL_SETTER;
142   }
143   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
144     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
145     return PERPENDICULAR_SETTER;
146   }
147   if (theType == SketchPlugin_ConstraintRadius::ID()) {
148     static const std::string RADIUS_SETTER("setRadius");
149     return RADIUS_SETTER;
150   }
151   if (theType == SketchPlugin_ConstraintRigid::ID()) {
152     static const std::string FIXED_SETTER("setFixed");
153     return FIXED_SETTER;
154   }
155   if (theType == SketchPlugin_ConstraintTangent::ID()) {
156     static const std::string TANGENT_SETTER("setTangent");
157     return TANGENT_SETTER;
158   }
159   if (theType == SketchPlugin_ConstraintVertical::ID()) {
160     static const std::string VERTICAL_SETTER("setVertical");
161     return VERTICAL_SETTER;
162   }
163
164   static const std::string DUMMY;
165   return DUMMY;
166 }
167
168 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
169 {
170   bool areAttributesDumped = true;
171   FeaturePtr aBase = feature();
172   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
173     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
174     if (aRefAttr && aRefAttr->isInitialized())
175       areAttributesDumped = theDumper.isDumped(aRefAttr);
176   }
177   if (!areAttributesDumped)
178     theDumper.postpone(aBase);
179   return areAttributesDumped;
180 }
181
182 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
183 {
184   FeaturePtr aBase = feature();
185   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
186   if (aSetter.empty())
187     return; // incorrect constraint type
188
189   // do not need to dump "Fixed" constraint for external object
190   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
191     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
192     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
193     if (!aFeature)
194       return;
195     AttributeSelectionPtr aAttr =
196       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
197     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
198       return;
199   }
200
201   // postpone constraint until all its attributes be dumped
202   if (!areAllAttributesDumped(theDumper))
203     return;
204
205   const std::string& aSketchName = theDumper.parentName(aBase);
206   theDumper << aSketchName << "." << aSetter << "(";
207
208   bool isFirstAttr = true;
209   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
210     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
211     if (aRefAttr && aRefAttr->isInitialized()) {
212       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
213       isFirstAttr = false;
214     }
215   }
216
217   AttributeDoublePtr aValueAttr;
218   if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
219       aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
220     aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
221   else
222     aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
223   if (aValueAttr && aValueAttr->isInitialized())
224     theDumper << ", " << aValueAttr;
225
226   if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
227     AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
228     theDumper << ", " << isSigned->value();
229   }
230
231   theDumper << ")" << std::endl;
232 }