Salome HOME
Update of transactions management after the nested operation finished in extrusion cut
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Movement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Movement.cpp
4 // Created:     8 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_Movement.h>
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <ModelAPI_ResultPart.h>
13 #include <ModelAPI_Session.h>
14
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_Lin.h>
17
18 //=================================================================================================
19 FeaturesPlugin_Movement::FeaturesPlugin_Movement()
20 {
21 }
22
23 //=================================================================================================
24 void FeaturesPlugin_Movement::initAttributes()
25 {
26   AttributeSelectionListPtr aSelection = 
27     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
28     FeaturesPlugin_Movement::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
29   // revolution works with faces always
30   aSelection->setSelectionType("SOLID");
31
32   data()->addAttribute(FeaturesPlugin_Movement::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
33   data()->addAttribute(FeaturesPlugin_Movement::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
34 }
35
36 //=================================================================================================
37 void FeaturesPlugin_Movement::execute()
38 {
39   // Getting objects.
40   ListOfShape anObjects;
41   std::list<ResultPtr> aContextes;
42   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Movement::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_Movement::AXIS_OBJECT_ID());
60   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
61     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
62   }
63   if(anEdge) {
64     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
65   }
66
67   // Getting distance.
68   double aDistance = real(FeaturesPlugin_Movement::DISTANCE_ID())->value();
69
70   // Moving 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     GeomAlgoAPI_Movement aMovementAlgo(aBaseShape, anAxis, aDistance, isPart);
78
79     // Checking that the algorithm worked properly.
80     if(!aMovementAlgo.isDone()) {
81       static const std::string aFeatureError = "Movement algorithm failed";
82       setError(aFeatureError);
83       break;
84     }
85     if(aMovementAlgo.shape()->isNull()) {
86       static const std::string aShapeError = "Resulting shape is Null";
87       setError(aShapeError);
88       break;
89     }
90     if(!aMovementAlgo.isValid()) {
91       std::string aFeatureError = "Warning: resulting shape is not valid";
92       setError(aFeatureError);
93       break;
94     }
95
96     // Setting result.
97     if (isPart) {
98       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
99
100       ResultPartPtr aCurrentResult;
101       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = results();
102       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
103       for(int a = 0; aResIter != aResults.end(); aResIter++, a++)  {
104         if (a == aResultIndex) {
105           aCurrentResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aResIter);
106           break;
107         }
108       }
109       ResultPartPtr aResultPart = document()->copyPart(aCurrentResult, anOrigin, aResultIndex);
110       aResultPart->setShape(*aContext, aMovementAlgo.shape());
111       setResult(aResultPart);
112     } else {
113       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
114       LoadNamingDS(aMovementAlgo, aResultBody, aBaseShape);
115       setResult(aResultBody, aResultIndex);
116     }
117     aResultIndex++;
118   }
119
120   // Remove the rest results if there were produced in the previous pass.
121   removeResults(aResultIndex);
122 }
123
124 void FeaturesPlugin_Movement::LoadNamingDS(const GeomAlgoAPI_Movement& theMovementAlgo,
125                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
126                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
127 {
128   // Store result.
129   theResultBody->storeModified(theBaseShape, theMovementAlgo.shape());
130
131   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theMovementAlgo.mapOfShapes();
132
133   int aMovedTag = 1;
134   std::string aMovedName = "Moved";
135   theResultBody->loadAndOrientModifiedShapes(theMovementAlgo.makeShape().get(),
136                                              theBaseShape, GeomAPI_Shape::FACE,
137                                              aMovedTag, aMovedName, *aSubShapes.get());
138
139 }