Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: revolution/revolution...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Revolution.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Revolution.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_Revolution.h"
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_BodyBuilder.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_ResultBody.h>
18
19 #include <GeomAlgoAPI_CompoundBuilder.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
21 #include <GeomAPI_Edge.h>
22 #include <GeomAPI_Lin.h>
23
24 #include <sstream>
25
26 //=================================================================================================
27 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
28 {
29 }
30
31 //=================================================================================================
32 void FeaturesPlugin_Revolution::initAttributes()
33 {
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   // revolution works with faces always
38   aSelection->setSelectionType("FACE");
39
40   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
41
42   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43
44   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(FROM_ANGLE_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_Revolution::execute()
65 {
66   /// sub feature of the composite should be set in the base list
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   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 revolution.";
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 axis.
105   std::shared_ptr<GeomAPI_Ax1> anAxis;
106   std::shared_ptr<GeomAPI_Edge> anEdge;
107   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
108   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
109     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
110   } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
111     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
112   }
113   if(anEdge) {
114     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
115   }
116
117   // Getting angles.
118   double aToAngle = real(TO_ANGLE_ID())->value();
119   double aFromAngle = real(FROM_ANGLE_ID())->value();
120
121   if(string(CREATION_METHOD())->value() == "ByAngles") {
122     aToAngle = real(TO_ANGLE_ID())->value();
123     aFromAngle =  real(FROM_ANGLE_ID())->value();
124   } else {
125     aToAngle = real(TO_OFFSET_ID())->value();
126     aFromAngle =  real(FROM_OFFSET_ID())->value();
127   }
128
129   // Getting bounding planes.
130   std::shared_ptr<GeomAPI_Shape> aToShape;
131   std::shared_ptr<GeomAPI_Shape> aFromShape;
132
133   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
134     anObjRef = selection(TO_OBJECT_ID());
135     if(anObjRef.get() != NULL) {
136       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
137       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
138         aToShape =  anObjRef->context()->shape();
139       }
140     }
141     anObjRef = selection(FROM_OBJECT_ID());
142     if(anObjRef.get() != NULL) {
143       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
144       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
145         aFromShape = anObjRef->context()->shape();
146       }
147     }
148   }
149
150   // Searching faces with common edges.
151   ListOfShape aShells;
152   ListOfShape aFreeFaces;
153   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
154   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
155   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
156
157   // Generating result for each shell and face.
158   int aResultIndex = 0;
159   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
160     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
161
162     GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
163     if(!aRevolAlgo.isDone()) {
164       static const std::string aPrismAlgoError = "Error: Revolution algorithm failed.";
165       setError(aPrismAlgoError);
166       aResultIndex = 0;
167       break;
168     }
169
170     // Check if shape is valid
171     if(!aRevolAlgo.shape().get() || aRevolAlgo.shape()->isNull()) {
172       static const std::string aShapeError = "Error: Resulting shape is Null.";
173       setError(aShapeError);
174       aResultIndex = 0;
175       break;
176     }
177     if(!aRevolAlgo.isValid()) {
178       std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
179       setError(aPrismAlgoError);
180       aResultIndex = 0;
181       break;
182     }
183
184     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
185     loadNamingDS(aRevolAlgo, aResultBody, aBaseShape);
186     setResult(aResultBody, aResultIndex);
187     aResultIndex++;
188   }
189
190   removeResults(aResultIndex);
191 }
192
193 //=================================================================================================
194 void FeaturesPlugin_Revolution::loadNamingDS(GeomAlgoAPI_Revolution& theRevolAlgo,
195                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody,
196                                              std::shared_ptr<GeomAPI_Shape> theBasis)
197 {
198   //load result
199   theResultBody->storeGenerated(theBasis, theRevolAlgo.shape());
200
201   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRevolAlgo.mapOfSubShapes();
202
203   //Insert lateral face : Face from Edge
204   const std::string aLatName = "LateralFace";
205   const int aLatTag = 1;
206   theResultBody->loadAndOrientGeneratedShapes(&theRevolAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
207
208   //Insert to faces
209   int aToFaceIndex = 1;
210   const std::string aToName = "ToFace";
211   int aToTag = 2;
212   const ListOfShape& aToFaces = theRevolAlgo.toShapes();
213   for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
214     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
215     if(aSubShapes->isBound(aToFace)) {
216       aToFace = aSubShapes->find(aToFace);
217     }
218     std::ostringstream aStr;
219     aStr << aToName << "_" << aToFaceIndex++;
220     theResultBody->generated(aToFace, aStr.str(), aToTag++);
221   }
222
223   //Insert from faces
224   int aFromFaceIndex = 1;
225   const std::string aFromName = "FromFace";
226   int aFromTag = aToTag > 10000 ? aToTag : 10000;
227   const ListOfShape& aFromFaces = theRevolAlgo.fromShapes();
228   for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
229     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
230     if(aSubShapes->isBound(aFromFace)) {
231       aFromFace = aSubShapes->find(aFromFace);
232     }
233     std::ostringstream aStr;
234     aStr << aFromName << "_" << aFromFaceIndex++;
235     theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
236   }
237 }