1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_Scale.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24 #include <GeomAlgoAPI_Tools.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_ResultPart.h>
32 #include <FeaturesPlugin_Tools.h>
34 //=================================================================================================
35 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
39 //=================================================================================================
40 void FeaturesPlugin_Scale::initAttributes()
42 data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
43 ModelAPI_AttributeString::typeId());
45 AttributeSelectionListPtr aSelection =
46 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
47 FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
49 data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
50 ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
53 ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID(),
56 ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID(),
58 ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID(),
60 ModelAPI_AttributeDouble::typeId());
63 //=================================================================================================
64 void FeaturesPlugin_Scale::execute()
66 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
67 std::string aMethodType = aMethodTypeAttr->value();
69 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
70 performScaleByFactor();
73 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
74 performScaleByDimensions();
78 //=================================================================================================
79 void FeaturesPlugin_Scale::performScaleByFactor()
82 ListOfShape anObjects;
83 std::list<ResultPtr> aContextes;
84 AttributeSelectionListPtr anObjectsSelList =
85 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
86 if (anObjectsSelList->size() == 0) {
89 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
90 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
91 anObjectsSelList->value(anObjectsIndex);
92 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
93 if(!anObject.get()) { // may be for not-activated parts
96 anObjects.push_back(anObject);
97 aContextes.push_back(anObjectAttr->context());
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 // Moving each object.
119 int aResultIndex = 0;
120 std::list<ResultPtr>::iterator aContext = aContextes.begin();
121 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
122 anObjectsIt++, aContext++) {
123 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
124 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
125 new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
127 if (!aScaleAlgo->check()) {
128 setError(aScaleAlgo->getError());
134 // Checking that the algorithm worked properly.
135 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
140 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
141 aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
142 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
143 setResult(aResultBody, aResultIndex);
147 // Remove the rest results if there were produced in the previous pass.
148 removeResults(aResultIndex);
151 //=================================================================================================
152 void FeaturesPlugin_Scale::performScaleByDimensions()
155 ListOfShape anObjects;
156 std::list<ResultPtr> aContextes;
157 AttributeSelectionListPtr anObjectsSelList =
158 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
159 if (anObjectsSelList->size() == 0) {
162 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
163 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
164 anObjectsSelList->value(anObjectsIndex);
165 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
166 if(!anObject.get()) { // may be for not-activated parts
169 anObjects.push_back(anObject);
170 aContextes.push_back(anObjectAttr->context());
173 // Getting the center point
174 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
175 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
176 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
177 if (anObjRef.get() != NULL) {
178 GeomShapePtr aShape = anObjRef->value();
180 aShape = anObjRef->context()->shape();
183 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
187 // Getting dimensions
188 double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
189 double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
190 double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
192 // Moving each object.
194 int aResultIndex = 0;
195 std::list<ResultPtr>::iterator aContext = aContextes.begin();
196 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
197 anObjectsIt++, aContext++) {
198 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
199 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
205 if (!aScaleAlgo->check()) {
206 setError(aScaleAlgo->getError());
212 // Checking that the algorithm worked properly.
213 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
218 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
219 aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
220 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
221 setResult(aResultBody, aResultIndex);
225 // Remove the rest results if there were produced in the previous pass.
226 removeResults(aResultIndex);