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);
143 aShapes.push_back(aBaseShape);
144 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
150 setResult(aResultBody, aResultIndex);
154 // Remove the rest results if there were produced in the previous pass.
155 removeResults(aResultIndex);
158 //=================================================================================================
159 void FeaturesPlugin_Scale::performScaleByDimensions()
162 ListOfShape anObjects;
163 std::list<ResultPtr> aContextes;
164 AttributeSelectionListPtr anObjectsSelList =
165 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
166 if (anObjectsSelList->size() == 0) {
169 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
170 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
171 anObjectsSelList->value(anObjectsIndex);
172 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
173 if(!anObject.get()) { // may be for not-activated parts
176 anObjects.push_back(anObject);
177 aContextes.push_back(anObjectAttr->context());
180 // Getting the center point
181 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
182 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
183 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
184 if (anObjRef.get() != NULL) {
185 GeomShapePtr aShape = anObjRef->value();
187 aShape = anObjRef->context()->shape();
190 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
194 // Getting dimensions
195 double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
196 double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
197 double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
199 // Moving each object.
201 int aResultIndex = 0;
202 std::list<ResultPtr>::iterator aContext = aContextes.begin();
203 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
204 anObjectsIt++, aContext++) {
205 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
206 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
212 if (!aScaleAlgo->check()) {
213 setError(aScaleAlgo->getError());
219 // Checking that the algorithm worked properly.
220 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
225 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
228 aShapes.push_back(aBaseShape);
229 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
235 setResult(aResultBody, aResultIndex);
239 // Remove the rest results if there were produced in the previous pass.
240 removeResults(aResultIndex);