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_Rotation.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
29 #include <GeomAlgoAPI_PointBuilder.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
36 #include <FeaturesPlugin_Tools.h>
38 //=================================================================================================
39 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
43 //=================================================================================================
44 void FeaturesPlugin_Rotation::initAttributes()
46 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
47 ModelAPI_AttributeString::typeId());
49 AttributeSelectionListPtr aSelection =
50 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
51 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
53 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
54 ModelAPI_AttributeSelection::typeId());
55 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
58 ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
60 ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
62 ModelAPI_AttributeSelection::typeId());
65 //=================================================================================================
66 void FeaturesPlugin_Rotation::execute()
68 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
69 std::string aMethodType = aMethodTypeAttr->value();
71 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
72 performTranslationByAxisAndAngle();
75 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
76 performTranslationByThreePoints();
80 //=================================================================================================
81 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
84 ListOfShape anObjects;
85 std::list<ResultPtr> aContextes;
86 AttributeSelectionListPtr anObjectsSelList =
87 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
88 if (anObjectsSelList->size() == 0) {
91 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
92 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
93 anObjectsSelList->value(anObjectsIndex);
94 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
98 anObjects.push_back(anObject);
99 aContextes.push_back(anObjectAttr->context());
103 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
104 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
105 GeomShapePtr aShape = anObjRef->value();
107 if (anObjRef->context().get()) {
108 aShape = anObjRef->context()->shape();
112 setError(aSelectionError);
117 if (aShape->isEdge())
119 anEdge = aShape->edge();
121 else if (aShape->isCompound())
123 GeomAPI_ShapeIterator anIt(aShape);
124 anEdge = anIt.current()->edge();
128 setError(aSelectionError);
134 setError(aSelectionError);
138 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
139 anEdge->line()->direction()));
142 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
144 // Rotating each object.
145 int aResultIndex = 0;
146 std::list<ResultPtr>::iterator aContext = aContextes.begin();
147 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
148 anObjectsIt++, aContext++) {
149 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
150 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
154 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
155 aTrsf->setRotation(anAxis, anAngle);
156 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
157 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
158 aResultPart->setTrsf(*aContext, aTrsf);
159 setResult(aResultPart, aResultIndex);
161 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
165 if (!aRotationAlgo->check()) {
166 setError(aRotationAlgo->getError());
170 aRotationAlgo->build();
172 // Checking that the algorithm worked properly.
173 if(!aRotationAlgo->isDone()) {
174 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
175 setError(aFeatureError);
178 if(aRotationAlgo->shape()->isNull()) {
179 static const std::string aShapeError = "Error: Resulting shape is Null.";
180 setError(aShapeError);
183 if(!aRotationAlgo->isValid()) {
184 std::string aFeatureError = "Error: Resulting shape is not valid.";
185 setError(aFeatureError);
189 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
190 aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
191 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
192 setResult(aResultBody, aResultIndex);
197 // Remove the rest results if there were produced in the previous pass.
198 removeResults(aResultIndex);
201 //=================================================================================================
202 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
205 ListOfShape anObjects;
206 std::list<ResultPtr> aContextes;
207 AttributeSelectionListPtr anObjectsSelList =
208 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
209 if (anObjectsSelList->size() == 0) {
212 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
213 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
214 anObjectsSelList->value(anObjectsIndex);
215 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
216 if(!anObject.get()) {
219 anObjects.push_back(anObject);
220 aContextes.push_back(anObjectAttr->context());
223 // Getting the center point and two points (start and end)
224 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
225 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
226 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
227 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
228 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
229 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
230 selection(FeaturesPlugin_Rotation::START_POINT_ID());
231 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
232 selection(FeaturesPlugin_Rotation::END_POINT_ID());
233 if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
234 && (anEndPointRef.get() != NULL)) {
235 GeomShapePtr aCenterShape = aCenterRef->value();
236 if (!aCenterShape.get())
237 aCenterShape = aCenterRef->context()->shape();
238 GeomShapePtr aStartShape = aStartPointRef->value();
239 if (!aStartShape.get())
240 aStartShape = aStartPointRef->context()->shape();
241 GeomShapePtr anEndShape = anEndPointRef->value();
242 if (!anEndShape.get())
243 anEndShape = anEndPointRef->context()->shape();
244 if (aStartShape && anEndShape && aCenterShape) {
245 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
246 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
247 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
251 // Rotating each object.
252 int aResultIndex = 0;
253 std::list<ResultPtr>::iterator aContext = aContextes.begin();
254 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
255 anObjectsIt++, aContext++) {
256 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
257 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
261 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
262 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
263 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
264 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
265 aResultPart->setTrsf(*aContext, aTrsf);
266 setResult(aResultPart, aResultIndex);
268 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
273 if (!aRotationAlgo->check()) {
274 setError(aRotationAlgo->getError());
278 aRotationAlgo->build();
280 // Checking that the algorithm worked properly.
281 if(!aRotationAlgo->isDone()) {
282 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
283 setError(aFeatureError);
286 if(aRotationAlgo->shape()->isNull()) {
287 static const std::string aShapeError = "Error : Resulting shape is Null.";
288 setError(aShapeError);
291 if(!aRotationAlgo->isValid()) {
292 std::string aFeatureError = "Error: Resulting shape is not valid.";
293 setError(aFeatureError);
297 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
298 aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
299 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
300 setResult(aResultBody, aResultIndex);