Salome HOME
Bug #1748: Naming incorrect for edges after translation
[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 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
24 {
25 }
26
27 void FeaturesPlugin_Placement::initAttributes()
28 {
29
30   AttributeSelectionListPtr aSelection = 
31     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
32     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
33
34   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
35   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
36   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
37   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
38 }
39
40 void FeaturesPlugin_Placement::execute()
41 {
42   // Getting objects.
43   ListOfShape anObjects;
44   std::list<ResultPtr> aContextes;
45   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
46   if(anObjectsSelList->size() == 0) {
47     return;
48   }
49   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
50     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = 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   // Verify the start shape
61   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
62   if(!anObjRef) {
63     return;
64   }
65   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
66   if(!aStartShape) {
67     static const std::string aSelectionError = "Error: The start shape selection is bad.";
68     setError(aSelectionError);
69     return;
70   }
71
72
73   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
74   ResultPtr aContextRes = anObjRef->context();
75   if (aContextRes.get()) {
76     aStartShapeContext = aContextRes->shape();
77   }
78   if(!aStartShapeContext.get()) {
79     static const std::string aContextError = "Error: The start shape selection context is bad.";
80     setError(aContextError);
81     return;
82   }
83
84   // Verify the end shape
85   anObjRef = selection(END_SHAPE_ID());
86   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
87   if(!anEndShape) {
88     static const std::string aSelectionError = "Error: The end shape selection is bad.";
89     setError(aSelectionError);
90     return;
91   }
92
93   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
94   aContextRes = anObjRef->context();
95   if(aContextRes.get()) {
96     anEndShapeContext = aContextRes->shape();
97   }
98   if(!anEndShapeContext.get()) {
99     static const std::string aContextError = "Error: The end shape selection context is bad.";
100     setError(aContextError);
101     return;
102   }
103
104   // Verify planarity of faces and linearity of edges
105   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
106   for (int i = 0; i < 2; i++) {
107     if (aShapes[i]->isFace()) {
108       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
109       if (!aFace->isPlanar()) {
110         static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
111         setError(aPlanarityError);
112         return;
113       }
114     }
115     else if (aShapes[i]->isEdge()) {
116       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
117       if (!anEdge->isLine()) {
118         static const std::string aLinearityError = "Error: One of selected endges is not linear.";
119         setError(aLinearityError);
120         return;
121       }
122     }
123   }
124
125   // Flags of the Placement
126   bool isReverse = boolean(REVERSE_ID())->value();
127   bool isCentering = boolean(CENTERING_ID())->value();
128
129   // Getting transformation.
130   GeomAlgoAPI_Placement aPlacementAlgo(
131     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
132   if(!aPlacementAlgo.isDone()) {
133     static const std::string aFeatureError = "Error: Placement algorithm failed.";
134     setError(aFeatureError);
135     return;
136   }
137   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
138
139   // Applying transformation to each object.
140   int aResultIndex = 0;
141   std::list<ResultPtr>::iterator aContext = aContextes.begin();
142   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
143       anObjectsIt++, aContext++) {
144
145     if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) { // for part results just set transformation
146       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
147       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
148       aResultPart->setTrsf(aContextRes, aTrsf);
149       setResult(aResultPart, aResultIndex);
150     } else {
151       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
152       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
153
154       // Checking that the algorithm worked properly.
155       if(!aTransformAlgo.isDone()) {
156         static const std::string aFeatureError = "Error: Transform algorithm failed.";
157         setError(aFeatureError);
158         break;
159       }
160       if(aTransformAlgo.shape()->isNull()) {
161         static const std::string aShapeError = "Error: Resulting shape is Null.";
162         setError(aShapeError);
163         break;
164       }
165       if(!aTransformAlgo.isValid()) {
166         std::string aFeatureError = "Error: Resulting shape is not valid.";
167         setError(aFeatureError);
168         break;
169       }
170
171       //LoadNamingDS
172       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
173       loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
174       setResult(aResultBody, aResultIndex);
175     }
176     aResultIndex++;
177   }
178
179   // Remove the rest results if there were produced in the previous pass.
180   removeResults(aResultIndex);
181 }
182
183 //============================================================================
184 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
185                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
186                                             std::shared_ptr<GeomAPI_Shape> theBaseShape)
187 {
188   //load result
189   theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
190
191   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
192
193     // put modifed faces in DF
194   int aPlacedTag = 1;
195   std::string aPlacedName = "Placed";
196
197   switch(theBaseShape->shapeType()) {
198     case GeomAPI_Shape::COMPOUND:
199     case GeomAPI_Shape::COMPSOLID:
200     case GeomAPI_Shape::SOLID:
201     case GeomAPI_Shape::SHELL:
202       theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
203                                                  theBaseShape, GeomAPI_Shape::FACE,
204                                                  aPlacedTag, aPlacedName + "_Face", *aSubShapes.get());
205     case GeomAPI_Shape::FACE:
206     case GeomAPI_Shape::WIRE:
207       theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
208                                                  theBaseShape, GeomAPI_Shape::EDGE,
209                                                  ++aPlacedTag, aPlacedName + "_Edge", *aSubShapes.get());
210     case GeomAPI_Shape::EDGE:
211       theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
212                                                  theBaseShape, GeomAPI_Shape::VERTEX,
213                                                  ++aPlacedTag, aPlacedName + "_Vertex", *aSubShapes.get());
214   }
215 }