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 under
9 // the terms of the GNU Lesser General Public License 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.
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAlgo_BooleanOperations.hxx>
22 #include <BRepAlgo_DSAccess.hxx>
23 #include <BRepBndLib.hxx>
24 #include <BRepTools_Substitution.hxx>
25 #include <Geom_Surface.hxx>
26 #include <TopLoc_Location.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopOpeBRep_DSFiller.hxx>
31 #include <TopOpeBRepBuild_HBuilder.hxx>
32 #include <TopOpeBRepDS_BuildTool.hxx>
33 #include <TopOpeBRepDS_HDataStructure.hxx>
34 #include <TopOpeBRepTool_GeomTool.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
38 //=======================================================================
41 //=======================================================================
42 BRepAlgo_BooleanOperations::BRepAlgo_BooleanOperations() :
43 myApproxNbPntMax (30) ,
44 myApproxTol3D (1.e-7) ,
49 //=======================================================================
52 //=======================================================================
53 void BRepAlgo_BooleanOperations::Shapes2d (const TopoDS_Shape& S1,
54 const TopoDS_Shape& S2)
56 // S1 doit etre une face ou un ensemble de faces
57 // S2 doit etre une edge.
59 if (S2.ShapeType() != TopAbs_EDGE) return;
61 BRep_Builder Builder ;
63 Builder.MakeWire (Wire);
64 Builder.Add (Wire, S2);
66 TopExp_Explorer Exp (S1, TopAbs_FACE) ;
67 if (!Exp.More()) return ;
68 const TopoDS_Face& FirstFace = TopoDS::Face (Exp.Current()) ;
71 const Handle(Geom_Surface)& Surf = BRep_Tool::Surface (FirstFace, Loc) ;
74 Builder.MakeFace (Face, Surf, Loc, BRep_Tool::Tolerance (FirstFace)) ;
75 Builder.Add (Face, Wire) ;
76 Face.Orientation (FirstFace.Orientation()) ;
82 myDSA.Load (myS1, myS2) ;
83 Handle (TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS() ;
84 myDSA.myDSFiller.Insert2d (myS1, myS2, HDS) ;
87 //=======================================================================
89 //purpose : Defines the arguments.
90 //=======================================================================
91 void BRepAlgo_BooleanOperations::Shapes (const TopoDS_Shape& S1,
92 const TopoDS_Shape& S2)
97 myDSA.Load(myS1, myS2);
98 Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
99 myDSA.myDSFiller.Insert(myS1,myS2,HDS);
100 // const Standard_Boolean CheckShapes = Standard_True;
101 // Standard_Boolean esp = HDS->EdgesSameParameter();
102 // Standard_Boolean localcheck = CheckShapes;
105 //=======================================================================
106 //function : SetApproxParameters
107 //purpose : Sets the parameters for the approximations.
108 //=======================================================================
109 void BRepAlgo_BooleanOperations::SetApproxParameters (const Standard_Integer NbPntMax,
110 const Standard_Real Tol3D,
111 const Standard_Real Tol2D)
113 myApproxNbPntMax = NbPntMax ;
114 myApproxTol3D = Tol3D ;
115 myApproxTol2D = Tol2D ;
118 //=======================================================================
121 //=======================================================================
122 void BRepAlgo_BooleanOperations::Define (const TopoDS_Shape& S1,
123 const TopoDS_Shape& S2,
124 Handle(TopOpeBRepDS_HDataStructure)& HDS)
126 ChangeDataStructure() = HDS;
131 //=======================================================================
133 //purpose : Performs the global boolean operation.
134 //=======================================================================
135 void BRepAlgo_BooleanOperations::Perform ()
137 TopOpeBRepDS_BuildTool& BTofBuilder = myDSA.myHB->ChangeBuildTool();
138 TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
139 GTofBTofBuilder.SetNbPntMax(myApproxNbPntMax);
140 GTofBTofBuilder.SetTolerances (myApproxTol3D, myApproxTol2D) ;
141 Handle(TopOpeBRepBuild_HBuilder)& HB = myDSA.myHB;
142 Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
143 HB->Perform(HDS,myS1,myS2);
146 //=======================================================================
148 //purpose : Performs the global boolean operation in regards of the
150 //=======================================================================
151 void BRepAlgo_BooleanOperations::Perform (const TopAbs_State State1,
152 const TopAbs_State State2)
160 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = ChangeBuilder() ;
161 HBuilder->MergeShapes (myS1, State1, myS2, State2) ;
163 const TopTools_ListOfShape& ListResults = HBuilder->Merged (myS1, State1) ;
164 Standard_Integer NbResults = ListResults.Extent() ;
166 if (NbResults == 1) {
167 myShape = ListResults.First() ;
169 BRep_Builder Builder ;
170 Builder.MakeCompound (TopoDS::Compound (myShape)) ;
171 TopTools_ListIteratorOfListOfShape Iter ;
172 for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
173 Builder.Add (myShape, Iter.Value()) ;
175 TopExp_Explorer Explorer ;
176 for (Explorer.Init (myShape, TopAbs_FACE) ; Explorer.More() ; Explorer.Next()) {
177 myMapShape.Add (Explorer.Current()) ;
179 for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
180 myMapShape.Add (Explorer.Current()) ;
185 //=======================================================================
188 //=======================================================================
189 const TopoDS_Shape& BRepAlgo_BooleanOperations::Common()
191 Perform (TopAbs_IN, TopAbs_IN) ;
195 //=======================================================================
198 //=======================================================================
199 const TopoDS_Shape& BRepAlgo_BooleanOperations::Fus()
201 Perform (TopAbs_OUT, TopAbs_OUT) ;
205 //=======================================================================
208 //=======================================================================
209 const TopoDS_Shape& BRepAlgo_BooleanOperations::Cut()
211 Perform (TopAbs_OUT, TopAbs_IN) ;
215 //=======================================================================
218 //=======================================================================
219 const TopoDS_Shape& BRepAlgo_BooleanOperations::Section()
221 // Standard_Boolean bcw = BuilderCanWork();
222 // if ( ! bcw || myshapeisnull) return;
230 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
232 const TopTools_ListOfShape& ListResults = HBuilder->Section() ;
233 Standard_Integer NbResults = ListResults.Extent() ;
235 if (NbResults == 1) {
236 myShape = ListResults.First() ;
238 BRep_Builder Builder ;
239 Builder.MakeCompound (TopoDS::Compound (myShape)) ;
240 TopTools_ListIteratorOfListOfShape Iter ;
241 for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
242 Builder.Add (myShape, Iter.Value()) ;
244 TopExp_Explorer Explorer ;
245 for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
246 myMapShape.Add (Explorer.Current()) ;
253 //=======================================================================
256 //=======================================================================
257 const TopoDS_Shape& BRepAlgo_BooleanOperations::Shape()
262 //=======================================================================
265 //=======================================================================
266 const TopoDS_Shape& BRepAlgo_BooleanOperations::ShapeFrom (const TopoDS_Shape& Shape)
270 if (!myShape.IsNull()) {
272 TopoDS_Shape ShapeToDel ;
273 if (Shape.IsSame (myS1)) {
279 BRepTools_Substitution Substitute ;
281 TopTools_ListOfShape NullFaces ;
284 TopExp_Explorer ExpFac ;
285 for (ExpFac.Init (ShapeToDel, TopAbs_FACE) ; ExpFac.More() ; ExpFac.Next()) {
286 const TopoDS_Face& Face = TopoDS::Face (ExpFac.Current()) ;
287 const TopTools_ListOfShape& ListResults = Modified (Face) ;
288 if (ListResults.Extent() == 0) {
289 if (myMapShape.Contains (Face)) Substitute.Substitute (Face, NullFaces) ;
291 TopTools_ListIteratorOfListOfShape ItrFace ;
292 for (ItrFace.Initialize (ListResults) ; ItrFace.More() ; ItrFace.Next()) {
293 Substitute.Substitute (TopoDS::Face (ItrFace.Value()), NullFaces) ;
298 Substitute.Build (myShape) ;
299 if (Substitute.IsCopied (myShape)) {
300 const TopTools_ListOfShape& ListResults = Substitute.Copy (myShape) ;
301 Standard_Integer NbResults = ListResults.Extent() ;
302 if (NbResults == 1) {
303 myResult = ListResults.First() ;
304 } else if (NbResults > 1) {
305 BRep_Builder Builder ;
306 Builder.MakeCompound (TopoDS::Compound (myResult)) ;
307 TopTools_ListIteratorOfListOfShape ItrResult ;
308 for (ItrResult.Initialize (ListResults) ; ItrResult.More() ; ItrResult.Next()) {
309 Builder.Add (myResult, ItrResult.Value()) ;
320 //=======================================================================
321 //function : Modified
323 //=======================================================================
324 const TopTools_ListOfShape& BRepAlgo_BooleanOperations::Modified (const TopoDS_Shape& Shape)
326 return myDSA.Modified(Shape);
329 //=======================================================================
330 //function : IsDeleted
332 //=======================================================================
333 Standard_Boolean BRepAlgo_BooleanOperations::IsDeleted (const TopoDS_Shape& Shape)
335 Standard_Boolean Deleted = Standard_True ;
337 Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;
339 if ( myMapShape.Contains (Shape)
340 || HBuilder->IsMerged (Shape, TopAbs_OUT)
341 || HBuilder->IsMerged (Shape, TopAbs_IN)
342 || HBuilder->IsMerged (Shape, TopAbs_ON)
343 || HBuilder->IsSplit (Shape, TopAbs_OUT)
344 || HBuilder->IsSplit (Shape, TopAbs_IN)
345 || HBuilder->IsSplit (Shape, TopAbs_ON))
346 Deleted = Standard_False ;
351 //=======================================================================
352 //function : DataStructure
354 //=======================================================================
355 const Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::DataStructure() const
360 //=======================================================================
361 //function : DataStructure
363 //=======================================================================
364 Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::ChangeDataStructure()
366 return myDSA.ChangeDS();
369 //=======================================================================
372 //=======================================================================
373 const Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::Builder() const
375 return myDSA.Builder();
378 //=======================================================================
381 //=======================================================================
382 Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::ChangeBuilder()
384 return myDSA.ChangeBuilder();
387 //=======================================================================
388 //function : DataStructureAccess
389 //purpose : returns the member myDSA.
390 //=======================================================================
391 BRepAlgo_DSAccess& BRepAlgo_BooleanOperations::DataStructureAccess()