Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeBuilder.cpp
1 // Copyright (C) 2014-2023  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_ShapeBuilder.h"
21
22 #include "GeomAlgoAPI_MakeShapeCustom.h"
23
24 #include <GeomAPI_ShapeIterator.h>
25
26 #include <BRep_Builder.hxx>
27 #include <BRepBuilderAPI_Copy.hxx>
28 #include <BRepExtrema_DistShapeShape.hxx>
29 #include <BRepTools_ReShape.hxx>
30 #include <Precision.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Shape.hxx>
36
37 //==================================================================================================
38 void GeomAlgoAPI_ShapeBuilder::add(std::shared_ptr<GeomAPI_Shape> theShape,
39                                    const std::shared_ptr<GeomAPI_Shape> theShapeToAdd)
40 {
41   if(!theShape.get() || !theShapeToAdd.get()) {
42     return;
43   }
44
45   TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
46   const TopoDS_Shape& aShapeToAdd = theShapeToAdd->impl<TopoDS_Shape>();
47
48   BRep_Builder aBuilder;
49   aBuilder.Add(*aShape, aShapeToAdd);
50 }
51
52
53 //==================================================================================================
54 void GeomAlgoAPI_ShapeBuilder::remove(std::shared_ptr<GeomAPI_Shape> theShape,
55                                       const std::shared_ptr<GeomAPI_Shape> theShapeToRemove)
56 {
57   if(!theShape.get() || !theShapeToRemove.get()) {
58     return;
59   }
60
61   TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
62   const TopoDS_Shape& aShapeToRemove = theShapeToRemove->impl<TopoDS_Shape>();
63
64   BRep_Builder aBuilder;
65   aBuilder.Remove(*aShape, aShapeToRemove);
66 }
67
68 //==================================================================================================
69 GeomAlgoAPI_ShapeBuilder::GeomAlgoAPI_ShapeBuilder()
70 {
71 }
72
73 //==================================================================================================
74 void GeomAlgoAPI_ShapeBuilder::removeInternal(const std::shared_ptr<GeomAPI_Shape> theShape)
75 {
76   GeomShapePtr aResultShape;
77
78   GeomAPI_Shape::ShapeType aBaseShapeType = theShape->shapeType();
79   if(aBaseShapeType == GeomAPI_Shape::WIRE) {
80     aResultShape = theShape->emptyCopied();
81     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom>
82       aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
83     for(GeomAPI_ShapeIterator anIter(theShape); anIter.more(); anIter.next()) {
84       GeomShapePtr aSubShape = anIter.current();
85       GeomShapePtr aSubShapeCopy = aSubShape->emptyCopied();
86       for(GeomAPI_ShapeIterator aSubIter(aSubShape); aSubIter.more(); aSubIter.next()) {
87         GeomShapePtr aSubOfSubShape = aSubIter.current();
88         if(aSubOfSubShape->orientation() != GeomAPI_Shape::INTERNAL) {
89           GeomAlgoAPI_ShapeBuilder::add(aSubShapeCopy, aSubOfSubShape);
90         }
91       }
92       aMakeShapeCustom->addModified(aSubShape, aSubShapeCopy);
93       GeomAlgoAPI_ShapeBuilder::add(aResultShape, aSubShapeCopy);
94     }
95     this->appendAlgo(aMakeShapeCustom);
96   } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
97     const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
98     BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape);
99     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
100       new GeomAlgoAPI_MakeShape(aCopyBuilder)));
101     if(!aCopyBuilder->IsDone()) {
102       return;
103     }
104     TopoDS_Shape aShape = aCopyBuilder->Shape();
105     TopoDS_Shape aShapeCopy = aShape.EmptyCopied();
106     BRep_Builder aBuilder;
107     for(TopoDS_Iterator anIt(aShape); anIt.More(); anIt.Next()) {
108       const TopoDS_Shape& aSubShape = anIt.Value();
109       if(aSubShape.ShapeType() == TopAbs_WIRE
110           && aSubShape.Orientation() != TopAbs_INTERNAL) {
111         aBuilder.Add(aShapeCopy, aSubShape);
112       }
113     }
114     aResultShape.reset(new GeomAPI_Shape());
115     aResultShape->setImpl(new TopoDS_Shape(aShapeCopy));
116   }
117
118   setShape(aResultShape);
119   setDone(true);
120 }
121
122 //==================================================================================================
123 void GeomAlgoAPI_ShapeBuilder::addInternal(const std::shared_ptr<GeomAPI_Shape> theShape,
124                                            const ListOfShape& theShapesToAdd)
125 {
126   // Get base shape.
127   if(!theShape.get()) {
128     return;
129   }
130   const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
131   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
132
133   // Copy base shape.
134   BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape);
135   this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aCopyBuilder)));
136   if(!aCopyBuilder->IsDone()) {
137     return;
138   }
139   TopoDS_Shape aResultShape = aCopyBuilder->Shape();
140
141   // Copy sub-shapes from list to new shape.
142   BRep_Builder aBuilder;
143   std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
144   for(ListOfShape::const_iterator
145       anIt = theShapesToAdd.cbegin(); anIt != theShapesToAdd.cend(); ++anIt) {
146     TopoDS_Shape aShapeToAdd = (*anIt)->impl<TopoDS_Shape>();
147     TopoDS_Shape aModShapeToAdd = aShapeToAdd;
148     aModShapeToAdd.Orientation(TopAbs_INTERNAL);
149     for(TopExp_Explorer aResExp(aResultShape, TopAbs_VERTEX); aResExp.More(); aResExp.Next()) {
150       const TopoDS_Vertex& aVertexInRes = TopoDS::Vertex(aResExp.Current());
151       const gp_Pnt aPntInRes = BRep_Tool::Pnt(aVertexInRes);
152       for(TopExp_Explorer anAddExp(aShapeToAdd, TopAbs_VERTEX); anAddExp.More(); anAddExp.Next()) {
153         const TopoDS_Vertex& aVertexInAdd = TopoDS::Vertex(anAddExp.Current());
154         const gp_Pnt aPntInAdd = BRep_Tool::Pnt(aVertexInAdd);
155         if(aPntInRes.Distance(aPntInAdd) < Precision::Confusion()) {
156           BRepTools_ReShape aReShape;
157           TopoDS_Shape aVertexInResMod = aVertexInRes;
158           aVertexInResMod.Orientation(aVertexInAdd.Orientation());
159           aReShape.Replace(aVertexInAdd, aVertexInResMod);
160           aModShapeToAdd = aReShape.Apply(aModShapeToAdd);
161         }
162       }
163     }
164
165     GeomShapePtr aGeomBaseShape(new GeomAPI_Shape());
166     GeomShapePtr aGeomModShape(new GeomAPI_Shape());
167     aGeomBaseShape->setImpl(new TopoDS_Shape(aShapeToAdd));
168     aGeomModShape->setImpl(new TopoDS_Shape(aModShapeToAdd));
169     aMakeShapeCustom->addModified(aGeomBaseShape, aGeomModShape);
170     aShapeToAdd = aModShapeToAdd;
171
172     TopAbs_ShapeEnum aShapeToAddType = aShapeToAdd.ShapeType();
173     if(aBaseShapeType == TopAbs_WIRE) {
174       if(aShapeToAddType == TopAbs_VERTEX) {
175         // Find on which edge vertex is lie and add to this edge.
176         for(TopExp_Explorer
177             aResultExp(aResultShape, TopAbs_EDGE); aResultExp.More(); aResultExp.Next()) {
178           TopoDS_Shape anEdge = aResultExp.Current();
179           BRepExtrema_DistShapeShape aDist(anEdge, aShapeToAdd);
180           aDist.Perform();
181           if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
182             aShapeToAdd.Orientation(TopAbs_INTERNAL);
183             Standard_Boolean aFreeFlag = anEdge.Free();
184             anEdge.Free(Standard_True);
185             aBuilder.Add(anEdge, aShapeToAdd);
186             anEdge.Free(aFreeFlag);
187             break;
188           }
189         }
190       }
191     } else if(aBaseShapeType == TopAbs_FACE) {
192       if(aShapeToAddType == TopAbs_EDGE) {
193         aShapeToAdd.Orientation(TopAbs_INTERNAL);
194         TopoDS_Wire aWire;
195         aBuilder.MakeWire(aWire);
196         aBuilder.Add(aWire, aShapeToAdd);
197         aShapeToAdd = aWire;
198         aShapeToAdd.Orientation(TopAbs_INTERNAL);
199       }
200       aBuilder.Add(aResultShape, aShapeToAdd);
201     }
202   }
203   this->appendAlgo(aMakeShapeCustom);
204
205   // Set result.
206   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
207   aShape->setImpl(new TopoDS_Shape(aResultShape));
208   setShape(aShape);
209   setDone(true);
210 }