Salome HOME
58188025ebc697bab1bdd3f3536ac855d42538f4
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_MultiRotation.cpp
1 // Copyright (C) 2017-2023  CEA, EDF
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 // File:        FeaturesAPI_MultiRotation.cpp
21 // Created:     04 Apr 2017
22 // Author:      Clarisse Genrault (CEA)
23
24 #include <FeaturesAPI_MultiRotation.h>
25
26 #include <ModelHighAPI_Double.h>
27 #include <ModelHighAPI_Dumper.h>
28 #include <ModelHighAPI_Tools.h>
29
30 //==================================================================================================
31 FeaturesAPI_MultiRotation::FeaturesAPI_MultiRotation(
32   const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
34 {
35   initialize();
36 }
37
38
39 //==================================================================================================
40 FeaturesAPI_MultiRotation::FeaturesAPI_MultiRotation(
41   const std::shared_ptr<ModelAPI_Feature>& theFeature,
42   const std::list<ModelHighAPI_Selection>& theMainObjects,
43   const ModelHighAPI_Selection& theAxis,
44   const ModelHighAPI_Integer& theNumber)
45 : ModelHighAPI_Interface(theFeature)
46 {
47   if(initialize()) {
48     fillAttribute(theMainObjects, mainObjects());
49     fillAttribute(theAxis, axisAngular());
50     fillAttribute("",useStepAngular());
51     setNumberAngular(theNumber);
52   }
53 }
54
55 //==================================================================================================
56 FeaturesAPI_MultiRotation::FeaturesAPI_MultiRotation(
57   const std::shared_ptr<ModelAPI_Feature>& theFeature,
58   const std::list<ModelHighAPI_Selection>& theMainObjects,
59   const ModelHighAPI_Selection& theAxis,
60   const ModelHighAPI_Double& theStep,
61   const ModelHighAPI_Integer& theNumber)
62 : ModelHighAPI_Interface(theFeature)
63 {
64   if(initialize()) {
65     fillAttribute(theMainObjects, mainObjects());
66     fillAttribute(theAxis, axisAngular());
67     fillAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),useStepAngular());
68     fillAttribute(theStep, stepAngular());
69     setNumberAngular(theNumber);
70   }
71 }
72
73 //==================================================================================================
74 FeaturesAPI_MultiRotation::~FeaturesAPI_MultiRotation()
75 {
76 }
77
78 //==================================================================================================
79 void FeaturesAPI_MultiRotation::setMainObjects(
80   const std::list<ModelHighAPI_Selection>& theMainObjects)
81 {
82   fillAttribute(theMainObjects, mainObjects());
83
84   execute();
85 }
86
87 //==================================================================================================
88 void FeaturesAPI_MultiRotation::setAxisAngular(const ModelHighAPI_Selection& theAxis)
89 {
90   fillAttribute(theAxis, axisAngular());
91
92   execute();
93 }
94
95 //==================================================================================================
96 void FeaturesAPI_MultiRotation::setStepAngular(const ModelHighAPI_Double& theStep)
97 {
98   fillAttribute(theStep, stepAngular());
99
100   execute();
101 }
102
103 //==================================================================================================
104 void FeaturesAPI_MultiRotation::setNumberAngular(const ModelHighAPI_Integer& theNumber)
105 {
106   fillAttribute(theNumber, nbAngular());
107
108   execute();
109 }
110
111 //==================================================================================================
112 void FeaturesAPI_MultiRotation::dump(ModelHighAPI_Dumper& theDumper) const
113 {
114   FeaturePtr aBase = feature();
115   const std::string& aDocName = theDumper.name(aBase->document());
116
117   AttributeSelectionListPtr anAttrObjects =
118     aBase->selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
119   theDumper << aBase << " = model.addMultiRotation(" << aDocName << ", " << anAttrObjects;
120
121   AttributeSelectionPtr anAttrAxis =
122     aBase->selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
123   theDumper << ", " << anAttrAxis;
124
125   if (aBase->string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->isInitialized()
126       && !aBase->string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value().empty()) {
127     AttributeDoublePtr anAttrStepAngular =
128       aBase->real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID());
129       theDumper << ", " << anAttrStepAngular;
130   }
131
132   AttributeIntegerPtr anAttrNumberAngular =
133     aBase->integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID());
134   theDumper << ", " << anAttrNumberAngular;
135
136   if (!aBase->data()->version().empty())
137     theDumper << ", keepSubResults = True";
138
139   theDumper << ")" << std::endl;
140 }
141
142 //==================================================================================================
143 MultiRotationPtr addMultiRotation(const std::shared_ptr<ModelAPI_Document>& thePart,
144                                   const std::list<ModelHighAPI_Selection>& theMainObjects,
145                                   const ModelHighAPI_Selection& theAxis,
146                                   const ModelHighAPI_Double& theStep,
147                                   const ModelHighAPI_Integer& theNumber,
148                                   const bool keepSubResults)
149 {
150   std::shared_ptr<ModelAPI_Feature> aFeature =
151     thePart->addFeature(FeaturesAPI_MultiRotation::ID());
152   if (!keepSubResults)
153     aFeature->data()->setVersion("");
154
155   MultiRotationPtr aResult;
156   if (theNumber.string().empty()) {
157     // rotate for the whole circle
158     double aStepVal = theStep.value();
159     std::wstring aStepStr = theStep.string();
160     std::wostringstream aStepValAsStr;
161     aStepValAsStr << aStepVal;
162
163     ModelHighAPI_Integer aNumber = aStepStr == aStepValAsStr.str()
164                                  ? ModelHighAPI_Integer((int)aStepVal)
165                                  : ModelHighAPI_Integer(aStepStr);
166     aResult.reset(new FeaturesAPI_MultiRotation(aFeature, theMainObjects, theAxis, aNumber));
167   }
168   else {
169     aResult.reset(new FeaturesAPI_MultiRotation(aFeature, theMainObjects,
170                                                 theAxis, theStep, theNumber));
171   }
172   return aResult;
173 }