1 // Copyright (C) 2014-2022 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 ResultPtr aTextureSource;
97 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
98 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
99 anObjSelList, isKeepSubShapes, anObjects, aParts, aTextureSource))
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 // Copy image data, if any
148 ModelAPI_Tools::copyImageAttribute(aTextureSource, aResultBody);
149 setResult(aResultBody, aResultIndex++);
152 // Remove the rest results if there were produced in the previous pass.
153 removeResults(aResultIndex);
156 //=================================================================================================
157 void FeaturesPlugin_Scale::performScaleByDimensions()
159 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
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))
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();
177 aShape = anObjRef->context()->shape();
180 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
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();
189 // Collect transformation for each object
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,
201 // Checking that the algorithm worked properly.
202 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
207 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
208 aMakeShapeList->appendAlgo(aScaleAlgo);
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++);
225 // Remove the rest results if there were produced in the previous pass.
226 removeResults(aResultIndex);