1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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>
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>
43 #include <SketcherPrs_Tools.h>
45 SketchAPI_Constraint::SketchAPI_Constraint(
46 const std::shared_ptr<ModelAPI_Feature> & theFeature)
47 : ModelHighAPI_Interface(theFeature)
49 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
54 SketchAPI_Constraint::~SketchAPI_Constraint()
58 bool SketchAPI_Constraint::initialize()
61 throwException("Constraint exception: The feature is NULL.");
67 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
69 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
72 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
74 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
77 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
79 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
82 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
84 fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
87 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
89 fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
92 static const std::string& constraintTypeToSetter(const std::string& theType)
94 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
95 static const std::string COINCIDENCE_SETTER("setCoincident");
96 return COINCIDENCE_SETTER;
98 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
99 static const std::string COLLINEAR_SETTER("setCollinear");
100 return COLLINEAR_SETTER;
102 if (theType == SketchPlugin_ConstraintDistance::ID()) {
103 static const std::string DISTANCE_SETTER("setDistance");
104 return DISTANCE_SETTER;
106 if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
107 static const std::string DISTANCE_SETTER("setHorizontalDistance");
108 return DISTANCE_SETTER;
110 if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
111 static const std::string DISTANCE_SETTER("setVerticalDistance");
112 return DISTANCE_SETTER;
114 if (theType == SketchPlugin_ConstraintEqual::ID()) {
115 static const std::string EQUAL_SETTER("setEqual");
118 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
119 static const std::string HORIZONTAL_SETTER("setHorizontal");
120 return HORIZONTAL_SETTER;
122 if (theType == SketchPlugin_ConstraintLength::ID()) {
123 static const std::string LENGTH_SETTER("setLength");
124 return LENGTH_SETTER;
126 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
127 static const std::string MIDDLE_SETTER("setMiddlePoint");
128 return MIDDLE_SETTER;
130 if (theType == SketchPlugin_ConstraintParallel::ID()) {
131 static const std::string PARALLEL_SETTER("setParallel");
132 return PARALLEL_SETTER;
134 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
135 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
136 return PERPENDICULAR_SETTER;
138 if (theType == SketchPlugin_ConstraintRadius::ID()) {
139 static const std::string RADIUS_SETTER("setRadius");
140 return RADIUS_SETTER;
142 if (theType == SketchPlugin_ConstraintRigid::ID()) {
143 static const std::string FIXED_SETTER("setFixed");
146 if (theType == SketchPlugin_ConstraintTangent::ID()) {
147 static const std::string TANGENT_SETTER("setTangent");
148 return TANGENT_SETTER;
150 if (theType == SketchPlugin_ConstraintVertical::ID()) {
151 static const std::string VERTICAL_SETTER("setVertical");
152 return VERTICAL_SETTER;
155 static const std::string DUMMY;
159 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
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);
168 if (!areAttributesDumped)
169 theDumper.postpone(aBase);
170 return areAttributesDumped;
173 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
175 FeaturePtr aBase = feature();
176 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
178 return; // incorrect constraint type
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());
186 AttributeSelectionPtr aAttr =
187 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
188 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
192 // postpone constraint until all its attributes be dumped
193 if (!areAllAttributesDumped(theDumper))
196 const std::string& aSketchName = theDumper.parentName(aBase);
197 theDumper << aSketchName << "." << aSetter << "(";
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;
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());
213 aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
214 if (aValueAttr && aValueAttr->isInitialized())
215 theDumper << ", " << aValueAttr;
217 if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
218 AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
219 theDumper << ", " << isSigned->value();
222 theDumper << ")" << std::endl;