Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/shaper
[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 //=================================================================================================
18 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
19 {
20 }
21
22 //=================================================================================================
23 void FeaturesPlugin_Rotation::initAttributes()
24 {
25   AttributeSelectionListPtr aSelection = 
26     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
27     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
28   // revolution works with faces always
29   aSelection->setSelectionType("SOLID");
30
31   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
32   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
33 }
34
35 //=================================================================================================
36 void FeaturesPlugin_Rotation::execute()
37 {
38   // Getting objects.
39   ListOfShape anObjects;
40   std::list<ResultPtr> aContextes;
41   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
42   if (anObjectsSelList->size() == 0) {
43     return;
44   }
45   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
46     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
47     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
48     if(!anObject.get()) {
49       return;
50     }
51     anObjects.push_back(anObject);
52     aContextes.push_back(anObjectAttr->context());
53   }
54
55   //Getting axis.
56   std::shared_ptr<GeomAPI_Ax1> anAxis;
57   std::shared_ptr<GeomAPI_Edge> anEdge;
58   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
59   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
60     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
61   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
62              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
63     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
64   }
65   if(anEdge) {
66     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
67   }
68
69   // Getting angle.
70   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
71
72   // Rotating each object.
73   int aResultIndex = 0;
74   std::list<ResultPtr>::iterator aContext = aContextes.begin();
75   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); 
76         anObjectsIt++, aContext++) {
77     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
78     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
79
80     // Setting result.
81     if (isPart) {
82       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
83       aTrsf->setRotation(anAxis, anAngle);
84       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
85       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
86       aResultPart->setTrsf(*aContext, aTrsf);
87       setResult(aResultPart);
88     } else {
89       GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
90
91       // Checking that the algorithm worked properly.
92       if(!aRotationAlgo.isDone()) {
93         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
94         setError(aFeatureError);
95         break;
96       }
97       if(aRotationAlgo.shape()->isNull()) {
98         static const std::string aShapeError = "Error: Resulting shape is Null.";
99         setError(aShapeError);
100         break;
101       }
102       if(!aRotationAlgo.isValid()) {
103         std::string aFeatureError = "Error: Resulting shape is not valid.";
104         setError(aFeatureError);
105         break;
106       }
107
108       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
109       loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
110       setResult(aResultBody, aResultIndex);
111     }
112     aResultIndex++;
113   }
114
115   // Remove the rest results if there were produced in the previous pass.
116   removeResults(aResultIndex);
117 }
118
119 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
120                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
121                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
122 {
123   // Store result.
124   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
125
126   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
127
128   int aRotatedTag = 1;
129   std::string aRotatedName = "Rotated";
130   theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
131                                              theBaseShape, GeomAPI_Shape::FACE,
132                                              aRotatedTag, aRotatedName, *aSubShapes.get());
133
134 }