Salome HOME
Copyright update 2021
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
1 // Copyright (C) 2014-2021  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 #include <FeaturesPlugin_Tools.h>
22
23 #include <GeomAPI_Trsf.h>
24
25 #include <GeomAlgoAPI_MakeShapeList.h>
26 #include <GeomAlgoAPI_PointBuilder.h>
27 #include <GeomAlgoAPI_Scale.h>
28 #include <GeomAlgoAPI_Tools.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultPart.h>
35
36 static const std::string SCALE_VERSION_1("v9.5");
37
38
39 //=================================================================================================
40 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
41 {
42 }
43
44 //=================================================================================================
45 void FeaturesPlugin_Scale::initAttributes()
46 {
47   data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
48                        ModelAPI_AttributeString::typeId());
49
50   AttributeSelectionListPtr aSelection =
51     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52     FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
53
54   data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
55                        ModelAPI_AttributeSelection::typeId());
56
57   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
58                        ModelAPI_AttributeDouble::typeId());
59
60   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID(),
61                        ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID(),
63                        ModelAPI_AttributeDouble::typeId());
64   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID(),
65                        ModelAPI_AttributeDouble::typeId());
66
67   if (!aSelection->isInitialized()) {
68     // new feature, not read from file
69     data()->setVersion(SCALE_VERSION_1);
70   }
71 }
72
73 //=================================================================================================
74 void FeaturesPlugin_Scale::execute()
75 {
76   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
77   std::string aMethodType = aMethodTypeAttr->value();
78
79   if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
80     performScaleByFactor();
81   }
82   else if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
83     performScaleByDimensions();
84   }
85 }
86
87 //=================================================================================================
88 void FeaturesPlugin_Scale::performScaleByFactor()
89 {
90   bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
91
92   // Getting objects.
93   GeomAPI_ShapeHierarchy anObjects;
94   std::list<ResultPtr> aParts;
95   AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
96   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
97        anObjSelList, isKeepSubShapes, anObjects, aParts))
98     return;
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   // Collect transformation for each object
118   std::string anError;
119   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
120   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
121        anObjectsIt != anObjects.end(); ++anObjectsIt) {
122     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
123     std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
124         new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
125
126     // Checking that the algorithm worked properly.
127     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
128       setError(anError);
129       break;
130     }
131
132     anObjects.markModified(aBaseShape, aScaleAlgo->shape());
133     aMakeShapeList->appendAlgo(aScaleAlgo);
134   }
135
136   // Build results of the scaling
137   int aResultIndex = 0;
138   const ListOfShape& anOriginalShapes = anObjects.objects();
139   ListOfShape aTopLevel;
140   anObjects.topLevelObjects(aTopLevel);
141   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
142     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
143     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
144                                              aMakeShapeList, *anIt, "Scaled");
145     setResult(aResultBody, aResultIndex++);
146   }
147
148   // Remove the rest results if there were produced in the previous pass.
149   removeResults(aResultIndex);
150 }
151
152 //=================================================================================================
153 void FeaturesPlugin_Scale::performScaleByDimensions()
154 {
155   bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
156
157   // Getting objects.
158   GeomAPI_ShapeHierarchy anObjects;
159   std::list<ResultPtr> aParts;
160   AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
161   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
162        anObjSelList, isKeepSubShapes, anObjects, aParts))
163     return;
164
165   // Getting the center point
166   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
167   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
168     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
169   if (anObjRef.get() != NULL) {
170     GeomShapePtr aShape = anObjRef->value();
171     if (!aShape.get()) {
172       aShape = anObjRef->context()->shape();
173     }
174     if (aShape) {
175       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
176     }
177   }
178
179   // Getting dimensions
180   double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
181   double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
182   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
183
184   // Collect transformation for each object
185   std::string anError;
186   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
187   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
188        anObjectsIt != anObjects.end(); ++anObjectsIt) {
189     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
190     std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
191                                                                         aCenterPoint,
192                                                                         aScaleFactorX,
193                                                                         aScaleFactorY,
194                                                                         aScaleFactorZ));
195
196     // Checking that the algorithm worked properly.
197     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
198       setError(anError);
199       break;
200     }
201
202     anObjects.markModified(aBaseShape, aScaleAlgo->shape());
203     aMakeShapeList->appendAlgo(aScaleAlgo);
204   }
205
206   // Build results of the scaling
207   int aResultIndex = 0;
208   const ListOfShape& anOriginalShapes = anObjects.objects();
209   ListOfShape aTopLevel;
210   anObjects.topLevelObjects(aTopLevel);
211   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
212     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
213     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
214                                              aMakeShapeList, *anIt, "Scaled");
215     setResult(aResultBody, aResultIndex++);
216   }
217
218   // Remove the rest results if there were produced in the previous pass.
219   removeResults(aResultIndex);
220 }