Salome HOME
Finalization of the sketch drawer "Create dimensions" flag implemented
[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     aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
142     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
143     setResult(aResultBody, aResultIndex);
144     aResultIndex++;
145   }
146
147   // Remove the rest results if there were produced in the previous pass.
148   removeResults(aResultIndex);
149 }
150
151 //=================================================================================================
152 void FeaturesPlugin_Scale::performScaleByDimensions()
153 {
154   // Getting objects.
155   ListOfShape anObjects;
156   std::list<ResultPtr> aContextes;
157   AttributeSelectionListPtr anObjectsSelList =
158     selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
159   if (anObjectsSelList->size() == 0) {
160     return;
161   }
162   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
163     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
164       anObjectsSelList->value(anObjectsIndex);
165     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
166     if(!anObject.get()) { // may be for not-activated parts
167       return;
168     }
169     anObjects.push_back(anObject);
170     aContextes.push_back(anObjectAttr->context());
171   }
172
173   // Getting the center point
174   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
175   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
176     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
177   if (anObjRef.get() != NULL) {
178     GeomShapePtr aShape = anObjRef->value();
179     if (!aShape.get()) {
180       aShape = anObjRef->context()->shape();
181     }
182     if (aShape) {
183       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
184     }
185   }
186
187   // Getting dimensions
188   double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
189   double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
190   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
191
192   // Moving each object.
193   std::string anError;
194   int aResultIndex = 0;
195   std::list<ResultPtr>::iterator aContext = aContextes.begin();
196   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
197         anObjectsIt++, aContext++) {
198     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
199     std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
200                                                                         aCenterPoint,
201                                                                         aScaleFactorX,
202                                                                         aScaleFactorY,
203                                                                         aScaleFactorZ));
204
205     if (!aScaleAlgo->check()) {
206       setError(aScaleAlgo->getError());
207       return;
208     }
209
210     aScaleAlgo->build();
211
212     // Checking that the algorithm worked properly.
213     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
214       setError(anError);
215       break;
216     }
217
218     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
219     aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
220     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
221     setResult(aResultBody, aResultIndex);
222     aResultIndex++;
223   }
224
225   // Remove the rest results if there were produced in the previous pass.
226   removeResults(aResultIndex);
227 }