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