Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
1 // Copyright (C) 2014-2019  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 <FeaturesPlugin_Scale.h>
21
22 #include <GeomAlgoAPI_PointBuilder.h>
23 #include <GeomAlgoAPI_Tools.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_ResultPart.h>
30
31 #include <FeaturesPlugin_Tools.h>
32
33 //=================================================================================================
34 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
35 {
36 }
37
38 //=================================================================================================
39 void FeaturesPlugin_Scale::initAttributes()
40 {
41   data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
42                        ModelAPI_AttributeString::typeId());
43
44   AttributeSelectionListPtr aSelection =
45     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
46     FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
47
48   data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
49                        ModelAPI_AttributeSelection::typeId());
50
51   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
52                        ModelAPI_AttributeDouble::typeId());
53
54   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID(),
55                        ModelAPI_AttributeDouble::typeId());
56   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID(),
57                        ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID(),
59                        ModelAPI_AttributeDouble::typeId());
60 }
61
62 //=================================================================================================
63 void FeaturesPlugin_Scale::execute()
64 {
65   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
66   std::string aMethodType = aMethodTypeAttr->value();
67
68   if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
69     performScaleByFactor();
70   }
71
72   if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
73     performScaleByDimensions();
74   }
75 }
76
77 //=================================================================================================
78 void FeaturesPlugin_Scale::performScaleByFactor()
79 {
80   // Getting objects.
81   ListOfShape anObjects;
82   std::list<ResultPtr> aContextes;
83   AttributeSelectionListPtr anObjectsSelList =
84     selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
85   if (anObjectsSelList->size() == 0) {
86     return;
87   }
88   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
89     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
90       anObjectsSelList->value(anObjectsIndex);
91     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
92     if(!anObject.get()) { // may be for not-activated parts
93       return;
94     }
95     anObjects.push_back(anObject);
96     aContextes.push_back(anObjectAttr->context());
97   }
98
99   // Getting the center point
100   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
101   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
102     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
103   if (anObjRef.get() != NULL) {
104     GeomShapePtr aShape = anObjRef->value();
105     if (!aShape.get()) {
106       aShape = anObjRef->context()->shape();
107     }
108     if (aShape) {
109       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
110     }
111   }
112
113   // Getting scale factor
114   double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
115
116   // Moving each object.
117   std::string anError;
118   int aResultIndex = 0;
119   std::list<ResultPtr>::iterator aContext = aContextes.begin();
120   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
121         anObjectsIt++, aContext++) {
122     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
123     std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
124       new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
125
126     if (!aScaleAlgo->check()) {
127       setError(aScaleAlgo->getError());
128       return;
129     }
130
131     aScaleAlgo->build();
132
133     // Checking that the algorithm worked properly.
134     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
135       setError(anError);
136       break;
137     }
138
139     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
140
141     ListOfShape aShapes;
142     aShapes.push_back(aBaseShape);
143     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
144                                              aShapes,
145                                              ListOfShape(),
146                                              aScaleAlgo,
147                                              aScaleAlgo->shape(),
148                                              "Scaled");
149     setResult(aResultBody, aResultIndex);
150     aResultIndex++;
151   }
152
153   // Remove the rest results if there were produced in the previous pass.
154   removeResults(aResultIndex);
155 }
156
157 //=================================================================================================
158 void FeaturesPlugin_Scale::performScaleByDimensions()
159 {
160   // Getting objects.
161   ListOfShape anObjects;
162   std::list<ResultPtr> aContextes;
163   AttributeSelectionListPtr anObjectsSelList =
164     selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
165   if (anObjectsSelList->size() == 0) {
166     return;
167   }
168   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
169     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
170       anObjectsSelList->value(anObjectsIndex);
171     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
172     if(!anObject.get()) { // may be for not-activated parts
173       return;
174     }
175     anObjects.push_back(anObject);
176     aContextes.push_back(anObjectAttr->context());
177   }
178
179   // Getting the center point
180   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
181   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
182     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
183   if (anObjRef.get() != NULL) {
184     GeomShapePtr aShape = anObjRef->value();
185     if (!aShape.get()) {
186       aShape = anObjRef->context()->shape();
187     }
188     if (aShape) {
189       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
190     }
191   }
192
193   // Getting dimensions
194   double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
195   double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
196   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
197
198   // Moving each object.
199   std::string anError;
200   int aResultIndex = 0;
201   std::list<ResultPtr>::iterator aContext = aContextes.begin();
202   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
203         anObjectsIt++, aContext++) {
204     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
205     std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
206                                                                         aCenterPoint,
207                                                                         aScaleFactorX,
208                                                                         aScaleFactorY,
209                                                                         aScaleFactorZ));
210
211     if (!aScaleAlgo->check()) {
212       setError(aScaleAlgo->getError());
213       return;
214     }
215
216     aScaleAlgo->build();
217
218     // Checking that the algorithm worked properly.
219     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
220       setError(anError);
221       break;
222     }
223
224     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
225
226     ListOfShape aShapes;
227     aShapes.push_back(aBaseShape);
228     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
229                                              aShapes,
230                                              ListOfShape(),
231                                              aScaleAlgo,
232                                              aScaleAlgo->shape(),
233                                              "Scaled");
234     setResult(aResultBody, aResultIndex);
235     aResultIndex++;
236   }
237
238   // Remove the rest results if there were produced in the previous pass.
239   removeResults(aResultIndex);
240 }