1 // Copyright (C) 2014-2019 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 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
96 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
97 anObjSelList, isKeepSubShapes, anObjects, aParts))
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();
107 aShape = anObjRef->context()->shape();
110 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
114 // Getting scale factor
115 double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
117 // Collect transformation for each object
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));
126 // Checking that the algorithm worked properly.
127 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
132 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
133 aMakeShapeList->appendAlgo(aScaleAlgo);
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++);
148 // Remove the rest results if there were produced in the previous pass.
149 removeResults(aResultIndex);
152 //=================================================================================================
153 void FeaturesPlugin_Scale::performScaleByDimensions()
155 bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
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))
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();
172 aShape = anObjRef->context()->shape();
175 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
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();
184 // Collect transformation for each object
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,
196 // Checking that the algorithm worked properly.
197 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
202 anObjects.markModified(aBaseShape, aScaleAlgo->shape());
203 aMakeShapeList->appendAlgo(aScaleAlgo);
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++);
218 // Remove the rest results if there were produced in the previous pass.
219 removeResults(aResultIndex);