1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchAPI_Constraint.h"
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24 #include <SketchAPI_SketchEntity.h>
25 #include <SketchAPI_Point.h>
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>
46 #include <SketcherPrs_Tools.h>
50 SketchAPI_Constraint::SketchAPI_Constraint(
51 const std::shared_ptr<ModelAPI_Feature> & theFeature)
52 : ModelHighAPI_Interface(theFeature)
54 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
59 SketchAPI_Constraint::~SketchAPI_Constraint()
63 bool SketchAPI_Constraint::initialize()
66 throwException("Constraint exception: The feature is NULL.");
72 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
74 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
77 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
79 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
82 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
84 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
87 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
89 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
92 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
94 fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
97 double SketchAPI_Constraint::value() const
99 AttributeDoublePtr aValueAttr = feature()->real(SketchPlugin_Constraint::VALUE());
100 return aValueAttr->isInitialized() ? aValueAttr->value()
101 : std::numeric_limits<double>::quiet_NaN();
104 static const std::string& constraintTypeToSetter(const std::string& theType)
106 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
107 static const std::string COINCIDENCE_SETTER("setCoincident");
108 return COINCIDENCE_SETTER;
110 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
111 static const std::string COLLINEAR_SETTER("setCollinear");
112 return COLLINEAR_SETTER;
114 if (theType == SketchPlugin_ConstraintDistance::ID()) {
115 static const std::string DISTANCE_SETTER("setDistance");
116 return DISTANCE_SETTER;
118 if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
119 static const std::string DISTANCE_SETTER("setHorizontalDistance");
120 return DISTANCE_SETTER;
122 if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
123 static const std::string DISTANCE_SETTER("setVerticalDistance");
124 return DISTANCE_SETTER;
126 if (theType == SketchPlugin_ConstraintEqual::ID()) {
127 static const std::string EQUAL_SETTER("setEqual");
130 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
131 static const std::string HORIZONTAL_SETTER("setHorizontal");
132 return HORIZONTAL_SETTER;
134 if (theType == SketchPlugin_ConstraintLength::ID()) {
135 static const std::string LENGTH_SETTER("setLength");
136 return LENGTH_SETTER;
138 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
139 static const std::string MIDDLE_SETTER("setMiddlePoint");
140 return MIDDLE_SETTER;
142 if (theType == SketchPlugin_ConstraintParallel::ID()) {
143 static const std::string PARALLEL_SETTER("setParallel");
144 return PARALLEL_SETTER;
146 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
147 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
148 return PERPENDICULAR_SETTER;
150 if (theType == SketchPlugin_ConstraintRadius::ID()) {
151 static const std::string RADIUS_SETTER("setRadius");
152 return RADIUS_SETTER;
154 if (theType == SketchPlugin_ConstraintRigid::ID()) {
155 static const std::string FIXED_SETTER("setFixed");
158 if (theType == SketchPlugin_ConstraintTangent::ID()) {
159 static const std::string TANGENT_SETTER("setTangent");
160 return TANGENT_SETTER;
162 if (theType == SketchPlugin_ConstraintVertical::ID()) {
163 static const std::string VERTICAL_SETTER("setVertical");
164 return VERTICAL_SETTER;
167 static const std::string DUMMY;
171 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
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);
180 if (!areAttributesDumped)
181 theDumper.postpone(aBase);
182 return areAttributesDumped;
185 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
187 FeaturePtr aBase = feature();
188 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
190 return; // incorrect constraint type
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());
198 AttributeSelectionPtr aAttr =
199 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
200 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
204 const std::string& aSketchName = theDumper.parentName(aBase);
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())
211 AttributeRefAttrPtr aPointRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(1));
212 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(0));
214 if (!theDumper.isDumped(aRefAttr))
216 theDumper.postpone(aBase);
219 aFeature = ModelAPI_Feature::feature(aPointRefAttr->object());
221 theDumper.name(aFeature, false, true, true); // mark point as dumped
223 theDumper << theDumper.name(aFeature) << " = " << aSketchName << "." << aSetter << "(";
224 if (aRefAttr && aRefAttr->isInitialized()) {
225 theDumper << aRefAttr;
227 theDumper << ")" << std::endl;
231 // postpone constraint until all its attributes be dumped
232 if (!areAllAttributesDumped(theDumper))
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;
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());
250 aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
251 if (aValueAttr && aValueAttr->isInitialized())
252 theDumper << ", " << aValueAttr;
254 if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
255 AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
256 theDumper << ", " << isSigned->value();
258 theDumper << ")" << std::endl;