]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
Extrusion cut execute draft version. No naming!
[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 <GeomAlgoAPI_Extrusion.h>
21 #include <GeomAlgoAPI_FaceBuilder.h>
22 #include <GeomAlgoAPI_Prism.h>
23
24 using namespace std;
25 #define _LATERAL_TAG 1
26 #define _FIRST_TAG 2
27 #define _LAST_TAG 3
28 #define EDGE 6
29
30 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
31 {
32 }
33
34 void FeaturesPlugin_Extrusion::initAttributes()
35 {
36   AttributeSelectionListPtr aSelection = 
37     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38     FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39   // extrusion works with faces always
40   aSelection->setSelectionType("FACE");
41
42   data()->addAttribute(FeaturesPlugin_Extrusion::TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
43   data()->addAttribute(FeaturesPlugin_Extrusion::FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
44
45   data()->addAttribute(FeaturesPlugin_Extrusion::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
46   data()->addAttribute(FeaturesPlugin_Extrusion::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47
48   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
49   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::TO_OBJECT_ID());
50 }
51
52 void FeaturesPlugin_Extrusion::execute()
53 {
54   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
55
56   // Getting bounding planes.
57   std::shared_ptr<GeomAPI_Shape> aFromShape;
58   std::shared_ptr<GeomAPI_Shape> aToShape;
59
60   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
61   if (anObjRef) {
62     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
63   }
64   anObjRef = selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
65   if (anObjRef) {
66     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
67   }
68
69   // Getting sizes.
70   double aFromSize = real(FeaturesPlugin_Extrusion::FROM_SIZE_ID())->value();
71   double aToSize = real(FeaturesPlugin_Extrusion::TO_SIZE_ID())->value();
72
73   // for each selected face generate a result
74   int anIndex = 0, aResultIndex = 0;
75   for(; anIndex < aFaceRefs->size(); anIndex++) {
76     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
77     ResultPtr aContextRes = aFaceRef->context();
78     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
79     if (!aContext.get()) {
80       static const std::string aContextError = "The selection context is bad";
81       setError(aContextError);
82       break;
83     }
84
85     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
86     int aFacesNum = -1; // this mean that "aFace" is used
87     ResultConstructionPtr aConstruction =
88       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
89     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
90       if (aConstruction.get()) {
91         aFacesNum = aConstruction->facesNum();
92       } else {
93         static const std::string aFaceError = "Can not find basis for extrusion";
94         setError(aFaceError);
95         break;
96       }
97     }
98
99     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
100       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
101       std::shared_ptr<GeomAPI_Shape> aBaseShape;
102       if (aFacesNum == -1) {
103         aBaseShape = aValueFace;
104       } else {
105         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
106       }
107
108       GeomAlgoAPI_Prism aFeature(aBaseShape, aFromShape, aFromSize, aToShape, aToSize);
109       if(!aFeature.isDone()) {
110         static const std::string aFeatureError = "Extrusion algorithm failed";
111         setError(aFeatureError);
112         break;
113       }
114
115       // Check if shape is valid
116       if(aFeature.shape()->isNull()) {
117         static const std::string aShapeError = "Resulting shape is Null";
118         setError(aShapeError);
119         break;
120       }
121       if(!aFeature.isValid()) {
122         std::string aFeatureError = "Warning: resulting shape is not valid";
123         setError(aFeatureError);
124         break;
125       }
126       //LoadNamingDS
127       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
128
129       setResult(aResultBody, aResultIndex);
130       aResultIndex++;
131
132       if (aFacesNum == -1)
133         break;
134     }
135   }
136   // remove the rest results if there were produced in the previous pass
137   removeResults(aResultIndex);
138 }
139
140 //============================================================================
141 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
142                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
143                                             std::shared_ptr<GeomAPI_Shape> theBasis,
144                                             std::shared_ptr<GeomAPI_Shape> theContext)
145 {
146   //load result
147   if(theBasis->isEqual(theContext))
148     theResultBody->store(theFeature.shape());
149   else
150     theResultBody->storeGenerated(theBasis, theFeature.shape());
151
152   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
153
154   //Insert lateral face : Face from Edge
155   std::string aLatName = "LateralFace";
156   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes.get());
157
158   //Insert bottom face
159   std::string aBotName = "BottomFace";
160   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
161   if(!aBottomFace->isNull()) {
162     if(aSubShapes->isBound(aBottomFace)) {
163       aBottomFace = aSubShapes->find(aBottomFace);
164     }
165     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
166   }
167
168   //Insert top face
169   std::string aTopName = "TopFace";
170   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
171   if (!aTopFace->isNull()) {
172     if (aSubShapes->isBound(aTopFace)) {
173       aTopFace = aSubShapes->find(aTopFace);
174     }
175     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
176   }
177 }