Salome HOME
b8bd1ee23397beff8c0285659dac7140c3cbf29e
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeSketch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_CompositeSketch.cpp
4 // Created:     11 September 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_CompositeSketch.h>
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_AttributeReference.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <GeomAlgoAPI_Prism.h>
18 #include <GeomAlgoAPI_Revolution.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAPI_ShapeExplorer.h>
21
22 #include <sstream>
23
24 //=================================================================================================
25 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
26 {
27   // Initialize sketch launcher.
28   if(theInitFlags & InitSketchLauncher) {
29     data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
30     ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
31   }
32
33   // Initialize selection list.
34   if(theInitFlags & InitBaseObjectsList) {
35     data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
36   }
37 }
38
39 //=================================================================================================
40 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
41 {
42   FeaturePtr aNew = document()->addFeature(theID, false);
43   if(aNew) {
44     data()->reference(SKETCH_ID())->setValue(aNew);
45   }
46
47   // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
48   document()->setCurrentFeature(aNew, false);
49   return aNew;
50 }
51
52 //=================================================================================================
53 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
54 {
55   ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
56   return aObj.get() ? 1 : 0;
57 }
58
59 //=================================================================================================
60 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex, bool forTree)
61 {
62   if(theIndex == 0) {
63     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
64   }
65
66   return std::shared_ptr<ModelAPI_Feature>();
67 }
68
69 //=================================================================================================
70 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
71 {
72   if(theIndex == 0) {
73     FeaturePtr aFeature =
74       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
75     if(aFeature.get()) {
76       return aFeature->data()->featureId();
77     }
78   }
79
80   return -1;
81 }
82
83 //=================================================================================================
84 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
85 {
86   // Check is this feature of result
87   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
88   if(!aFeature.get()) {
89     return false;
90   }
91
92   ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
93   return aSub == theObject;
94 }
95
96 //=================================================================================================
97 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
98 {
99   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
100   if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
101     aBaseObjectsSelectionList->clear();
102   }
103
104   reference(SKETCH_ID())->setValue(ObjectPtr());
105 }
106
107 //=================================================================================================
108 void FeaturesPlugin_CompositeSketch::erase()
109 {
110   if(data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
111     FeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_Feature>(reference(SKETCH_ID())->value());
112     if(aSketch.get() && aSketch->data()->isValid()) {
113       document()->removeFeature(aSketch);
114     }
115   }
116
117   ModelAPI_CompositeFeature::erase();
118 }
119
120 //=================================================================================================
121 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
122                                                    const bool theIsMakeShells)
123 {
124   theBaseShapesList.clear();
125
126   ListOfShape aBaseFacesList;
127   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
128   if(!aBaseObjectsSelectionList.get()) {
129     setError("Error: Could not get base objects selection list.");
130     return;
131   }
132   if(aBaseObjectsSelectionList->size() == 0) {
133     setError("Error: Base objects list is empty.");
134     return;
135   }
136   for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
137     AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
138     if(!aBaseObjectSelection.get()) {
139       setError("Error: One of the selected base objects is empty.");
140       return;
141     }
142     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
143     if(aBaseShape.get() && !aBaseShape->isNull()) {
144       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
145       if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
146          aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
147         setError("Error: Selected shapes has unsupported type.");
148         return;
149       }
150       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
151                                    theBaseShapesList.push_back(aBaseShape);
152     } else {
153       // This may be the whole sketch result selected, check and get faces.
154       ResultConstructionPtr aConstruction =
155         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
156       if(!aConstruction.get()) {
157         setError("Error: One of selected sketches does not have results.");
158         return;
159       }
160       int aFacesNum = aConstruction->facesNum();
161       if(aFacesNum == 0) {
162         // Probably it can be construction.
163         aBaseShape = aConstruction->shape();
164         if(aBaseShape.get() && !aBaseShape->isNull()) {
165           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
166           if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
167              aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
168             setError("Error: Selected shapes has unsupported type.");
169             return;
170           }
171           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
172                                        theBaseShapesList.push_back(aBaseShape);
173         }
174       } else {
175         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
176           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
177           if(!aBaseFace.get() || aBaseFace->isNull()) {
178             setError("Error: One of the faces on selected sketch is Null.");
179             return;
180           }
181           aBaseFacesList.push_back(aBaseFace);
182         }
183       }
184     }
185   }
186
187   // Searching faces with common edges.
188   if(theIsMakeShells) {
189     ListOfShape aShells;
190     ListOfShape aFreeFaces;
191     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
192     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
193     theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
194     theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
195   } else {
196     theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
197   }
198 }
199
200 //=================================================================================================
201 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
202 {
203   // Check that algo is done.
204   if(!theMakeShape->isDone()) {
205     setError("Error: " + getKind() + " algorithm failed.");
206     return false;
207   }
208
209   // Check if shape is not null.
210   if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
211     setError("Error: Resulting shape is null.");
212     return false;
213   }
214
215   // Check that resulting shape is valid.
216   if(!theMakeShape->isValid()) {
217     setError("Error: Resulting shape is not valid.");
218     return false;
219   }
220
221   return true;
222 }
223
224 //=================================================================================================
225 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
226                                                  const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
227                                                  const int theResultIndex)
228 {
229   // Create result body.
230   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
231
232   // Store generated shape.
233   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
234
235   // Store generated edges/faces.
236   int aGenTag = 1;
237   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape, aGenTag);
238
239   setResult(aResultBody, theResultIndex);
240 }
241
242 //=================================================================================================
243 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
244                                                             const GeomShapePtr theBaseShape,
245                                                             const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
246                                                             int& theTag)
247 {
248   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
249   GeomAPI_Shape::ShapeType aShapeTypeToExplode;
250   std::string aGenName = "Generated_";
251
252   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
253   switch(aBaseShapeType) {
254     case GeomAPI_Shape::VERTEX: {
255       aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
256       aGenName += "Edge";
257       break;
258     }
259     case GeomAPI_Shape::EDGE:
260     case GeomAPI_Shape::WIRE: {
261       std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
262       GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
263       ListOfShape aV1History, aV2History;
264       theMakeShape->generated(aV1, aV1History);
265       theMakeShape->generated(aV2, aV2History);
266       theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
267       theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
268     }
269     case GeomAPI_Shape::FACE:
270     case GeomAPI_Shape::SHELL: {
271       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
272       aGenName += "Face";
273       break;
274     }
275   }
276   theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, aShapeTypeToExplode,
277                                               theTag++, aGenName, *aMapOfSubShapes.get());
278
279   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
280   if(aMakeSweep.get()) {
281     // Store from shapes.
282     storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->fromShapes(), "From_", theTag);
283
284     // Store to shapes.
285     storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->toShapes(), "To_", theTag);
286   }
287 }
288
289 //=================================================================================================
290 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
291                                                  const GeomAPI_Shape::ShapeType theBaseShapeType,
292                                                  const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
293                                                  const ListOfShape& theShapes,
294                                                  const std::string theName,
295                                                  int& theTag)
296 {
297   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
298   std::string aShapeTypeStr = "Face";
299   switch(theBaseShapeType) {
300     case GeomAPI_Shape::VERTEX: {
301       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
302       aShapeTypeStr = "Vertex";
303       break;
304     }
305     case GeomAPI_Shape::EDGE:
306     case GeomAPI_Shape::WIRE: {
307       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
308       aShapeTypeStr = "Edge";
309       break;
310     }
311     case GeomAPI_Shape::FACE:
312     case GeomAPI_Shape::SHELL: {
313       aShapeTypeToExplore = GeomAPI_Shape::FACE;
314       aShapeTypeStr = "Face";
315       break;
316     }
317   }
318
319   // Store shapes.
320   int aShapeIndex = 1;
321   std::string aName = theName + aShapeTypeStr;
322   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
323     GeomShapePtr aShape = *anIt;
324     for(GeomAPI_ShapeExplorer anExp(aShape, aShapeTypeToExplore); anExp.more(); anExp.next()) {
325       GeomShapePtr aSubShape = anExp.current();
326       if(theMapOfSubShapes->isBound(aSubShape)) {
327         aSubShape = theMapOfSubShapes->find(aSubShape);
328       }
329       std::ostringstream aStr;
330       aStr << aName << "_" << aShapeIndex++;
331       theResultBody->generated(aSubShape, aStr.str(), theTag++);
332     }
333   }
334 }