1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_ShapeBuilder.h"
22 #include "GeomAlgoAPI_MakeShapeCustom.h"
24 #include <GeomAPI_ShapeIterator.h>
26 #include <BRep_Builder.hxx>
27 #include <BRepBuilderAPI_Copy.hxx>
28 #include <BRepExtrema_DistShapeShape.hxx>
29 #include <BRepTools_ReShape.hxx>
30 #include <Precision.hxx>
31 #include <TopExp_Explorer.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Shape.hxx>
37 //==================================================================================================
38 void GeomAlgoAPI_ShapeBuilder::add(std::shared_ptr<GeomAPI_Shape> theShape,
39 const std::shared_ptr<GeomAPI_Shape> theShapeToAdd)
41 if(!theShape.get() || !theShapeToAdd.get()) {
45 TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
46 const TopoDS_Shape& aShapeToAdd = theShapeToAdd->impl<TopoDS_Shape>();
48 BRep_Builder aBuilder;
49 aBuilder.Add(*aShape, aShapeToAdd);
53 //==================================================================================================
54 void GeomAlgoAPI_ShapeBuilder::remove(std::shared_ptr<GeomAPI_Shape> theShape,
55 const std::shared_ptr<GeomAPI_Shape> theShapeToRemove)
57 if(!theShape.get() || !theShapeToRemove.get()) {
61 TopoDS_Shape* aShape = theShape->implPtr<TopoDS_Shape>();
62 const TopoDS_Shape& aShapeToRemove = theShapeToRemove->impl<TopoDS_Shape>();
64 BRep_Builder aBuilder;
65 aBuilder.Remove(*aShape, aShapeToRemove);
68 //==================================================================================================
69 GeomAlgoAPI_ShapeBuilder::GeomAlgoAPI_ShapeBuilder()
73 //==================================================================================================
74 void GeomAlgoAPI_ShapeBuilder::removeInternal(const std::shared_ptr<GeomAPI_Shape> theShape)
76 GeomShapePtr aResultShape;
78 GeomAPI_Shape::ShapeType aBaseShapeType = theShape->shapeType();
79 if(aBaseShapeType == GeomAPI_Shape::WIRE) {
80 aResultShape = theShape->emptyCopied();
81 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom>
82 aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
83 for(GeomAPI_ShapeIterator anIter(theShape); anIter.more(); anIter.next()) {
84 GeomShapePtr aSubShape = anIter.current();
85 GeomShapePtr aSubShapeCopy = aSubShape->emptyCopied();
86 for(GeomAPI_ShapeIterator aSubIter(aSubShape); aSubIter.more(); aSubIter.next()) {
87 GeomShapePtr aSubOfSubShape = aSubIter.current();
88 if(aSubOfSubShape->orientation() != GeomAPI_Shape::INTERNAL) {
89 GeomAlgoAPI_ShapeBuilder::add(aSubShapeCopy, aSubOfSubShape);
92 aMakeShapeCustom->addModified(aSubShape, aSubShapeCopy);
93 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aSubShapeCopy);
95 this->appendAlgo(aMakeShapeCustom);
96 } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
97 const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
98 BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape);
99 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
100 new GeomAlgoAPI_MakeShape(aCopyBuilder)));
101 if(!aCopyBuilder->IsDone()) {
104 TopoDS_Shape aShape = aCopyBuilder->Shape();
105 TopoDS_Shape aShapeCopy = aShape.EmptyCopied();
106 BRep_Builder aBuilder;
107 for(TopoDS_Iterator anIt(aShape); anIt.More(); anIt.Next()) {
108 const TopoDS_Shape& aSubShape = anIt.Value();
109 if(aSubShape.ShapeType() == TopAbs_WIRE
110 && aSubShape.Orientation() != TopAbs_INTERNAL) {
111 aBuilder.Add(aShapeCopy, aSubShape);
114 aResultShape.reset(new GeomAPI_Shape());
115 aResultShape->setImpl(new TopoDS_Shape(aShapeCopy));
118 setShape(aResultShape);
122 //==================================================================================================
123 void GeomAlgoAPI_ShapeBuilder::addInternal(const std::shared_ptr<GeomAPI_Shape> theShape,
124 const ListOfShape& theShapesToAdd)
127 if(!theShape.get()) {
130 const TopoDS_Shape& aBaseShape = theShape->impl<TopoDS_Shape>();
131 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
134 BRepBuilderAPI_Copy* aCopyBuilder = new BRepBuilderAPI_Copy(aBaseShape);
135 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aCopyBuilder)));
136 if(!aCopyBuilder->IsDone()) {
139 TopoDS_Shape aResultShape = aCopyBuilder->Shape();
141 // Copy sub-shapes from list to new shape.
142 BRep_Builder aBuilder;
143 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMakeShapeCustom(new GeomAlgoAPI_MakeShapeCustom());
144 for(ListOfShape::const_iterator
145 anIt = theShapesToAdd.cbegin(); anIt != theShapesToAdd.cend(); ++anIt) {
146 TopoDS_Shape aShapeToAdd = (*anIt)->impl<TopoDS_Shape>();
147 TopoDS_Shape aModShapeToAdd = aShapeToAdd;
148 aModShapeToAdd.Orientation(TopAbs_INTERNAL);
149 for(TopExp_Explorer aResExp(aResultShape, TopAbs_VERTEX); aResExp.More(); aResExp.Next()) {
150 const TopoDS_Vertex& aVertexInRes = TopoDS::Vertex(aResExp.Current());
151 const gp_Pnt aPntInRes = BRep_Tool::Pnt(aVertexInRes);
152 for(TopExp_Explorer anAddExp(aShapeToAdd, TopAbs_VERTEX); anAddExp.More(); anAddExp.Next()) {
153 const TopoDS_Vertex& aVertexInAdd = TopoDS::Vertex(anAddExp.Current());
154 const gp_Pnt aPntInAdd = BRep_Tool::Pnt(aVertexInAdd);
155 if(aPntInRes.Distance(aPntInAdd) < Precision::Confusion()) {
156 BRepTools_ReShape aReShape;
157 TopoDS_Shape aVertexInResMod = aVertexInRes;
158 aVertexInResMod.Orientation(aVertexInAdd.Orientation());
159 aReShape.Replace(aVertexInAdd, aVertexInResMod);
160 aModShapeToAdd = aReShape.Apply(aModShapeToAdd);
165 GeomShapePtr aGeomBaseShape(new GeomAPI_Shape());
166 GeomShapePtr aGeomModShape(new GeomAPI_Shape());
167 aGeomBaseShape->setImpl(new TopoDS_Shape(aShapeToAdd));
168 aGeomModShape->setImpl(new TopoDS_Shape(aModShapeToAdd));
169 aMakeShapeCustom->addModified(aGeomBaseShape, aGeomModShape);
170 aShapeToAdd = aModShapeToAdd;
172 TopAbs_ShapeEnum aShapeToAddType = aShapeToAdd.ShapeType();
173 if(aBaseShapeType == TopAbs_WIRE) {
174 if(aShapeToAddType == TopAbs_VERTEX) {
175 // Find on which edge vertex is lie and add to this edge.
177 aResultExp(aResultShape, TopAbs_EDGE); aResultExp.More(); aResultExp.Next()) {
178 TopoDS_Shape anEdge = aResultExp.Current();
179 BRepExtrema_DistShapeShape aDist(anEdge, aShapeToAdd);
181 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
182 aShapeToAdd.Orientation(TopAbs_INTERNAL);
183 Standard_Boolean aFreeFlag = anEdge.Free();
184 anEdge.Free(Standard_True);
185 aBuilder.Add(anEdge, aShapeToAdd);
186 anEdge.Free(aFreeFlag);
191 } else if(aBaseShapeType == TopAbs_FACE) {
192 if(aShapeToAddType == TopAbs_EDGE) {
193 aShapeToAdd.Orientation(TopAbs_INTERNAL);
195 aBuilder.MakeWire(aWire);
196 aBuilder.Add(aWire, aShapeToAdd);
198 aShapeToAdd.Orientation(TopAbs_INTERNAL);
200 aBuilder.Add(aResultShape, aShapeToAdd);
203 this->appendAlgo(aMakeShapeCustom);
206 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
207 aShape->setImpl(new TopoDS_Shape(aResultShape));