]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp
Salome HOME
Feature #534: 5.01. Body placement by 3D rotation in respect to axis
[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 #define _ROTATED_TAG 1
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   // revolution works with faces always
31   aSelection->setSelectionType("SOLID");
32
33   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
34   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
35 }
36
37 //=================================================================================================
38 void FeaturesPlugin_Rotation::execute()
39 {
40   // Getting objects.
41   ListOfShape anObjects;
42   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
43   if (anObjectsSelList->size() == 0) {
44     return;
45   }
46   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
47     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
48     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
49     if(!anObject.get()) {
50       return;
51     }
52     anObjects.push_back(anObject);
53   }
54
55   //Getting axe.
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   }
62   if(anEdge) {
63     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
64   }
65
66   // Getting angle.
67   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
68
69   // Rotating each object.
70   int aResultIndex = 0;
71   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
72     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
73     GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
74
75     // Checking that the algorithm worked properly.
76     if(!aRotationAlgo.isDone()) {
77       static const std::string aFeatureError = "Rotation algorithm failed";
78       setError(aFeatureError);
79       break;
80     }
81     if(aRotationAlgo.shape()->isNull()) {
82       static const std::string aShapeError = "Resulting shape is Null";
83       setError(aShapeError);
84       break;
85     }
86     if(!aRotationAlgo.isValid()) {
87       std::string aFeatureError = "Warning: resulting shape is not valid";
88       setError(aFeatureError);
89       break;
90     }
91
92     // Setting result.
93     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
94     LoadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
95     setResult(aResultBody, aResultIndex);
96     aResultIndex++;
97   }
98
99   // Remove the rest results if there were produced in the previous pass.
100   removeResults(aResultIndex);
101 }
102
103 void FeaturesPlugin_Rotation::LoadNamingDS(const GeomAlgoAPI_Rotation& theRotaionAlgo,
104                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
105                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
106 {
107   // Store result.
108   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
109
110   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfShapes();
111
112   int aRotatedTag = 1;
113   std::string aRotatedName = "Rotated";
114   theResultBody->loadAndOrientModifiedShapes(theRotaionAlgo.makeShape().get(),
115                                              theBaseShape, GeomAPI_Shape::FACE,
116                                              aRotatedTag, aRotatedName, *aSubShapes.get());
117
118 }