1 // Created on: 1993-10-15
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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.
17 #include <BRepAlgoAPI_BooleanOperation.ixx>
19 #include <BRepAlgoAPI.hxx>
20 #include <BRepAlgoAPI_Check.hxx>
22 #include <BRepLib_FuseEdges.hxx>
24 #include <TopTools_MapOfShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
27 #include <BOPAlgo_PaveFiller.hxx>
28 #include <BOPAlgo_BOP.hxx>
29 #include <BOPDS_DS.hxx>
31 //=======================================================================
32 //function : BRepAlgoAPI_BooleanOperation
34 //=======================================================================
35 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation(const TopoDS_Shape& aS1,
36 const TopoDS_Shape& aS2,
37 const BOPAlgo_Operation anOp)
41 myBuilderCanWork(Standard_False),
47 myFuseEdges(Standard_False)
50 //=======================================================================
51 //function : BRepAlgoAPI_BooleanOperation
53 //=======================================================================
54 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation(const TopoDS_Shape& aS1,
55 const TopoDS_Shape& aS2,
56 const BOPAlgo_PaveFiller& aDSFiller,
57 const BOPAlgo_Operation anOp)
61 myBuilderCanWork(Standard_False),
67 myFuseEdges(Standard_False)
69 if ((Standard_Address) &aDSFiller!=NULL) {
70 myDSFiller=(BOPAlgo_PaveFiller*)&aDSFiller;
73 //=======================================================================
76 //=======================================================================
77 void BRepAlgoAPI_BooleanOperation::Destroy()
79 if (myBuilder!=NULL) {
83 if (myDSFiller!=NULL && myEntryType) {
92 //=======================================================================
93 //function : SetOperation
95 //=======================================================================
96 void BRepAlgoAPI_BooleanOperation::SetOperation (const BOPAlgo_Operation anOp)
100 //=======================================================================
101 //function : Operation
103 //=======================================================================
104 BOPAlgo_Operation BRepAlgoAPI_BooleanOperation::Operation ()const
109 //=======================================================================
110 //function : FuseEdges
112 //=======================================================================
113 Standard_Boolean BRepAlgoAPI_BooleanOperation::FuseEdges ()const
118 //=======================================================================
121 //=======================================================================
122 const TopoDS_Shape& BRepAlgoAPI_BooleanOperation::Shape1() const
127 //=======================================================================
130 //=======================================================================
131 const TopoDS_Shape& BRepAlgoAPI_BooleanOperation::Shape2() const
136 //=======================================================================
137 //function : BuilderCanWork
139 //=======================================================================
140 Standard_Boolean BRepAlgoAPI_BooleanOperation::BuilderCanWork() const
142 return myBuilderCanWork;
144 //=======================================================================
145 //function : ErrorStatus
147 //=======================================================================
148 Standard_Integer BRepAlgoAPI_BooleanOperation::ErrorStatus()const
150 return myErrorStatus;
152 //=======================================================================
153 //function : Modified
155 //=======================================================================
156 const TopTools_ListOfShape& BRepAlgoAPI_BooleanOperation::Modified(const TopoDS_Shape& aS)
158 if (myBuilder==NULL) {
163 myGenerated = myBuilder->Modified(aS);
166 TopTools_ListOfShape theLS;
167 theLS.Assign(myGenerated);
175 //=======================================================================
176 //function : IsDeleted
178 //=======================================================================
179 Standard_Boolean BRepAlgoAPI_BooleanOperation::IsDeleted(const TopoDS_Shape& aS)
181 Standard_Boolean bDeleted = Standard_True;
182 if (myBuilder != NULL) {
183 bDeleted=myBuilder->IsDeleted(aS);
188 //=======================================================================
189 //function : PrepareFiller
191 //=======================================================================
192 Standard_Boolean BRepAlgoAPI_BooleanOperation::PrepareFiller()
194 Standard_Boolean bIsNewFiller=Standard_False;
197 if (myS1.IsNull() || myS2.IsNull()) {
202 if (myOperation==BOPAlgo_UNKNOWN) {
207 if (myDSFiller==NULL) {
208 bIsNewFiller=!bIsNewFiller;
210 myDSFiller=new BOPAlgo_PaveFiller;
212 if (myDSFiller==NULL) {
217 BOPCol_ListOfShape aLS;
221 myDSFiller->SetArguments(aLS);
226 //=======================================================================
229 //=======================================================================
230 void BRepAlgoAPI_BooleanOperation::Build()
232 Standard_Boolean bIsNewFiller;
233 Standard_Integer iErr;
235 //dump arguments and result of boolean operation in tcl script
236 char *pathdump = getenv("CSF_DEBUG_BOP");
237 Standard_Boolean isDump = (pathdump != NULL),
238 isDumpArgs = Standard_False,
239 isDumpRes = Standard_False;
240 Standard_CString aPath = pathdump;
242 myBuilderCanWork=Standard_False;
245 bIsNewFiller=PrepareFiller();
247 if (myErrorStatus!=1) {
248 // there was errors during the preparation
254 myDSFiller->Perform();
257 if (myBuilder!=NULL) {
262 const TopoDS_Shape& aS1 = myS1;
263 const TopoDS_Shape& aS2 = myS2;
266 BRepAlgoAPI_Check aChekArgs(aS1, aS2, myOperation);
267 isDumpArgs = !aChekArgs.IsValid();
272 myBuilder=new BOPAlgo_BOP;
273 myBuilder->AddArgument(aS1);
274 myBuilder->AddTool(aS2);
275 myBuilder->SetOperation(myOperation);
277 myBuilder->PerformWithFiller(*myDSFiller);
278 iErr = myBuilder->ErrorStatus();
281 myBuilderCanWork=Standard_True;
282 myShape=myBuilder->Shape();
285 BRepAlgoAPI_Check aCheckRes(myShape);
286 isDumpRes = !aCheckRes.IsValid();
287 if (isDumpArgs || isDumpRes) {
288 BRepAlgoAPI::DumpOper(aPath, aS1, aS2, myShape, myOperation, isDumpArgs);
295 myErrorStatus=100+iErr;
301 //=======================================================================
302 //function : SectionEdges
304 //=======================================================================
305 const TopTools_ListOfShape& BRepAlgoAPI_BooleanOperation::SectionEdges()
307 if (myBuilder==NULL) {
312 Standard_Integer aNb, i, j, aNbCurves, nE;
313 BOPDS_ListIteratorOfListOfPaveBlock anIt;
315 const BOPDS_PDS& pDS = myDSFiller->PDS();
316 BOPDS_VectorOfInterfFF& aFFs=pDS->InterfFF();
320 for (i = 0; i < aNb; i++) {
321 BOPDS_InterfFF& aFFi=aFFs(i);
322 const BOPDS_VectorOfCurve& aSeqOfCurve=aFFi.Curves();
324 aNbCurves=aSeqOfCurve.Extent();
325 for (j=0; j<aNbCurves; j++) {
326 const BOPDS_Curve& aCurve=aSeqOfCurve(j);
327 const BOPDS_ListOfPaveBlock& aSectEdges = aCurve.PaveBlocks();
329 anIt.Initialize(aSectEdges);
330 for(; anIt.More(); anIt.Next()) {
331 const Handle(BOPDS_PaveBlock)& aPB = anIt.Value();
333 const TopoDS_Shape& aE = pDS->Shape(nE);
334 myGenerated.Append(aE);
340 TopTools_ListOfShape theLS;
341 theLS.Assign(myGenerated);
349 // ================================================================================================
350 // function: Generated
352 // ================================================================================================
353 const TopTools_ListOfShape& BRepAlgoAPI_BooleanOperation::Generated(const TopoDS_Shape& S)
355 if (myBuilder==NULL) {
361 const TopTools_ListOfShape& aL = myBuilder->Generated(S);
362 return RefinedList(aL);
365 return myBuilder->Generated(S);
368 // ================================================================================================
369 // function: HasModified
371 // ================================================================================================
372 Standard_Boolean BRepAlgoAPI_BooleanOperation::HasModified() const
374 if (myBuilder==NULL) {
375 return Standard_False;
377 return myBuilder->HasModified();
380 // ================================================================================================
381 // function: HasGenerated
383 // ================================================================================================
384 Standard_Boolean BRepAlgoAPI_BooleanOperation::HasGenerated() const
386 if (myBuilder==NULL) {
387 return Standard_False;
389 return myBuilder->HasGenerated();
392 // ================================================================================================
393 // function: HasDeleted
395 // ================================================================================================
396 Standard_Boolean BRepAlgoAPI_BooleanOperation::HasDeleted() const
398 if (myBuilder==NULL) {
399 return Standard_False;
401 return myBuilder->HasDeleted();
403 //=======================================================================
404 //function : RefineEdges
406 //=======================================================================
408 void BRepAlgoAPI_BooleanOperation::RefineEdges ()
410 if(myFuseEdges) return; //Edges have been refined yet
412 BRepLib_FuseEdges FE(myShape);
413 FE.SetConcatBSpl(Standard_True);
415 // avoid fusing old edges
416 TopTools_IndexedMapOfShape mapOldEdges;
417 TopExp::MapShapes (myS1, TopAbs_EDGE, mapOldEdges);
418 TopExp::MapShapes (myS2, TopAbs_EDGE, mapOldEdges);
419 FE.AvoidEdges (mapOldEdges);
421 // Get List of edges that have been fused
422 myFuseEdges = Standard_False;
423 myModifFaces.Clear();
425 TopTools_DataMapOfIntegerListOfShape aFusedEdges;
427 FE.Edges(aFusedEdges);
428 Standard_Integer nle = aFusedEdges.Extent();
431 myShape = FE.Shape();
433 TopTools_DataMapOfIntegerShape aResultEdges;
435 FE.ResultEdges(aResultEdges);
436 FE.Faces(myModifFaces);
437 myFuseEdges = Standard_True;
440 for(i = 1; i <= nle; ++i) {
441 const TopoDS_Shape& aNewE = aResultEdges(i);
442 const TopTools_ListOfShape& aListOfOldEdges = aFusedEdges(i);
443 TopTools_ListIteratorOfListOfShape anIter(aListOfOldEdges);
444 for(; anIter.More(); anIter.Next()) {
445 myEdgeMap.Bind(anIter.Value(), aNewE);
451 //=======================================================================
452 //function : RefinedList
454 //=======================================================================
455 const TopTools_ListOfShape&
456 BRepAlgoAPI_BooleanOperation::RefinedList(const TopTools_ListOfShape& theL)
459 TopTools_MapOfShape aMap;
461 TopTools_ListIteratorOfListOfShape anIter(theL);
463 for(; anIter.More(); anIter.Next()) {
464 const TopoDS_Shape& anS = anIter.Value();
466 if(anS.ShapeType() == TopAbs_EDGE) {
467 if(myEdgeMap.IsBound(anS)) {
468 const TopoDS_Shape& aNewEdge = myEdgeMap.Find(anS);
469 if(aMap.Add(aNewEdge)) {
470 myGenerated.Append(aNewEdge);
474 myGenerated.Append(anS);
477 else if (anS.ShapeType() == TopAbs_FACE) {
478 if(myModifFaces.IsBound(anS)) {
479 myGenerated.Append(myModifFaces.Find(anS));
482 myGenerated.Append(anS);
486 myGenerated.Append(anS);