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