1 // Copyright (C) 2014-2017 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<mailto: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_ConstraintAngle.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintCollinear.h>
29 #include <SketchPlugin_ConstraintDistance.h>
30 #include <SketchPlugin_ConstraintEqual.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMiddle.h>
34 #include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintPerpendicular.h>
36 #include <SketchPlugin_ConstraintRadius.h>
37 #include <SketchPlugin_ConstraintRigid.h>
38 #include <SketchPlugin_ConstraintTangent.h>
39 #include <SketchPlugin_ConstraintVertical.h>
40 #include <SketchPlugin_SketchEntity.h>
42 #include <SketcherPrs_Tools.h>
44 SketchAPI_Constraint::SketchAPI_Constraint(
45 const std::shared_ptr<ModelAPI_Feature> & theFeature)
46 : ModelHighAPI_Interface(theFeature)
48 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
53 SketchAPI_Constraint::~SketchAPI_Constraint()
57 bool SketchAPI_Constraint::initialize()
60 throwException("Constraint exception: The feature is NULL.");
66 static const std::string& constraintTypeToSetter(const std::string& theType)
68 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
69 static const std::string COINCIDENCE_SETTER("setCoincident");
70 return COINCIDENCE_SETTER;
72 if (theType == SketchPlugin_ConstraintAngle::ID()) {
73 static const std::string ANGLE_SETTER("setAngle");
76 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
77 static const std::string COLLINEAR_SETTER("setCollinear");
78 return COLLINEAR_SETTER;
80 if (theType == SketchPlugin_ConstraintDistance::ID()) {
81 static const std::string DISTANCE_SETTER("setDistance");
82 return DISTANCE_SETTER;
84 if (theType == SketchPlugin_ConstraintEqual::ID()) {
85 static const std::string EQUAL_SETTER("setEqual");
88 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
89 static const std::string HORIZONTAL_SETTER("setHorizontal");
90 return HORIZONTAL_SETTER;
92 if (theType == SketchPlugin_ConstraintLength::ID()) {
93 static const std::string LENGTH_SETTER("setLength");
96 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
97 static const std::string MIDDLE_SETTER("setMiddlePoint");
100 if (theType == SketchPlugin_ConstraintParallel::ID()) {
101 static const std::string PARALLEL_SETTER("setParallel");
102 return PARALLEL_SETTER;
104 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
105 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
106 return PERPENDICULAR_SETTER;
108 if (theType == SketchPlugin_ConstraintRadius::ID()) {
109 static const std::string RADIUS_SETTER("setRadius");
110 return RADIUS_SETTER;
112 if (theType == SketchPlugin_ConstraintRigid::ID()) {
113 static const std::string FIXED_SETTER("setFixed");
116 if (theType == SketchPlugin_ConstraintTangent::ID()) {
117 static const std::string TANGENT_SETTER("setTangent");
118 return TANGENT_SETTER;
120 if (theType == SketchPlugin_ConstraintVertical::ID()) {
121 static const std::string VERTICAL_SETTER("setVertical");
122 return VERTICAL_SETTER;
125 static const std::string DUMMY;
129 static std::string angleTypeToString(int theAngleType)
131 switch (theAngleType) {
132 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
133 return std::string("Complementary");
134 case SketcherPrs_Tools::ANGLE_BACKWARD:
135 return std::string("Backward");
139 return std::string();
142 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
144 FeaturePtr aBase = feature();
145 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
147 return; // incorrect constraint type
149 // do not need to dump "Fixed" constraint for external object
150 if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
151 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
152 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
155 AttributeSelectionPtr aAttr =
156 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
157 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
161 bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
162 std::string aSetterSuffix;
164 aSetterSuffix = angleTypeToString(aBase->integer(
165 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
167 const std::string& aSketchName = theDumper.parentName(aBase);
168 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
170 bool isFirstAttr = true;
171 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
172 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
173 if (aRefAttr && aRefAttr->isInitialized()) {
174 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
179 AttributeDoublePtr aValueAttr = aBase->real(
180 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
181 if (aValueAttr && aValueAttr->isInitialized())
182 theDumper << ", " << aValueAttr;
184 theDumper << ")" << std::endl;