Salome HOME
Fix for crash on abort of Boolean with one argument set.
[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 = theShape->emptyCopied();
64   GeomAPI_Shape::ShapeType aBaseShapeType = theShape->shapeType();
65   std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
66   for(GeomAPI_ShapeIterator anIter(theShape); anIter.more(); anIter.next()) {
67     GeomShapePtr aSubShape = anIter.current();
68     if(aBaseShapeType == GeomAPI_Shape::WIRE) {
69       GeomShapePtr aSubShapeCopy = aSubShape->emptyCopied();
70       aMakeShapeCustom->addModified(aSubShape, aSubShapeCopy);
71       for(GeomAPI_ShapeIterator aSubIter(aSubShape); aSubIter.more(); aSubIter.next()) {
72         GeomShapePtr aSubOfSubShape = aSubIter.current();
73         if(aSubOfSubShape->orientation() != GeomAPI_Shape::INTERNAL) {
74           GeomAlgoAPI_ShapeBuilder::add(aSubShapeCopy, aSubOfSubShape);
75         }
76       }
77       GeomAlgoAPI_ShapeBuilder::add(aResultShape, aSubShapeCopy);
78     } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
79       if(aSubShape->shapeType() == GeomAPI_Shape::WIRE
80           && aSubShape->orientation() != GeomAPI_Shape::INTERNAL) {
81         GeomAlgoAPI_ShapeBuilder::add(aResultShape, aSubShape);
82       }
83     }
84   }
85
86   this->appendAlgo(aMakeShapeCustom);
87
88   setShape(aResultShape);
89   setDone(true);
90 }
91
92 //==================================================================================================
93 void GeomAlgoAPI_ShapeBuilder::add(const std::shared_ptr<GeomAPI_Shape> theShape,
94                                    const ListOfShape& theShapesToAdd)
95 {
96   // Get base shape.
97   if(!theShape.get()) {
98     return;
99   }
100   const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
101   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
102
103   // Copy base shape.
104   BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape, Standard_False);
105   this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aCopyBuilder)));
106   if(!aCopyBuilder->IsDone()) {
107     return;
108   }
109   TopoDS_Shape aResultShape = aCopyBuilder->Shape();
110
111   // Copy sub-shapes from list to new shape.
112   BRep_Builder aBuilder;
113   std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
114   for(ListOfShape::const_iterator anIt = theShapesToAdd.cbegin(); anIt != theShapesToAdd.cend(); ++anIt) {
115     TopoDS_Shape aShapeToAdd = (*anIt)->impl<TopoDS_Shape>();
116     for(TopExp_Explorer aResExp(aResultShape, TopAbs_VERTEX); aResExp.More(); aResExp.Next()) {
117       const TopoDS_Vertex& aVertexInRes = TopoDS::Vertex(aResExp.Current());
118       const gp_Pnt aPntInRes = BRep_Tool::Pnt(aVertexInRes);
119       for(TopExp_Explorer anAddExp(aShapeToAdd, TopAbs_VERTEX); anAddExp.More(); anAddExp.Next()) {
120         const TopoDS_Vertex& aVertexInAdd = TopoDS::Vertex(anAddExp.Current());
121         const gp_Pnt aPntInAdd = BRep_Tool::Pnt(aVertexInAdd);
122         if(aPntInRes.Distance(aPntInAdd) < Precision::Confusion()) {
123           BRepTools_ReShape aReShape;
124           TopoDS_Shape aVertexInResMod = aVertexInRes;
125           aVertexInResMod.Orientation(aVertexInAdd.Orientation());
126           aReShape.Replace(aVertexInAdd, aVertexInResMod);
127           TopoDS_Shape aModShape = aReShape.Apply(aShapeToAdd);
128
129           GeomShapePtr aGeomBaseShape(new GeomAPI_Shape());
130           GeomShapePtr aGeomModShape(new GeomAPI_Shape());
131           aGeomBaseShape->setImpl(new TopoDS_Shape(aShapeToAdd));
132           aGeomModShape->setImpl(new TopoDS_Shape(aModShape));
133           aMakeShapeCustom->addModified(aGeomBaseShape, aGeomModShape);
134           aShapeToAdd = aModShape;
135         }
136       }
137     }
138     TopAbs_ShapeEnum aShapeToAddType = aShapeToAdd.ShapeType();
139     if(aBaseShapeType == TopAbs_WIRE) {
140       if(aShapeToAddType == TopAbs_VERTEX) {
141         // Find on which edge vertex is lie and add to this edge.
142         for(TopExp_Explorer aResultExp(aResultShape, TopAbs_EDGE); aResultExp.More(); aResultExp.Next()) {
143           TopoDS_Shape anEdge = aResultExp.Current();
144           BRepExtrema_DistShapeShape aDist(anEdge, aShapeToAdd);
145           aDist.Perform();
146           if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
147             aShapeToAdd.Orientation(TopAbs_INTERNAL);
148             Standard_Boolean aFreeFlag = anEdge.Free();
149             anEdge.Free(Standard_True);
150             aBuilder.Add(anEdge, aShapeToAdd);
151             anEdge.Free(aFreeFlag);
152             break;
153           }
154         }
155       }
156     } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
157       if(aShapeToAddType == GeomAPI_Shape::EDGE) {
158         aShapeToAdd.Orientation(TopAbs_INTERNAL);
159         TopoDS_Wire aWire;
160         aBuilder.MakeWire(aWire);
161         aBuilder.Add(aWire, aShapeToAdd);
162         aShapeToAdd = aWire;
163         aShapeToAdd.Orientation(TopAbs_INTERNAL);
164       }
165       aBuilder.Add(aResultShape, aShapeToAdd);
166     }
167   }
168   this->appendAlgo(aMakeShapeCustom);
169
170   // Set result.
171   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
172   aShape->setImpl(new TopoDS_Shape(aResultShape));
173   setShape(aShape);
174   setDone(true);
175 }