Salome HOME
Add ModelAPI_AttributeIntArray to SWIG
[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()) {
51       return;
52     }
53     anObjects.push_back(anObject);
54     aContextes.push_back(anObjectAttr->context());
55   }
56
57   //Getting axis.
58   std::shared_ptr<GeomAPI_Ax1> anAxis;
59   std::shared_ptr<GeomAPI_Edge> anEdge;
60   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
61   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
62     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
63   } else if (anObjRef && !anObjRef->value() && anObjRef->context() && 
64              anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
65     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
66   }
67   if(anEdge) {
68     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
69   }
70
71   // Getting distance.
72   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
73
74   // Moving each object.
75   int aResultIndex = 0;
76   std::list<ResultPtr>::iterator aContext = aContextes.begin();
77   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
78         anObjectsIt++, aContext++) {
79     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
80     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
81
82     // Setting result.
83     if (isPart) {
84       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
85       aTrsf->setTranslation(anAxis, aDistance);
86       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
87       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
88       aResultPart->setTrsf(*aContext, aTrsf);
89       setResult(aResultPart);
90     } else {
91       GeomAlgoAPI_Translation aMovementAlgo(aBaseShape, anAxis, aDistance);
92
93       // Checking that the algorithm worked properly.
94       if(!aMovementAlgo.isDone()) {
95         static const std::string aFeatureError = "Movement algorithm failed";
96         setError(aFeatureError);
97         break;
98       }
99       if(aMovementAlgo.shape()->isNull()) {
100         static const std::string aShapeError = "Resulting shape is Null";
101         setError(aShapeError);
102         break;
103       }
104       if(!aMovementAlgo.isValid()) {
105         std::string aFeatureError = "Warning: resulting shape is not valid";
106         setError(aFeatureError);
107         break;
108       }
109
110       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
111       LoadNamingDS(aMovementAlgo, aResultBody, aBaseShape);
112       setResult(aResultBody, aResultIndex);
113     }
114     aResultIndex++;
115   }
116
117   // Remove the rest results if there were produced in the previous pass.
118   removeResults(aResultIndex);
119 }
120
121 void FeaturesPlugin_Translation::LoadNamingDS(const GeomAlgoAPI_Translation& theMovementAlgo,
122                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
123                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
124 {
125   // Store result.
126   theResultBody->storeModified(theBaseShape, theMovementAlgo.shape());
127
128   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theMovementAlgo.mapOfShapes();
129
130   int aMovedTag = 1;
131   std::string aMovedName = "Moved";
132   theResultBody->loadAndOrientModifiedShapes(theMovementAlgo.makeShape().get(),
133                                               theBaseShape, GeomAPI_Shape::FACE,
134                                               aMovedTag, aMovedName, *aSubShapes.get());
135
136 }