Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Sewing.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Sewing.h"
21
22 #include <BRep_Builder.hxx>
23 #include <BRepBuilderAPI_MakeShell.hxx>
24 #include <BRepBuilderAPI_Sewing.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28
29 //==================================================================================================
30 GeomAlgoAPI_Sewing::GeomAlgoAPI_Sewing(const ListOfShape& theShapes)
31 {
32   myBuildShell = true;
33   build(theShapes);
34 }
35
36 GeomAlgoAPI_Sewing::GeomAlgoAPI_Sewing(const ListOfShape& theShapes, const bool theAllowNonManifold, const double theTolerance)
37 {
38   myBuildShell = false;
39   build(theShapes, theAllowNonManifold, theTolerance);
40 }
41
42 void GeomAlgoAPI_Sewing::build(const ListOfShape& theShapes, const bool theAllowNonManifold, const double theTolerance)
43 {
44   if(theShapes.empty()) {
45     return;
46   }
47
48   BRepBuilderAPI_Sewing* aSewingBuilder = new BRepBuilderAPI_Sewing();
49   this->setImpl(aSewingBuilder);
50
51   if (!myBuildShell)
52   {
53     aSewingBuilder->SetTolerance(theTolerance);
54     aSewingBuilder->SetFaceMode(Standard_True);
55     aSewingBuilder->SetFloatingEdgesMode(Standard_False);
56     aSewingBuilder->SetNonManifoldMode(theAllowNonManifold);
57   }
58
59   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
60     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
61     aSewingBuilder->Add(aShape);
62   }
63
64   aSewingBuilder->Perform();
65
66   TopoDS_Shape aResult = aSewingBuilder->SewedShape();
67   if (myBuildShell)
68   {
69     BRep_Builder aBuilder;
70     if(aResult.ShapeType() == TopAbs_COMPOUND) {
71       TopoDS_Compound aResultCompound;
72       aBuilder.MakeCompound(aResultCompound);
73       for(TopoDS_Iterator anIt(aResult); anIt.More(); anIt.Next()) {
74         const TopoDS_Shape aSubShape = anIt.Value();
75         if(aSubShape.ShapeType() == TopAbs_SHELL) {
76           aBuilder.Add(aResultCompound, aSubShape);
77         } else if (aSubShape.ShapeType() == TopAbs_FACE) {
78           TopoDS_Shell aShell;
79           aBuilder.MakeShell(aShell);
80           aBuilder.Add(aShell, aSubShape);
81           aBuilder.Add(aResultCompound, aShell);
82         }
83       }
84       aResult = aResultCompound;
85     } else if(aResult.ShapeType() == TopAbs_FACE) {
86       TopoDS_Shell aShell;
87       aBuilder.MakeShell(aShell);
88       aBuilder.Add(aShell, aResult);
89       aResult = aShell;
90     }
91   }
92
93   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
94   aShape->setImpl(new TopoDS_Shape(aResult));
95   this->setShape(aShape);
96   this->setDone(true);
97 }
98
99 //==================================================================================================
100 #include <GeomAPI_ShapeIterator.h>
101
102 void GeomAlgoAPI_Sewing::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
103                                   ListOfShape& theHistory)
104 {
105   if(!theShape.get()) {
106     return;
107   }
108
109   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
110   const BRepBuilderAPI_Sewing& aSewingBuilder = this->impl<BRepBuilderAPI_Sewing>();
111
112   TopoDS_Shape aModifiedShape = aSewingBuilder.Modified(aShape);
113   if(aModifiedShape.IsEqual(aShape)) {
114     aModifiedShape = aSewingBuilder.ModifiedSubShape(aShape);
115   }
116
117   for(TopExp_Explorer anExp(aModifiedShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
118     GeomShapePtr aGeomShape(new GeomAPI_Shape());
119     aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
120     theHistory.push_back(aGeomShape);
121   }
122
123   if (theShape->shapeType() < GeomAPI_Shape::FACE) {
124     ListOfShape aNewShapes;
125     // collect faces and parent shapes, if it is not done yet
126     if (!isNewShapesCollected(theShape, GeomAPI_Shape::FACE))
127       collectNewShapes(theShape, GeomAPI_Shape::FACE);
128
129     for (GeomAPI_ShapeIterator anIt(shape()); anIt.more(); anIt.next()) {
130       GeomShapePtr anOldShapesCompound =
131           oldShapesForNew(theShape, anIt.current(), GeomAPI_Shape::FACE);
132       if (!anOldShapesCompound->isNull())
133         aNewShapes.push_back(anIt.current());
134     }
135
136     if (!aNewShapes.empty())
137       theHistory = aNewShapes;
138   }
139 }