Salome HOME
43b25be205b2270e3e0eec2bb0c5ce5d840f75c8
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Placement.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "FeaturesPlugin_Placement.h"
8
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultPart.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_BodyBuilder.h>
16
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Face.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAlgoAPI_Placement.h>
21 #include <GeomAlgoAPI_Transform.h>
22
23 #define _MODIFIEDF_TAG 1
24 #define _MODIFIEDE_TAG 2
25 #define _MODIFIEDV_TAG 3
26 #define _FACE 4
27 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
28 {
29 }
30
31 void FeaturesPlugin_Placement::initAttributes()
32 {
33
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   // extrusion works with faces always
38   aSelection->setSelectionType("SOLID");
39
40   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
41   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
42   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
43   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
44 }
45
46 void FeaturesPlugin_Placement::execute()
47 {
48   // Getting objects.
49   ListOfShape anObjects;
50   std::list<ResultPtr> aContextes;
51   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
52   if(anObjectsSelList->size() == 0) {
53     return;
54   }
55   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
56     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
57     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
58     if(!anObject.get()) { // may be for not-activated parts
59       eraseResults();
60       return;
61     }
62     anObjects.push_back(anObject);
63     aContextes.push_back(anObjectAttr->context());
64   }
65
66   // Verify the start shape
67   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
68   if(!anObjRef) {
69     return;
70   }
71   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
72   if(!aStartShape) {
73     static const std::string aSelectionError = "Error: The start shape selection is bad.";
74     setError(aSelectionError);
75     return;
76   }
77
78
79   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
80   ResultPtr aContextRes = anObjRef->context();
81   if (aContextRes.get()) {
82     aStartShapeContext = aContextRes->shape();
83   }
84   if(!aStartShapeContext.get()) {
85     static const std::string aContextError = "Error: The start shape selection context is bad.";
86     setError(aContextError);
87     return;
88   }
89
90   // Verify the end shape
91   anObjRef = selection(END_SHAPE_ID());
92   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
93   if(!anEndShape) {
94     static const std::string aSelectionError = "Error: The end shape selection is bad.";
95     setError(aSelectionError);
96     return;
97   }
98
99   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
100   aContextRes = anObjRef->context();
101   if(aContextRes.get()) {
102     anEndShapeContext = aContextRes->shape();
103   }
104   if(!anEndShapeContext.get()) {
105     static const std::string aContextError = "Error: The end shape selection context is bad.";
106     setError(aContextError);
107     return;
108   }
109
110   // Verify planarity of faces and linearity of edges
111   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
112   for (int i = 0; i < 2; i++) {
113     if (aShapes[i]->isFace()) {
114       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
115       if (!aFace->isPlanar()) {
116         static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
117         setError(aPlanarityError);
118         return;
119       }
120     }
121     else if (aShapes[i]->isEdge()) {
122       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
123       if (!anEdge->isLine()) {
124         static const std::string aLinearityError = "Error: One of selected endges is not linear.";
125         setError(aLinearityError);
126         return;
127       }
128     }
129   }
130
131   // Flags of the Placement
132   bool isReverse = boolean(REVERSE_ID())->value();
133   bool isCentering = boolean(CENTERING_ID())->value();
134
135   // Getting transformation.
136   GeomAlgoAPI_Placement aPlacementAlgo(
137     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
138   if(!aPlacementAlgo.isDone()) {
139     static const std::string aFeatureError = "Error: Placement algorithm failed.";
140     setError(aFeatureError);
141     return;
142   }
143   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
144
145   // Applying transformation to each object.
146   int aResultIndex = 0;
147   std::list<ResultPtr>::iterator aContext = aContextes.begin();
148   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
149       anObjectsIt++, aContext++) {
150
151     if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) { // for part results just set transformation
152       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154       aResultPart->setTrsf(aContextRes, aTrsf);
155       setResult(aResultPart, aResultIndex);
156     } else {
157       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
158       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
159
160       // Checking that the algorithm worked properly.
161       if(!aTransformAlgo.isDone()) {
162         static const std::string aFeatureError = "Error: Transform algorithm failed.";
163         setError(aFeatureError);
164         break;
165       }
166       if(aTransformAlgo.shape()->isNull()) {
167         static const std::string aShapeError = "Error: Resulting shape is Null.";
168         setError(aShapeError);
169         break;
170       }
171       if(!aTransformAlgo.isValid()) {
172         std::string aFeatureError = "Error: Resulting shape is not valid.";
173         setError(aFeatureError);
174         break;
175       }
176
177       //LoadNamingDS
178       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
179       LoadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
180       setResult(aResultBody, aResultIndex);
181     }
182     aResultIndex++;
183   }
184
185   // Remove the rest results if there were produced in the previous pass.
186   removeResults(aResultIndex);
187 }
188
189 //============================================================================
190 void FeaturesPlugin_Placement::LoadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
191                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
192                                             std::shared_ptr<GeomAPI_Shape> theSlaveObject)
193 {
194   //load result
195   theResultBody->storeModified(theSlaveObject, theTransformAlgo.shape()); // the initial Slave, the resulting Slave
196
197   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
198
199     // put modifed faces in DF
200   std::string aModName = "Modified";
201   theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
202                                              theSlaveObject, _FACE,
203                                              _MODIFIEDF_TAG, aModName, *aSubShapes.get());
204 }