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