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