Salome HOME
4e854c22a5f491e742f68ad57121d9b4c916382b
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.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 <FeaturesPlugin_Scale.h>
22
23 #include <GeomAlgoAPI_PointBuilder.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       eraseResults();
94       return;
95     }
96     anObjects.push_back(anObject);
97     aContextes.push_back(anObjectAttr->context());
98   }
99
100   // Getting the center point
101   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
102   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
103     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
104   if (anObjRef.get() != NULL) {
105     GeomShapePtr aShape = anObjRef->value();
106     if (!aShape.get()) {
107       aShape = anObjRef->context()->shape();
108     }
109     if (aShape) {
110       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
111     }
112   }
113
114   // Getting scale factor
115   double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
116
117   // Moving each object.
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     GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint, aScaleFactor);
124
125     if (!aScaleAlgo.check()) {
126       setError(aScaleAlgo.getError());
127       return;
128     }
129
130     aScaleAlgo.build();
131
132     // Checking that the algorithm worked properly.
133     if(!aScaleAlgo.isDone()) {
134       static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
135       setError(aFeatureError);
136       break;
137     }
138     if(aScaleAlgo.shape()->isNull()) {
139       static const std::string aShapeError = "Error: Resulting shape is Null.";
140       setError(aShapeError);
141       break;
142     }
143     if(!aScaleAlgo.isValid()) {
144       std::string aFeatureError = "Error: Resulting shape is not valid.";
145       setError(aFeatureError);
146       break;
147     }
148
149     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
150     loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
151     setResult(aResultBody, aResultIndex);
152     aResultIndex++;
153   }
154
155   // Remove the rest results if there were produced in the previous pass.
156   removeResults(aResultIndex);
157 }
158
159 //=================================================================================================
160 void FeaturesPlugin_Scale::performScaleByDimensions()
161 {
162   // Getting objects.
163   ListOfShape anObjects;
164   std::list<ResultPtr> aContextes;
165   AttributeSelectionListPtr anObjectsSelList =
166     selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
167   if (anObjectsSelList->size() == 0) {
168     return;
169   }
170   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
171     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
172       anObjectsSelList->value(anObjectsIndex);
173     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
174     if(!anObject.get()) { // may be for not-activated parts
175       eraseResults();
176       return;
177     }
178     anObjects.push_back(anObject);
179     aContextes.push_back(anObjectAttr->context());
180   }
181
182   // Getting the center point
183   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
184   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
185     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
186   if (anObjRef.get() != NULL) {
187     GeomShapePtr aShape = anObjRef->value();
188     if (!aShape.get()) {
189       aShape = anObjRef->context()->shape();
190     }
191     if (aShape) {
192       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
193     }
194   }
195
196   // Getting dimensions
197   double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
198   double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
199   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
200
201   // Moving each object.
202   int aResultIndex = 0;
203   std::list<ResultPtr>::iterator aContext = aContextes.begin();
204   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
205         anObjectsIt++, aContext++) {
206     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
207     GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint,
208                                  aScaleFactorX, aScaleFactorY, aScaleFactorZ);
209
210     if (!aScaleAlgo.check()) {
211       setError(aScaleAlgo.getError());
212       return;
213     }
214
215     aScaleAlgo.build();
216
217     // Checking that the algorithm worked properly.
218     if(!aScaleAlgo.isDone()) {
219       static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
220       setError(aFeatureError);
221       break;
222     }
223     if(aScaleAlgo.shape()->isNull()) {
224       static const std::string aShapeError = "Error: Resulting shape is Null.";
225       setError(aShapeError);
226       break;
227     }
228     if(!aScaleAlgo.isValid()) {
229       std::string aFeatureError = "Error: Resulting shape is not valid.";
230       setError(aFeatureError);
231       break;
232     }
233
234     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
235     loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
236     setResult(aResultBody, aResultIndex);
237     aResultIndex++;
238   }
239
240   // Remove the rest results if there were produced in the previous pass.
241   removeResults(aResultIndex);
242 }
243
244 //=================================================================================================
245 void FeaturesPlugin_Scale::loadNamingDS(GeomAlgoAPI_Scale& theScaleAlgo,
246                                         std::shared_ptr<ModelAPI_ResultBody> theResultBody,
247                                         std::shared_ptr<GeomAPI_Shape> theBaseShape)
248 {
249   // Store and name the result.
250   theResultBody->storeModified(theBaseShape, theScaleAlgo.shape());
251
252   // Name the faces
253   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theScaleAlgo.mapOfSubShapes();
254   std::string aScaledName = "Scaled";
255   FeaturesPlugin_Tools::storeModifiedShapes(theScaleAlgo, theResultBody,
256                                             theBaseShape, 1, 2, 3, aScaledName,
257                                             *aSubShapes.get());
258 }