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