Salome HOME
Issue #1916: Fatal error when create sketch on body Remove subshapes
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Rotation.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_Rotation.h>
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <ModelAPI_ResultPart.h>
13
14 #include <GeomAPI_Edge.h>
15 #include <GeomAPI_Lin.h>
16
17 #include <FeaturesPlugin_Tools.h>
18
19 //=================================================================================================
20 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
21 {
22 }
23
24 //=================================================================================================
25 void FeaturesPlugin_Rotation::initAttributes()
26 {
27   AttributeSelectionListPtr aSelection =
28     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
30
31   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
32                        ModelAPI_AttributeSelection::typeId());
33   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
34 }
35
36 //=================================================================================================
37 void FeaturesPlugin_Rotation::execute()
38 {
39   // Getting objects.
40   ListOfShape anObjects;
41   std::list<ResultPtr> aContextes;
42   AttributeSelectionListPtr anObjectsSelList =
43     selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
44   if (anObjectsSelList->size() == 0) {
45     return;
46   }
47   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
48     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
49       anObjectsSelList->value(anObjectsIndex);
50     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
51     if(!anObject.get()) {
52       return;
53     }
54     anObjects.push_back(anObject);
55     aContextes.push_back(anObjectAttr->context());
56   }
57
58   //Getting axis.
59   std::shared_ptr<GeomAPI_Ax1> anAxis;
60   std::shared_ptr<GeomAPI_Edge> anEdge;
61   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
62     selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
63   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
64     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
65   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
66              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
67     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
68   }
69   if(anEdge) {
70     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
71                                                           anEdge->line()->direction()));
72   }
73
74   // Getting angle.
75   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
76
77   // Rotating each object.
78   int aResultIndex = 0;
79   std::list<ResultPtr>::iterator aContext = aContextes.begin();
80   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
81         anObjectsIt++, aContext++) {
82     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
83     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
84
85     // Setting result.
86     if (isPart) {
87       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
88       aTrsf->setRotation(anAxis, anAngle);
89       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
90       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
91       aResultPart->setTrsf(*aContext, aTrsf);
92       setResult(aResultPart, aResultIndex);
93     } else {
94       GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
95
96       // Checking that the algorithm worked properly.
97       if(!aRotationAlgo.isDone()) {
98         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
99         setError(aFeatureError);
100         break;
101       }
102       if(aRotationAlgo.shape()->isNull()) {
103         static const std::string aShapeError = "Error: Resulting shape is Null.";
104         setError(aShapeError);
105         break;
106       }
107       if(!aRotationAlgo.isValid()) {
108         std::string aFeatureError = "Error: Resulting shape is not valid.";
109         setError(aFeatureError);
110         break;
111       }
112
113       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
114       loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
115       setResult(aResultBody, aResultIndex);
116     }
117     aResultIndex++;
118   }
119
120   // Remove the rest results if there were produced in the previous pass.
121   removeResults(aResultIndex);
122 }
123
124 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
125                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
126                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
127 {
128   // Store result.
129   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
130
131   int aRotatedTag = 1;
132   std::string aRotatedName = "Rotated";
133   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
134
135   FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
136                                             theBaseShape, aRotatedTag, aRotatedName,
137                                             *aSubShapes.get());
138 }