1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_Scale.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <GeomAPI_Trsf.h>
25 #include <GeomAlgoAPI_MakeShapeList.h>
26 #include <GeomAlgoAPI_PointBuilder.h>
27 #include <GeomAlgoAPI_Scale.h>
28 #include <GeomAlgoAPI_Tools.h>
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>
36 static const std::string SCALE_VERSION_1("v9.5");
39 //=================================================================================================
40 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
44 //=================================================================================================
45 void FeaturesPlugin_Scale::initAttributes()
47 data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
48 ModelAPI_AttributeString::typeId());
50 AttributeSelectionListPtr aSelection =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52 FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
54 data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
55 ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
58 ModelAPI_AttributeDouble::typeId());
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());
67 if (!aSelection->isInitialized()) {
68 // new feature, not read from file
69 data()->setVersion(SCALE_VERSION_1);
73 //=================================================================================================
74 void FeaturesPlugin_Scale::execute()
76 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
77 std::string aMethodType = aMethodTypeAttr->value();
79 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
80 performScaleByFactor();
82 else if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
83 performScaleByDimensions();
87 //=================================================================================================
88 void FeaturesPlugin_Scale::performScaleByFactor()
90 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
93 GeomAPI_ShapeHierarchy anObjects;
94 std::list<ResultPtr> aParts;
95 std::string theTextureFile;
96 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
97 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
98 anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
101 // Getting the center point
102 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
103 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
104 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
105 if (anObjRef.get() != NULL) {
106 GeomShapePtr aShape = anObjRef->value();
108 aShape = anObjRef->context()->shape();
111 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
115 // Getting scale factor
116 double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
118 // Collect transformation for each object
120 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
121 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
122 anObjectsIt != anObjects.end(); ++anObjectsIt) {
123 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
124 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
125 new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
127 // Checking that the algorithm worked properly.
128 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
133 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
134 aMakeShapeList->appendAlgo(aScaleAlgo);
137 // Build results of the scaling
138 int aResultIndex = 0;
139 const ListOfShape& anOriginalShapes = anObjects.objects();
140 ListOfShape aTopLevel;
141 anObjects.topLevelObjects(aTopLevel);
142 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
143 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
144 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
145 aMakeShapeList, *anIt, "Scaled");
146 aResultBody->setTextureFile(theTextureFile);
147 setResult(aResultBody, aResultIndex++);
150 // Remove the rest results if there were produced in the previous pass.
151 removeResults(aResultIndex);
154 //=================================================================================================
155 void FeaturesPlugin_Scale::performScaleByDimensions()
157 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
160 GeomAPI_ShapeHierarchy anObjects;
161 std::list<ResultPtr> aParts;
162 std::string theTextureFile;
163 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
164 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
165 anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
168 // Getting the center point
169 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
170 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
171 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
172 if (anObjRef.get() != NULL) {
173 GeomShapePtr aShape = anObjRef->value();
175 aShape = anObjRef->context()->shape();
178 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
182 // Getting dimensions
183 double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
184 double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
185 double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
187 // Collect transformation for each object
189 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
190 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
191 anObjectsIt != anObjects.end(); ++anObjectsIt) {
192 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
193 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
199 // Checking that the algorithm worked properly.
200 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
205 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
206 aMakeShapeList->appendAlgo(aScaleAlgo);
209 // Build results of the scaling
210 int aResultIndex = 0;
211 const ListOfShape& anOriginalShapes = anObjects.objects();
212 ListOfShape aTopLevel;
213 anObjects.topLevelObjects(aTopLevel);
214 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
215 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
216 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
217 aMakeShapeList, *anIt, "Scaled");
218 aResultBody->setTextureFile(theTextureFile);
219 setResult(aResultBody, aResultIndex++);
222 // Remove the rest results if there were produced in the previous pass.
223 removeResults(aResultIndex);