1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_ShapeBuilder.cpp
4 // Created: 27 April 2016
5 // Author: Dmitry Bobylev
7 #include "GeomAlgoAPI_ShapeBuilder.h"
9 #include "GeomAlgoAPI_MakeShapeCustom.h"
11 #include <GeomAPI_ShapeIterator.h>
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>
20 #include <TopoDS_Edge.hxx>
21 #include <TopoDS_Iterator.hxx>
22 #include <TopoDS_Shape.hxx>
24 //==================================================================================================
25 void GeomAlgoAPI_ShapeBuilder::add(std::shared_ptr<GeomAPI_Shape> theShape,
26 const std::shared_ptr<GeomAPI_Shape> theShapeToAdd)
28 if(!theShape.get() || !theShapeToAdd.get()) {
32 TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
33 const TopoDS_Shape& aShapeToAdd = theShapeToAdd->impl<TopoDS_Shape>();
35 BRep_Builder aBuilder;
36 aBuilder.Add(*aShape, aShapeToAdd);
40 //==================================================================================================
41 void GeomAlgoAPI_ShapeBuilder::remove(std::shared_ptr<GeomAPI_Shape> theShape,
42 const std::shared_ptr<GeomAPI_Shape> theShapeToRemove)
44 if(!theShape.get() || !theShapeToRemove.get()) {
48 TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
49 const TopoDS_Shape& aShapeToRemove = theShapeToRemove->impl<TopoDS_Shape>();
51 BRep_Builder aBuilder;
52 aBuilder.Remove(*aShape, aShapeToRemove);
55 //==================================================================================================
56 GeomAlgoAPI_ShapeBuilder::GeomAlgoAPI_ShapeBuilder()
60 //==================================================================================================
61 void GeomAlgoAPI_ShapeBuilder::removeInternal(const std::shared_ptr<GeomAPI_Shape> theShape)
63 GeomShapePtr aResultShape;
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);
78 aMakeShapeCustom->addModified(aSubShape, aSubShapeCopy);
79 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aSubShapeCopy);
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()) {
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);
99 aResultShape.reset(new GeomAPI_Shape());
100 aResultShape->setImpl(new TopoDS_Shape(aShapeCopy));
103 setShape(aResultShape);
107 //==================================================================================================
108 void GeomAlgoAPI_ShapeBuilder::addInternal(const std::shared_ptr<GeomAPI_Shape> theShape,
109 const ListOfShape& theShapesToAdd)
112 if(!theShape.get()) {
115 const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
116 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
119 BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape);
120 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aCopyBuilder)));
121 if(!aCopyBuilder->IsDone()) {
124 TopoDS_Shape aResultShape = aCopyBuilder->Shape();
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);
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;
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);
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);
174 } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
175 if(aShapeToAddType == GeomAPI_Shape::EDGE) {
176 aShapeToAdd.Orientation(TopAbs_INTERNAL);
178 aBuilder.MakeWire(aWire);
179 aBuilder.Add(aWire, aShapeToAdd);
181 aShapeToAdd.Orientation(TopAbs_INTERNAL);
183 aBuilder.Add(aResultShape, aShapeToAdd);
186 this->appendAlgo(aMakeShapeCustom);
189 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
190 aShape->setImpl(new TopoDS_Shape(aResultShape));