]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Translation.cpp
Salome HOME
Issue #1834: Fix length of lines
[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(),
32                        ModelAPI_AttributeSelection::typeId());
33   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
34                        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 = 
44     selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
45   if (anObjectsSelList->size() == 0) {
46     return;
47   }
48   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
49     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = 
50       anObjectsSelList->value(anObjectsIndex);
51     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
52     if(!anObject.get()) { // may be for not-activated parts
53       eraseResults();
54       return;
55     }
56     anObjects.push_back(anObject);
57     aContextes.push_back(anObjectAttr->context());
58   }
59
60   //Getting axis.
61   std::shared_ptr<GeomAPI_Ax1> anAxis;
62   std::shared_ptr<GeomAPI_Edge> anEdge;
63   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = 
64     selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
65   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
66     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
67   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
68              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
69     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
70   }
71   if(anEdge) {
72     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), 
73                                                           anEdge->line()->direction()));
74   }
75
76   // Getting distance.
77   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
78
79   // Moving each object.
80   int aResultIndex = 0;
81   std::list<ResultPtr>::iterator aContext = aContextes.begin();
82   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
83         anObjectsIt++, aContext++) {
84     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
85     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
86
87     // Setting result.
88     if (isPart) {
89       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
90       aTrsf->setTranslation(anAxis, aDistance);
91       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
92       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
93       aResultPart->setTrsf(*aContext, aTrsf);
94       setResult(aResultPart, aResultIndex);
95     } else {
96       GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
97
98       // Checking that the algorithm worked properly.
99       if(!aTranslationAlgo.isDone()) {
100         static const std::string aFeatureError = "Error: Translation algorithm failed.";
101         setError(aFeatureError);
102         break;
103       }
104       if(aTranslationAlgo.shape()->isNull()) {
105         static const std::string aShapeError = "Error: Resulting shape is Null.";
106         setError(aShapeError);
107         break;
108       }
109       if(!aTranslationAlgo.isValid()) {
110         std::string aFeatureError = "Error: Resulting shape is not valid.";
111         setError(aFeatureError);
112         break;
113       }
114
115       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
116       loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
117       setResult(aResultBody, aResultIndex);
118     }
119     aResultIndex++;
120   }
121
122   // Remove the rest results if there were produced in the previous pass.
123   removeResults(aResultIndex);
124 }
125
126 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
127                                               std::shared_ptr<ModelAPI_ResultBody> theResultBody,
128                                               std::shared_ptr<GeomAPI_Shape> theBaseShape)
129 {
130   // Store result.
131   theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
132
133   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
134
135   int aTranslatedTag = 1;
136   std::string aTranslatedName = "Translated";
137
138   switch(theBaseShape->shapeType()) {
139     case GeomAPI_Shape::COMPOUND:
140     case GeomAPI_Shape::COMPSOLID:
141     case GeomAPI_Shape::SOLID:
142     case GeomAPI_Shape::SHELL:
143       theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
144                                 theBaseShape, GeomAPI_Shape::FACE,
145                                 aTranslatedTag, aTranslatedName + "_Face", *aSubShapes.get());
146     case GeomAPI_Shape::FACE:
147     case GeomAPI_Shape::WIRE:
148       theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
149                                 theBaseShape, GeomAPI_Shape::EDGE,
150                                 ++aTranslatedTag, aTranslatedName + "_Edge", *aSubShapes.get());
151     case GeomAPI_Shape::EDGE:
152       theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
153                               theBaseShape, GeomAPI_Shape::VERTEX,
154                               ++aTranslatedTag, aTranslatedName + "_Vertex", *aSubShapes.get());
155   }
156 }