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.
16 #include <BOPTest.ixx>
22 #include <NCollection_BaseAllocator.hxx>
23 #include <NCollection_IncAllocator.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Compound.hxx>
27 #include <TopoDS_Iterator.hxx>
29 #include <BRep_Builder.hxx>
31 #include <BOPAlgo_PaveFiller.hxx>
32 #include <BOPAlgo_Operation.hxx>
33 #include <BOPAlgo_BOP.hxx>
34 #include <BOPAlgo_MakerVolume.hxx>
35 #include <BOPDS_DS.hxx>
36 #include <BOPTest_DrawableShape.hxx>
37 #include <BOPCol_ListOfShape.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <IntTools_FaceFace.hxx>
41 #include <IntTools_Curve.hxx>
42 #include <DrawTrSurf.hxx>
43 #include <Draw_Color.hxx>
45 #include <BRepAlgoAPI_BooleanOperation.hxx>
46 #include <BRepAlgoAPI_Common.hxx>
47 #include <BRepAlgoAPI_Fuse.hxx>
48 #include <BRepAlgoAPI_Cut.hxx>
49 #include <BRepAlgoAPI_Section.hxx>
50 #include <BOPAlgo_Section.hxx>
53 static BOPAlgo_PaveFiller* pPF=NULL;
57 Standard_Integer bopsmt(Draw_Interpretor& di,
60 const BOPAlgo_Operation aOp);
62 Standard_Integer bsmt (Draw_Interpretor& di,
65 const BOPAlgo_Operation aOp);
67 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
68 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
69 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
70 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
71 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
74 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
80 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
81 static Standard_Integer bopnews (Draw_Interpretor&, Standard_Integer, const char**);
83 static Standard_Integer bparallelmode(Draw_Interpretor&, Standard_Integer, const char**);
85 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
87 //=======================================================================
88 //function : BOPCommands
90 //=======================================================================
91 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
93 static Standard_Boolean done = Standard_False;
97 const char* g = "BOP commands";
100 theCommands.Add("bop" , "use bop s1 s2 [tol]" , __FILE__, bop, g);
101 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
102 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
103 theCommands.Add("bopcut" , "use bopcut r" , __FILE__,bopcut, g);
104 theCommands.Add("boptuc" , "use boptuc r" , __FILE__,boptuc, g);
105 theCommands.Add("bopsection", "use bopsection r" , __FILE__,bopsection, g);
107 theCommands.Add("bcommon" , "use bcommon r s1 s2 [tol]" , __FILE__,bcommon, g);
108 theCommands.Add("bfuse" , "use bfuse r s1 s2 [tol]" , __FILE__,bfuse, g);
109 theCommands.Add("bcut" , "use bcut r s1 s2 [tol]" , __FILE__,bcut, g);
110 theCommands.Add("btuc" , "use btuc r s1 s2 [tol]" , __FILE__,btuc, g);
111 theCommands.Add("bsection", "Use >bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]",
112 __FILE__, bsection, g);
114 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d]", __FILE__, bopcurves, g);
115 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, g);
116 theCommands.Add("bparallelmode", "bparallelmode [1/0] : show / set parallel mode for boolean operations",
117 __FILE__, bparallelmode, g);
118 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni] [-s] [tol]",
119 __FILE__, mkvolume , g);
122 //=======================================================================
125 //=======================================================================
126 Standard_Integer bop (Draw_Interpretor& di, Standard_Integer n, const char** a)
129 Standard_Integer iErr;
131 TopoDS_Shape aS1, aS2;
132 BOPCol_ListOfShape aLC;
134 if (n < 3 || n > 4) {
135 di << " use bop Shape1 Shape2 [tol]\n";
139 aS1=DBRep::Get(a[1]);
140 aS2=DBRep::Get(a[2]);
142 if (aS1.IsNull() || aS2.IsNull()) {
143 di << " null shapes are not allowed \n";
149 aTol = Draw::Atof(a[3]);
159 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
160 pPF=new BOPAlgo_PaveFiller(aAL);
162 pPF->SetArguments(aLC);
163 pPF->SetFuzzyValue(aTol);
166 iErr=pPF->ErrorStatus();
168 Sprintf(buf, " ErrorStatus : %d\n", iErr);
175 //=======================================================================
176 //function : bopcommon
178 //=======================================================================
179 Standard_Integer bopcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
181 return bopsmt(di, n, a, BOPAlgo_COMMON);
183 //=======================================================================
186 //=======================================================================
187 Standard_Integer bopfuse(Draw_Interpretor& di, Standard_Integer n, const char** a)
189 return bopsmt(di, n, a, BOPAlgo_FUSE);
191 //=======================================================================
194 //=======================================================================
195 Standard_Integer bopcut(Draw_Interpretor& di, Standard_Integer n, const char** a)
197 return bopsmt(di, n, a, BOPAlgo_CUT);
199 //=======================================================================
202 //=======================================================================
203 Standard_Integer boptuc(Draw_Interpretor& di, Standard_Integer n, const char** a)
205 return bopsmt(di, n, a, BOPAlgo_CUT21);
207 //=======================================================================
210 //=======================================================================
211 Standard_Integer bopsmt(Draw_Interpretor& di,
214 const BOPAlgo_Operation aOp)
217 di << " use bopsmt r\n [tol]";
222 di << " prepare PaveFiller first\n";
226 if (pPF->ErrorStatus()) {
227 di << " PaveFiller has not been done\n";
232 Standard_Integer aNb, iErr;
235 const BOPCol_ListOfShape& aLC=pPF->Arguments();
238 Sprintf (buf, " wrong number of arguments %s\n", aNb);
243 const TopoDS_Shape& aS1=aLC.First();
244 const TopoDS_Shape& aS2=aLC.Last();
246 aBOP.AddArgument(aS1);
248 aBOP.SetOperation(aOp);
250 aBOP.PerformWithFiller(*pPF);
251 iErr=aBOP.ErrorStatus();
253 Sprintf(buf, " ErrorStatus : %d\n", iErr);
258 const TopoDS_Shape& aR=aBOP.Shape();
260 di << " null shape\n";
264 DBRep::Set(a[1], aR);
267 //=======================================================================
268 //function : bopsection
270 //=======================================================================
271 Standard_Integer bopsection(Draw_Interpretor& di, Standard_Integer n, const char** a)
274 di << " use bopsmt r\n";
279 di << " prepare PaveFiller first\n";
283 if (pPF->ErrorStatus()) {
284 di << " PaveFiller has not been done\n";
289 Standard_Integer aNb, iErr;
290 BOPAlgo_Section aBOP;
292 const BOPCol_ListOfShape& aLC=pPF->Arguments();
295 Sprintf (buf, " wrong number of arguments %s\n", aNb);
300 const TopoDS_Shape& aS1=aLC.First();
301 const TopoDS_Shape& aS2=aLC.Last();
303 aBOP.AddArgument(aS1);
304 aBOP.AddArgument(aS2);
306 aBOP.PerformWithFiller(*pPF);
307 iErr=aBOP.ErrorStatus();
309 Sprintf(buf, " ErrorStatus : %d\n", iErr);
314 const TopoDS_Shape& aR=aBOP.Shape();
316 di << " null shape\n";
320 DBRep::Set(a[1], aR);
323 //=======================================================================
326 //=======================================================================
327 Standard_Integer bcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
329 return bsmt(di, n, a, BOPAlgo_COMMON);
331 //=======================================================================
334 //=======================================================================
335 Standard_Integer bfuse (Draw_Interpretor& di, Standard_Integer n, const char** a)
337 return bsmt(di, n, a, BOPAlgo_FUSE);
339 //=======================================================================
342 //=======================================================================
343 Standard_Integer bcut (Draw_Interpretor& di, Standard_Integer n, const char** a)
345 return bsmt(di, n, a, BOPAlgo_CUT);
347 //=======================================================================
350 //=======================================================================
351 Standard_Integer btuc (Draw_Interpretor& di, Standard_Integer n, const char** a)
353 return bsmt(di, n, a, BOPAlgo_CUT21);
355 //=======================================================================
356 //function : bsection
358 //=======================================================================
359 Standard_Integer bsection(Draw_Interpretor& di,
363 const char* usage = " Usage: bsection Result s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]\n";
369 TopoDS_Shape aS1 = DBRep::Get(a[2]);
370 TopoDS_Shape aS2 = DBRep::Get(a[3]);
372 if (aS1.IsNull() || aS2.IsNull()) {
373 di << " Null shapes are not allowed \n";
377 Standard_Boolean bApp, bPC1, bPC2;
381 bApp = Standard_True;
382 bPC1 = Standard_True;
383 bPC2 = Standard_True;
386 for (i = 4; i < n; ++i) {
387 if (!strcmp(a[i], "-n2d")) {
388 bPC1 = Standard_False;
389 bPC2 = Standard_False;
391 else if (!strcmp(a[i], "-n2d1")) {
392 bPC1 = Standard_False;
394 else if (!strcmp(a[i], "-n2d2")) {
395 bPC2 = Standard_False;
397 else if (!strcmp(a[i], "-na")) {
398 bApp = Standard_False;
401 aTol = Draw::Atof(a[i]);
405 Standard_Integer iErr;
408 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
409 aSec.Approximation(bApp);
410 aSec.ComputePCurveOn1(bPC1);
411 aSec.ComputePCurveOn2(bPC2);
412 aSec.SetFuzzyValue(aTol);
415 iErr=aSec.ErrorStatus();
416 if (!aSec.IsDone()) {
417 Sprintf(buf, " ErrorStatus : %d\n", iErr);
422 const TopoDS_Shape& aR=aSec.Shape();
424 di << " null shape\n";
427 DBRep::Set(a[1], aR);
431 //=======================================================================
434 //=======================================================================
435 Standard_Integer bsmt (Draw_Interpretor& di,
438 const BOPAlgo_Operation aOp)
441 Standard_Integer iErr;
442 TopoDS_Shape aS1, aS2;
443 BOPCol_ListOfShape aLC;
446 if (n < 4 || n > 5) {
447 di << " use bx r s1 s2 [tol]\n";
451 aS1=DBRep::Get(a[2]);
452 aS2=DBRep::Get(a[3]);
454 if (aS1.IsNull() || aS2.IsNull()) {
455 di << " null shapes are not allowed \n";
461 aTol = Draw::Atof(a[4]);
467 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
468 BOPAlgo_PaveFiller aPF(aAL);
470 aPF.SetArguments(aLC);
471 aPF.SetFuzzyValue(aTol);
474 iErr=aPF.ErrorStatus();
476 Sprintf(buf, " ErrorStatus : %d\n", iErr);
481 BRepAlgoAPI_BooleanOperation* pBuilder=NULL;
483 if (aOp==BOPAlgo_COMMON) {
484 pBuilder=new BRepAlgoAPI_Common(aS1, aS2, aPF);
486 else if (aOp==BOPAlgo_FUSE) {
487 pBuilder=new BRepAlgoAPI_Fuse(aS1, aS2, aPF);
489 else if (aOp==BOPAlgo_CUT) {
490 pBuilder=new BRepAlgoAPI_Cut (aS1, aS2, aPF);
492 else if (aOp==BOPAlgo_CUT21) {
493 pBuilder=new BRepAlgoAPI_Cut(aS1, aS2, aPF, Standard_False);
496 iErr = pBuilder->ErrorStatus();
497 if (!pBuilder->IsDone()) {
498 Sprintf(buf, " ErrorStatus : %d\n", iErr);
502 const TopoDS_Shape& aR=pBuilder->Shape();
504 di << " null shape\n";
507 DBRep::Set(a[1], aR);
511 //=======================================================================
514 //=======================================================================
515 Standard_Integer bopnews (Draw_Interpretor& di,
520 di << " use bopnews -v[e,f]\n";
525 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
530 Standard_CString aText;
531 Standard_Integer i, i1, i2, iFound;
532 Draw_Color aTextColor(Draw_cyan);
534 Handle(BOPTest_DrawableShape) aDShape;
536 const BOPDS_PDS& pDS=pPF->PDS();
539 if (!strcmp (a[1], "-f")) {
542 else if (!strcmp (a[1], "-e")){
545 else if (!strcmp (a[1], "-v")){
549 di << " use bopnews -v[e,f]\n";
554 i1=pDS->NbSourceShapes();
556 for (i=i1; i<i2; ++i) {
557 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
558 if (aSI.ShapeType()==aT) {
559 const TopoDS_Shape& aS=aSI.Shape();
561 Sprintf (buf, "z%d", i);
563 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
564 Draw::Set (aText, aDShape);
566 Sprintf (buf, " z%d", i);
577 di << " not found\n";
583 //=======================================================================
584 //function : bopcurves
586 //=======================================================================
587 Standard_Integer bopcurves (Draw_Interpretor& di,
592 di << " use bopcurves F1 F2 [-2d]\n";
596 TopoDS_Shape S1 = DBRep::Get(a[1]);
597 TopoDS_Shape S2 = DBRep::Get(a[2]);
598 TopAbs_ShapeEnum aType;
600 if (S1.IsNull() || S2.IsNull()) {
601 di << " Null shapes is not allowed \n";
605 aType=S1.ShapeType();
606 if (aType != TopAbs_FACE) {
607 di << " Type mismatch F1\n";
610 aType=S2.ShapeType();
611 if (aType != TopAbs_FACE) {
612 di << " Type mismatch F2\n";
617 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
618 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
620 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone, bMake2dCurves;
621 Standard_Integer i, aNbCurves;
622 Standard_Real anAppTol, aTolR;
623 TCollection_AsciiString aNm("c_");
625 bMake2dCurves = Standard_False;
627 if (!strcasecmp(a[3],"-2d")) {
628 bMake2dCurves = Standard_True;
630 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d \n";
636 aToApproxC3d = Standard_True;
637 aToApproxC2dOnS1 = bMake2dCurves;
638 aToApproxC2dOnS2 = bMake2dCurves;
642 IntTools_FaceFace aFF;
644 aFF.SetParameters (aToApproxC3d,
649 aFF.Perform (aF1, aF2);
651 anIsDone=aFF.IsDone();
653 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
657 aFF.PrepareLines3D(Standard_False);
658 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
661 aTolR=aFF.TolReached3d();
662 di << "Tolerance Reached=" << aTolR << "\n";
664 aNbCurves=aSCs.Length();
666 di << " has no 3d curve\n";
671 di << aNbCurves << " curve(s) found.\n";
674 for (i=1; i<=aNbCurves; i++) {
675 const IntTools_Curve& anIC=aSCs(i);
677 Handle (Geom_Curve) aC3D = anIC.Curve();
680 di << " has Null 3d curve# " << i << "\n";
684 TCollection_AsciiString anIndx(i), aNmx;
687 Standard_CString nameC = aNmx.ToCString();
689 DrawTrSurf::Set(nameC, aC3D);
693 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
694 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
696 if (aPC1.IsNull() && aPC2.IsNull()) {
697 di << " \n has Null 2d curves# " << i << "\n";
702 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
703 pc2Nx = pc2N + anIndx;
704 Standard_CString nameC2d2 = pc2Nx.ToCString();
706 DrawTrSurf::Set(nameC2d2, aPC2);
707 di << "(" << nameC2d2 << ") ";
708 di << " \n Null first 2d curve of the curve #" << i << "\n";
711 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
712 pc1Nx = pc1N + anIndx;
713 Standard_CString nameC2d1 = pc1Nx.ToCString();
715 DrawTrSurf::Set(nameC2d1, aPC1);
716 di << "(" << nameC2d1;
720 di << ") \n Null second 2d curve of the curve #" << i << "\n";
723 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
724 pc2Nx = pc2N + anIndx;
725 Standard_CString nameC2d2 = pc2Nx.ToCString();
727 DrawTrSurf::Set(nameC2d2, aPC2);
728 di << ", " << nameC2d2 << ") ";
738 //=======================================================================
739 //function : bparallelmode
741 //=======================================================================
742 Standard_Integer bparallelmode(Draw_Interpretor& di, Standard_Integer n, const char** a)
746 Standard_Boolean isParallelOn = Draw::Atoi (a[1]) == 1;
747 if (isParallelOn == 1)
749 BOPAlgo_Algo::SetParallelMode(Standard_True);
750 di << "Parallel mode for boolean operations has been enabled";
754 BOPAlgo_Algo::SetParallelMode(Standard_False);
755 di << "Parallel mode for boolean operations has been disabled";
760 di << "Parallel mode state for boolean operations: "
761 << (BOPAlgo_Algo::GetParallelMode()? "enabled" : "disabled");
767 //=======================================================================
768 //function : mkvolume
770 //=======================================================================
771 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
774 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni] [-s] [tol]\n";
776 di << " -c - use this option if the arguments are compounds\n";
777 di << " containing shapes that should be interfered;\n";
778 di << " -ni - use this option if the arguments should not be interfered;\n";
779 di << " -s - use this option to run the operation in non parallel mode;\n";
780 di << " tol - additional tolerance value (real).\n";
784 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
786 Standard_Boolean bToIntersect, bRunParallel, bCompounds;
790 BOPCol_ListOfShape aLS;
793 bToIntersect = Standard_True;
794 bRunParallel = Standard_True;
795 bCompounds = Standard_False;
797 for (i = 2; i < n; ++i) {
798 aS = DBRep::Get(a[i]);
803 if (!strcmp(a[i], "-c")) {
804 bCompounds = Standard_True;
806 else if (!strcmp(a[i], "-ni")) {
807 bToIntersect = Standard_False;
809 else if (!strcmp(a[i], "-s")) {
810 bRunParallel = Standard_False;
813 aTol = Draw::Atof(a[i]);
819 di << "No shapes to process.\n";
824 // treat list of arguments for the case of compounds
825 if (bToIntersect && bCompounds) {
826 BOPCol_ListOfShape aLSx;
827 BOPCol_ListIteratorOfListOfShape aItLS;
829 aItLS.Initialize(aLS);
830 for (; aItLS.More(); aItLS.Next()) {
831 const TopoDS_Shape& aSx = aItLS.Value();
832 TopoDS_Iterator aItS(aSx);
833 for (; aItS.More(); aItS.Next()) {
834 const TopoDS_Shape& aSxS = aItS.Value();
843 BOPAlgo_MakerVolume aMV;
844 aMV.SetArguments(aLS);
845 aMV.SetIntersect(bToIntersect);
846 aMV.SetRunParallel(bRunParallel);
847 aMV.SetFuzzyValue(aTol);
850 if (aMV.ErrorStatus()) {
851 di << "Error status: " << aMV.ErrorStatus();
855 const TopoDS_Shape& aR = aMV.Shape();
857 DBRep::Set(a[1], aR);