0025477: Boolean Operations with additional tolerance - Fuzzy Boolean operations
[occt.git] / src / BRepAlgoAPI / BRepAlgoAPI_Section.cxx
CommitLineData
b311480e 1// Created on: 1994-02-18
2// Created by: Remi LEQUETTE
3// Copyright (c) 1994-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 17// modified by Michael KLOKOV Wed Mar 6 15:01:25 2002
4e57c75e 18// modified by Eugeny MALTCHIKOV Wed Jul 04 11:13:01 2012
7fd59977 19
20#include <BRepAlgoAPI_Section.ixx>
21
22
23#include <BRepBuilderAPI_MakeFace.hxx>
24#include <BRepBuilderAPI_MakeShell.hxx>
25#include <Geom_Plane.hxx>
26#include <Geom2d_TrimmedCurve.hxx>
4e57c75e 27
7fd59977 28#include <TopExp.hxx>
29#include <TopTools_IndexedMapOfShape.hxx>
30#include <TopTools_MapOfShape.hxx>
31#include <TopoDS.hxx>
32#include <TopoDS_Face.hxx>
33#include <BRep_Tool.hxx>
34
4e57c75e 35#include <BOPAlgo_PaveFiller.hxx>
36#include <BOPAlgo_BOP.hxx>
37#include <BOPDS_DS.hxx>
7fd59977 38
b1d15f53 39//
40static
41 TopoDS_Shape MakeShape(const Handle(Geom_Surface)& );
7fd59977 42
b1d15f53 43static
44 Standard_Boolean HasAncestorFaces(const BOPAlgo_PPaveFiller&,
45 const TopoDS_Shape&,
46 TopoDS_Shape&,
47 TopoDS_Shape&);
7fd59977 48
b1d15f53 49//=======================================================================
50//function : BRepAlgoAPI_Section
51//purpose :
52//=======================================================================
53BRepAlgoAPI_Section::BRepAlgoAPI_Section()
54:
55 BRepAlgoAPI_BooleanOperation()
56{
57 myOperation=BOPAlgo_SECTION;
58 InitParameters();
59}
60//=======================================================================
61//function : ~BRepAlgoAPI_Section
62//purpose :
63//=======================================================================
64BRepAlgoAPI_Section::~BRepAlgoAPI_Section()
65{
66}
67//=======================================================================
68//function : InitParameters
69//purpose :
70//=======================================================================
71void BRepAlgoAPI_Section::InitParameters()
72{
73 myparameterschanged = Standard_False;
74 myshapeisnull = Standard_False;
75 myApprox = Standard_False;
76 myComputePCurve1 = Standard_False;
77 myComputePCurve2 = Standard_False;
78}
7fd59977 79//=======================================================================
80//function : Constructor
81//purpose :
82//=======================================================================
83BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape& Sh1,
4e57c75e 84 const TopoDS_Shape& Sh2,
85 const Standard_Boolean PerformNow)
b1d15f53 86:
87 BRepAlgoAPI_BooleanOperation(Sh1, Sh2, BOPAlgo_SECTION)
7fd59977 88{
89 InitParameters();
90 myparameterschanged = Standard_True;
91
92 if(myS1.IsNull() || myS2.IsNull()) {
7fd59977 93 myshapeisnull = Standard_True;
94 }
4e57c75e 95 if (PerformNow) {
7fd59977 96 Build();
4e57c75e 97 }
7fd59977 98}
b1d15f53 99//=======================================================================
100//function : BRepAlgoAPI_Section
101//purpose :
102//=======================================================================
103BRepAlgoAPI_Section::BRepAlgoAPI_Section
104 (const TopoDS_Shape& aS1,
105 const TopoDS_Shape& aS2,
106 const BOPAlgo_PaveFiller& aDSF,
107 const Standard_Boolean PerformNow)
108:
109 BRepAlgoAPI_BooleanOperation(aS1, aS2, aDSF, BOPAlgo_SECTION)
7fd59977 110{
111 InitParameters();
112 myparameterschanged = Standard_True;
113
114 if(myS1.IsNull() || myS2.IsNull()) {
7fd59977 115 myshapeisnull = Standard_True;
116 }
117
4e57c75e 118 if (PerformNow) {
7fd59977 119 Build();
120 }
121}
7fd59977 122//=======================================================================
123//function : Constructor
124//purpose :
125//=======================================================================
b1d15f53 126BRepAlgoAPI_Section::BRepAlgoAPI_Section
127 (const TopoDS_Shape& Sh,
128 const gp_Pln& Pl,
129 const Standard_Boolean PerformNow)
130:
131 BRepAlgoAPI_BooleanOperation(Sh, MakeShape(new Geom_Plane(Pl)),
132 BOPAlgo_SECTION)
7fd59977 133{
134 InitParameters();
135 myparameterschanged = Standard_True;
136
137 if(Sh.IsNull() || myS2.IsNull()) {
7fd59977 138 myshapeisnull = Standard_True;
139 }
4e57c75e 140 if (PerformNow) {
7fd59977 141 Build();
4e57c75e 142 }
7fd59977 143}
7fd59977 144//=======================================================================
145//function : Constructor
146//purpose :
147//=======================================================================
b1d15f53 148BRepAlgoAPI_Section::BRepAlgoAPI_Section
149 (const TopoDS_Shape& Sh,
150 const Handle(Geom_Surface)& Sf,
151 const Standard_Boolean PerformNow)
152:
153 BRepAlgoAPI_BooleanOperation(Sh, MakeShape(Sf), BOPAlgo_SECTION)
7fd59977 154{
155 InitParameters();
156 myparameterschanged = Standard_True;
157
158 if(Sh.IsNull() || myS2.IsNull()) {
7fd59977 159 myshapeisnull = Standard_True;
160 }
4e57c75e 161 if (PerformNow) {
7fd59977 162 Build();
4e57c75e 163 }
7fd59977 164}
7fd59977 165//=======================================================================
166//function : Constructor
167//purpose :
168//=======================================================================
b1d15f53 169BRepAlgoAPI_Section::BRepAlgoAPI_Section
170 (const Handle(Geom_Surface)& Sf,
171 const TopoDS_Shape& Sh,
172 const Standard_Boolean PerformNow)
173:
174 BRepAlgoAPI_BooleanOperation(MakeShape(Sf), Sh, BOPAlgo_SECTION)
7fd59977 175{
176 InitParameters();
177 myparameterschanged = Standard_True;
178
179 if(myS1.IsNull() || Sh.IsNull()) {
7fd59977 180 myshapeisnull = Standard_True;
181 }
4e57c75e 182 if (PerformNow) {
7fd59977 183 Build();
4e57c75e 184 }
7fd59977 185}
7fd59977 186//=======================================================================
187//function : Constructor
188//purpose :
189//=======================================================================
b1d15f53 190BRepAlgoAPI_Section::BRepAlgoAPI_Section
191 (const Handle(Geom_Surface)& Sf1,
192 const Handle(Geom_Surface)& Sf2,
193 const Standard_Boolean PerformNow)
194:
195 BRepAlgoAPI_BooleanOperation(MakeShape(Sf1),
196 MakeShape(Sf2),
197 BOPAlgo_SECTION)
7fd59977 198{
199 InitParameters();
200 myparameterschanged = Standard_True;
201
202 if(myS1.IsNull() || myS2.IsNull()) {
7fd59977 203 myshapeisnull = Standard_True;
204 }
205
4e57c75e 206 if (PerformNow) {
7fd59977 207 Build();
4e57c75e 208 }
7fd59977 209}
7fd59977 210//=======================================================================
211//function : Init1
212//purpose :
213//=======================================================================
214void BRepAlgoAPI_Section::Init1(const TopoDS_Shape& S1)
215{
216 if(!S1.IsNull()) {
217 if (!S1.IsEqual(myS1)) {
218 myS1 = S1;
219
220 if(!myS2.IsNull()) {
4e57c75e 221 myshapeisnull = Standard_False;
7fd59977 222 }
223 myparameterschanged = Standard_True;
224 }
225 }
226 else {
227 if(!myS1.IsNull()) {
228 myS1 = S1;
229 myshapeisnull = Standard_True;
230 myparameterschanged = Standard_True;
231 }
232 }
233
234 if(myparameterschanged)
235 NotDone();
236}
7fd59977 237//=======================================================================
238//function : Init1
239//purpose :
240//=======================================================================
241void BRepAlgoAPI_Section::Init1(const gp_Pln& Pl)
242{
243 Init1(MakeShape(new Geom_Plane(Pl)));
244}
7fd59977 245//=======================================================================
246//function : Init1
247//purpose :
248//=======================================================================
249void BRepAlgoAPI_Section::Init1(const Handle(Geom_Surface)& Sf)
250{
251 Init1(MakeShape(Sf));
252}
7fd59977 253//=======================================================================
254//function : Init2
255//purpose :
256//=======================================================================
257void BRepAlgoAPI_Section::Init2(const TopoDS_Shape& S2)
258{
259 if(!S2.IsNull()) {
260 if (!S2.IsEqual(myS2)) {
261 myS2 = S2;
262
263 if(!myS1.IsNull()) {
4e57c75e 264 myshapeisnull = Standard_False;
7fd59977 265 }
266 myparameterschanged = Standard_True;
267 }
268 }
269 else {
270 if(!myS2.IsNull()) {
271 myS2 = S2;
272 myshapeisnull = Standard_True;
273 myparameterschanged = Standard_True;
274 }
275 }
276
277 if(myparameterschanged) {
278 NotDone();
279 }
280}
7fd59977 281//=======================================================================
282//function : Init2
283//purpose :
284//=======================================================================
285void BRepAlgoAPI_Section::Init2(const gp_Pln& Pl)
286{
287 Init2(MakeShape(new Geom_Plane(Pl)));
288}
7fd59977 289//=======================================================================
290//function : Init2
291//purpose :
292//=======================================================================
293void BRepAlgoAPI_Section::Init2(const Handle(Geom_Surface)& Sf)
294{
295 Init2(MakeShape(Sf));
296}
7fd59977 297//=======================================================================
298//function : Approximation
299//purpose :
300//=======================================================================
301void BRepAlgoAPI_Section::Approximation(const Standard_Boolean B)
302{
303 if(myApprox != B) {
304 myApprox = B;
305 myparameterschanged = Standard_True;
306 }
307}
7fd59977 308//=======================================================================
309//function : ComputePCurveOn1
310//purpose :
311//=======================================================================
312void BRepAlgoAPI_Section::ComputePCurveOn1(const Standard_Boolean B)
313{
314 if(myComputePCurve1 != B) {
315 myComputePCurve1 = B;
316 myparameterschanged = Standard_True;
317 }
318}
7fd59977 319//=======================================================================
320//function : ComputePCurveOn2
321//purpose :
322//=======================================================================
323void BRepAlgoAPI_Section::ComputePCurveOn2(const Standard_Boolean B)
324{
325 if(myComputePCurve2 != B) {
326 myComputePCurve2 = B;
327 myparameterschanged = Standard_True;
328 }
329}
7fd59977 330//=======================================================================
331//function : Build
332//purpose :
333//=======================================================================
334void BRepAlgoAPI_Section::Build()
335{
336 if(myshapeisnull) {
337 myErrorStatus = 2;
338 NotDone();
339 return;
340 }
341
342 if(myparameterschanged) {
7fd59977 343 Standard_Boolean bIsNewFiller = PrepareFiller();
344 //
345 if (myErrorStatus!=1) {
346 NotDone();
347 // there were errors during the preparation
348 return;
349 }
350 //
351 if (bIsNewFiller) {
b1d15f53 352 BOPAlgo_SectionAttribute theSecAttr(myApprox,
353 myComputePCurve1,
354 myComputePCurve2);
4e57c75e 355 myDSFiller->SetSectionAttribute(theSecAttr);
356 myDSFiller->Perform();
7fd59977 357 }
4e57c75e 358 //
359 BRepAlgoAPI_BooleanOperation::Build();
360 //
7fd59977 361 myparameterschanged = Standard_False;
362 }
363}
7fd59977 364//=======================================================================
365//function : HasAncestorFaceOn1
366//purpose :
367//=======================================================================
b1d15f53 368Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn1
369 (const TopoDS_Shape& E,
370 TopoDS_Shape& F) const
7fd59977 371{
372 Standard_Boolean aResult = Standard_False;
373 if(E.IsNull()) {
374 return aResult;
375 }
376
377 if(E.ShapeType() != TopAbs_EDGE) {
378 return aResult;
379 }
380 TopoDS_Shape F1, F2;
4e57c75e 381 aResult = HasAncestorFaces(myDSFiller, E, F1, F2);
7fd59977 382
383 if(F1.IsNull()) {
384 return Standard_False;
385 }
386 F = F1;
387 return aResult;
388}
7fd59977 389//=======================================================================
390//function : HasAncestorFaceOn2
391//purpose :
392//=======================================================================
b1d15f53 393Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn2
394 (const TopoDS_Shape& E,
395 TopoDS_Shape& F) const
7fd59977 396{
397 Standard_Boolean aResult = Standard_False;
398 if(E.IsNull()) {
399 return aResult;
400 }
401
402 if(E.ShapeType() != TopAbs_EDGE) {
403 return aResult;
404 }
405 TopoDS_Shape F1, F2;
4e57c75e 406 aResult = HasAncestorFaces(myDSFiller, E, F1, F2);
7fd59977 407
408 if(F2.IsNull()) {
409 return Standard_False;
410 }
411 F = F2;
412 return aResult;
413}
414
7fd59977 415//=======================================================================
b1d15f53 416//function : HasAncestorFaces
7fd59977 417//purpose :
418//=======================================================================
b1d15f53 419Standard_Boolean HasAncestorFaces(const BOPAlgo_PPaveFiller& theDSFiller,
420 const TopoDS_Shape& E,
421 TopoDS_Shape& F1,
422 TopoDS_Shape&F2)
7fd59977 423{
b1d15f53 424
7a9d451a 425 Standard_Integer aNb, i, j, nE, nF1, nF2, aNbCurves;
4e57c75e 426 //
427 const BOPDS_PDS& pDS = theDSFiller->PDS();
428 BOPDS_VectorOfInterfFF& aFFs=pDS->InterfFF();
429 //
430 aNb=aFFs.Extent();
7a9d451a 431 //section edges
4e57c75e 432 for (i = 0; i < aNb; i++) {
433 BOPDS_InterfFF& aFFi=aFFs(i);
434 aFFi.Indices(nF1, nF2);
435 //
4e57c75e 436 const BOPDS_VectorOfCurve& aSeqOfCurve=aFFi.Curves();
437 aNbCurves=aSeqOfCurve.Extent();
438 for (j=0; j<aNbCurves; j++) {
439 const BOPDS_Curve& aCurve=aSeqOfCurve(j);
440
441 const BOPDS_ListOfPaveBlock& aSectEdges = aCurve.PaveBlocks();
442
443 BOPDS_ListIteratorOfListOfPaveBlock anIt;
444 anIt.Initialize(aSectEdges);
7fd59977 445
446 for(; anIt.More(); anIt.Next()) {
4e57c75e 447 const Handle(BOPDS_PaveBlock)& aPB = anIt.Value();
448 nE = aPB->Edge();
449 if(nE < 0) continue;
450 //
451 if(E.IsSame(pDS->Shape(nE))) {
452 F1 = pDS->Shape(nF1);
453 F2 = pDS->Shape(nF2);
454 return Standard_True;
455 }
7fd59977 456 }
457 }
7fd59977 458 }
459 return Standard_False;
460}
b1d15f53 461//=======================================================================
462//function : MakeShape
463//purpose :
464//=======================================================================
465TopoDS_Shape MakeShape(const Handle(Geom_Surface)& S)
466{
467 GeomAbs_Shape c = S->Continuity();
468 if (c >= GeomAbs_C2) {
469 return BRepBuilderAPI_MakeFace(S, Precision::Confusion());
470 }
471 return BRepBuilderAPI_MakeShell(S);
472}