1 // Created on: 1997-11-20
2 // Created by: Prestataire Mary FABIEN
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and / or modify it
9 // under the terms of the GNU Lesser General Public version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <BRepAlgo_BooleanOperations.ixx>
18 #include <TopOpeBRep_DSFiller.hxx>
19 #include <TopOpeBRepDS_BuildTool.hxx>
20 #include <TopOpeBRepTool_GeomTool.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepTools_Substitution.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <TopTools_ListOfShape.hxx>
27 #include <TopTools_ListIteratorOfListOfShape.hxx>
28 #include <Geom_Surface.hxx>
29 #include <TopLoc_Location.hxx>
30 #include <Bnd_Box.hxx>
31 #include <BRepBndLib.hxx>
33 //=======================================================================
36 //=======================================================================
37 BRepAlgo_BooleanOperations::BRepAlgo_BooleanOperations() :
38 myApproxNbPntMax (30) ,
39 myApproxTol3D (1.e-7) ,
40 myApproxTol2D (1.e-7) ,
41 myApproxRelativeTol (Standard_True)
45 //=======================================================================
48 //=======================================================================
49 void BRepAlgo_BooleanOperations::Shapes2d (const TopoDS_Shape& S1,
50 const TopoDS_Shape& S2)
52 // S1 doit etre une face ou un ensemble de faces
53 // S2 doit etre une edge.
55 if (S2.ShapeType() != TopAbs_EDGE) return;
57 BRep_Builder Builder ;
59 Builder.MakeWire (Wire);
60 Builder.Add (Wire, S2);
62 TopExp_Explorer Exp (S1, TopAbs_FACE) ;
63 if (!Exp.More()) return ;
64 const TopoDS_Face& FirstFace = TopoDS::Face (Exp.Current()) ;
67 const Handle(Geom_Surface)& Surf = BRep_Tool::Surface (FirstFace, Loc) ;
70 Builder.MakeFace (Face, Surf, Loc, BRep_Tool::Tolerance (FirstFace)) ;
71 Builder.Add (Face, Wire) ;
72 Face.Orientation (FirstFace.Orientation()) ;
78 myDSA.Load (myS1, myS2) ;
79 Handle (TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS() ;
80 myDSA.myDSFiller.Insert2d (myS1, myS2, HDS) ;
83 //=======================================================================
85 //purpose : Defines the arguments.
86 //=======================================================================
87 void BRepAlgo_BooleanOperations::Shapes (const TopoDS_Shape& S1,
88 const TopoDS_Shape& S2)
93 myDSA.Load(myS1, myS2);
94 Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
95 myDSA.myDSFiller.Insert(myS1,myS2,HDS);
96 // const Standard_Boolean CheckShapes = Standard_True;
97 // Standard_Boolean esp = HDS->EdgesSameParameter();
98 // Standard_Boolean localcheck = CheckShapes;
101 //=======================================================================
102 //function : SetApproxParameters
103 //purpose : Sets the parameters for the approximations.
104 //=======================================================================
105 void BRepAlgo_BooleanOperations::SetApproxParameters (const Standard_Integer NbPntMax,
106 const Standard_Real Tol3D,
107 const Standard_Real Tol2D,
108 const Standard_Boolean RelativeTol)
110 myApproxNbPntMax = NbPntMax ;
111 myApproxTol3D = Tol3D ;
112 myApproxTol2D = Tol2D ;
113 myApproxRelativeTol = RelativeTol ;
116 //=======================================================================
119 //=======================================================================
120 void BRepAlgo_BooleanOperations::Define (const TopoDS_Shape& S1,
121 const TopoDS_Shape& S2,
122 Handle(TopOpeBRepDS_HDataStructure)& HDS)
124 ChangeDataStructure() = HDS;
129 //=======================================================================
131 //purpose : Performs the global boolean operation.
132 //=======================================================================
133 void BRepAlgo_BooleanOperations::Perform ()
135 TopOpeBRepDS_BuildTool& BTofBuilder = myDSA.myHB->ChangeBuildTool();
136 TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
137 GTofBTofBuilder.SetNbPntMax(myApproxNbPntMax);
138 GTofBTofBuilder.SetTolerances (myApproxTol3D, myApproxTol2D, myApproxRelativeTol) ;
139 Handle(TopOpeBRepBuild_HBuilder)& HB = myDSA.myHB;
140 Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
141 HB->Perform(HDS,myS1,myS2);
144 //=======================================================================
146 //purpose : Performs the global boolean operation in regards of the
148 //=======================================================================
149 void BRepAlgo_BooleanOperations::Perform (const TopAbs_State State1,
150 const TopAbs_State State2)
158 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = ChangeBuilder() ;
159 HBuilder->MergeShapes (myS1, State1, myS2, State2) ;
161 const TopTools_ListOfShape& ListResults = HBuilder->Merged (myS1, State1) ;
162 Standard_Integer NbResults = ListResults.Extent() ;
164 if (NbResults == 1) {
165 myShape = ListResults.First() ;
167 BRep_Builder Builder ;
168 Builder.MakeCompound (TopoDS::Compound (myShape)) ;
169 TopTools_ListIteratorOfListOfShape Iter ;
170 for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
171 Builder.Add (myShape, Iter.Value()) ;
173 TopExp_Explorer Explorer ;
174 for (Explorer.Init (myShape, TopAbs_FACE) ; Explorer.More() ; Explorer.Next()) {
175 myMapShape.Add (Explorer.Current()) ;
177 for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
178 myMapShape.Add (Explorer.Current()) ;
183 //=======================================================================
186 //=======================================================================
187 const TopoDS_Shape& BRepAlgo_BooleanOperations::Common()
189 Perform (TopAbs_IN, TopAbs_IN) ;
193 //=======================================================================
196 //=======================================================================
197 const TopoDS_Shape& BRepAlgo_BooleanOperations::Fus()
199 Perform (TopAbs_OUT, TopAbs_OUT) ;
203 //=======================================================================
206 //=======================================================================
207 const TopoDS_Shape& BRepAlgo_BooleanOperations::Cut()
209 Perform (TopAbs_OUT, TopAbs_IN) ;
213 //=======================================================================
216 //=======================================================================
217 const TopoDS_Shape& BRepAlgo_BooleanOperations::Section()
219 // Standard_Boolean bcw = BuilderCanWork();
220 // if ( ! bcw || myshapeisnull) return;
228 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
230 const TopTools_ListOfShape& ListResults = HBuilder->Section() ;
231 Standard_Integer NbResults = ListResults.Extent() ;
233 if (NbResults == 1) {
234 myShape = ListResults.First() ;
236 BRep_Builder Builder ;
237 Builder.MakeCompound (TopoDS::Compound (myShape)) ;
238 TopTools_ListIteratorOfListOfShape Iter ;
239 for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
240 Builder.Add (myShape, Iter.Value()) ;
242 TopExp_Explorer Explorer ;
243 for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
244 myMapShape.Add (Explorer.Current()) ;
251 //=======================================================================
254 //=======================================================================
255 const TopoDS_Shape& BRepAlgo_BooleanOperations::Shape()
260 //=======================================================================
263 //=======================================================================
264 const TopoDS_Shape& BRepAlgo_BooleanOperations::ShapeFrom (const TopoDS_Shape& Shape)
268 if (!myShape.IsNull()) {
270 TopoDS_Shape ShapeToDel ;
271 if (Shape.IsSame (myS1)) {
277 BRepTools_Substitution Substitute ;
279 TopTools_ListOfShape NullFaces ;
282 TopExp_Explorer ExpFac ;
283 for (ExpFac.Init (ShapeToDel, TopAbs_FACE) ; ExpFac.More() ; ExpFac.Next()) {
284 const TopoDS_Face& Face = TopoDS::Face (ExpFac.Current()) ;
285 const TopTools_ListOfShape& ListResults = Modified (Face) ;
286 if (ListResults.Extent() == 0) {
287 if (myMapShape.Contains (Face)) Substitute.Substitute (Face, NullFaces) ;
289 TopTools_ListIteratorOfListOfShape ItrFace ;
290 for (ItrFace.Initialize (ListResults) ; ItrFace.More() ; ItrFace.Next()) {
291 Substitute.Substitute (TopoDS::Face (ItrFace.Value()), NullFaces) ;
296 Substitute.Build (myShape) ;
297 if (Substitute.IsCopied (myShape)) {
298 const TopTools_ListOfShape& ListResults = Substitute.Copy (myShape) ;
299 Standard_Integer NbResults = ListResults.Extent() ;
300 if (NbResults == 1) {
301 myResult = ListResults.First() ;
302 } else if (NbResults > 1) {
303 BRep_Builder Builder ;
304 Builder.MakeCompound (TopoDS::Compound (myResult)) ;
305 TopTools_ListIteratorOfListOfShape ItrResult ;
306 for (ItrResult.Initialize (ListResults) ; ItrResult.More() ; ItrResult.Next()) {
307 Builder.Add (myResult, ItrResult.Value()) ;
318 //=======================================================================
319 //function : Modified
321 //=======================================================================
322 const TopTools_ListOfShape& BRepAlgo_BooleanOperations::Modified (const TopoDS_Shape& Shape)
324 return myDSA.Modified(Shape);
327 //=======================================================================
328 //function : IsDeleted
330 //=======================================================================
331 Standard_Boolean BRepAlgo_BooleanOperations::IsDeleted (const TopoDS_Shape& Shape)
333 Standard_Boolean Deleted = Standard_True ;
335 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
337 if ( myMapShape.Contains (Shape)
338 || HBuilder->IsMerged (Shape, TopAbs_OUT)
339 || HBuilder->IsMerged (Shape, TopAbs_IN)
340 || HBuilder->IsMerged (Shape, TopAbs_ON)
341 || HBuilder->IsSplit (Shape, TopAbs_OUT)
342 || HBuilder->IsSplit (Shape, TopAbs_IN)
343 || HBuilder->IsSplit (Shape, TopAbs_ON))
344 Deleted = Standard_False ;
349 //=======================================================================
350 //function : DataStructure
352 //=======================================================================
353 const Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::DataStructure() const
358 //=======================================================================
359 //function : DataStructure
361 //=======================================================================
362 Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::ChangeDataStructure()
364 return myDSA.ChangeDS();
367 //=======================================================================
370 //=======================================================================
371 const Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::Builder() const
373 return myDSA.Builder();
376 //=======================================================================
379 //=======================================================================
380 Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::ChangeBuilder()
382 return myDSA.ChangeBuilder();
385 //=======================================================================
386 //function : DataStructureAccess
387 //purpose : returns the member myDSA.
388 //=======================================================================
389 BRepAlgo_DSAccess& BRepAlgo_BooleanOperations::DataStructureAccess()