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