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