summaryrefslogtreecommitdiff
path: root/src/BRepAlgo/BRepAlgo_BooleanOperations.cxx
blob: 63f00f3fe880969122fc89495826f245d894c0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// File:	BRepAlgo_BooleanOperation.cxx
// Created:	Thu Nov 20 17:08:08 1997
// Author:	Prestataire Mary FABIEN
//		<fbi@langdox.paris1.matra-dtv.fr>


#include <BRepAlgo_BooleanOperations.ixx>
#include <TopOpeBRep_DSFiller.hxx>
#include <TopOpeBRepDS_BuildTool.hxx>
#include <TopOpeBRepTool_GeomTool.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <BRepTools_Substitution.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <Geom_Surface.hxx>
#include <TopLoc_Location.hxx>
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>

//=======================================================================
//function : Create
//purpose  : 
//=======================================================================
  BRepAlgo_BooleanOperations::BRepAlgo_BooleanOperations() :
myApproxNbPntMax (30) ,
myApproxTol3D (1.e-7) ,
myApproxTol2D (1.e-7) ,
myApproxRelativeTol (Standard_True)
{
}

//=======================================================================
//function : Shapes2d
//purpose  : 
//=======================================================================
  void BRepAlgo_BooleanOperations::Shapes2d (const TopoDS_Shape& S1,
					    const TopoDS_Shape& S2) 
{
  // S1 doit etre une face ou un ensemble de faces
  // S2 doit etre une edge.

  if (S2.ShapeType() != TopAbs_EDGE) return;

  BRep_Builder Builder ;
  TopoDS_Wire Wire ;
  Builder.MakeWire (Wire);
  Builder.Add (Wire, S2);
  
  TopExp_Explorer Exp (S1, TopAbs_FACE) ;
  if (!Exp.More()) return ;
  const TopoDS_Face& FirstFace = TopoDS::Face (Exp.Current()) ;

  TopLoc_Location Loc;
  const Handle(Geom_Surface)& Surf = BRep_Tool::Surface (FirstFace, Loc) ; 

  TopoDS_Face Face ;
  Builder.MakeFace (Face, Surf, Loc, BRep_Tool::Tolerance (FirstFace)) ;
  Builder.Add (Face, Wire) ;
  Face.Orientation (FirstFace.Orientation()) ;
  
  myS1 = S1 ;
  myS2 = Face ;

  myDSA.Init() ;
  myDSA.Load (myS1, myS2) ;
  Handle (TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS() ;
  myDSA.myDSFiller.Insert2d (myS1, myS2, HDS) ;
}

//=======================================================================
//function : Shapes
//purpose  : Defines the arguments.
//=======================================================================
  void BRepAlgo_BooleanOperations::Shapes (const TopoDS_Shape& S1,
					   const TopoDS_Shape& S2)
{
  myS1 = S1;
  myS2 = S2;
  myDSA.Init();
  myDSA.Load(myS1, myS2);
  Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
  myDSA.myDSFiller.Insert(myS1,myS2,HDS);
  //  const Standard_Boolean CheckShapes = Standard_True;
  //  Standard_Boolean esp = HDS->EdgesSameParameter();
  //  Standard_Boolean localcheck = CheckShapes;
}

//=======================================================================
//function : SetApproxParameters
//purpose  : Sets the parameters for the approximations.
//=======================================================================
  void BRepAlgo_BooleanOperations::SetApproxParameters (const Standard_Integer NbPntMax,
							const Standard_Real Tol3D,
							const Standard_Real Tol2D,
							const Standard_Boolean RelativeTol)
{
  myApproxNbPntMax = NbPntMax ;
  myApproxTol3D = Tol3D ;
  myApproxTol2D = Tol2D ;
  myApproxRelativeTol = RelativeTol ;
}

//=======================================================================
//function : Define
//purpose  : 
//=======================================================================
  void BRepAlgo_BooleanOperations::Define (const TopoDS_Shape& S1,
					   const TopoDS_Shape& S2,
					   Handle(TopOpeBRepDS_HDataStructure)& HDS)
{
  ChangeDataStructure() = HDS;
  myS1 = S1;
  myS2 = S2;
}

//=======================================================================
//function : Perform
//purpose  : Performs the global boolean operation.
//=======================================================================
  void BRepAlgo_BooleanOperations::Perform ()
{
  TopOpeBRepDS_BuildTool& BTofBuilder = myDSA.myHB->ChangeBuildTool();
  TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
  GTofBTofBuilder.SetNbPntMax(myApproxNbPntMax);
  GTofBTofBuilder.SetTolerances (myApproxTol3D, myApproxTol2D, myApproxRelativeTol) ;
  Handle(TopOpeBRepBuild_HBuilder)& HB = myDSA.myHB;
  Handle(TopOpeBRepDS_HDataStructure)& HDS = myDSA.ChangeDS();
  HB->Perform(HDS,myS1,myS2);
}

//=======================================================================
//function : Perform
//purpose  : Performs the global boolean operation in regards of the
//           given states.
//=======================================================================
  void BRepAlgo_BooleanOperations::Perform (const TopAbs_State State1,
					   const TopAbs_State State2)
{
  Perform() ;

  myShape.Nullify() ;
  myResult.Nullify() ;
  myMapShape.Clear() ;

  Handle(TopOpeBRepBuild_HBuilder)& HBuilder = ChangeBuilder() ;
  HBuilder->MergeShapes (myS1, State1, myS2, State2) ;

  const TopTools_ListOfShape& ListResults = HBuilder->Merged (myS1, State1) ;
  Standard_Integer NbResults = ListResults.Extent() ;
  if (NbResults > 0) {
    if (NbResults == 1) {
      myShape = ListResults.First() ;
    } else {
      BRep_Builder Builder ;
      Builder.MakeCompound (TopoDS::Compound (myShape)) ;
      TopTools_ListIteratorOfListOfShape Iter ;
      for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
	Builder.Add (myShape, Iter.Value()) ;
    }
    TopExp_Explorer Explorer ;
    for (Explorer.Init (myShape, TopAbs_FACE) ; Explorer.More() ; Explorer.Next()) {
      myMapShape.Add (Explorer.Current()) ;
    }
    for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
      myMapShape.Add (Explorer.Current()) ;
    }
  }
}

