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>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_ResultPart.h>
31 #include <FeaturesPlugin_Tools.h>
33 //=================================================================================================
34 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
38 //=================================================================================================
39 void FeaturesPlugin_Scale::initAttributes()
41 data()->addAttribute(FeaturesPlugin_Scale::CREATION_METHOD(),
42 ModelAPI_AttributeString::typeId());
44 AttributeSelectionListPtr aSelection =
45 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
46 FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
48 data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
49 ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
52 ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID(),
55 ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID(),
57 ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID(),
59 ModelAPI_AttributeDouble::typeId());
62 //=================================================================================================
63 void FeaturesPlugin_Scale::execute()
65 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Scale::CREATION_METHOD());
66 std::string aMethodType = aMethodTypeAttr->value();
68 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
69 performScaleByFactor();
72 if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
73 performScaleByDimensions();
77 //=================================================================================================
78 void FeaturesPlugin_Scale::performScaleByFactor()
81 ListOfShape anObjects;
82 std::list<ResultPtr> aContextes;
83 AttributeSelectionListPtr anObjectsSelList =
84 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
85 if (anObjectsSelList->size() == 0) {
88 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
89 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
90 anObjectsSelList->value(anObjectsIndex);
91 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
92 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.
118 int aResultIndex = 0;
119 std::list<ResultPtr>::iterator aContext = aContextes.begin();
120 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
121 anObjectsIt++, aContext++) {
122 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
123 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
124 new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
126 if (!aScaleAlgo->check()) {
127 setError(aScaleAlgo->getError());
133 // Checking that the algorithm worked properly.
134 if(!aScaleAlgo->isDone()) {
135 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
136 setError(aFeatureError);
139 if(aScaleAlgo->shape()->isNull()) {
140 static const std::string aShapeError = "Error: Resulting shape is Null.";
141 setError(aShapeError);
144 if(!aScaleAlgo->isValid()) {
145 std::string aFeatureError = "Error: Resulting shape is not valid.";
146 setError(aFeatureError);
150 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
151 aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
152 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
153 setResult(aResultBody, aResultIndex);
157 // Remove the rest results if there were produced in the previous pass.
158 removeResults(aResultIndex);
161 //=================================================================================================
162 void FeaturesPlugin_Scale::performScaleByDimensions()
165 ListOfShape anObjects;
166 std::list<ResultPtr> aContextes;
167 AttributeSelectionListPtr anObjectsSelList =
168 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
169 if (anObjectsSelList->size() == 0) {
172 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
173 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
174 anObjectsSelList->value(anObjectsIndex);
175 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
176 if(!anObject.get()) { // may be for not-activated parts
180 anObjects.push_back(anObject);
181 aContextes.push_back(anObjectAttr->context());
184 // Getting the center point
185 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
186 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
187 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
188 if (anObjRef.get() != NULL) {
189 GeomShapePtr aShape = anObjRef->value();
191 aShape = anObjRef->context()->shape();
194 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
198 // Getting dimensions
199 double aScaleFactorX = real(FeaturesPlugin_Scale::SCALE_FACTOR_X_ID())->value();
200 double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
201 double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
203 // Moving each object.
204 int aResultIndex = 0;
205 std::list<ResultPtr>::iterator aContext = aContextes.begin();
206 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
207 anObjectsIt++, aContext++) {
208 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
209 std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
215 if (!aScaleAlgo->check()) {
216 setError(aScaleAlgo->getError());
222 // Checking that the algorithm worked properly.
223 if(!aScaleAlgo->isDone()) {
224 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
225 setError(aFeatureError);
228 if(aScaleAlgo->shape()->isNull()) {
229 static const std::string aShapeError = "Error: Resulting shape is Null.";
230 setError(aShapeError);
233 if(!aScaleAlgo->isValid()) {
234 std::string aFeatureError = "Error: Resulting shape is not valid.";
235 setError(aFeatureError);
239 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
240 aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
241 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
242 setResult(aResultBody, aResultIndex);
246 // Remove the rest results if there were produced in the previous pass.
247 removeResults(aResultIndex);