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 = 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);
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);
86 this->appendAlgo(aMakeShapeCustom);
88 setShape(aResultShape);
92 //==================================================================================================
93 void GeomAlgoAPI_ShapeBuilder::add(const std::shared_ptr<GeomAPI_Shape> theShape,
94 const ListOfShape& theShapesToAdd)
100 const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
101 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
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()) {
109 TopoDS_Shape aResultShape = aCopyBuilder->Shape();
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);
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;
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);
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);
156 } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
157 if(aShapeToAddType == GeomAPI_Shape::EDGE) {
158 aShapeToAdd.Orientation(TopAbs_INTERNAL);
160 aBuilder.MakeWire(aWire);
161 aBuilder.Add(aWire, aShapeToAdd);
163 aShapeToAdd.Orientation(TopAbs_INTERNAL);
165 aBuilder.Add(aResultShape, aShapeToAdd);
168 this->appendAlgo(aMakeShapeCustom);
171 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
172 aShape->setImpl(new TopoDS_Shape(aResultShape));