Salome HOME
#1707 Fatal error when Recover box
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Translation.cpp
4 // Created:     8 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_Translation.h>
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_Session.h>
15
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
18
19 //=================================================================================================
20 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
21 {
22 }
23
24 //=================================================================================================
25 void FeaturesPlugin_Translation::initAttributes()
26 {
27   AttributeSelectionListPtr aSelection = 
28     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29     FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
30
31   data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
32   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
33 }
34
35 //=================================================================================================
36 void FeaturesPlugin_Translation::execute()
37 {
38   // Getting objects.
39   ListOfShape anObjects;
40   std::list<ResultPtr> aContextes;
41   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::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 = anObjectsSelList->value(anObjectsIndex);
47     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
48     if(!anObject.get()) { // may be for not-activated parts
49       eraseResults();
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_Translation::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 distance.
71   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
72
73   // Moving 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
81     // Setting result.
82     if (isPart) {
83       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
84       aTrsf->setTranslation(anAxis, aDistance);
85       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
86       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
87       aResultPart->setTrsf(*aContext, aTrsf);
88       setResult(aResultPart);
89     } else {
90       GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
91
92       // Checking that the algorithm worked properly.
93       if(!aTranslationAlgo.isDone()) {
94         static const std::string aFeatureError = "Error: Translation algorithm failed.";
95         setError(aFeatureError);
96         break;
97       }
98       if(aTranslationAlgo.shape()->isNull()) {
99         static const std::string aShapeError = "Error: Resulting shape is Null.";
100         setError(aShapeError);
101         break;
102       }
103       if(!aTranslationAlgo.isValid()) {
104         std::string aFeatureError = "Error: Resulting shape is not valid.";
105         setError(aFeatureError);
106         break;
107       }
108
109       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
110       loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
111       setResult(aResultBody, aResultIndex);
112     }
113     aResultIndex++;
114   }
115
116   // Remove the rest results if there were produced in the previous pass.
117   removeResults(aResultIndex);
118 }
119
120 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
121                                               std::shared_ptr<ModelAPI_ResultBody> theResultBody,
122                                               std::shared_ptr<GeomAPI_Shape> theBaseShape)
123 {
124   // Store result.
125   theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
126
127   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
128
129   int aTranslatedTag = 1;
130   std::string aTranslatedName = "Translated";
131   theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
132                                              theBaseShape, GeomAPI_Shape::FACE,
133                                              aTranslatedTag, aTranslatedName, *aSubShapes.get());
134
135 }