]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp
Salome HOME
Improvement #635: Move maximum functionality to API class
[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_Session.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   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
41   if (anObjectsSelList->size() == 0) {
42     return;
43   }
44   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
45     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
46     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
47     if(!anObject.get()) {
48       return;
49     }
50     anObjects.push_back(anObject);
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   }
60   if(anEdge) {
61     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
62   }
63
64   // Getting angle.
65   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
66
67   // Rotating each object.
68   int aResultIndex = 0;
69   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
70     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
71     GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
72
73     // Checking that the algorithm worked properly.
74     if(!aRotationAlgo.isDone()) {
75       static const std::string aFeatureError = "Rotation algorithm failed";
76       setError(aFeatureError);
77       break;
78     }
79     if(aRotationAlgo.shape()->isNull()) {
80       static const std::string aShapeError = "Resulting shape is Null";
81       setError(aShapeError);
82       break;
83     }
84     if(!aRotationAlgo.isValid()) {
85       std::string aFeatureError = "Warning: resulting shape is not valid";
86       setError(aFeatureError);
87       break;
88     }
89
90     // Setting result.
91     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
92     LoadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
93     setResult(aResultBody, aResultIndex);
94     aResultIndex++;
95   }
96
97   // Remove the rest results if there were produced in the previous pass.
98   removeResults(aResultIndex);
99 }
100
101 void FeaturesPlugin_Rotation::LoadNamingDS(const GeomAlgoAPI_Rotation& theRotaionAlgo,
102                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
103                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
104 {
105   // Store result.
106   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
107
108   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfShapes();
109
110   int aRotatedTag = 1;
111   std::string aRotatedName = "Rotated";
112   theResultBody->loadAndOrientModifiedShapes(theRotaionAlgo.makeShape().get(),
113                                              theBaseShape, GeomAPI_Shape::FACE,
114                                              aRotatedTag, aRotatedName, *aSubShapes.get());
115
116 }