Salome HOME
17f3158ddc9d88b631d7ebc654b4e016987f45ea
[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::setSketchObjectToList()
122 {
123   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
124   if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
125     return;
126   }
127
128   AttributeReferencePtr aSketchLauncherRef = reference(SKETCH_ID());
129   if(!aSketchLauncherRef.get() || !aSketchLauncherRef->isInitialized()) {
130     return;
131   }
132
133   FeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aSketchLauncherRef->value());
134
135   if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
136     return;
137   }
138
139   ResultPtr aSketchRes = aSketchFeature->results().front();
140   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
141   if(!aConstruction.get()) {
142     return;
143   }
144
145   if(aBaseObjectsSelectionList->size() == 0) {
146     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
147   }
148 }
149
150 //=================================================================================================
151 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
152                                                    const bool theIsMakeShells)
153 {
154   theBaseShapesList.clear();
155
156   ListOfShape aBaseFacesList;
157   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
158   if(!aBaseObjectsSelectionList.get()) {
159     setError("Error: Could not get base objects selection list.");
160     return;
161   }
162   if(aBaseObjectsSelectionList->size() == 0) {
163     setError("Error: Base objects list is empty.");
164     return;
165   }
166   for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
167     AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
168     if(!aBaseObjectSelection.get()) {
169       setError("Error: One of the selected base objects is empty.");
170       return;
171     }
172     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
173     if(aBaseShape.get() && !aBaseShape->isNull()) {
174       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
175       if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
176          aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
177         setError("Error: Selected shapes has unsupported type.");
178         return;
179       }
180       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
181                                    theBaseShapesList.push_back(aBaseShape);
182     } else {
183       // This may be the whole sketch result selected, check and get faces.
184       ResultConstructionPtr aConstruction =
185         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
186       if(!aConstruction.get()) {
187         setError("Error: One of selected sketches does not have results.");
188         return;
189       }
190       int aFacesNum = aConstruction->facesNum();
191       if(aFacesNum == 0) {
192         // Probably it can be construction.
193         aBaseShape = aConstruction->shape();
194         if(aBaseShape.get() && !aBaseShape->isNull()) {
195           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
196           if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
197              aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
198             setError("Error: Selected shapes has unsupported type.");
199             return;
200           }
201           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
202                                        theBaseShapesList.push_back(aBaseShape);
203         }
204       } else {
205         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
206           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
207           if(!aBaseFace.get() || aBaseFace->isNull()) {
208             setError("Error: One of the faces on selected sketch is Null.");
209             return;
210           }
211           aBaseFacesList.push_back(aBaseFace);
212         }
213       }
214     }
215   }
216
217   // Searching faces with common edges.
218   if(theIsMakeShells) {
219     ListOfShape aShells;
220     ListOfShape aFreeFaces;
221     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
222     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
223     theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
224     theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
225   } else {
226     theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
227   }
228 }
229
230 //=================================================================================================
231 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
232 {
233   // Check that algo is done.
234   if(!theMakeShape->isDone()) {
235     setError("Error: " + getKind() + " algorithm failed.");
236     return false;
237   }
238
239   // Check if shape is not null.
240   if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
241     setError("Error: Resulting shape is null.");
242     return false;
243   }
244
245   // Check that resulting shape is valid.
246   if(!theMakeShape->isValid()) {
247     setError("Error: Resulting shape is not valid.");
248     return false;
249   }
250
251   return true;
252 }
253
254 //=================================================================================================
255 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
256                                                  const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
257                                                  const int theResultIndex)
258 {
259   // Create result body.
260   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
261
262   // Store generated shape.
263   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
264
265   // Store generated edges/faces.
266   int aGenTag = 1;
267   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape, aGenTag);
268
269   setResult(aResultBody, theResultIndex);
270 }
271
272 //=================================================================================================
273 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
274                                                             const GeomShapePtr theBaseShape,
275                                                             const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
276                                                             int& theTag)
277 {
278   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
279   GeomAPI_Shape::ShapeType aShapeTypeToExplode;
280   std::string aGenName = "Generated_";
281
282   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
283   switch(aBaseShapeType) {
284     case GeomAPI_Shape::VERTEX: {
285       aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
286       aGenName += "Edge";
287       break;
288     }
289     case GeomAPI_Shape::EDGE:
290     case GeomAPI_Shape::WIRE: {
291       std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
292       GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
293       ListOfShape aV1History, aV2History;
294       theMakeShape->generated(aV1, aV1History);
295       theMakeShape->generated(aV2, aV2History);
296       theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
297       theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
298     }
299     case GeomAPI_Shape::FACE:
300     case GeomAPI_Shape::SHELL: {
301       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
302       aGenName += "Face";
303       break;
304     }
305   }
306   theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, aShapeTypeToExplode,
307                                               theTag++, aGenName, *aMapOfSubShapes.get());
308
309   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
310   if(aMakeSweep.get()) {
311     // Store from shapes.
312     storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->fromShapes(), "From_", theTag);
313
314     // Store to shapes.
315     storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->toShapes(), "To_", theTag);
316   }
317 }
318
319 //=================================================================================================
320 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
321                                                  const GeomAPI_Shape::ShapeType theBaseShapeType,
322                                                  const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
323                                                  const ListOfShape& theShapes,
324                                                  const std::string theName,
325                                                  int& theTag)
326 {
327   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
328   std::string aShapeTypeStr = "Face";
329   switch(theBaseShapeType) {
330     case GeomAPI_Shape::VERTEX: {
331       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
332       aShapeTypeStr = "Vertex";
333       break;
334     }
335     case GeomAPI_Shape::EDGE:
336     case GeomAPI_Shape::WIRE: {
337       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
338       aShapeTypeStr = "Edge";
339       break;
340     }
341     case GeomAPI_Shape::FACE:
342     case GeomAPI_Shape::SHELL: {
343       aShapeTypeToExplore = GeomAPI_Shape::FACE;
344       aShapeTypeStr = "Face";
345       break;
346     }
347   }
348
349   // Store shapes.
350   int aShapeIndex = 1;
351   std::string aName = theName + aShapeTypeStr;
352   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
353     GeomShapePtr aShape = *anIt;
354     for(GeomAPI_ShapeExplorer anExp(aShape, aShapeTypeToExplore); anExp.more(); anExp.next()) {
355       GeomShapePtr aSubShape = anExp.current();
356       if(theMapOfSubShapes->isBound(aSubShape)) {
357         aSubShape = theMapOfSubShapes->find(aSubShape);
358       }
359       std::ostringstream aStr;
360       aStr << aName << "_" << aShapeIndex++;
361       theResultBody->generated(aSubShape, aStr.str(), theTag++);
362     }
363   }
364 }