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