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