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