Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2022  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 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
160 {
161   bool areAttributesDumped = true;
162   FeaturePtr aBase = feature();
163   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
164     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
165     if (aRefAttr && aRefAttr->isInitialized())
166       areAttributesDumped = theDumper.isDumped(aRefAttr);
167   }
168   if (!areAttributesDumped)
169     theDumper.postpone(aBase);
170   return areAttributesDumped;
171 }
172
173 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
174 {
175   FeaturePtr aBase = feature();
176   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
177   if (aSetter.empty())
178     return; // incorrect constraint type
179
180   // do not need to dump "Fixed" constraint for external object
181   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
182     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
183     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
184     if (!aFeature)
185       return;
186     AttributeSelectionPtr aAttr =
187       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
188     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
189       return;
190   }
191
192   // postpone constraint until all its attributes be dumped
193   if (!areAllAttributesDumped(theDumper))
194     return;
195
196   const std::string& aSketchName = theDumper.parentName(aBase);
197   theDumper << aSketchName << "." << aSetter << "(";
198
199   bool isFirstAttr = true;
200   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
201     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
202     if (aRefAttr && aRefAttr->isInitialized()) {
203       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
204       isFirstAttr = false;
205     }
206   }
207
208   AttributeDoublePtr aValueAttr;
209   if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
210       aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
211     aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
212   else
213     aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
214   if (aValueAttr && aValueAttr->isInitialized())
215     theDumper << ", " << aValueAttr;
216
217   if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
218     AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
219     theDumper << ", " << isSigned->value();
220   }
221
222   theDumper << ")" << std::endl;
223 }