Salome HOME
Issue #1834: Fix length of lines
[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(), 
30                        ModelAPI_AttributeSelection::typeId());
31   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
32 }
33
34 //=================================================================================================
35 void FeaturesPlugin_Rotation::execute()
36 {
37   // Getting objects.
38   ListOfShape anObjects;
39   std::list<ResultPtr> aContextes;
40   AttributeSelectionListPtr anObjectsSelList = 
41     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 = 
47       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 =
60     selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
61   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
62     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
63   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
64              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
65     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
66   }
67   if(anEdge) {
68     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), 
69                                                           anEdge->line()->direction()));
70   }
71
72   // Getting angle.
73   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
74
75   // Rotating each object.
76   int aResultIndex = 0;
77   std::list<ResultPtr>::iterator aContext = aContextes.begin();
78   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); 
79         anObjectsIt++, aContext++) {
80     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
81     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
82
83     // Setting result.
84     if (isPart) {
85       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
86       aTrsf->setRotation(anAxis, anAngle);
87       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
88       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
89       aResultPart->setTrsf(*aContext, aTrsf);
90       setResult(aResultPart, aResultIndex);
91     } else {
92       GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
93
94       // Checking that the algorithm worked properly.
95       if(!aRotationAlgo.isDone()) {
96         static const std::string aFeatureError = "Error: Rotation algorithm failed.";
97         setError(aFeatureError);
98         break;
99       }
100       if(aRotationAlgo.shape()->isNull()) {
101         static const std::string aShapeError = "Error: Resulting shape is Null.";
102         setError(aShapeError);
103         break;
104       }
105       if(!aRotationAlgo.isValid()) {
106         std::string aFeatureError = "Error: Resulting shape is not valid.";
107         setError(aFeatureError);
108         break;
109       }
110
111       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
112       loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
113       setResult(aResultBody, aResultIndex);
114     }
115     aResultIndex++;
116   }
117
118   // Remove the rest results if there were produced in the previous pass.
119   removeResults(aResultIndex);
120 }
121
122 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
123                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
124                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
125 {
126   // Store result.
127   theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
128
129   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
130
131   int aRotatedTag = 1;
132   std::string aRotatedName = "Rotated";
133
134   switch(theBaseShape->shapeType()) {
135     case GeomAPI_Shape::COMPOUND:
136     case GeomAPI_Shape::COMPSOLID:
137     case GeomAPI_Shape::SOLID:
138     case GeomAPI_Shape::SHELL:
139       theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
140                                     theBaseShape, GeomAPI_Shape::FACE,
141                                     aRotatedTag, aRotatedName + "_Face", *aSubShapes.get());
142     case GeomAPI_Shape::FACE:
143     case GeomAPI_Shape::WIRE:
144       theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
145                                   theBaseShape, GeomAPI_Shape::EDGE,
146                                   ++aRotatedTag, aRotatedName + "_Edge", *aSubShapes.get());
147     case GeomAPI_Shape::EDGE:
148       theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
149                                 theBaseShape, GeomAPI_Shape::VERTEX,
150                                 ++aRotatedTag, aRotatedName + "_Vertex", *aSubShapes.get());
151   }
152 }