Salome HOME
Issues #2850, #2860: Change type of angle while Python Dump, because the configuratio...
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Constraint.h"
22
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 #include <SketchPlugin_Constraint.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintCollinear.h>
29 #include <SketchPlugin_ConstraintDistance.h>
30 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
31 #include <SketchPlugin_ConstraintDistanceVertical.h>
32 #include <SketchPlugin_ConstraintEqual.h>
33 #include <SketchPlugin_ConstraintHorizontal.h>
34 #include <SketchPlugin_ConstraintLength.h>
35 #include <SketchPlugin_ConstraintMiddle.h>
36 #include <SketchPlugin_ConstraintParallel.h>
37 #include <SketchPlugin_ConstraintPerpendicular.h>
38 #include <SketchPlugin_ConstraintRadius.h>
39 #include <SketchPlugin_ConstraintRigid.h>
40 #include <SketchPlugin_ConstraintTangent.h>
41 #include <SketchPlugin_ConstraintVertical.h>
42 #include <SketchPlugin_SketchEntity.h>
43
44 #include <SketcherPrs_Tools.h>
45
46 SketchAPI_Constraint::SketchAPI_Constraint(
47     const std::shared_ptr<ModelAPI_Feature> & theFeature)
48 : ModelHighAPI_Interface(theFeature)
49 {
50   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
51   if (aConstraint)
52     initialize();
53 }
54
55 SketchAPI_Constraint::~SketchAPI_Constraint()
56 {
57 }
58
59 bool SketchAPI_Constraint::initialize()
60 {
61   if (!feature()) {
62     throwException("Constraint exception: The feature is NULL.");
63     return false;
64   }
65   return true;
66 }
67
68 void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
69 {
70   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
71 }
72
73 void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
74 {
75   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
76 }
77
78 void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
79 {
80   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
81 }
82
83 void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
84 {
85   fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
86 }
87
88 void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
89 {
90   fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
91 }
92
93 static const std::string& constraintTypeToSetter(const std::string& theType)
94 {
95   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
96     static const std::string COINCIDENCE_SETTER("setCoincident");
97     return COINCIDENCE_SETTER;
98   }
99   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
100     static const std::string COLLINEAR_SETTER("setCollinear");
101     return COLLINEAR_SETTER;
102   }
103   if (theType == SketchPlugin_ConstraintDistance::ID()) {
104     static const std::string DISTANCE_SETTER("setDistance");
105     return DISTANCE_SETTER;
106   }
107   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
108     static const std::string DISTANCE_SETTER("setHorizontalDistance");
109     return DISTANCE_SETTER;
110   }
111   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
112     static const std::string DISTANCE_SETTER("setVerticalDistance");
113     return DISTANCE_SETTER;
114   }
115   if (theType == SketchPlugin_ConstraintEqual::ID()) {
116     static const std::string EQUAL_SETTER("setEqual");
117     return EQUAL_SETTER;
118   }
119   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
120     static const std::string HORIZONTAL_SETTER("setHorizontal");
121     return HORIZONTAL_SETTER;
122   }
123   if (theType == SketchPlugin_ConstraintLength::ID()) {
124     static const std::string LENGTH_SETTER("setLength");
125     return LENGTH_SETTER;
126   }
127   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
128     static const std::string MIDDLE_SETTER("setMiddlePoint");
129     return MIDDLE_SETTER;
130   }
131   if (theType == SketchPlugin_ConstraintParallel::ID()) {
132     static const std::string PARALLEL_SETTER("setParallel");
133     return PARALLEL_SETTER;
134   }
135   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
136     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
137     return PERPENDICULAR_SETTER;
138   }
139   if (theType == SketchPlugin_ConstraintRadius::ID()) {
140     static const std::string RADIUS_SETTER("setRadius");
141     return RADIUS_SETTER;
142   }
143   if (theType == SketchPlugin_ConstraintRigid::ID()) {
144     static const std::string FIXED_SETTER("setFixed");
145     return FIXED_SETTER;
146   }
147   if (theType == SketchPlugin_ConstraintTangent::ID()) {
148     static const std::string TANGENT_SETTER("setTangent");
149     return TANGENT_SETTER;
150   }
151   if (theType == SketchPlugin_ConstraintVertical::ID()) {
152     static const std::string VERTICAL_SETTER("setVertical");
153     return VERTICAL_SETTER;
154   }
155
156   static const std::string DUMMY;
157   return DUMMY;
158 }
159
160 static std::string angleTypeToString(int theAngleType)
161 {
162   switch (theAngleType) {
163   case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
164     return std::string("Complementary");
165   case SketcherPrs_Tools::ANGLE_BACKWARD:
166     return std::string("Backward");
167   default:
168     break;
169   }
170   return std::string();
171 }
172
173 bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
174 {
175   bool areAttributesDumped = true;
176   FeaturePtr aBase = feature();
177   for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
178     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
179     if (aRefAttr && aRefAttr->isInitialized())
180       areAttributesDumped = theDumper.isDumped(aRefAttr);
181   }
182   if (!areAttributesDumped)
183     theDumper.postpone(aBase);
184   return areAttributesDumped;
185 }
186
187 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
188 {
189   FeaturePtr aBase = feature();
190   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
191   if (aSetter.empty())
192     return; // incorrect constraint type
193
194   // do not need to dump "Fixed" constraint for external object
195   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
196     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
197     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
198     if (!aFeature)
199       return;
200     AttributeSelectionPtr aAttr =
201       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
202     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
203       return;
204   }
205
206   // postpone constraint until all its attributes be dumped
207   if (!areAllAttributesDumped(theDumper))
208     return;
209
210   const std::string& aSketchName = theDumper.parentName(aBase);
211   theDumper << aBase << " = " << aSketchName << "." << aSetter << "(";
212
213   bool isFirstAttr = true;
214   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
215     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
216     if (aRefAttr && aRefAttr->isInitialized()) {
217       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
218       isFirstAttr = false;
219     }
220   }
221
222   AttributeDoublePtr aValueAttr;
223   if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
224       aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
225     aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
226   else
227     aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
228   if (aValueAttr && aValueAttr->isInitialized())
229     theDumper << ", " << aValueAttr;
230
231   if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
232     AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
233     theDumper << ", " << isSigned->value();
234   }
235
236   theDumper << ")" << std::endl;
237 }