Salome HOME
0930cf0f4699786415d0872c22fcced5e0342ec2
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-2017  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<mailto: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_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>
41
42 #include <SketcherPrs_Tools.h>
43
44 SketchAPI_Constraint::SketchAPI_Constraint(
45     const std::shared_ptr<ModelAPI_Feature> & theFeature)
46 : ModelHighAPI_Interface(theFeature)
47 {
48   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
49   if (aConstraint)
50     initialize();
51 }
52
53 SketchAPI_Constraint::~SketchAPI_Constraint()
54 {
55 }
56
57 bool SketchAPI_Constraint::initialize()
58 {
59   if (!feature()) {
60     throwException("Constraint exception: The feature is NULL.");
61     return false;
62   }
63   return true;
64 }
65
66 static const std::string& constraintTypeToSetter(const std::string& theType)
67 {
68   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
69     static const std::string COINCIDENCE_SETTER("setCoincident");
70     return COINCIDENCE_SETTER;
71   }
72   if (theType == SketchPlugin_ConstraintAngle::ID()) {
73     static const std::string ANGLE_SETTER("setAngle");
74     return ANGLE_SETTER;
75   }
76   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
77     static const std::string COLLINEAR_SETTER("setCollinear");
78     return COLLINEAR_SETTER;
79   }
80   if (theType == SketchPlugin_ConstraintDistance::ID()) {
81     static const std::string DISTANCE_SETTER("setDistance");
82     return DISTANCE_SETTER;
83   }
84   if (theType == SketchPlugin_ConstraintEqual::ID()) {
85     static const std::string EQUAL_SETTER("setEqual");
86     return EQUAL_SETTER;
87   }
88   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
89     static const std::string HORIZONTAL_SETTER("setHorizontal");
90     return HORIZONTAL_SETTER;
91   }
92   if (theType == SketchPlugin_ConstraintLength::ID()) {
93     static const std::string LENGTH_SETTER("setLength");
94     return LENGTH_SETTER;
95   }
96   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
97     static const std::string MIDDLE_SETTER("setMiddlePoint");
98     return MIDDLE_SETTER;
99   }
100   if (theType == SketchPlugin_ConstraintParallel::ID()) {
101     static const std::string PARALLEL_SETTER("setParallel");
102     return PARALLEL_SETTER;
103   }
104   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
105     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
106     return PERPENDICULAR_SETTER;
107   }
108   if (theType == SketchPlugin_ConstraintRadius::ID()) {
109     static const std::string RADIUS_SETTER("setRadius");
110     return RADIUS_SETTER;
111   }
112   if (theType == SketchPlugin_ConstraintRigid::ID()) {
113     static const std::string FIXED_SETTER("setFixed");
114     return FIXED_SETTER;
115   }
116   if (theType == SketchPlugin_ConstraintTangent::ID()) {
117     static const std::string TANGENT_SETTER("setTangent");
118     return TANGENT_SETTER;
119   }
120   if (theType == SketchPlugin_ConstraintVertical::ID()) {
121     static const std::string VERTICAL_SETTER("setVertical");
122     return VERTICAL_SETTER;
123   }
124
125   static const std::string DUMMY;
126   return DUMMY;
127 }
128
129 static std::string angleTypeToString(int theAngleType)
130 {
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");
136   default:
137     break;
138   }
139   return std::string();
140 }
141
142 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
143 {
144   FeaturePtr aBase = feature();
145   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
146   if (aSetter.empty())
147     return; // incorrect constraint type
148
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());
153     if (!aFeature)
154       return;
155     AttributeSelectionPtr aAttr =
156       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
157     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
158       return;
159   }
160
161   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
162   std::string aSetterSuffix;
163   if (isAngle)
164     aSetterSuffix = angleTypeToString(aBase->integer(
165                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
166
167   const std::string& aSketchName = theDumper.parentName(aBase);
168   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
169
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;
175       isFirstAttr = false;
176     }
177   }
178
179   AttributeDoublePtr aValueAttr = aBase->real(
180       isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
181   if (aValueAttr && aValueAttr->isInitialized())
182     theDumper << ", " << aValueAttr;
183
184   theDumper << ")" << std::endl;
185 }