Salome HOME
GeomAlgoAPI_Translation now derived from GeomAlgoAPI_MakeShape
[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   // revolution works with faces always
31   aSelection->setSelectionType("SOLID");
32
33   data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
34   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
35 }
36
37 //=================================================================================================
38 void FeaturesPlugin_Translation::execute()
39 {
40   // Getting objects.
41   ListOfShape anObjects;
42   std::list<ResultPtr> aContextes;
43   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
44   if (anObjectsSelList->size() == 0) {
45     return;
46   }
47   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
48     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
49     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
50     if(!anObject.get()) { // may be for not-activated parts
51       eraseResults();
52       return;
53     }
54     anObjects.push_back(anObject);
55     aContextes.push_back(anObjectAttr->context());
56   }
57
58   //Getting axis.
59   std::shared_ptr<GeomAPI_Ax1> anAxis;
60   std::shared_ptr<GeomAPI_Edge> anEdge;
61   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
62   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
63     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
64   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
65              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
66     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
67   }
68   if(anEdge) {
69     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
70   }
71
72   // Getting distance.
73   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
74
75   // Moving 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->setTranslation(anAxis, aDistance);
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);
91     } else {
92       GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
93
94       // Checking that the algorithm worked properly.
95       if(!aTranslationAlgo.isDone()) {
96         static const std::string aFeatureError = "Translation algorithm failed";
97         setError(aFeatureError);
98         break;
99       }
100       if(aTranslationAlgo.shape()->isNull()) {
101         static const std::string aShapeError = "Resulting shape is Null";
102         setError(aShapeError);
103         break;
104       }
105       if(!aTranslationAlgo.isValid()) {
106         std::string aFeatureError = "Warning: resulting shape is not valid";
107         setError(aFeatureError);
108         break;
109       }
110
111       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
112       loadNamingDS(aTranslationAlgo, 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_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
123                                               std::shared_ptr<ModelAPI_ResultBody> theResultBody,
124                                               std::shared_ptr<GeomAPI_Shape> theBaseShape)
125 {
126   // Store result.
127   theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
128
129   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
130
131   int aTranslatedTag = 1;
132   std::string aTranslatedName = "Translated";
133   theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
134                                              theBaseShape, GeomAPI_Shape::FACE,
135                                              aTranslatedTag, aTranslatedName, *aSubShapes.get());
136
137 }