Salome HOME
Merge branch 'Dev_1.1.1' of newgeom:newgeom into Dev_1.2.0
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Extrusion.cpp
4 // Created:     30 May 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "FeaturesPlugin_Extrusion.h"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <GeomAPI_Pln.h>
21 #include <GeomAPI_XYZ.h>
22 #include <GeomAlgoAPI_Extrusion.h>
23 #include <GeomAlgoAPI_FaceBuilder.h>
24 #include <GeomAlgoAPI_Prism.h>
25
26 using namespace std;
27 #define _LATERAL_TAG 1
28 #define _FIRST_TAG 2
29 #define _LAST_TAG 3
30 #define EDGE 6
31
32 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
33 {
34 }
35
36 void FeaturesPlugin_Extrusion::initAttributes()
37 {
38   AttributeSelectionListPtr aSelection = 
39     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40     FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41   // extrusion works with faces always
42   aSelection->setSelectionType("FACE");
43
44   data()->addAttribute(FeaturesPlugin_Extrusion::TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(FeaturesPlugin_Extrusion::FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
46
47   //data()->addAttribute(FeaturesPlugin_Extrusion::AXIS_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
48
49   data()->addAttribute(FeaturesPlugin_Extrusion::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(FeaturesPlugin_Extrusion::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::TO_OBJECT_ID());
54 }
55
56 void FeaturesPlugin_Extrusion::execute()
57 {
58   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
59
60   std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape);
61   std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape);
62
63   // Getting bounding planes.
64   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
65   if (anObjRef) {
66     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
67   }
68   anObjRef = selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
69   if (anObjRef) {
70     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
71   }
72
73   // Getting sizes.
74   double aFromSize = real(FeaturesPlugin_Extrusion::FROM_SIZE_ID())->value();
75   double aToSize = real(FeaturesPlugin_Extrusion::TO_SIZE_ID())->value();
76
77   // for each selected face generate a result
78   int anIndex = 0, aResultIndex = 0;
79   for(; anIndex < aFaceRefs->size(); anIndex++) {
80     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
81     ResultPtr aContextRes = aFaceRef->context();
82     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
83     if (!aContext.get()) {
84       static const std::string aContextError = "The selection context is bad";
85       setError(aContextError);
86       break;
87     }
88
89     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
90     int aFacesNum = -1; // this mean that "aFace" is used
91     ResultConstructionPtr aConstruction =
92       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
93     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
94       if (aConstruction.get()) {
95         aFacesNum = aConstruction->facesNum();
96       } else {
97         static const std::string aFaceError = "Can not find basis for extrusion";
98         setError(aFaceError);
99         break;
100       }
101     }
102
103     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
104       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
105       std::shared_ptr<GeomAPI_Shape> aBaseShape;
106       if (aFacesNum == -1) {
107         aBaseShape = aValueFace;
108       } else {
109         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
110       }
111
112       // If bounding faces was not set creating them.
113       std::shared_ptr<GeomAPI_Face> aBaseFace(new GeomAPI_Face(aBaseShape));
114       std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace->getPlane();
115       std::shared_ptr<GeomAPI_Dir> aBaseDir = aBasePlane->direction();
116       std::shared_ptr<GeomAPI_Pnt> aBaseLoc = aBasePlane->location();
117
118       if(!aFromShape) {
119         aFromShape = GeomAlgoAPI_FaceBuilder::plane(aBaseLoc, aBaseDir);
120       }
121       if(!aToShape) {
122         aToShape = GeomAlgoAPI_FaceBuilder::plane(aBaseLoc, aBaseDir);
123       }
124
125       // Moving bounding faces according to "from" and "to" sizes.
126       std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
127       std::shared_ptr<GeomAPI_Pln> aFromPlane = aFromFace->getPlane();
128       std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPlane->location();
129       std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPlane->direction();
130
131       std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
132       std::shared_ptr<GeomAPI_Pln> aToPlane = aToFace->getPlane();
133       std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPlane->location();
134       std::shared_ptr<GeomAPI_Dir> aToDir = aToPlane->direction();
135
136       bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
137
138       std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? aFromSize : -aFromSize))));
139       aFromShape = GeomAlgoAPI_FaceBuilder::plane(aFromPnt, aFromDir);
140
141       std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? -aToSize : aToSize))));
142       aToShape = GeomAlgoAPI_FaceBuilder::plane(aToPnt, aToDir);
143
144       //GeomAlgoAPI_Extrusion aFeature(aFace, aFromSize);
145       GeomAlgoAPI_Prism aFeature(aBaseShape, aFromShape, aToShape);
146       if(!aFeature.isDone()) {
147         static const std::string aFeatureError = "Extrusion algorithm failed";
148         setError(aFeatureError);
149         break;
150       }
151
152       // Check if shape is valid
153       if(aFeature.shape()->isNull()) {
154         static const std::string aShapeError = "Resulting shape is Null";
155         setError(aShapeError);
156         break;
157       }
158       if(!aFeature.isValid()) {
159         std::string aFeatureError = "Warning: resulting shape is not valid";
160         setError(aFeatureError);
161         break;
162       }
163       //LoadNamingDS
164       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
165
166       setResult(aResultBody, aResultIndex);
167       aResultIndex++;
168
169       if (aFacesNum == -1)
170         break;
171     }
172   }
173   // remove the rest results if there were produced in the previous pass
174   removeResults(aResultIndex);
175 }
176
177 //============================================================================
178 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
179                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
180                                             std::shared_ptr<GeomAPI_Shape> theBasis,
181                                             std::shared_ptr<GeomAPI_Shape> theContext)
182 {
183   //load result
184   if(theBasis->isEqual(theContext))
185     theResultBody->store(theFeature.shape());
186   else
187     theResultBody->storeGenerated(theContext, theFeature.shape());
188
189   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
190   theFeature.mapOfShapes(*aSubShapes);
191
192   //Insert lateral face : Face from Edge
193   std::string aLatName = "LateralFace";
194   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
195
196   //Insert bottom face
197   std::string aBotName = "BottomFace";
198   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
199   if(!aBottomFace->isNull()) {
200     if(aSubShapes->isBound(aBottomFace)) {
201       aBottomFace = aSubShapes->find(aBottomFace);
202     }
203     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
204   }
205
206   //Insert top face
207   std::string aTopName = "TopFace";
208   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
209   if (!aTopFace->isNull()) {
210     if (aSubShapes->isBound(aTopFace)) {
211       aTopFace = aSubShapes->find(aTopFace);
212     }
213     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
214   }
215 }
216
217 //============================================================================
218 void FeaturesPlugin_Extrusion::clearResult()
219 {
220   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
221   std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
222   aResultBody->store(anEmptyShape);
223   setResult(aResultBody);
224 }