Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/shaper
[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_BodyBuilder.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_ResultBody.h>
17
18 #include <GeomAlgoAPI_CompoundBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Lin.h>
22
23 #include <sstream>
24
25 //=================================================================================================
26 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
27 {
28 }
29
30 //=================================================================================================
31 void FeaturesPlugin_Revolution::initAttributes()
32 {
33   AttributeSelectionListPtr aSelection = 
34     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36   // revolution works with faces always
37   aSelection->setSelectionType("FACE");
38
39   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
40
41   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
42
43   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
44   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
45
46   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
48
49   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
51
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
54 }
55
56 //=================================================================================================
57 void FeaturesPlugin_Revolution::execute()
58 {
59   // Getting faces.
60   ListOfShape aFacesList;
61   AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
62   for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
63     AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
64     std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
65     if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
66       aFacesList.push_back(aFaceShape);
67     } else { // This may be the whole sketch result selected, check and get faces.
68       ResultPtr aContext = aFaceSel->context();
69       std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
70       if(!aContextShape.get()) {
71         static const std::string aContextError = "Error: The selection context is bad.";
72         setError(aContextError);
73         return;
74       }
75       ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
76       if(!aConstruction.get()) {
77         static const std::string aFaceError = "Error: Can not find basis for revolution.";
78         setError(aFaceError);
79         return;
80       }
81       int aFacesNum = aConstruction->facesNum();
82       for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
83         aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
84         aFacesList.push_back(aFaceShape);
85       }
86     }
87   }
88
89   //Getting axis.
90   std::shared_ptr<GeomAPI_Ax1> anAxis;
91   std::shared_ptr<GeomAPI_Edge> anEdge;
92   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
93   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
94     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
95   } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
96     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
97   }
98   if(anEdge) {
99     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
100   }
101
102   // Getting angles.
103   double aToAngle = real(TO_ANGLE_ID())->value();
104   double aFromAngle = real(FROM_ANGLE_ID())->value();
105
106   if(string(CREATION_METHOD())->value() == "ByAngles") {
107     aToAngle = real(TO_ANGLE_ID())->value();
108     aFromAngle =  real(FROM_ANGLE_ID())->value();
109   } else {
110     aToAngle = real(TO_OFFSET_ID())->value();
111     aFromAngle =  real(FROM_OFFSET_ID())->value();
112   }
113
114   // Getting bounding planes.
115   std::shared_ptr<GeomAPI_Shape> aToShape;
116   std::shared_ptr<GeomAPI_Shape> aFromShape;
117
118   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
119     anObjRef = selection(TO_OBJECT_ID());
120     if(anObjRef.get() != NULL) {
121       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
122       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
123         aToShape =  anObjRef->context()->shape();
124       }
125     }
126     anObjRef = selection(FROM_OBJECT_ID());
127     if(anObjRef.get() != NULL) {
128       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
129       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
130         aFromShape = anObjRef->context()->shape();
131       }
132     }
133   }
134
135   // Searching faces with common edges.
136   ListOfShape aShells;
137   ListOfShape aFreeFaces;
138   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
139   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
140   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
141
142   // Generating result for each shell and face.
143   int aResultIndex = 0;
144   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
145     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
146
147     GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
148     if(!aRevolAlgo.isDone()) {
149       static const std::string aPrismAlgoError = "Error: Revolution algorithm failed.";
150       setError(aPrismAlgoError);
151       aResultIndex = 0;
152       break;
153     }
154
155     // Check if shape is valid
156     if(!aRevolAlgo.shape().get() || aRevolAlgo.shape()->isNull()) {
157       static const std::string aShapeError = "Error: Resulting shape is Null.";
158       setError(aShapeError);
159       aResultIndex = 0;
160       break;
161     }
162     if(!aRevolAlgo.isValid()) {
163       std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
164       setError(aPrismAlgoError);
165       aResultIndex = 0;
166       break;
167     }
168
169     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
170     loadNamingDS(aRevolAlgo, aResultBody, aBaseShape);
171     setResult(aResultBody, aResultIndex);
172     aResultIndex++;
173   }
174
175   removeResults(aResultIndex);
176 }
177
178 //=================================================================================================
179 void FeaturesPlugin_Revolution::loadNamingDS(GeomAlgoAPI_Revolution& theRevolAlgo,
180                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody,
181                                              std::shared_ptr<GeomAPI_Shape> theBasis)
182 {
183   //load result
184   theResultBody->storeGenerated(theBasis, theRevolAlgo.shape());
185
186   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRevolAlgo.mapOfSubShapes();
187
188   //Insert lateral face : Face from Edge
189   const std::string aLatName = "LateralFace";
190   const int aLatTag = 1;
191   theResultBody->loadAndOrientGeneratedShapes(&theRevolAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
192
193   //Insert to faces
194   int aToFaceIndex = 1;
195   const std::string aToName = "ToFace";
196   int aToTag = 2;
197   const ListOfShape& aToFaces = theRevolAlgo.toFaces();
198   for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
199     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
200     if(aSubShapes->isBound(aToFace)) {
201       aToFace = aSubShapes->find(aToFace);
202     }
203     std::ostringstream aStr;
204     aStr << aToName << "_" << aToFaceIndex++;
205     theResultBody->generated(aToFace, aStr.str(), aToTag++);
206   }
207
208   //Insert from faces
209   int aFromFaceIndex = 1;
210   const std::string aFromName = "FromFace";
211   int aFromTag = aToTag > 10000 ? aToTag : 10000;
212   const ListOfShape& aFromFaces = theRevolAlgo.fromFaces();
213   for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
214     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
215     if(aSubShapes->isBound(aFromFace)) {
216       aFromFace = aSubShapes->find(aFromFace);
217     }
218     std::ostringstream aStr;
219     aStr << aFromName << "_" << aFromFaceIndex++;
220     theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
221   }
222 }