Salome HOME
Merge branch 'Pre_2.8.0_development'
[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_ConstraintDistanceHorizontal.h>
32 #include <SketchPlugin_ConstraintDistanceVertical.h>
33 #include <SketchPlugin_ConstraintEqual.h>
34 #include <SketchPlugin_ConstraintHorizontal.h>
35 #include <SketchPlugin_ConstraintLength.h>
36 #include <SketchPlugin_ConstraintMiddle.h>
37 #include <SketchPlugin_ConstraintParallel.h>
38 #include <SketchPlugin_ConstraintPerpendicular.h>
39 #include <SketchPlugin_ConstraintRadius.h>
40 #include <SketchPlugin_ConstraintRigid.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketchPlugin_SketchEntity.h>
44
45 #include <SketcherPrs_Tools.h>
46
47 SketchAPI_Constraint::SketchAPI_Constraint(
48     const std::shared_ptr<ModelAPI_Feature> & theFeature)
49 : ModelHighAPI_Interface(theFeature)
50 {
51   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
52   if (aConstraint)
53     initialize();
54 }
55
56 SketchAPI_Constraint::~SketchAPI_Constraint()
57 {
58 }
59
60 bool SketchAPI_Constraint::initialize()
61 {
62   if (!feature()) {
63     throwException("Constraint exception: The feature is NULL.");
64     return false;
65   }
66   return true;
67 }
68
69 static const std::string& constraintTypeToSetter(const std::string& theType)
70 {
71   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
72     static const std::string COINCIDENCE_SETTER("setCoincident");
73     return COINCIDENCE_SETTER;
74   }
75   if (theType == SketchPlugin_ConstraintAngle::ID()) {
76     static const std::string ANGLE_SETTER("setAngle");
77     return ANGLE_SETTER;
78   }
79   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
80     static const std::string COLLINEAR_SETTER("setCollinear");
81     return COLLINEAR_SETTER;
82   }
83   if (theType == SketchPlugin_ConstraintDistance::ID()) {
84     static const std::string DISTANCE_SETTER("setDistance");
85     return DISTANCE_SETTER;
86   }
87   if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
88     static const std::string DISTANCE_SETTER("setHorizontalDistance");
89     return DISTANCE_SETTER;
90   }
91   if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
92     static const std::string DISTANCE_SETTER("setVerticalDistance");
93     return DISTANCE_SETTER;
94   }
95   if (theType == SketchPlugin_ConstraintEqual::ID()) {
96     static const std::string EQUAL_SETTER("setEqual");
97     return EQUAL_SETTER;
98   }
99   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
100     static const std::string HORIZONTAL_SETTER("setHorizontal");
101     return HORIZONTAL_SETTER;
102   }
103   if (theType == SketchPlugin_ConstraintLength::ID()) {
104     static const std::string LENGTH_SETTER("setLength");
105     return LENGTH_SETTER;
106   }
107   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
108     static const std::string MIDDLE_SETTER("setMiddlePoint");
109     return MIDDLE_SETTER;
110   }
111   if (theType == SketchPlugin_ConstraintParallel::ID()) {
112     static const std::string PARALLEL_SETTER("setParallel");
113     return PARALLEL_SETTER;
114   }
115   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
116     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
117     return PERPENDICULAR_SETTER;
118   }
119   if (theType == SketchPlugin_ConstraintRadius::ID()) {
120     static const std::string RADIUS_SETTER("setRadius");
121     return RADIUS_SETTER;
122   }
123   if (theType == SketchPlugin_ConstraintRigid::ID()) {
124     static const std::string FIXED_SETTER("setFixed");
125     return FIXED_SETTER;
126   }
127   if (theType == SketchPlugin_ConstraintTangent::ID()) {
128     static const std::string TANGENT_SETTER("setTangent");
129     return TANGENT_SETTER;
130   }
131   if (theType == SketchPlugin_ConstraintVertical::ID()) {
132     static const std::string VERTICAL_SETTER("setVertical");
133     return VERTICAL_SETTER;
134   }
135
136   static const std::string DUMMY;
137   return DUMMY;
138 }
139
140 static std::string angleTypeToString(int theAngleType)
141 {
142   switch (theAngleType) {
143   case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
144     return std::string("Complementary");
145   case SketcherPrs_Tools::ANGLE_BACKWARD:
146     return std::string("Backward");
147   default:
148     break;
149   }
150   return std::string();
151 }
152
153 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
154 {
155   FeaturePtr aBase = feature();
156   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
157   if (aSetter.empty())
158     return; // incorrect constraint type
159
160   // do not need to dump "Fixed" constraint for external object
161   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
162     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
163     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
164     if (!aFeature)
165       return;
166     AttributeSelectionPtr aAttr =
167       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
168     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
169       return;
170   }
171
172   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
173   std::string aSetterSuffix;
174   if (isAngle)
175     aSetterSuffix = angleTypeToString(aBase->integer(
176                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
177
178   const std::string& aSketchName = theDumper.parentName(aBase);
179   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
180
181   bool isFirstAttr = true;
182   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
183     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
184     if (aRefAttr && aRefAttr->isInitialized()) {
185       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
186       isFirstAttr = false;
187     }
188   }
189
190   AttributeDoublePtr aValueAttr = aBase->real(
191       isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
192   if (aValueAttr && aValueAttr->isInitialized())
193     theDumper << ", " << aValueAttr;
194
195   theDumper << ")" << std::endl;
196 }