Salome HOME
Updated generated shapes storing.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeSketch.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <FeaturesPlugin_CompositeSketch.h>
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_Prism.h>
32 #include <GeomAlgoAPI_Revolution.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_SketchBuilder.h>
35
36 #include <GeomAPI_PlanarEdges.h>
37 #include <GeomAPI_ShapeExplorer.h>
38
39 #include <map>
40 #include <sstream>
41
42 static void storeSubShape(ResultBodyPtr theResultBody,
43                           const GeomShapePtr theShape,
44                           const GeomAPI_Shape::ShapeType theType,
45                           const std::string theName,
46                           int& theShapeIndex);
47
48 //=================================================================================================
49 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
50 {
51   // Initialize sketch launcher.
52   if(theInitFlags & InitSketchLauncher) {
53     data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
54     ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
55   }
56
57   // Initialize selection list.
58   if(theInitFlags & InitBaseObjectsList) {
59     data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
60   }
61 }
62
63 //=================================================================================================
64 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
65 {
66   FeaturePtr aNew = document()->addFeature(theID, false);
67   if(aNew) {
68     data()->reference(SKETCH_ID())->setValue(aNew);
69   }
70
71   // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
72   document()->setCurrentFeature(aNew, false);
73   return aNew;
74 }
75
76 //=================================================================================================
77 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
78 {
79   ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
80   return aObj.get() ? 1 : 0;
81 }
82
83 //=================================================================================================
84 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
85                                                                              bool forTree)
86 {
87   if(theIndex == 0) {
88     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
89   }
90
91   return std::shared_ptr<ModelAPI_Feature>();
92 }
93
94 //=================================================================================================
95 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
96 {
97   if(theIndex == 0) {
98     FeaturePtr aFeature =
99       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
100     if(aFeature.get()) {
101       return aFeature->data()->featureId();
102     }
103   }
104
105   return -1;
106 }
107
108 //=================================================================================================
109 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
110 {
111   // Check is this feature of result
112   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
113   if(!aFeature.get()) {
114     return false;
115   }
116
117   ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
118   return aSub == theObject;
119 }
120
121 //=================================================================================================
122 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
123 {
124   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
125   if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
126     aBaseObjectsSelectionList->clear();
127   }
128
129   reference(SKETCH_ID())->setValue(ObjectPtr());
130 }
131
132 //=================================================================================================
133 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
134                                                    const bool theIsMakeShells)
135 {
136   theBaseShapesList.clear();
137
138   ListOfShape aBaseFacesList;
139   std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
140   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
141   if(!aBaseObjectsSelectionList.get()) {
142     setError("Error: Could not get base objects selection list.");
143     return;
144   }
145   if(aBaseObjectsSelectionList->size() == 0) {
146     setError("Error: Base objects list is empty.");
147     return;
148   }
149   for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
150     AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
151     if(!aBaseObjectSelection.get()) {
152       setError("Error: Selected base object is empty.");
153       return;
154     }
155     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
156     if(aBaseShape.get() && !aBaseShape->isNull()) {
157       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
158       if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
159         setError("Error: Selected shapes has unsupported type.");
160         return;
161       }
162       ResultConstructionPtr aConstruction =
163         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
164       if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
165           aST == GeomAPI_Shape::WIRE) {
166         // It is a wire on the sketch, store it to make face later.
167         aSketchWiresMap[aConstruction].push_back(aBaseShape);
168         continue;
169       } else {
170       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
171                                    theBaseShapesList.push_back(aBaseShape);
172       }
173     } else {
174       // This may be the whole sketch result selected, check and get faces.
175       ResultConstructionPtr aConstruction =
176         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
177       if(!aConstruction.get()) {
178         setError("Error: Selected sketches does not have results.");
179         return;
180       }
181       int aFacesNum = aConstruction->facesNum();
182       if(aFacesNum == 0) {
183         // Probably it can be construction.
184         aBaseShape = aConstruction->shape();
185         if(aBaseShape.get() && !aBaseShape->isNull()) {
186           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
187           if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
188              aST != GeomAPI_Shape::WIRE &&
189              aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
190             setError("Error: Selected shapes has unsupported type.");
191             return;
192           }
193           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
194                                        theBaseShapesList.push_back(aBaseShape);
195         }
196       } else {
197         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
198           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
199           if(!aBaseFace.get() || aBaseFace->isNull()) {
200             setError("Error: One of the faces on selected sketch is null.");
201             return;
202           }
203           aBaseFacesList.push_back(aBaseFace);
204         }
205       }
206     }
207   }
208
209   // Make faces from sketch wires.
210   for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
211       anIt != aSketchWiresMap.cend(); ++anIt) {
212     const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
213       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
214     const ListOfShape& aWiresList = (*anIt).second;
215     ListOfShape aFaces;
216     GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
217                                                aSketchPlanarEdges->norm(),
218                                                aWiresList,
219                                                aFaces);
220     aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
221   }
222
223   // Searching faces with common edges.
224   if(theIsMakeShells && aBaseFacesList.size() > 1) {
225     ListOfShape aShells;
226     ListOfShape aFreeFaces;
227     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
228     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
229                                           aShells, aFreeFaces);
230     theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
231     theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
232   } else {
233     theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
234                              aBaseFacesList.end());
235   }
236 }
237
238 //=================================================================================================
239 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(
240   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
241 {
242   // Check that algo is done.
243   if(!theMakeShape->isDone()) {
244     setError("Error: " + getKind() + " algorithm failed.");
245     return false;
246   }
247
248   // Check if shape is not null.
249   if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
250     setError("Error: Resulting shape is null.");
251     return false;
252   }
253
254   // Check that resulting shape is valid.
255   if(!theMakeShape->isValid()) {
256     setError("Error: Resulting shape is not valid.");
257     return false;
258   }
259
260   return true;
261 }
262
263 //=================================================================================================
264 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
265                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
266                                         const int theIndex)
267 {
268   // Create result body.
269   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
270
271   // Store generated shape.
272   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
273
274   // Store generated edges/faces.
275   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
276
277   setResult(aResultBody, theIndex);
278 }
279
280 //=================================================================================================
281 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
282                                         const GeomShapePtr theBaseShape,
283                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
284 {
285   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
286   GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
287
288   switch(aBaseShapeType) {
289     case GeomAPI_Shape::EDGE: {
290             aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
291       break;
292     }
293     case GeomAPI_Shape::WIRE: {
294       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
295       break;
296     }
297     case GeomAPI_Shape::FACE:
298     case GeomAPI_Shape::SHELL: {
299       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
300       break;
301     }
302     case GeomAPI_Shape::COMPOUND: {
303       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
304     }
305   }
306
307   int aLateralIndex = 1;
308   int aBaseEdgeIndex = 1;
309   int aVertexIndex = 1;
310   int aBaseVertexIndex = 1;
311
312   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
313       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
314     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
315   }
316   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
317       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
318     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
319   }
320   // issue #2197: make naming of edges generated from vertices
321   if (aShapeTypeToExplode == GeomAPI_Shape::EDGE) {
322     GeomAPI_DataMapOfShapeShape aFacesFromFromEdges;
323     GeomAPI_ShapeExplorer anEdgeExp(theBaseShape, GeomAPI_Shape::EDGE);
324     for(; anEdgeExp.more(); anEdgeExp.next()) {
325       ListOfShape aGenerated;
326       theMakeShape->generated(anEdgeExp.current(), aGenerated);
327       ListOfShape::iterator aGenIter = aGenerated.begin();
328       for(; aGenIter != aGenerated.end(); aGenIter++) {
329         GeomShapePtr aGen = *aGenIter;
330         if (aGen.get() && !aGen->isNull()) {
331           if ((*aGenIter)->shapeType() == GeomAPI_Shape::FACE) { // normal case
332             aFacesFromFromEdges.bind(aGen, anEdgeExp.current());
333           }
334         }
335       }
336     }
337
338     // closed revolution of 1-3 faces can not distinguish lateral and base edges
339     if (aFacesFromFromEdges.size() <= 3) {
340       bool isClosed = false; // lateral edges are closed (in full revolution)
341       GeomAPI_DataMapOfShapeShape anEdgesFromVertices;
342       GeomAPI_ShapeExplorer aVertExp(theBaseShape, GeomAPI_Shape::VERTEX);
343       for(; aVertExp.more(); aVertExp.next()) {
344         ListOfShape aGenerated;
345         theMakeShape->generated(aVertExp.current(), aGenerated);
346         ListOfShape::iterator aGenIter = aGenerated.begin();
347         for(; aGenIter != aGenerated.end(); aGenIter++) {
348           std::shared_ptr<GeomAPI_Shape> aGenerated = *aGenIter;
349           if (anEdgesFromVertices.isBound(aGenerated)) // already here
350             continue;
351           std::ostringstream aStream;
352           aStream << "Lateral_" << aLateralIndex++;
353           theResultBody->generated(aGenerated, aStream.str());
354
355           anEdgesFromVertices.bind(aGenerated, aVertExp.current());
356           std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aGenerated));
357           isClosed = isClosed || anEdge->isClosed();
358         }
359       }
360       if (isClosed) {
361         GeomAPI_ShapeExplorer anEdgesExp(theMakeShape->shape(), GeomAPI_Shape::EDGE);
362         for(; anEdgesExp.more(); anEdgesExp.next()) {
363           if (!anEdgesFromVertices.isBound(anEdgesExp.current())) {
364             // found a base edge
365             std::ostringstream aStream;
366             aStream << "Base_Edge_" << aBaseEdgeIndex++;
367             theResultBody->generated(anEdgesExp.current(), aStream.str());
368             // only one orientation is needed
369             anEdgesFromVertices.bind(anEdgesExp.current(), anEdgesExp.current());
370           }
371         }
372       } else if (aFacesFromFromEdges.size() == 1) { // 2233: sphere created by the revolution:
373         // vertices at degenerated edges will have the same name
374         GeomAPI_DataMapOfShapeShape aVertices;
375         GeomAPI_ShapeExplorer aVertExp(theMakeShape->shape(), GeomAPI_Shape::VERTEX);
376         for(int anIndex = 1; aVertExp.more(); aVertExp.next()) {
377           if (!aVertices.isBound(aVertExp.current())) {
378             // found a base edge
379             std::ostringstream aStream;
380             aStream << "Vertex_" << aVertexIndex++;
381             theResultBody->generated(aVertExp.current(), aStream.str());
382             // only one orientation is needed
383             aVertices.bind(aVertExp.current(), aVertExp.current());
384           }
385         }
386       }
387     } else { // issue #2197, test case 4 : edges that produce fully-revolved face,
388       // but contain only 2 edges (on apex of revolution)
389       GeomAPI_ShapeExplorer anEdgeExp(theBaseShape, GeomAPI_Shape::EDGE);
390       for(; anEdgeExp.more(); anEdgeExp.next()) {
391         ListOfShape aGenerated;
392         theMakeShape->generated(anEdgeExp.current(), aGenerated);
393         ListOfShape::iterator aGenIter = aGenerated.begin();
394         for(; aGenIter != aGenerated.end(); aGenIter++) {
395           GeomShapePtr aGen = (*aGenIter);
396           if (aGen.get() && !aGen->isNull()) {
397             GeomAPI_ShapeExplorer aFaceEdgeExp(aGen, GeomAPI_Shape::EDGE);
398             int aNumEdges = 0;
399             int aNumClosed = 0;
400             GeomShapePtr aNotClosedEdge;
401             GeomEdgePtr aDegenerateEdge;
402             GeomAPI_DataMapOfShapeShape alreadyIterated;
403             for(; aFaceEdgeExp.more(); aFaceEdgeExp.next()) {
404               std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aFaceEdgeExp.current()));
405               if (anEdge->isDegenerated()) {
406                 aDegenerateEdge = anEdge;
407                 continue;
408               }
409               if (alreadyIterated.isBound(anEdge))
410                 continue;
411               alreadyIterated.bind(anEdge, anEdge);
412               aNumEdges++;
413               if (anEdge->isClosed()) {
414                 aNumClosed++;
415               } else {
416                 aNotClosedEdge = anEdge;
417               }
418             }
419             if (aNumEdges == 2 && aNumClosed == 1) {
420               std::ostringstream aStream;
421               aStream << "Base_Edge_" << aBaseEdgeIndex++;
422               theResultBody->generated(aNotClosedEdge, aStream.str());
423               if (aDegenerateEdge.get()) { // export vertex of the degenerated edge (apex) #2520
424                 GeomAPI_ShapeExplorer anEdgeExp(aDegenerateEdge, GeomAPI_Shape::VERTEX);
425                 if (anEdgeExp.more()) {
426                   std::ostringstream aStream;
427                   aStream << "Base_Vertex_" << aBaseVertexIndex++;
428                   theResultBody->generated(anEdgeExp.current(), aStream.str());
429                 }
430               }
431             }
432           }
433         }
434       }
435     }
436   }
437
438   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
439     std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
440   if(aMakeSweep.get()) {
441     // Store from shapes.
442     storeShapes(theResultBody, aBaseShapeType, aMakeSweep->fromShapes(), "From_");
443
444     // Store to shapes.
445     storeShapes(theResultBody, aBaseShapeType, aMakeSweep->toShapes(), "To_");
446   }
447 }
448
449 //=================================================================================================
450 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
451                               const GeomAPI_Shape::ShapeType theBaseShapeType,
452                               const ListOfShape& theShapes,
453                               const std::string theName)
454 {
455   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
456   std::string aShapeTypeStr = "Face";
457   switch(theBaseShapeType) {
458     case GeomAPI_Shape::VERTEX: {
459       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
460       aShapeTypeStr = "Vertex";
461       break;
462     }
463     case GeomAPI_Shape::EDGE:
464     case GeomAPI_Shape::WIRE: {
465       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
466       aShapeTypeStr = "Edge";
467       break;
468     }
469     case GeomAPI_Shape::FACE:
470     case GeomAPI_Shape::SHELL: {
471       aShapeTypeToExplore = GeomAPI_Shape::FACE;
472       aShapeTypeStr = "Face";
473       break;
474     }
475     case GeomAPI_Shape::COMPOUND: {
476       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
477       break;
478     }
479   }
480
481   // Store shapes.
482   int aShapeIndex = 1;
483   int aFaceIndex = 1;
484   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
485     GeomShapePtr aShape = *anIt;
486
487     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
488       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
489       storeSubShape(theResultBody,
490                     aShape,
491                     aShape->shapeType(),
492                     aName,
493                     aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex);
494     } else {
495       std::string aName = theName + aShapeTypeStr;
496       storeSubShape(theResultBody,
497                     aShape,
498                     aShapeTypeToExplore,
499                     aName,
500                     aShapeIndex);
501       if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
502         aName = theName + "Vertex";
503         storeSubShape(theResultBody,
504                       aShape,
505                       GeomAPI_Shape::VERTEX,
506                       aName,
507                       aShapeIndex);
508       }
509     }
510   }
511 }
512
513 void storeSubShape(ResultBodyPtr theResultBody,
514                    const GeomShapePtr theShape,
515                    const GeomAPI_Shape::ShapeType theType,
516                    const std::string theName,
517                    int& theShapeIndex)
518 {
519   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
520     GeomShapePtr aSubShape = anExp.current();
521     std::ostringstream aStr;
522     aStr << theName << "_" << theShapeIndex++;
523     theResultBody->generated(aSubShape, aStr.str());
524   }
525 }