//=======================================================================
//function : Common
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::Common() 
{
  Perform (TopAbs_IN, TopAbs_IN) ;
  return myShape ;
}

//=======================================================================
//function : fus
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::Fus() 
{
  Perform (TopAbs_OUT, TopAbs_OUT) ;
  return myShape ;
}

//=======================================================================
//function : cut
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::Cut() 
{
  Perform (TopAbs_OUT, TopAbs_IN) ;
  return myShape ;
}

//=======================================================================
//function : Section
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::Section() 
{
//  Standard_Boolean bcw = BuilderCanWork();
//  if ( ! bcw || myshapeisnull) return;

  Perform () ;

  myShape.Nullify() ;
  myResult.Nullify() ;
  myMapShape.Clear() ;
  
  Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;

  const TopTools_ListOfShape& ListResults = HBuilder->Section() ;
  Standard_Integer NbResults = ListResults.Extent() ;
  if (NbResults > 0) {
    if (NbResults == 1) {
      myShape = ListResults.First() ;
    } else {
      BRep_Builder Builder ;
      Builder.MakeCompound (TopoDS::Compound (myShape)) ;
      TopTools_ListIteratorOfListOfShape Iter ;
      for (Iter.Initialize (ListResults) ; Iter.More() ; Iter.Next())
	Builder.Add (myShape, Iter.Value()) ;
    }
    TopExp_Explorer Explorer ;
    for (Explorer.Init (myShape, TopAbs_EDGE) ; Explorer.More() ; Explorer.Next()) {
      myMapShape.Add (Explorer.Current()) ;
    }
  }

  return myShape ;
}

//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::Shape() 
{
  return myShape;
}

