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