Salome HOME
modif
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Rotation.cpp
1 // Copyright (C) 2014-2022  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
18 //
19
20 #include "FeaturesAPI_Rotation.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 //==================================================================================================
26 FeaturesAPI_Rotation::FeaturesAPI_Rotation(const std::shared_ptr<ModelAPI_Feature>& theFeature)
27 : ModelHighAPI_Interface(theFeature)
28 {
29   initialize();
30 }
31
32 //==================================================================================================
33 FeaturesAPI_Rotation::FeaturesAPI_Rotation(const std::shared_ptr<ModelAPI_Feature>& theFeature,
34                                            const std::list<ModelHighAPI_Selection>& theMainObjects,
35                                            const ModelHighAPI_Selection& theAxisObject,
36                                            const ModelHighAPI_Double& theAngle)
37 : ModelHighAPI_Interface(theFeature)
38 {
39   if(initialize()) {
40     fillAttribute(theMainObjects, mymainObjects);
41     fillAttribute(theAxisObject, myaxisObject);
42     setAngle(theAngle);
43   }
44 }
45
46 //==================================================================================================
47 FeaturesAPI_Rotation::FeaturesAPI_Rotation(const std::shared_ptr<ModelAPI_Feature>& theFeature,
48                                            const std::list<ModelHighAPI_Selection>& theMainObjects,
49                                            const ModelHighAPI_Selection& theCenterPoint,
50                                            const ModelHighAPI_Selection& theStartPoint,
51                                            const ModelHighAPI_Selection& theEndPoint)
52 : ModelHighAPI_Interface(theFeature)
53 {
54   if(initialize()) {
55     fillAttribute(theMainObjects, mymainObjects);
56     setPoints(theCenterPoint, theStartPoint, theEndPoint);
57   }
58 }
59
60 //==================================================================================================
61 FeaturesAPI_Rotation::~FeaturesAPI_Rotation()
62 {
63
64 }
65
66 //==================================================================================================
67 void FeaturesAPI_Rotation::setMainObjects(const std::list<ModelHighAPI_Selection>& theMainObjects)
68 {
69   fillAttribute(theMainObjects, mymainObjects);
70
71   execute();
72 }
73
74 //==================================================================================================
75 void FeaturesAPI_Rotation::setAxisObject(const ModelHighAPI_Selection& theAxisObject)
76 {
77   fillAttribute(FeaturesPlugin_Rotation::CREATION_METHOD_BY_ANGLE(), mycreationMethod);
78   fillAttribute(theAxisObject, myaxisObject);
79
80   execute();
81 }
82
83 //==================================================================================================
84 void FeaturesAPI_Rotation::setAngle(const ModelHighAPI_Double& theAngle)
85 {
86   fillAttribute(FeaturesPlugin_Rotation::CREATION_METHOD_BY_ANGLE(), mycreationMethod);
87   fillAttribute(theAngle, myangle);
88
89   execute();
90 }
91
92 //==================================================================================================
93 void FeaturesAPI_Rotation::setPoints(const ModelHighAPI_Selection& theCenterPoint,
94                                      const ModelHighAPI_Selection& theStartPoint,
95                                      const ModelHighAPI_Selection& theEndPoint)
96 {
97   fillAttribute(FeaturesPlugin_Rotation::CREATION_METHOD_BY_THREE_POINTS(), mycreationMethod);
98   fillAttribute(theCenterPoint, centerPoint());
99   fillAttribute(theStartPoint, startPoint());
100   fillAttribute(theEndPoint, endPoint());
101
102   execute();
103 }
104
105 //==================================================================================================
106 void FeaturesAPI_Rotation::dump(ModelHighAPI_Dumper& theDumper) const
107 {
108   FeaturePtr aBase = feature();
109   const std::string& aDocName = theDumper.name(aBase->document());
110
111   AttributeSelectionListPtr anAttrObjects =
112     aBase->selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
113
114   theDumper << aBase << " = model.addRotation(" << aDocName << ", " << anAttrObjects;
115
116   std::string aCreationMethod =
117     aBase->string(FeaturesPlugin_Rotation::CREATION_METHOD())->value();
118
119   if (aCreationMethod == FeaturesPlugin_Rotation::CREATION_METHOD_BY_ANGLE()) {
120     AttributeSelectionPtr anAttrAxis = aBase->selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
121     AttributeDoublePtr anAttrAngle = aBase->real(FeaturesPlugin_Rotation::ANGLE_ID());
122     theDumper << ", axis = " << anAttrAxis << ", angle = " << anAttrAngle;
123   } else if (aCreationMethod == FeaturesPlugin_Rotation::CREATION_METHOD_BY_THREE_POINTS()) {
124     AttributeSelectionPtr anAttrCenterPoint =
125       aBase->selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
126     AttributeSelectionPtr anAttrStartPoint =
127       aBase->selection(FeaturesPlugin_Rotation::START_POINT_ID());
128     AttributeSelectionPtr anAttrEndPoint =
129       aBase->selection(FeaturesPlugin_Rotation::END_POINT_ID());
130     theDumper << ", center = " << anAttrCenterPoint
131               << ", start = " << anAttrStartPoint
132               << ", end = " << anAttrEndPoint;
133   }
134
135   if (!aBase->data()->version().empty())
136     theDumper << ", keepSubResults = True";
137
138   theDumper << ")" << std::endl;
139 }
140
141 //==================================================================================================
142 RotationPtr addRotation(const std::shared_ptr<ModelAPI_Document>& part,
143                         const std::list<ModelHighAPI_Selection>& objects,
144                         const ModelHighAPI_Selection& axis,
145                         const std::pair<ModelHighAPI_Selection, ModelHighAPI_Double>& angle,
146                         const ModelHighAPI_Selection& center,
147                         const ModelHighAPI_Selection& start,
148                         const ModelHighAPI_Selection& end,
149                         const bool keepSubResults)
150 {
151   std::shared_ptr<ModelAPI_Feature> aFeature = part->addFeature(FeaturesAPI_Rotation::ID());
152   if (!keepSubResults)
153     aFeature->data()->setVersion("");
154
155   bool byAxis = false;
156   bool byPoints = false;
157   ModelHighAPI_Selection aPoints[3];
158   if (angle.first.variantType() == ModelHighAPI_Selection::VT_Empty) {
159     if (axis.variantType() == ModelHighAPI_Selection::VT_Empty) {
160       byPoints = true;
161       aPoints[0] = center;
162       aPoints[1] = start;
163       aPoints[2] = end;
164     }
165     else
166       byAxis = true;
167   }
168   else {
169     byPoints = true;
170     aPoints[0] = axis;
171     aPoints[1] = angle.first;
172     aPoints[2] = center;
173   }
174
175
176   RotationPtr aRotation;
177   if (byAxis)
178     aRotation.reset(new FeaturesAPI_Rotation(aFeature, objects, axis, angle.second));
179   else if (byPoints) {
180     aRotation.reset(new FeaturesAPI_Rotation(aFeature, objects,
181                                              aPoints[0], aPoints[1], aPoints[2]));
182   }
183   return aRotation;
184 }