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