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 <BRep_Builder.hxx>
29 #include <BOPAlgo_PaveFiller.hxx>
30 #include <BOPAlgo_Operation.hxx>
31 #include <BOPAlgo_BOP.hxx>
32 #include <BOPAlgo_MakerVolume.hxx>
33 #include <BOPDS_DS.hxx>
34 #include <BOPTest_DrawableShape.hxx>
35 #include <BOPCol_ListOfShape.hxx>
37 #include <TCollection_AsciiString.hxx>
38 #include <IntTools_FaceFace.hxx>
39 #include <IntTools_Curve.hxx>
40 #include <DrawTrSurf.hxx>
41 #include <Draw_Color.hxx>
43 #include <BRepAlgoAPI_BooleanOperation.hxx>
44 #include <BRepAlgoAPI_Common.hxx>
45 #include <BRepAlgoAPI_Fuse.hxx>
46 #include <BRepAlgoAPI_Cut.hxx>
47 #include <BRepAlgoAPI_Section.hxx>
48 #include <BOPAlgo_Section.hxx>
51 static BOPAlgo_PaveFiller* pPF=NULL;
55 Standard_Integer bopsmt(Draw_Interpretor& di,
58 const BOPAlgo_Operation aOp);
60 Standard_Integer bsmt (Draw_Interpretor& di,
63 const BOPAlgo_Operation aOp);
65 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
66 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
67 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
68 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
69 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
70 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
73 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
74 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
79 static Standard_Integer bopnews (Draw_Interpretor&, Standard_Integer, const char**);
81 static Standard_Integer bparallelmode(Draw_Interpretor&, Standard_Integer, const char**);
83 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
85 //=======================================================================
86 //function : BOPCommands
88 //=======================================================================
89 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
91 static Standard_Boolean done = Standard_False;
95 const char* g = "BOP commands";
98 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
99 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
100 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
101 theCommands.Add("bopcut" , "use bopcut" , __FILE__,bopcut, g);
102 theCommands.Add("boptuc" , "use boptuc" , __FILE__,boptuc, g);
103 theCommands.Add("bopsection", "use bopsection" , __FILE__,bopsection, g);
105 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
106 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
107 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
108 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
109 theCommands.Add("bsection", "Use >bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
110 __FILE__, bsection, g);
112 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d]", __FILE__, bopcurves, g);
113 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, g);
114 theCommands.Add("bparallelmode", "bparallelmode [1/0] : show / set parallel mode for boolean operations", __FILE__, bparallelmode, g);
115 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-ni (do not intersect)] [-s (run in non parallel mode)]", __FILE__, mkvolume , g);
118 //=======================================================================
121 //=======================================================================
122 Standard_Integer bop (Draw_Interpretor& di, Standard_Integer n, const char** a)
125 Standard_Integer iErr;
126 TopoDS_Shape aS1, aS2;
127 BOPCol_ListOfShape aLC;
130 di << " use bop Shape1 Shape2\n";
134 aS1=DBRep::Get(a[1]);
135 aS2=DBRep::Get(a[2]);
137 if (aS1.IsNull() || aS2.IsNull()) {
138 di << " null shapes are not allowed \n";
149 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
150 pPF=new BOPAlgo_PaveFiller(aAL);
152 pPF->SetArguments(aLC);
155 iErr=pPF->ErrorStatus();
157 Sprintf(buf, " ErrorStatus : %d\n", iErr);
164 //=======================================================================
165 //function : bopcommon
167 //=======================================================================
168 Standard_Integer bopcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
170 return bopsmt(di, n, a, BOPAlgo_COMMON);
172 //=======================================================================
175 //=======================================================================
176 Standard_Integer bopfuse(Draw_Interpretor& di, Standard_Integer n, const char** a)
178 return bopsmt(di, n, a, BOPAlgo_FUSE);
180 //=======================================================================
183 //=======================================================================
184 Standard_Integer bopcut(Draw_Interpretor& di, Standard_Integer n, const char** a)
186 return bopsmt(di, n, a, BOPAlgo_CUT);
188 //=======================================================================
191 //=======================================================================
192 Standard_Integer boptuc(Draw_Interpretor& di, Standard_Integer n, const char** a)
194 return bopsmt(di, n, a, BOPAlgo_CUT21);
196 //=======================================================================
199 //=======================================================================
200 Standard_Integer bopsmt(Draw_Interpretor& di,
203 const BOPAlgo_Operation aOp)
206 di << " use bopsmt r\n";
211 di << " prepare PaveFiller first\n";
215 if (pPF->ErrorStatus()) {
216 di << " PaveFiller has not been done\n";
221 Standard_Integer aNb, iErr;
224 const BOPCol_ListOfShape& aLC=pPF->Arguments();
227 Sprintf (buf, " wrong number of arguments %s\n", aNb);
232 const TopoDS_Shape& aS1=aLC.First();
233 const TopoDS_Shape& aS2=aLC.Last();
235 aBOP.AddArgument(aS1);
237 aBOP.SetOperation(aOp);
239 aBOP.PerformWithFiller(*pPF);
240 iErr=aBOP.ErrorStatus();
242 Sprintf(buf, " ErrorStatus : %d\n", iErr);
247 const TopoDS_Shape& aR=aBOP.Shape();
249 di << " null shape\n";
253 DBRep::Set(a[1], aR);
256 //=======================================================================
257 //function : bopsection
259 //=======================================================================
260 Standard_Integer bopsection(Draw_Interpretor& di, Standard_Integer n, const char** a)
263 di << " use bopsmt r\n";
268 di << " prepare PaveFiller first\n";
272 if (pPF->ErrorStatus()) {
273 di << " PaveFiller has not been done\n";
278 Standard_Integer aNb, iErr;
279 BOPAlgo_Section aBOP;
281 const BOPCol_ListOfShape& aLC=pPF->Arguments();
284 Sprintf (buf, " wrong number of arguments %s\n", aNb);
289 const TopoDS_Shape& aS1=aLC.First();
290 const TopoDS_Shape& aS2=aLC.Last();
292 aBOP.AddArgument(aS1);
293 aBOP.AddArgument(aS2);
295 aBOP.PerformWithFiller(*pPF);
296 iErr=aBOP.ErrorStatus();
298 Sprintf(buf, " ErrorStatus : %d\n", iErr);
303 const TopoDS_Shape& aR=aBOP.Shape();
305 di << " null shape\n";
309 DBRep::Set(a[1], aR);
312 //=======================================================================
315 //=======================================================================
316 Standard_Integer bcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
318 return bsmt(di, n, a, BOPAlgo_COMMON);
320 //=======================================================================
323 //=======================================================================
324 Standard_Integer bfuse (Draw_Interpretor& di, Standard_Integer n, const char** a)
326 return bsmt(di, n, a, BOPAlgo_FUSE);
328 //=======================================================================
331 //=======================================================================
332 Standard_Integer bcut (Draw_Interpretor& di, Standard_Integer n, const char** a)
334 return bsmt(di, n, a, BOPAlgo_CUT);
336 //=======================================================================
339 //=======================================================================
340 Standard_Integer btuc (Draw_Interpretor& di, Standard_Integer n, const char** a)
342 return bsmt(di, n, a, BOPAlgo_CUT21);
344 //=======================================================================
345 //function : bsection
347 //=======================================================================
348 Standard_Integer bsection(Draw_Interpretor& di,
352 const char* usage = " Usage: bsection Result s1 s2 [-n2d/-n2d1/-n2d2] [-na]\n";
358 TopoDS_Shape aS1 = DBRep::Get(a[2]);
359 TopoDS_Shape aS2 = DBRep::Get(a[3]);
361 if (aS1.IsNull() || aS2.IsNull()) {
362 di << " Null shapes are not allowed \n";
366 Standard_Boolean bApp, bPC1, bPC2;
368 bApp = Standard_True;
369 bPC1 = Standard_True;
370 bPC2 = Standard_True;
372 Standard_Boolean isbadparameter = Standard_False;
375 const char* key1 = a[4];
376 const char* key2 = (n > 5) ? a[5] : NULL;
377 const char* pcurveconf = NULL;
380 (!strcasecmp(key1,"-n2d") ||
381 !strcasecmp(key1,"-n2d1") ||
382 !strcasecmp(key1,"-n2d2"))) {
386 if (!strcasecmp(key1,"-na")) {
387 bApp = Standard_False;
390 isbadparameter = Standard_True;
394 if(!strcasecmp(key2,"-na")) {
395 bApp = Standard_False;
398 isbadparameter = Standard_True;
402 if(!isbadparameter && pcurveconf) {
403 if (!strcasecmp(pcurveconf, "-n2d1")) {
404 bPC1 = Standard_False;
407 if (!strcasecmp(pcurveconf, "-n2d2")) {
408 bPC2 = Standard_False;
411 if (!strcasecmp(pcurveconf, "-n2d")) {
412 bPC1 = Standard_False;
413 bPC2 = Standard_False;
420 if(!isbadparameter) {
421 Standard_Integer iErr;
424 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
425 aSec.Approximation(bApp);
426 aSec.ComputePCurveOn1(bPC1);
427 aSec.ComputePCurveOn2(bPC2);
430 iErr=aSec.ErrorStatus();
431 if (!aSec.IsDone()) {
432 Sprintf(buf, " ErrorStatus : %d\n", iErr);
437 const TopoDS_Shape& aR=aSec.Shape();
439 di << " null shape\n";
442 DBRep::Set(a[1], aR);
450 //=======================================================================
453 //=======================================================================
454 Standard_Integer bsmt (Draw_Interpretor& di,
457 const BOPAlgo_Operation aOp)
460 Standard_Integer iErr;
461 TopoDS_Shape aS1, aS2;
462 BOPCol_ListOfShape aLC;
465 di << " use bx r s1 s2\n";
469 aS1=DBRep::Get(a[2]);
470 aS2=DBRep::Get(a[3]);
472 if (aS1.IsNull() || aS2.IsNull()) {
473 di << " null shapes are not allowed \n";
479 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
480 BOPAlgo_PaveFiller aPF(aAL);
482 aPF.SetArguments(aLC);
485 iErr=aPF.ErrorStatus();
487 Sprintf(buf, " ErrorStatus : %d\n", iErr);
492 BRepAlgoAPI_BooleanOperation* pBuilder=NULL;
494 if (aOp==BOPAlgo_COMMON) {
495 pBuilder=new BRepAlgoAPI_Common(aS1, aS2, aPF);
497 else if (aOp==BOPAlgo_FUSE) {
498 pBuilder=new BRepAlgoAPI_Fuse(aS1, aS2, aPF);
500 else if (aOp==BOPAlgo_CUT) {
501 pBuilder=new BRepAlgoAPI_Cut (aS1, aS2, aPF);
503 else if (aOp==BOPAlgo_CUT21) {
504 pBuilder=new BRepAlgoAPI_Cut(aS1, aS2, aPF, Standard_False);
507 iErr = pBuilder->ErrorStatus();
508 if (!pBuilder->IsDone()) {
509 Sprintf(buf, " ErrorStatus : %d\n", iErr);
513 const TopoDS_Shape& aR=pBuilder->Shape();
515 di << " null shape\n";
518 DBRep::Set(a[1], aR);
522 //=======================================================================
525 //=======================================================================
526 Standard_Integer bopnews (Draw_Interpretor& di,
531 di << " use bopnews -v[e,f]\n";
536 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
541 Standard_CString aText;
542 Standard_Integer i, i1, i2, iFound;
543 Draw_Color aTextColor(Draw_cyan);
545 Handle(BOPTest_DrawableShape) aDShape;
547 const BOPDS_PDS& pDS=pPF->PDS();
550 if (!strcmp (a[1], "-f")) {
553 else if (!strcmp (a[1], "-e")){
556 else if (!strcmp (a[1], "-v")){
560 di << " use bopnews -v[e,f]\n";
565 i1=pDS->NbSourceShapes();
567 for (i=i1; i<i2; ++i) {
568 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
569 if (aSI.ShapeType()==aT) {
570 const TopoDS_Shape& aS=aSI.Shape();
572 Sprintf (buf, "z%d", i);
574 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
575 Draw::Set (aText, aDShape);
577 Sprintf (buf, " z%d", i);
588 di << " not found\n";
594 //=======================================================================
595 //function : bopcurves
597 //=======================================================================
598 Standard_Integer bopcurves (Draw_Interpretor& di,
603 di << " use bopcurves F1 F2 [-2d]\n";
607 TopoDS_Shape S1 = DBRep::Get(a[1]);
608 TopoDS_Shape S2 = DBRep::Get(a[2]);
609 TopAbs_ShapeEnum aType;
611 if (S1.IsNull() || S2.IsNull()) {
612 di << " Null shapes is not allowed \n";
616 aType=S1.ShapeType();
617 if (aType != TopAbs_FACE) {
618 di << " Type mismatch F1\n";
621 aType=S2.ShapeType();
622 if (aType != TopAbs_FACE) {
623 di << " Type mismatch F2\n";
628 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
629 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
631 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone, bMake2dCurves;
632 Standard_Integer i, aNbCurves;
633 Standard_Real anAppTol, aTolR;
634 TCollection_AsciiString aNm("c_");
636 bMake2dCurves = Standard_False;
638 if (!strcasecmp(a[3],"-2d")) {
639 bMake2dCurves = Standard_True;
641 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d \n";
647 aToApproxC3d = Standard_True;
648 aToApproxC2dOnS1 = bMake2dCurves;
649 aToApproxC2dOnS2 = bMake2dCurves;
653 IntTools_FaceFace aFF;
655 aFF.SetParameters (aToApproxC3d,
660 aFF.Perform (aF1, aF2);
662 anIsDone=aFF.IsDone();
664 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
668 aFF.PrepareLines3D(Standard_False);
669 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
672 aTolR=aFF.TolReached3d();
673 di << "Tolerance Reached=" << aTolR << "\n";
675 aNbCurves=aSCs.Length();
677 di << " has no 3d curve\n";
682 di << aNbCurves << " curve(s) found.\n";
685 for (i=1; i<=aNbCurves; i++) {
686 const IntTools_Curve& anIC=aSCs(i);
688 Handle (Geom_Curve) aC3D = anIC.Curve();
691 di << " has Null 3d curve# " << i << "\n";
695 TCollection_AsciiString anIndx(i), aNmx;
698 Standard_CString nameC = aNmx.ToCString();
700 DrawTrSurf::Set(nameC, aC3D);
704 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
705 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
707 if (aPC1.IsNull() && aPC2.IsNull()) {
708 di << " \n has Null 2d curves# " << i << "\n";
713 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
714 pc2Nx = pc2N + anIndx;
715 Standard_CString nameC2d2 = pc2Nx.ToCString();
717 DrawTrSurf::Set(nameC2d2, aPC2);
718 di << "(" << nameC2d2 << ") ";
719 di << " \n Null first 2d curve of the curve #" << i << "\n";
722 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
723 pc1Nx = pc1N + anIndx;
724 Standard_CString nameC2d1 = pc1Nx.ToCString();
726 DrawTrSurf::Set(nameC2d1, aPC1);
727 di << "(" << nameC2d1;
731 di << ") \n Null second 2d curve of the curve #" << i << "\n";
734 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
735 pc2Nx = pc2N + anIndx;
736 Standard_CString nameC2d2 = pc2Nx.ToCString();
738 DrawTrSurf::Set(nameC2d2, aPC2);
739 di << ", " << nameC2d2 << ") ";
749 //=======================================================================
750 //function : bparallelmode
752 //=======================================================================
753 Standard_Integer bparallelmode(Draw_Interpretor& di, Standard_Integer n, const char** a)
757 Standard_Boolean isParallelOn = Draw::Atoi (a[1]) == 1;
758 if (isParallelOn == 1)
760 BOPAlgo_Algo::SetParallelMode(Standard_True);
761 di << "Parallel mode for boolean operations has been enabled";
765 BOPAlgo_Algo::SetParallelMode(Standard_False);
766 di << "Parallel mode for boolean operations has been disabled";
771 di << "Parallel mode state for boolean operations: "
772 << (BOPAlgo_Algo::GetParallelMode()? "enabled" : "disabled");
778 //=======================================================================
779 //function : mkvolume
781 //=======================================================================
782 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
784 const char* usage = "Usage: mkvolume r b1 b2 ... [-ni (do not intersect)] [-s (run in non parallel mode)]\n";
790 Standard_Boolean bToIntersect, bRunParallel;
791 Standard_Integer i, aNb;
794 bToIntersect = Standard_True;
795 bRunParallel = Standard_True;
797 if (!strcmp(a[n-1], "-ni")) {
798 bToIntersect = Standard_False;
801 else if (!strcmp(a[n-1], "-s")) {
802 bRunParallel = Standard_False;
806 if (!strcmp(a[n-2], "-ni")) {
807 bToIntersect = Standard_False;
810 else if (!strcmp(a[n-2], "-s")) {
811 bRunParallel = Standard_False;
817 di << "no shapes to process.\n";
822 BOPCol_ListOfShape aLS;
824 for (i = 2; i < aNb; ++i) {
825 aS = DBRep::Get(a[i]);
832 di << "no shapes to process.\n";
837 BOPAlgo_MakerVolume aMV;
838 aMV.SetArguments(aLS);
839 aMV.SetIntersect(bToIntersect);
840 aMV.SetRunParallel(bRunParallel);
843 if (aMV.ErrorStatus()) {
844 di << "Error status: " << aMV.ErrorStatus();
848 const TopoDS_Shape& aR = aMV.Shape();
850 DBRep::Set(a[1], aR);