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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchAPI_Constraint.h"
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
26 #include <SketchPlugin_Constraint.h>
27 #include <SketchPlugin_ConstraintAngle.h>
28 #include <SketchPlugin_ConstraintCoincidence.h>
29 #include <SketchPlugin_ConstraintCollinear.h>
30 #include <SketchPlugin_ConstraintDistance.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 static const std::string& constraintTypeToSetter(const std::string& theType)
69 if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
70 static const std::string COINCIDENCE_SETTER("setCoincident");
71 return COINCIDENCE_SETTER;
73 if (theType == SketchPlugin_ConstraintAngle::ID()) {
74 static const std::string ANGLE_SETTER("setAngle");
77 if (theType == SketchPlugin_ConstraintCollinear::ID()) {
78 static const std::string COLLINEAR_SETTER("setCollinear");
79 return COLLINEAR_SETTER;
81 if (theType == SketchPlugin_ConstraintDistance::ID()) {
82 static const std::string DISTANCE_SETTER("setDistance");
83 return DISTANCE_SETTER;
85 if (theType == SketchPlugin_ConstraintEqual::ID()) {
86 static const std::string EQUAL_SETTER("setEqual");
89 if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
90 static const std::string HORIZONTAL_SETTER("setHorizontal");
91 return HORIZONTAL_SETTER;
93 if (theType == SketchPlugin_ConstraintLength::ID()) {
94 static const std::string LENGTH_SETTER("setLength");
97 if (theType == SketchPlugin_ConstraintMiddle::ID()) {
98 static const std::string MIDDLE_SETTER("setMiddlePoint");
101 if (theType == SketchPlugin_ConstraintParallel::ID()) {
102 static const std::string PARALLEL_SETTER("setParallel");
103 return PARALLEL_SETTER;
105 if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
106 static const std::string PERPENDICULAR_SETTER("setPerpendicular");
107 return PERPENDICULAR_SETTER;
109 if (theType == SketchPlugin_ConstraintRadius::ID()) {
110 static const std::string RADIUS_SETTER("setRadius");
111 return RADIUS_SETTER;
113 if (theType == SketchPlugin_ConstraintRigid::ID()) {
114 static const std::string FIXED_SETTER("setFixed");
117 if (theType == SketchPlugin_ConstraintTangent::ID()) {
118 static const std::string TANGENT_SETTER("setTangent");
119 return TANGENT_SETTER;
121 if (theType == SketchPlugin_ConstraintVertical::ID()) {
122 static const std::string VERTICAL_SETTER("setVertical");
123 return VERTICAL_SETTER;
126 static const std::string DUMMY;
130 static std::string angleTypeToString(int theAngleType)
132 switch (theAngleType) {
133 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
134 return std::string("Complementary");
135 case SketcherPrs_Tools::ANGLE_BACKWARD:
136 return std::string("Backward");
140 return std::string();
143 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
145 FeaturePtr aBase = feature();
146 const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
148 return; // incorrect constraint type
150 // do not need to dump "Fixed" constraint for external object
151 if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
152 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
153 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
156 AttributeSelectionPtr aAttr =
157 aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
158 if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
162 bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
163 std::string aSetterSuffix;
165 aSetterSuffix = angleTypeToString(aBase->integer(
166 SketchPlugin_ConstraintAngle::TYPE_ID())->value());
168 const std::string& aSketchName = theDumper.parentName(aBase);
169 theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
171 bool isFirstAttr = true;
172 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
173 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
174 if (aRefAttr && aRefAttr->isInitialized()) {
175 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
180 AttributeDoublePtr aValueAttr = aBase->real(
181 isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
182 if (aValueAttr && aValueAttr->isInitialized())
183 theDumper << ", " << aValueAttr;
185 theDumper << ")" << std::endl;