//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================
  const TopoDS_Shape& BRepAlgo_BooleanOperations::ShapeFrom (const TopoDS_Shape& Shape) 
{
  myResult.Nullify() ;

  if (!myShape.IsNull()) {
    
    TopoDS_Shape ShapeToDel ;
    if (Shape.IsSame (myS1)) {
      ShapeToDel = myS2 ;
    } else {
      ShapeToDel = myS1 ;
    }

    BRepTools_Substitution Substitute ;

    TopTools_ListOfShape NullFaces ;
    NullFaces.Clear() ;

    TopExp_Explorer ExpFac ;
    for (ExpFac.Init (ShapeToDel, TopAbs_FACE) ; ExpFac.More() ; ExpFac.Next()) {
      const TopoDS_Face& Face = TopoDS::Face (ExpFac.Current()) ;
      const TopTools_ListOfShape& ListResults = Modified (Face) ;
      if (ListResults.Extent() == 0) {
	if (myMapShape.Contains (Face)) Substitute.Substitute (Face, NullFaces) ;
      } else {
	TopTools_ListIteratorOfListOfShape ItrFace ;
	for (ItrFace.Initialize (ListResults) ; ItrFace.More() ; ItrFace.Next()) {
	  Substitute.Substitute (TopoDS::Face (ItrFace.Value()), NullFaces) ;
	}
      }
    }

    Substitute.Build (myShape) ;
    if (Substitute.IsCopied (myShape)) {
      const TopTools_ListOfShape& ListResults = Substitute.Copy (myShape) ;
      Standard_Integer NbResults = ListResults.Extent() ;
      if (NbResults == 1) {
	myResult = ListResults.First() ;
      } else if (NbResults > 1) {
	BRep_Builder Builder ;
	Builder.MakeCompound (TopoDS::Compound (myResult)) ;
	TopTools_ListIteratorOfListOfShape ItrResult ;
	for (ItrResult.Initialize (ListResults) ; ItrResult.More() ; ItrResult.Next()) {
	  Builder.Add (myResult, ItrResult.Value()) ;
	}
      }
    } else {
      myResult = myShape ;
    }

  }  
  return myResult ;
}

//=======================================================================
//function : Modified
//purpose  : 
//=======================================================================
  const TopTools_ListOfShape& BRepAlgo_BooleanOperations::Modified (const TopoDS_Shape& Shape) 
{
  return myDSA.Modified(Shape);
}

//=======================================================================
//function : IsDeleted
//purpose  : 
//=======================================================================
  Standard_Boolean BRepAlgo_BooleanOperations::IsDeleted (const TopoDS_Shape& Shape)
{
  Standard_Boolean Deleted = Standard_True ; 

  Handle(TopOpeBRepBuild_HBuilder)& HBuilder = myDSA.myHB ;

  if (   myMapShape.Contains (Shape)
      || HBuilder->IsMerged (Shape, TopAbs_OUT)
      || HBuilder->IsMerged (Shape, TopAbs_IN)
      || HBuilder->IsMerged (Shape, TopAbs_ON)
      || HBuilder->IsSplit (Shape, TopAbs_OUT)
      || HBuilder->IsSplit (Shape, TopAbs_IN)
      || HBuilder->IsSplit (Shape, TopAbs_ON))
    Deleted = Standard_False ;

  return Deleted ;    
}

//=======================================================================
//function : DataStructure
//purpose  : 
//=======================================================================
  const Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::DataStructure() const
{
  return myDSA.DS();
}

//=======================================================================
//function : DataStructure
//purpose  : 
//=======================================================================
  Handle(TopOpeBRepDS_HDataStructure)& BRepAlgo_BooleanOperations::ChangeDataStructure()
{
  return myDSA.ChangeDS();
}

//=======================================================================
//function : Builder
//purpose  : 
//=======================================================================
  const Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::Builder() const 
{
  return myDSA.Builder();
}

//=======================================================================
//function : Builder
//purpose  : 
//=======================================================================
  Handle(TopOpeBRepBuild_HBuilder)& BRepAlgo_BooleanOperations::ChangeBuilder() 
{
  return myDSA.ChangeBuilder();
}

//=======================================================================
//function : DataStructureAccess
//purpose  : returns the member myDSA.
//=======================================================================
  BRepAlgo_DSAccess& BRepAlgo_BooleanOperations::DataStructureAccess()
{
  return myDSA;
}