Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: extrusion cut.
[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
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_AttributeReference.h>
18
19 #include <GeomAlgoAPI_CompoundBuilder.h>
20 #include <GeomAlgoAPI_Prism.h>
21 #include <GeomAlgoAPI_ShapeTools.h>
22
23 #include <sstream>
24
25 //=================================================================================================
26 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
27 {
28 }
29
30 //=================================================================================================
31 void FeaturesPlugin_Extrusion::initAttributes()
32 {
33   AttributeSelectionListPtr aSelection = 
34     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36   // extrusion works with faces always
37   aSelection->setSelectionType("FACE");
38
39   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
40
41   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
42   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
43
44   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
46
47   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
49
50   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
52
53   // Composite Sketch attribute
54   data()->addAttribute(FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID(),
55                        ModelAPI_AttributeReference::typeId());
56   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
57                        FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID());
58 }
59
60 //=================================================================================================
61 void FeaturesPlugin_Extrusion::execute()
62 {
63   /// feature extrusion does not have the next attribute
64   AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
65   if (aFacesSelectionList.get() && !aFacesSelectionList->isInitialized()) {
66     AttributeReferencePtr aSketchAttr = reference(SKETCH_OBJECT_ID());
67     if (aSketchAttr.get() && aSketchAttr->isInitialized())
68       setSketchObjectToList();
69   }
70
71   // Getting faces.
72   ListOfShape aFacesList;
73   for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
74     AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
75     std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
76     if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
77       aFacesList.push_back(aFaceShape);
78     } else { // This may be the whole sketch result selected, check and get faces.
79       ResultPtr aContext = aFaceSel->context();
80       std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
81       if(!aContextShape.get()) {
82         static const std::string aContextError = "Error: The selection context is bad.";
83         setError(aContextError);
84         return;
85       }
86       ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
87       if(!aConstruction.get()) {
88         static const std::string aFaceError = "Error: Can not find basis for extrusion.";
89         setError(aFaceError);
90         return;
91       }
92       int aFacesNum = aConstruction->facesNum();
93       for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
94         aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
95         aFacesList.push_back(aFaceShape);
96       }
97     }
98   }
99
100   // Getting sizes.
101   double aToSize = 0.0;
102   double aFromSize = 0.0;
103
104   if(string(CREATION_METHOD())->value() == "BySizes") {
105     aToSize = real(TO_SIZE_ID())->value();
106     aFromSize =  real(FROM_SIZE_ID())->value();
107   } else {
108     aToSize = real(TO_OFFSET_ID())->value();
109     aFromSize =  real(FROM_OFFSET_ID())->value();
110   }
111
112   // Getting bounding planes.
113   std::shared_ptr<GeomAPI_Shape> aToShape;
114   std::shared_ptr<GeomAPI_Shape> aFromShape;
115
116   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
117     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
118     if(anObjRef.get() != NULL) {
119       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
120       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
121         aToShape =  anObjRef->context()->shape();
122       }
123     }
124     anObjRef = selection(FROM_OBJECT_ID());
125     if(anObjRef.get() != NULL) {
126       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
127       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
128         aFromShape = anObjRef->context()->shape();
129       }
130     }
131   }
132
133   // Searching faces with common edges.
134   ListOfShape aShells;
135   ListOfShape aFreeFaces;
136   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
137   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
138   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
139
140   // Generating result for each shell and face.
141   int aResultIndex = 0;
142   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
143     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
144
145     GeomAlgoAPI_Prism aPrismAlgo(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
146     if(!aPrismAlgo.isDone()) {
147       static const std::string aPrismAlgoError = "Error: Extrusion algorithm failed.";
148       setError(aPrismAlgoError);
149       aResultIndex = 0;
150       break;
151     }
152
153     // Check if shape is valid
154     if(!aPrismAlgo.shape().get() || aPrismAlgo.shape()->isNull()) {
155       static const std::string aShapeError = "Error: Resulting shape is Null.";
156       setError(aShapeError);
157       aResultIndex = 0;
158       break;
159     }
160     if(!aPrismAlgo.isValid()) {
161       std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
162       setError(aPrismAlgoError);
163       aResultIndex = 0;
164       break;
165     }
166
167     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
168     loadNamingDS(aPrismAlgo, aResultBody, aBaseShape);
169     setResult(aResultBody, aResultIndex);
170     aResultIndex++;
171   }
172
173   removeResults(aResultIndex);
174 }
175
176 //=================================================================================================
177 void FeaturesPlugin_Extrusion::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
178 {
179   AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
180   if (aFacesSelectionList.get() && aFacesSelectionList->size() > 0)
181     aFacesSelectionList->clear();
182 }
183
184 //=================================================================================================
185 void FeaturesPlugin_Extrusion::loadNamingDS(GeomAlgoAPI_Prism& thePrismAlgo,
186                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
187                                             std::shared_ptr<GeomAPI_Shape> theBasis)
188 {
189   //load result
190   theResultBody->storeGenerated(theBasis, thePrismAlgo.shape());
191
192   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = thePrismAlgo.mapOfSubShapes();
193
194   //Insert lateral face : Face from Edge
195   const std::string aLatName = "LateralFace";
196   const int aLatTag = 1;
197   theResultBody->loadAndOrientGeneratedShapes(&thePrismAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
198
199   //Insert to faces
200   int aToFaceIndex = 1;
201   const std::string aToName = "ToFace";
202   int aToTag = 2;
203   const ListOfShape& aToFaces = thePrismAlgo.toShapes();
204   for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
205     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
206     if(aSubShapes->isBound(aToFace)) {
207       aToFace = aSubShapes->find(aToFace);
208     }
209     std::ostringstream aStr;
210     aStr << aToName << "_" << aToFaceIndex++;
211     theResultBody->generated(aToFace, aStr.str(), aToTag++);
212   }
213
214   //Insert from faces
215   int aFromFaceIndex = 1;
216   const std::string aFromName = "FromFace";
217   int aFromTag = aToTag > 10000 ? aToTag : 10000;
218   const ListOfShape& aFromFaces = thePrismAlgo.fromShapes();
219   for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
220     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
221     if(aSubShapes->isBound(aFromFace)) {
222       aFromFace = aSubShapes->find(aFromFace);
223     }
224     std::ostringstream aStr;
225     aStr << aFromName << "_" << aFromFaceIndex++;
226     theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
227   }
228 }
229
230 //=================================================================================================
231 void FeaturesPlugin_Extrusion::setSketchObjectToList()
232 {
233   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
234                                                        reference(SKETCH_OBJECT_ID())->value());
235
236   if(aSketchFeature.get() && !aSketchFeature->results().empty()) {
237     ResultPtr aSketchRes = aSketchFeature->results().front();
238     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
239     if(aConstruction.get()) {
240       AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
241       if (aFacesSelectionList.get() && aFacesSelectionList->size() == 0)
242         aFacesSelectionList->append(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
243     }
244   }
245 }
246