Salome HOME
d16460bf212101f622f9d4a6b56c45ef974abab0
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Translation.cpp
1 // Copyright (C) 2014-2023  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_Translation.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 //==================================================================================================
26 FeaturesAPI_Translation::FeaturesAPI_Translation(
27   const std::shared_ptr<ModelAPI_Feature>& theFeature)
28 : ModelHighAPI_Interface(theFeature)
29 {
30   initialize();
31 }
32
33 //==================================================================================================
34 FeaturesAPI_Translation::FeaturesAPI_Translation(
35   const std::shared_ptr<ModelAPI_Feature>& theFeature,
36   const std::list<ModelHighAPI_Selection>& theMainObjects,
37   const ModelHighAPI_Selection& theAxisObject,
38   const ModelHighAPI_Double& theDistance)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   if(initialize()) {
42     fillAttribute(theMainObjects, mymainObjects);
43     setAxisAndDistance(theAxisObject, theDistance);
44   }
45 }
46
47 //==================================================================================================
48 FeaturesAPI_Translation::FeaturesAPI_Translation(
49   const std::shared_ptr<ModelAPI_Feature>& theFeature,
50   const std::list<ModelHighAPI_Selection>& theMainObjects,
51   const ModelHighAPI_Double& theDx,
52   const ModelHighAPI_Double& theDy,
53   const ModelHighAPI_Double& theDz)
54 : ModelHighAPI_Interface(theFeature)
55 {
56   if(initialize()) {
57     fillAttribute(theMainObjects, mymainObjects);
58     setDimensions(theDx, theDy, theDz);
59   }
60 }
61
62 //==================================================================================================
63 FeaturesAPI_Translation::FeaturesAPI_Translation(
64   const std::shared_ptr<ModelAPI_Feature>& theFeature,
65   const std::list<ModelHighAPI_Selection>& theMainObjects,
66   const ModelHighAPI_Selection& theStartPoint,
67   const ModelHighAPI_Selection& theEndPoint)
68 : ModelHighAPI_Interface(theFeature)
69 {
70   if(initialize()) {
71     fillAttribute(theMainObjects, mymainObjects);
72     setPoints(theStartPoint, theEndPoint);
73   }
74 }
75
76 //==================================================================================================
77 FeaturesAPI_Translation::~FeaturesAPI_Translation()
78 {
79
80 }
81
82 //==================================================================================================
83 void FeaturesAPI_Translation::setMainObjects(
84   const std::list<ModelHighAPI_Selection>& theMainObjects)
85 {
86   fillAttribute(theMainObjects, mymainObjects);
87
88   execute();
89 }
90
91 //==================================================================================================
92 void FeaturesAPI_Translation::setAxisAndDistance(const ModelHighAPI_Selection& theAxisObject,
93                                                  const ModelHighAPI_Double& theDistance)
94 {
95   fillAttribute(FeaturesPlugin_Translation::CREATION_METHOD_BY_DISTANCE(), mycreationMethod);
96   fillAttribute(theAxisObject, myaxisObject);
97   fillAttribute(theDistance, mydistance);
98
99   execute();
100 }
101
102 //==================================================================================================
103 void FeaturesAPI_Translation::setDimensions(const ModelHighAPI_Double& theDx,
104                                             const ModelHighAPI_Double& theDy,
105                                             const ModelHighAPI_Double& theDz)
106 {
107   fillAttribute(FeaturesPlugin_Translation::CREATION_METHOD_BY_DIMENSIONS(), mycreationMethod);
108   fillAttribute(theDx, mydx);
109   fillAttribute(theDy, mydy);
110   fillAttribute(theDz, mydz);
111
112   execute();
113 }
114
115 //==================================================================================================
116 void FeaturesAPI_Translation::setPoints(const ModelHighAPI_Selection& theStartPoint,
117                                         const ModelHighAPI_Selection& theEndPoint)
118 {
119   fillAttribute(FeaturesPlugin_Translation::CREATION_METHOD_BY_TWO_POINTS(), mycreationMethod);
120   fillAttribute(theStartPoint, mystartPoint);
121   fillAttribute(theEndPoint, myendPoint);
122
123   execute();
124 }
125
126 //==================================================================================================
127 void FeaturesAPI_Translation::dump(ModelHighAPI_Dumper& theDumper) const
128 {
129   FeaturePtr aBase = feature();
130   const std::string& aDocName = theDumper.name(aBase->document());
131
132   AttributeSelectionListPtr anAttrObjects =
133     aBase->selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
134   theDumper << aBase << " = model.addTranslation(" << aDocName << ", " << anAttrObjects;
135
136   std::string aCreationMethod =
137     aBase->string(FeaturesPlugin_Translation::CREATION_METHOD())->value();
138
139   if (aCreationMethod == FeaturesPlugin_Translation::CREATION_METHOD_BY_DISTANCE()) {
140     AttributeSelectionPtr anAttrAxis =
141       aBase->selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
142     AttributeDoublePtr anAttrDistance =
143       aBase->real(FeaturesPlugin_Translation::DISTANCE_ID());
144     theDumper << ", axis = " << anAttrAxis << ", distance = " << anAttrDistance;
145   } else if (aCreationMethod == FeaturesPlugin_Translation::CREATION_METHOD_BY_DIMENSIONS()) {
146     AttributeDoublePtr anAttrDx = aBase->real(FeaturesPlugin_Translation::DX_ID());
147     AttributeDoublePtr anAttrDy = aBase->real(FeaturesPlugin_Translation::DY_ID());
148     AttributeDoublePtr anAttrDz = aBase->real(FeaturesPlugin_Translation::DZ_ID());
149     theDumper << ", vector = [" << anAttrDx << ", " << anAttrDy << ", " << anAttrDz << "]";
150   } else if (aCreationMethod == FeaturesPlugin_Translation::CREATION_METHOD_BY_TWO_POINTS()) {
151     AttributeSelectionPtr anAttrStartPoint =
152       aBase->selection(FeaturesPlugin_Translation::START_POINT_ID());
153     AttributeSelectionPtr anAttrEndPoint =
154       aBase->selection(FeaturesPlugin_Translation::END_POINT_ID());
155     theDumper << ", startPoint = " << anAttrStartPoint << ", endPoint = " << anAttrEndPoint;
156   }
157
158   if (!aBase->data()->version().empty())
159     theDumper << ", keepSubResults = True";
160
161   theDumper << ")" << std::endl;
162 }
163
164 //==================================================================================================
165 TranslationPtr addTranslation(
166     const std::shared_ptr<ModelAPI_Document>& part,
167     const std::list<ModelHighAPI_Selection>& objects,
168     const std::pair<ModelHighAPI_Selection, ModelHighAPI_Double>& deprecated1,
169     const std::pair<ModelHighAPI_Selection, ModelHighAPI_Double>& deprecated2,
170     const std::pair<ModelHighAPI_Selection, ModelHighAPI_Double>& deprecated3,
171     const ModelHighAPI_Selection& axis, const ModelHighAPI_Double& distance,
172     const std::list<ModelHighAPI_Double>& vector,
173     const ModelHighAPI_Selection& startPoint, const ModelHighAPI_Selection& endPoint,
174     const bool keepSubResults)
175 {
176   std::shared_ptr<ModelAPI_Feature> aFeature = part->addFeature(FeaturesAPI_Translation::ID());
177   if (!keepSubResults)
178     aFeature->data()->setVersion("");
179
180   static const int VEC_SIZE = 3;
181
182   bool byAxis = axis.variantType() != ModelHighAPI_Selection::VT_Empty;
183   bool byVector = vector.size() == VEC_SIZE;
184   bool byPoints = startPoint.variantType() != ModelHighAPI_Selection::VT_Empty &&
185                   endPoint.variantType() != ModelHighAPI_Selection::VT_Empty;
186
187   ModelHighAPI_Selection firstSel, secondSel;
188   ModelHighAPI_Double values[VEC_SIZE];
189   if (byAxis) {
190     firstSel = axis;
191     values[0] = distance;
192   }
193   else if (byVector) {
194     std::list<ModelHighAPI_Double>::const_iterator it = vector.begin();
195     for (ModelHighAPI_Double* vIt = values; it != vector.end(); ++vIt, ++it)
196       *vIt = *it;
197   }
198   else if (byPoints) {
199     firstSel = startPoint;
200     secondSel = endPoint;
201   }
202   else {
203     byVector = deprecated1.first.variantType() == ModelHighAPI_Selection::VT_Empty;
204     if (byVector) {
205       values[0] = deprecated1.second;
206       values[1] = deprecated2.second;
207       values[2] = deprecated3.second;
208     }
209     else {
210       firstSel = deprecated1.first;
211       secondSel = deprecated2.first;
212       values[0] = deprecated2.second;
213       if (secondSel.variantType() == ModelHighAPI_Selection::VT_Empty)
214         byAxis = true;
215       else
216         byPoints = true;
217     }
218   }
219
220   TranslationPtr aTranslation;
221   if (byAxis)
222     aTranslation.reset(new FeaturesAPI_Translation(aFeature, objects, firstSel, values[0]));
223   else if (byVector) {
224     aTranslation.reset(new FeaturesAPI_Translation(aFeature, objects,
225                                                    values[0], values[1], values[2]));
226   }
227   else if (byPoints)
228     aTranslation.reset(new FeaturesAPI_Translation(aFeature, objects, firstSel, secondSel));
229
230   return aTranslation;
231 }