]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Translation.cpp
Salome HOME
Issue #1866: Naming incorrect on edges after translation
[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 #include <FeaturesPlugin_Tools.h>
20
21 //=================================================================================================
22 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
23 {
24 }
25
26 //=================================================================================================
27 void FeaturesPlugin_Translation::initAttributes()
28 {
29   AttributeSelectionListPtr aSelection =
30     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
31     FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
32
33   data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
34                        ModelAPI_AttributeSelection::typeId());
35   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
36                        ModelAPI_AttributeDouble::typeId());
37 }
38
39 //=================================================================================================
40 void FeaturesPlugin_Translation::execute()
41 {
42   // Getting objects.
43   ListOfShape anObjects;
44   std::list<ResultPtr> aContextes;
45   AttributeSelectionListPtr anObjectsSelList =
46     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
47   if (anObjectsSelList->size() == 0) {
48     return;
49   }
50   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
51     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
52       anObjectsSelList->value(anObjectsIndex);
53     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
54     if(!anObject.get()) { // may be for not-activated parts
55       eraseResults();
56       return;
57     }
58     anObjects.push_back(anObject);
59     aContextes.push_back(anObjectAttr->context());
60   }
61
62   //Getting axis.
63   std::shared_ptr<GeomAPI_Ax1> anAxis;
64   std::shared_ptr<GeomAPI_Edge> anEdge;
65   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
66     selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
67   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
68     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
69   } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
70              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
71     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
72   }
73   if(anEdge) {
74     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
75                                                           anEdge->line()->direction()));
76   }
77
78   // Getting distance.
79   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
80
81   // Moving each object.
82   int aResultIndex = 0;
83   std::list<ResultPtr>::iterator aContext = aContextes.begin();
84   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
85         anObjectsIt++, aContext++) {
86     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
87     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
88
89     // Setting result.
90     if (isPart) {
91       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
92       aTrsf->setTranslation(anAxis, aDistance);
93       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
94       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
95       aResultPart->setTrsf(*aContext, aTrsf);
96       setResult(aResultPart, aResultIndex);
97     } else {
98       GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
99
100       // Checking that the algorithm worked properly.
101       if(!aTranslationAlgo.isDone()) {
102         static const std::string aFeatureError = "Error: Translation algorithm failed.";
103         setError(aFeatureError);
104         break;
105       }
106       if(aTranslationAlgo.shape()->isNull()) {
107         static const std::string aShapeError = "Error: Resulting shape is Null.";
108         setError(aShapeError);
109         break;
110       }
111       if(!aTranslationAlgo.isValid()) {
112         std::string aFeatureError = "Error: Resulting shape is not valid.";
113         setError(aFeatureError);
114         break;
115       }
116
117       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
118       loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
119       setResult(aResultBody, aResultIndex);
120     }
121     aResultIndex++;
122   }
123
124   // Remove the rest results if there were produced in the previous pass.
125   removeResults(aResultIndex);
126 }
127
128 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
129                                               std::shared_ptr<ModelAPI_ResultBody> theResultBody,
130                                               std::shared_ptr<GeomAPI_Shape> theBaseShape)
131 {
132   // Store result.
133   theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
134
135   int aTranslatedTag = 1;
136   std::string aTranslatedName = "Translated";
137   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
138
139   FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody, theBaseShape, aTranslatedTag, aTranslatedName, *aSubShapes.get());
140 }