Salome HOME
Fix for selection the axis for movement/translation features
[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 #include <ModelAPI_ResultPart.h>
14
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_Lin.h>
17
18 //=================================================================================================
19 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
20 {
21 }
22
23 //=================================================================================================
24 void FeaturesPlugin_Rotation::initAttributes()
25 {
26   AttributeSelectionListPtr aSelection = 
27     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
28     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
29   // revolution works with faces always
30   aSelection->setSelectionType("SOLID");
31
32   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(), 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 = 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     aContextes.push_back(anObjectAttr->context());
54   }
55
56   //Getting axis.
57   std::shared_ptr<GeomAPI_Ax1> anAxis;
58   std::shared_ptr<GeomAPI_Edge> anEdge;
59   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
60   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
61     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
62   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
63              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
64     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
65   }
66   if(anEdge) {
67     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
68   }
69
70   // Getting angle.
71   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
72
73   // Rotating each object.
74   int aResultIndex = 0;
75   std::list<ResultPtr>::iterator aContext = aContextes.begin();
76   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); 
77         anObjectsIt++, aContext++) {
78     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
79     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
80     GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle, isPart);
81
82     // Checking that the algorithm worked properly.
83     if(!aRotationAlgo.isDone()) {
84       static const std::string aFeatureError = "Rotation algorithm failed";
85       setError(aFeatureError);
86       break;
87     }
88     if(aRotationAlgo.shape()->isNull()) {
89       static const std::string aShapeError = "Resulting shape is Null";
90       setError(aShapeError);
91       break;
92     }
93     if(!aRotationAlgo.isValid()) {
94       std::string aFeatureError = "Warning: resulting shape is not valid";
95       setError(aFeatureError);
96       break;
97     }
98
99     // Setting result.
100     if (isPart) {
101       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
102
103       ResultPartPtr aCurrentResult;
104       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = results();
105       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
106       for(int a = 0; aResIter != aResults.end(); aResIter++, a++)  {
107         if (a == aResultIndex) {
108           aCurrentResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aResIter);
109           break;
110         }
111       }
112       ResultPartPtr aResultPart = document()->copyPart(aCurrentResult, anOrigin, aResultIndex);
113       aResultPart->setShape(*aContext, aRotationAlgo.shape());
114       setResult(aResultPart);
115     } else {
116       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
117       LoadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
118       setResult(aResultBody, aResultIndex);
119     }
120     aResultIndex++;
121   }
122
123   // Remove the rest results if there were produced in the previous pass.
124   removeResults(aResultIndex);
125 }
126
127 void FeaturesPlugin_Rotation::LoadNamingDS(const GeomAlgoAPI_Rotation& theRotaionAlgo,
128                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
129                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
130 {
131   // Store result.
132   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
133
134   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfShapes();
135
136   int aRotatedTag = 1;
137   std::string aRotatedName = "Rotated";
138   theResultBody->loadAndOrientModifiedShapes(theRotaionAlgo.makeShape().get(),
139                                              theBaseShape, GeomAPI_Shape::FACE,
140                                              aRotatedTag, aRotatedName, *aSubShapes.get());
141
142 }