1 // Created on: 2000-03-16
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BOPAlgo_BOP.hxx>
18 #include <BOPAlgo_MakerVolume.hxx>
19 #include <BOPAlgo_Operation.hxx>
20 #include <BOPAlgo_PaveFiller.hxx>
21 #include <BOPAlgo_Section.hxx>
22 #include <BOPCol_ListOfShape.hxx>
23 #include <BOPDS_DS.hxx>
24 #include <BOPTest.hxx>
25 #include <BOPTest_Objects.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRepAlgoAPI_BooleanOperation.hxx>
28 #include <BRepAlgoAPI_Common.hxx>
29 #include <BRepAlgoAPI_Cut.hxx>
30 #include <BRepAlgoAPI_Fuse.hxx>
31 #include <BRepAlgoAPI_Section.hxx>
34 #include <DrawTrSurf.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom_Curve.hxx>
37 #include <IntTools_Curve.hxx>
38 #include <IntTools_FaceFace.hxx>
39 #include <IntTools_PntOn2Faces.hxx>
40 #include <NCollection_BaseAllocator.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Iterator.hxx>
44 #include <TopoDS_Shape.hxx>
49 static BOPAlgo_PaveFiller* pPF=NULL;
53 Standard_Integer bopsmt(Draw_Interpretor& di,
56 const BOPAlgo_Operation aOp);
59 Standard_Integer bsmt (Draw_Interpretor& di,
62 const BOPAlgo_Operation aOp);
64 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
65 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
66 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
67 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
68 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
69 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
71 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
73 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
74 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
80 //=======================================================================
81 //function : BOPCommands
83 //=======================================================================
84 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
86 static Standard_Boolean done = Standard_False;
90 const char* g = "BOPTest commands";
93 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
94 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
95 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
96 theCommands.Add("bopcut" , "use bopcut r" , __FILE__,bopcut, g);
97 theCommands.Add("boptuc" , "use boptuc r" , __FILE__,boptuc, g);
98 theCommands.Add("bopsection", "use bopsection r" , __FILE__,bopsection, g);
100 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
101 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
102 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
103 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
104 theCommands.Add("bsection", "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
105 __FILE__, bsection, g);
107 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d/-2d1/-2d2] [-p u1 v1 u2 v2]",
108 __FILE__, bopcurves, g);
109 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni]",
110 __FILE__, mkvolume , g);
113 //=======================================================================
116 //=======================================================================
117 Standard_Integer bop(Draw_Interpretor& di,
122 Standard_Boolean bRunParallel, bNonDestructive;
123 Standard_Integer iErr;
125 TopoDS_Shape aS1, aS2;
126 BOPCol_ListOfShape aLC;
129 di << " use bop s1 s2 \n";
133 aS1=DBRep::Get(a[1]);
134 aS2=DBRep::Get(a[2]);
136 if (aS1.IsNull() || aS2.IsNull()) {
137 di << " null shapes are not allowed \n";
141 aTol=BOPTest_Objects::FuzzyValue();
142 bRunParallel=BOPTest_Objects::RunParallel();
143 bNonDestructive = BOPTest_Objects::NonDestructive();
152 Handle(NCollection_BaseAllocator)aAL=
153 NCollection_BaseAllocator::CommonBaseAllocator();
154 pPF=new BOPAlgo_PaveFiller(aAL);
156 pPF->SetArguments(aLC);
157 pPF->SetFuzzyValue(aTol);
158 pPF->SetRunParallel(bRunParallel);
159 pPF->SetNonDestructive(bNonDestructive);
162 iErr=pPF->ErrorStatus();
164 Sprintf(buf, " ErrorStatus : %d\n", iErr);
171 //=======================================================================
172 //function : bopcommon
174 //=======================================================================
175 Standard_Integer bopcommon (Draw_Interpretor& di,
179 return bopsmt(di, n, a, BOPAlgo_COMMON);
181 //=======================================================================
184 //=======================================================================
185 Standard_Integer bopfuse(Draw_Interpretor& di,
189 return bopsmt(di, n, a, BOPAlgo_FUSE);
191 //=======================================================================
194 //=======================================================================
195 Standard_Integer bopcut(Draw_Interpretor& di,
199 return bopsmt(di, n, a, BOPAlgo_CUT);
201 //=======================================================================
204 //=======================================================================
205 Standard_Integer boptuc(Draw_Interpretor& di,
209 return bopsmt(di, n, a, BOPAlgo_CUT21);
211 //=======================================================================
214 //=======================================================================
215 Standard_Integer bopsmt(Draw_Interpretor& di,
218 const BOPAlgo_Operation aOp)
221 di << " use bopsmt r\n";
226 di << " prepare PaveFiller first\n";
230 if (pPF->ErrorStatus()) {
231 di << " PaveFiller has not been done\n";
236 Standard_Boolean bRunParallel;
237 Standard_Integer aNb, iErr;
240 const BOPCol_ListOfShape& aLC=pPF->Arguments();
243 Sprintf (buf, " wrong number of arguments %s\n", aNb);
248 bRunParallel=BOPTest_Objects::RunParallel();
250 const TopoDS_Shape& aS1=aLC.First();
251 const TopoDS_Shape& aS2=aLC.Last();
253 aBOP.AddArgument(aS1);
255 aBOP.SetOperation(aOp);
256 aBOP.SetRunParallel (bRunParallel);
258 aBOP.PerformWithFiller(*pPF);
259 iErr=aBOP.ErrorStatus();
261 Sprintf(buf, " ErrorStatus : %d\n", iErr);
266 const TopoDS_Shape& aR=aBOP.Shape();
268 di << " null shape\n";
272 DBRep::Set(a[1], aR);
275 //=======================================================================
276 //function : bopsection
278 //=======================================================================
279 Standard_Integer bopsection(Draw_Interpretor& di,
284 di << " use bopsection r\n";
289 di << " prepare PaveFiller first\n";
293 if (pPF->ErrorStatus()) {
294 di << " PaveFiller has not been done\n";
299 Standard_Boolean bRunParallel;
300 Standard_Integer aNb, iErr;
301 BOPAlgo_Section aBOP;
303 const BOPCol_ListOfShape& aLC=pPF->Arguments();
306 Sprintf (buf, " wrong number of arguments %s\n", aNb);
311 bRunParallel=BOPTest_Objects::RunParallel();
313 const TopoDS_Shape& aS1=aLC.First();
314 const TopoDS_Shape& aS2=aLC.Last();
316 aBOP.AddArgument(aS1);
317 aBOP.AddArgument(aS2);
318 aBOP.SetRunParallel (bRunParallel);
320 aBOP.PerformWithFiller(*pPF);
321 iErr=aBOP.ErrorStatus();
323 Sprintf(buf, " ErrorStatus : %d\n", iErr);
328 const TopoDS_Shape& aR=aBOP.Shape();
330 di << " null shape\n";
334 DBRep::Set(a[1], aR);
337 //=======================================================================
340 //=======================================================================
341 Standard_Integer bcommon (Draw_Interpretor& di,
345 return bsmt(di, n, a, BOPAlgo_COMMON);
347 //=======================================================================
350 //=======================================================================
351 Standard_Integer bfuse (Draw_Interpretor& di,
355 return bsmt(di, n, a, BOPAlgo_FUSE);
357 //=======================================================================
360 //=======================================================================
361 Standard_Integer bcut (Draw_Interpretor& di,
365 return bsmt(di, n, a, BOPAlgo_CUT);
367 //=======================================================================
370 //=======================================================================
371 Standard_Integer btuc (Draw_Interpretor& di,
375 return bsmt(di, n, a, BOPAlgo_CUT21);
377 //=======================================================================
378 //function : bsection
380 //=======================================================================
381 Standard_Integer bsection(Draw_Interpretor& di,
386 di << "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]\n";
390 TopoDS_Shape aS1, aS2;
392 aS1=DBRep::Get(a[2]);
393 aS2=DBRep::Get(a[3]);
394 if (aS1.IsNull() || aS2.IsNull()) {
395 di << " Null shapes are not allowed \n";
400 Standard_Boolean bRunParallel, bNonDestructive, bApp, bPC1, bPC2;
401 Standard_Integer i, iErr;
404 bApp = Standard_True;
405 bPC1 = Standard_True;
406 bPC2 = Standard_True;
407 aTol = BOPTest_Objects::FuzzyValue();
408 bRunParallel = BOPTest_Objects::RunParallel();
409 bNonDestructive = BOPTest_Objects::NonDestructive();
411 for (i = 4; i < n; ++i) {
412 if (!strcmp(a[i], "-n2d")) {
413 bPC1 = Standard_False;
414 bPC2 = Standard_False;
416 else if (!strcmp(a[i], "-n2d1")) {
417 bPC1 = Standard_False;
419 else if (!strcmp(a[i], "-n2d2")) {
420 bPC2 = Standard_False;
422 else if (!strcmp(a[i], "-na")) {
423 bApp = Standard_False;
427 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
429 aSec.Approximation(bApp);
430 aSec.ComputePCurveOn1(bPC1);
431 aSec.ComputePCurveOn2(bPC2);
433 aSec.SetFuzzyValue(aTol);
434 aSec.SetRunParallel(bRunParallel);
435 aSec.SetNonDestructive(bNonDestructive);
438 iErr=aSec.ErrorStatus();
439 if (!aSec.IsDone()) {
440 Sprintf(buf, " ErrorStatus : %d\n", iErr);
445 const TopoDS_Shape& aR=aSec.Shape();
447 di << " null shape\n";
450 DBRep::Set(a[1], aR);
453 //=======================================================================
456 //=======================================================================
457 Standard_Integer bsmt (Draw_Interpretor& di,
460 const BOPAlgo_Operation aOp)
463 Standard_Boolean bRunParallel, bNonDestructive;
464 Standard_Integer iErr;
465 TopoDS_Shape aS1, aS2;
466 BOPCol_ListOfShape aLC;
470 di << " use bx r s1 s2\n";
474 aS1=DBRep::Get(a[2]);
475 aS2=DBRep::Get(a[3]);
477 if (aS1.IsNull() || aS2.IsNull()) {
478 di << " null shapes are not allowed \n";
484 aTol=BOPTest_Objects::FuzzyValue();
485 bRunParallel = BOPTest_Objects::RunParallel();
486 bNonDestructive = BOPTest_Objects::NonDestructive();
488 Handle(NCollection_BaseAllocator)aAL=
489 NCollection_BaseAllocator::CommonBaseAllocator();
491 //---------------------------------------------------------------
492 BOPAlgo_PaveFiller aPF(aAL);
494 aPF.SetArguments(aLC);
495 aPF.SetFuzzyValue(aTol);
496 aPF.SetRunParallel(bRunParallel);
497 aPF.SetNonDestructive(bNonDestructive);
500 iErr=aPF.ErrorStatus();
502 Sprintf(buf, " ErrorStatus : %d\n", iErr);
507 //---------------------------------------------------------------
508 BOPAlgo_BOP aBOP(aAL);
510 aBOP.AddArgument(aS1);
512 aBOP.SetOperation(aOp);
513 aBOP.SetRunParallel(bRunParallel);
515 aBOP.PerformWithFiller(aPF);
517 iErr=aBOP.ErrorStatus();
519 Sprintf(buf, " ErrorStatus : %d\n", iErr);
523 const TopoDS_Shape& aR=aBOP.Shape();
525 di << " null shape\n";
529 DBRep::Set(a[1], aR);
532 //=======================================================================
533 //function : bopcurves
535 //=======================================================================
536 Standard_Integer bopcurves (Draw_Interpretor& di,
541 di << " use bopcurves F1 F2 [-2d/-2d1/-2d2]\n";
545 TopoDS_Shape S1 = DBRep::Get(a[1]);
546 TopoDS_Shape S2 = DBRep::Get(a[2]);
547 TopAbs_ShapeEnum aType;
549 if (S1.IsNull() || S2.IsNull()) {
550 di << " Null shapes is not allowed \n";
554 aType=S1.ShapeType();
555 if (aType != TopAbs_FACE) {
556 di << " Type mismatch F1\n";
559 aType=S2.ShapeType();
560 if (aType != TopAbs_FACE) {
561 di << " Type mismatch F2\n";
565 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
566 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
568 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone;
569 Standard_Integer aNbCurves, aNbPoints;
570 Standard_Real anAppTol, aTolR;
571 IntSurf_ListOfPntOn2S aListOfPnts;
572 TCollection_AsciiString aNm("c_"), aNp("p_");
574 anAppTol = 0.0000001;
575 aToApproxC3d = Standard_True;
576 aToApproxC2dOnS1 = Standard_False;
577 aToApproxC2dOnS2 = Standard_False;
580 for(Standard_Integer i = 3; i < n; i++)
582 if (!strcasecmp(a[i],"-2d")) {
583 aToApproxC2dOnS1 = Standard_True;
584 aToApproxC2dOnS2 = Standard_True;
586 else if (!strcasecmp(a[i],"-2d1")) {
587 aToApproxC2dOnS1 = Standard_True;
589 else if (!strcasecmp(a[i],"-2d2")) {
590 aToApproxC2dOnS2 = Standard_True;
592 else if (!strcasecmp(a[i],"-p")) {
594 const Standard_Real aU1 = Draw::Atof(a[++i]);
595 const Standard_Real aV1 = Draw::Atof(a[++i]);
596 const Standard_Real aU2 = Draw::Atof(a[++i]);
597 const Standard_Real aV2 = Draw::Atof(a[++i]);
599 aPt.SetValue(aU1, aV1, aU2, aV2);
600 aListOfPnts.Append(aPt);
603 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 [-2d/-2d1/-2d2] [-p u1 v1 u2 v2]\n";
610 IntTools_FaceFace aFF;
612 aFF.SetParameters (aToApproxC3d,
616 aFF.SetList(aListOfPnts);
618 aFF.Perform (aF1, aF2);
620 anIsDone=aFF.IsDone();
622 di << "Error: anIsDone=" << (Standard_Integer) anIsDone << "\n";
626 aFF.PrepareLines3D(Standard_False);
627 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
628 const IntTools_SequenceOfPntOn2Faces& aSPs = aFF.Points();
630 aNbCurves = aSCs.Length();
631 aNbPoints = aSPs.Length();
632 if (!aNbCurves && !aNbPoints) {
633 di << " has no 3d curves\n";
634 di << " has no 3d points\n";
638 aTolR=aFF.TolReached3d();
639 di << "Tolerance Reached=" << aTolR << "\n";
643 di << aNbCurves << " curve(s) found.\n";
645 for (Standard_Integer i=1; i<=aNbCurves; i++) {
646 const IntTools_Curve& anIC=aSCs(i);
648 Handle (Geom_Curve) aC3D = anIC.Curve();
651 di << " has Null 3d curve# " << i << "\n";
655 TCollection_AsciiString anIndx(i), aNmx;
658 Standard_CString nameC = aNmx.ToCString();
660 DrawTrSurf::Set(nameC, aC3D);
663 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
664 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
666 if (!aPC1.IsNull() || !aPC2.IsNull()) {
669 if (!aPC1.IsNull()) {
670 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
671 pc1Nx = pc1N + anIndx;
672 Standard_CString nameC2d1 = pc1Nx.ToCString();
674 DrawTrSurf::Set(nameC2d1, aPC1);
678 if (!aPC2.IsNull()) {
679 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
680 pc2Nx = pc2N + anIndx;
681 Standard_CString nameC2d2 = pc2Nx.ToCString();
683 DrawTrSurf::Set(nameC2d2, aPC2);
685 if (!aPC1.IsNull()) {
698 di << aNbPoints << " point(s) found.\n";
700 for (Standard_Integer i = 1; i <= aNbPoints; i++) {
701 const IntTools_PntOn2Faces& aPi = aSPs(i);
702 const gp_Pnt& aP = aPi.P1().Pnt();
704 TCollection_AsciiString anIndx(i), aNmx;
706 Standard_CString nameP = aNmx.ToCString();
708 DrawTrSurf::Set(nameP, aP);
716 //=======================================================================
717 //function : mkvolume
719 //=======================================================================
720 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
723 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni]\n";
725 di << " -c - use this option if the arguments are compounds\n";
726 di << " containing shapes that should be interfered;\n";
727 di << " -ni - use this option if the arguments should not be interfered;\n";
731 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
733 Standard_Boolean bToIntersect, bRunParallel, bNonDestructive, bCompounds;
737 BOPCol_ListOfShape aLS;
739 aTol = BOPTest_Objects::FuzzyValue();
740 bRunParallel = BOPTest_Objects::RunParallel();
741 bNonDestructive = BOPTest_Objects::NonDestructive();
743 bToIntersect = Standard_True;
744 bCompounds = Standard_False;
746 for (i = 2; i < n; ++i) {
747 aS = DBRep::Get(a[i]);
752 if (!strcmp(a[i], "-c")) {
753 bCompounds = Standard_True;
755 else if (!strcmp(a[i], "-ni")) {
756 bToIntersect = Standard_False;
762 di << "No shapes to process.\n";
767 // treat list of arguments for the case of compounds
768 if (bToIntersect && bCompounds) {
769 BOPCol_ListOfShape aLSx;
770 BOPCol_ListIteratorOfListOfShape aItLS;
772 aItLS.Initialize(aLS);
773 for (; aItLS.More(); aItLS.Next()) {
774 const TopoDS_Shape& aSx = aItLS.Value();
775 TopoDS_Iterator aItS(aSx);
776 for (; aItS.More(); aItS.Next()) {
777 const TopoDS_Shape& aSxS = aItS.Value();
786 BOPAlgo_MakerVolume aMV;
787 aMV.SetArguments(aLS);
788 aMV.SetIntersect(bToIntersect);
789 aMV.SetRunParallel(bRunParallel);
790 aMV.SetFuzzyValue(aTol);
791 aMV.SetNonDestructive(bNonDestructive);
794 if (aMV.ErrorStatus()) {
795 di << "Error status: " << aMV.ErrorStatus();
799 const TopoDS_Shape& aR = aMV.Shape();
801 DBRep::Set(a[1], aR);