Salome HOME
Issue #1369: Added "Create Shell" feature.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Sewing.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Sewing.cpp
4 // Created:     25 April 2016
5 // Author:      Dmitry Bobylev
6
7
8 #include "GeomAlgoAPI_Sewing.h"
9
10 #include <BRep_Builder.hxx>
11 #include <BRepBuilderAPI_MakeShell.hxx>
12 #include <BRepBuilderAPI_Sewing.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <TopoDS_Iterator.hxx>
15 #include <TopoDS_Shape.hxx>
16
17 //==================================================================================================
18 GeomAlgoAPI_Sewing::GeomAlgoAPI_Sewing(const ListOfShape& theShapes)
19 {
20   build(theShapes);
21 }
22
23 void GeomAlgoAPI_Sewing::build(const ListOfShape& theShapes)
24 {
25   if(theShapes.empty()) {
26     return;
27   }
28
29   BRepBuilderAPI_Sewing* aSewingBuilder = new BRepBuilderAPI_Sewing();
30   this->setImpl(aSewingBuilder);
31
32   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
33     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
34     aSewingBuilder->Add(aShape);
35   }
36
37   aSewingBuilder->Perform();
38
39   TopoDS_Shape aResult = aSewingBuilder->SewedShape();
40   BRep_Builder aBuilder;
41   TopoDS_Compound aResultCompound;
42   aBuilder.MakeCompound(aResultCompound);
43   for(TopoDS_Iterator anIt(aResult); anIt.More(); anIt.Next()) {
44     const TopoDS_Shape aSubShape = anIt.Value();
45     if(aSubShape.ShapeType() == TopAbs_SHELL) {
46       aBuilder.Add(aResultCompound, aSubShape);
47     } else if (aSubShape.ShapeType() == TopAbs_FACE) {
48       TopoDS_Shell aShell;
49       aBuilder.MakeShell(aShell);
50       aBuilder.Add(aShell, aSubShape);
51       aBuilder.Add(aResultCompound, aShell);
52     }
53   }
54   TopoDS_Iterator anIt(aResultCompound);
55   if(anIt.More()) {
56     aResult = aResultCompound;
57   }
58
59   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
60   aShape->setImpl(new TopoDS_Shape(aResult));
61   this->setShape(aShape);
62   this->setDone(true);
63 }
64
65 //==================================================================================================
66 void GeomAlgoAPI_Sewing::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
67                                   ListOfShape& theHistory)
68 {
69   static int anIndex = 0;
70   if(!theShape.get()) {
71     return;
72   }
73
74   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
75   const BRepBuilderAPI_Sewing& aSewingBuilder = this->impl<BRepBuilderAPI_Sewing>();
76
77   TopoDS_Shape aModifiedShape = aSewingBuilder.Modified(aShape);
78   if(aModifiedShape.IsEqual(aShape)) {
79     aModifiedShape = aSewingBuilder.ModifiedSubShape(aShape);
80   }
81
82   for(TopExp_Explorer anExp(aModifiedShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
83     GeomShapePtr aGeomShape(new GeomAPI_Shape());
84     aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
85     theHistory.push_back(aGeomShape);
86   }
87 }