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>
35 #include <ModelAPI_Tools.h>
37 static const std::string SCALE_VERSION_1("v9.5");
40 //=================================================================================================
41 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
45 //=================================================================================================
46 void FeaturesPlugin_Scale::initAttributes()
48 data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
49 ModelAPI_AttributeString::typeId());
51 AttributeSelectionListPtr aSelection =
52 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
53 FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
55 data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
56 ModelAPI_AttributeSelection::typeId());
58 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
59 ModelAPI_AttributeDouble::typeId());
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());
68 if (!aSelection->isInitialized()) {
69 // new feature, not read from file
70 data()->setVersion(SCALE_VERSION_1);
74 //=================================================================================================
75 void FeaturesPlugin_Scale::execute()
77 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
78 std::string aMethodType = aMethodTypeAttr->value();
80 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
81 performScaleByFactor();
83 else if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
84 performScaleByDimensions();
88 //=================================================================================================
89 void FeaturesPlugin_Scale::performScaleByFactor()
91 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
94 GeomAPI_ShapeHierarchy anObjects;
95 std::list<ResultPtr> aParts;
96 std::string theTextureFile;
97 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
98 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
99 anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
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();
109 aShape = anObjRef->context()->shape();
112 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
116 // Getting scale factor
117 double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
119 // Collect transformation for each object
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));
128 // Checking that the algorithm worked properly.
129 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
134 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
135 aMakeShapeList->appendAlgo(aScaleAlgo);
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 aResultBody->setTextureFile(theTextureFile);
148 setResult(aResultBody, aResultIndex++);
151 // Remove the rest results if there were produced in the previous pass.
152 removeResults(aResultIndex);
155 //=================================================================================================
156 void FeaturesPlugin_Scale::performScaleByDimensions()
158 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
161 GeomAPI_ShapeHierarchy anObjects;
162 std::list<ResultPtr> aParts;
163 std::string theTextureFile;
164 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
165 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
166 anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
169 // Getting the center point
170 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
171 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
172 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
173 if (anObjRef.get() != NULL) {
174 GeomShapePtr aShape = anObjRef->value();
176 aShape = anObjRef->context()->shape();
179 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
183 // Getting dimensions
184 double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
185 double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
186 double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
188 // Collect transformation for each object
190 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
191 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
192 anObjectsIt != anObjects.end(); ++anObjectsIt) {
193 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
194 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
200 // Checking that the algorithm worked properly.
201 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
206 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
207 aMakeShapeList->appendAlgo(aScaleAlgo);
210 // Build results of the scaling
211 int aResultIndex = 0;
212 const ListOfShape& anOriginalShapes = anObjects.objects();
213 ListOfShape aTopLevel;
214 anObjects.topLevelObjects(aTopLevel);
215 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
216 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
217 ModelAPI_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
218 aMakeShapeList, *anIt, "Scaled");
219 aResultBody->setTextureFile(theTextureFile);
220 setResult(aResultBody, aResultIndex++);
223 // Remove the rest results if there were produced in the previous pass.
224 removeResults(aResultIndex);