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>
50 static BOPAlgo_PaveFiller* pPF=NULL;
54 Standard_Integer bopsmt(Draw_Interpretor& di,
57 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 bopnews (Draw_Interpretor&, Standard_Integer, const char**);
80 static Standard_Integer bparallelmode(Draw_Interpretor&, Standard_Integer, const char**);
82 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
84 //=======================================================================
85 //function : BOPCommands
87 //=======================================================================
88 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
90 static Standard_Boolean done = Standard_False;
94 const char* g = "BOP commands";
97 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
98 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
99 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
100 theCommands.Add("bopcut" , "use bopcut" , __FILE__,bopcut, g);
101 theCommands.Add("boptuc" , "use boptuc" , __FILE__,boptuc, g);
102 theCommands.Add("bopsection", "use bopsection" , __FILE__,bopsection, g);
104 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
105 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
106 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
107 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
108 theCommands.Add("bsection", "Use >bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
109 __FILE__, bsection, g);
111 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d]", __FILE__, bopcurves, g);
112 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, g);
113 theCommands.Add("bparallelmode", "bparallelmode [1/0] : show / set parallel mode for boolean operations", __FILE__, bparallelmode, g);
114 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);
117 //=======================================================================
120 //=======================================================================
121 Standard_Integer bop (Draw_Interpretor& di, Standard_Integer n, const char** a)
124 Standard_Integer iErr;
125 TopoDS_Shape aS1, aS2;
126 BOPCol_ListOfShape aLC;
129 di << " use bop Shape1 Shape2\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";
148 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
149 pPF=new BOPAlgo_PaveFiller(aAL);
151 pPF->SetArguments(aLC);
154 iErr=pPF->ErrorStatus();
156 Sprintf(buf, " ErrorStatus : %d\n", iErr);
163 //=======================================================================
164 //function : bopcommon
166 //=======================================================================
167 Standard_Integer bopcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
169 return bopsmt(di, n, a, BOPAlgo_COMMON);
171 //=======================================================================
174 //=======================================================================
175 Standard_Integer bopfuse(Draw_Interpretor& di, Standard_Integer n, const char** a)
177 return bopsmt(di, n, a, BOPAlgo_FUSE);
179 //=======================================================================
182 //=======================================================================
183 Standard_Integer bopcut(Draw_Interpretor& di, Standard_Integer n, const char** a)
185 return bopsmt(di, n, a, BOPAlgo_CUT);
187 //=======================================================================
190 //=======================================================================
191 Standard_Integer boptuc(Draw_Interpretor& di, Standard_Integer n, const char** a)
193 return bopsmt(di, n, a, BOPAlgo_CUT21);
195 //=======================================================================
196 //function : bopsection
198 //=======================================================================
199 Standard_Integer bopsection(Draw_Interpretor& di, Standard_Integer n, const char** a)
201 return bopsmt(di, n, a, BOPAlgo_SECTION);
203 //=======================================================================
206 //=======================================================================
207 Standard_Integer bopsmt(Draw_Interpretor& di,
210 const BOPAlgo_Operation aOp)
213 di << " use bopsmt r\n";
218 di << " prepare PaveFiller first\n";
222 if (pPF->ErrorStatus()) {
223 di << " PaveFiller has not been done\n";
228 Standard_Integer aNb, iErr;
231 const BOPCol_ListOfShape& aLC=pPF->Arguments();
234 Sprintf (buf, " wrong number of arguments %s\n", aNb);
239 const TopoDS_Shape& aS1=aLC.First();
240 const TopoDS_Shape& aS2=aLC.Last();
242 aBOP.AddArgument(aS1);
244 aBOP.SetOperation(aOp);
246 aBOP.PerformWithFiller(*pPF);
247 iErr=aBOP.ErrorStatus();
249 Sprintf(buf, " ErrorStatus : %d\n", iErr);
254 const TopoDS_Shape& aR=aBOP.Shape();
256 di << " null shape\n";
260 DBRep::Set(a[1], aR);
263 //=======================================================================
266 //=======================================================================
267 Standard_Integer bcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
269 return bsmt(di, n, a, BOPAlgo_COMMON);
271 //=======================================================================
274 //=======================================================================
275 Standard_Integer bfuse (Draw_Interpretor& di, Standard_Integer n, const char** a)
277 return bsmt(di, n, a, BOPAlgo_FUSE);
279 //=======================================================================
282 //=======================================================================
283 Standard_Integer bcut (Draw_Interpretor& di, Standard_Integer n, const char** a)
285 return bsmt(di, n, a, BOPAlgo_CUT);
287 //=======================================================================
290 //=======================================================================
291 Standard_Integer btuc (Draw_Interpretor& di, Standard_Integer n, const char** a)
293 return bsmt(di, n, a, BOPAlgo_CUT21);
295 //=======================================================================
296 //function : bsection
298 //=======================================================================
299 Standard_Integer bsection(Draw_Interpretor& di,
303 const char* usage = " Usage: bsection Result s1 s2 [-n2d/-n2d1/-n2d2] [-na]\n";
309 TopoDS_Shape aS1 = DBRep::Get(a[2]);
310 TopoDS_Shape aS2 = DBRep::Get(a[3]);
312 if (aS1.IsNull() || aS2.IsNull()) {
313 di << " Null shapes are not allowed \n";
317 Standard_Boolean bApp, bPC1, bPC2;
319 bApp = Standard_True;
320 bPC1 = Standard_True;
321 bPC2 = Standard_True;
323 Standard_Boolean isbadparameter = Standard_False;
326 const char* key1 = a[4];
327 const char* key2 = (n > 5) ? a[5] : NULL;
328 const char* pcurveconf = NULL;
330 if (key1 && (!strcasecmp(key1,"-n2d") || !strcasecmp(key1,"-n2d1") || !strcasecmp(key1,"-n2d2"))) {
334 if (!strcasecmp(key1,"-na")) {
335 bApp = Standard_False;
338 isbadparameter = Standard_True;
342 if(!strcasecmp(key2,"-na")) {
343 bApp = Standard_False;
346 isbadparameter = Standard_True;
350 if(!isbadparameter && pcurveconf) {
351 if (!strcasecmp(pcurveconf, "-n2d1")) {
352 bPC1 = Standard_False;
355 if (!strcasecmp(pcurveconf, "-n2d2")) {
356 bPC2 = Standard_False;
359 if (!strcasecmp(pcurveconf, "-n2d")) {
360 bPC1 = Standard_False;
361 bPC2 = Standard_False;
368 if(!isbadparameter) {
369 Standard_Integer iErr;
372 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
373 aSec.Approximation(bApp);
374 aSec.ComputePCurveOn1(bPC1);
375 aSec.ComputePCurveOn2(bPC2);
378 iErr=aSec.ErrorStatus();
379 if (!aSec.IsDone()) {
380 Sprintf(buf, " ErrorStatus : %d\n", iErr);
385 const TopoDS_Shape& aR=aSec.Shape();
387 di << " null shape\n";
390 DBRep::Set(a[1], aR);
398 //=======================================================================
401 //=======================================================================
402 Standard_Integer bsmt (Draw_Interpretor& di,
405 const BOPAlgo_Operation aOp)
408 Standard_Integer iErr;
409 TopoDS_Shape aS1, aS2;
410 BOPCol_ListOfShape aLC;
413 di << " use bx r s1 s2\n";
417 aS1=DBRep::Get(a[2]);
418 aS2=DBRep::Get(a[3]);
420 if (aS1.IsNull() || aS2.IsNull()) {
421 di << " null shapes are not allowed \n";
427 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
428 BOPAlgo_PaveFiller aPF(aAL);
430 aPF.SetArguments(aLC);
433 iErr=aPF.ErrorStatus();
435 Sprintf(buf, " ErrorStatus : %d\n", iErr);
440 BRepAlgoAPI_BooleanOperation* pBuilder=NULL;
442 if (aOp==BOPAlgo_COMMON) {
443 pBuilder=new BRepAlgoAPI_Common(aS1, aS2, aPF);
445 else if (aOp==BOPAlgo_FUSE) {
446 pBuilder=new BRepAlgoAPI_Fuse(aS1, aS2, aPF);
448 else if (aOp==BOPAlgo_CUT) {
449 pBuilder=new BRepAlgoAPI_Cut (aS1, aS2, aPF);
451 else if (aOp==BOPAlgo_CUT21) {
452 pBuilder=new BRepAlgoAPI_Cut(aS1, aS2, aPF, Standard_False);
455 iErr = pBuilder->ErrorStatus();
456 if (!pBuilder->IsDone()) {
457 Sprintf(buf, " ErrorStatus : %d\n", iErr);
461 const TopoDS_Shape& aR=pBuilder->Shape();
463 di << " null shape\n";
466 DBRep::Set(a[1], aR);
470 //=======================================================================
473 //=======================================================================
474 Standard_Integer bopnews (Draw_Interpretor& di,
479 di << " use bopnews -v[e,f]\n";
484 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
489 Standard_CString aText;
490 Standard_Integer i, i1, i2, iFound;
491 Draw_Color aTextColor(Draw_cyan);
493 Handle(BOPTest_DrawableShape) aDShape;
495 const BOPDS_PDS& pDS=pPF->PDS();
498 if (!strcmp (a[1], "-f")) {
501 else if (!strcmp (a[1], "-e")){
504 else if (!strcmp (a[1], "-v")){
508 di << " use bopnews -v[e,f]\n";
513 i1=pDS->NbSourceShapes();
515 for (i=i1; i<i2; ++i) {
516 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
517 if (aSI.ShapeType()==aT) {
518 const TopoDS_Shape& aS=aSI.Shape();
520 Sprintf (buf, "z%d", i);
522 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
523 Draw::Set (aText, aDShape);
525 Sprintf (buf, " z%d", i);
536 di << " not found\n";
542 //=======================================================================
543 //function : bopcurves
545 //=======================================================================
546 Standard_Integer bopcurves (Draw_Interpretor& di,
551 di << " use bopcurves F1 F2 [-2d]\n";
555 TopoDS_Shape S1 = DBRep::Get(a[1]);
556 TopoDS_Shape S2 = DBRep::Get(a[2]);
557 TopAbs_ShapeEnum aType;
559 if (S1.IsNull() || S2.IsNull()) {
560 di << " Null shapes is not allowed \n";
564 aType=S1.ShapeType();
565 if (aType != TopAbs_FACE) {
566 di << " Type mismatch F1\n";
569 aType=S2.ShapeType();
570 if (aType != TopAbs_FACE) {
571 di << " Type mismatch F2\n";
576 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
577 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
579 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone, bMake2dCurves;
580 Standard_Integer i, aNbCurves;
581 Standard_Real anAppTol, aTolR;
582 TCollection_AsciiString aNm("c_");
584 bMake2dCurves = Standard_False;
586 if (!strcasecmp(a[3],"-2d")) {
587 bMake2dCurves = Standard_True;
589 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d \n";
595 aToApproxC3d = Standard_True;
596 aToApproxC2dOnS1 = bMake2dCurves;
597 aToApproxC2dOnS2 = bMake2dCurves;
601 IntTools_FaceFace aFF;
603 aFF.SetParameters (aToApproxC3d,
608 aFF.Perform (aF1, aF2);
610 anIsDone=aFF.IsDone();
612 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
616 aFF.PrepareLines3D(Standard_False);
617 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
620 aTolR=aFF.TolReached3d();
621 di << "Tolerance Reached=" << aTolR << "\n";
623 aNbCurves=aSCs.Length();
625 di << " has no 3d curve\n";
630 di << aNbCurves << " curve(s) found.\n";
633 for (i=1; i<=aNbCurves; i++) {
634 const IntTools_Curve& anIC=aSCs(i);
636 Handle (Geom_Curve) aC3D = anIC.Curve();
639 di << " has Null 3d curve# " << i << "\n";
643 TCollection_AsciiString anIndx(i), aNmx;
646 Standard_CString nameC = aNmx.ToCString();
648 DrawTrSurf::Set(nameC, aC3D);
652 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
653 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
655 if (aPC1.IsNull() && aPC2.IsNull()) {
656 di << " \n has Null 2d curves# " << i << "\n";
661 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
662 pc2Nx = pc2N + anIndx;
663 Standard_CString nameC2d2 = pc2Nx.ToCString();
665 DrawTrSurf::Set(nameC2d2, aPC2);
666 di << "(" << nameC2d2 << ") ";
667 di << " \n Null first 2d curve of the curve #" << i << "\n";
670 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
671 pc1Nx = pc1N + anIndx;
672 Standard_CString nameC2d1 = pc1Nx.ToCString();
674 DrawTrSurf::Set(nameC2d1, aPC1);
675 di << "(" << nameC2d1;
679 di << ") \n Null second 2d curve of the curve #" << i << "\n";
682 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
683 pc2Nx = pc2N + anIndx;
684 Standard_CString nameC2d2 = pc2Nx.ToCString();
686 DrawTrSurf::Set(nameC2d2, aPC2);
687 di << ", " << nameC2d2 << ") ";
697 //=======================================================================
698 //function : bparallelmode
700 //=======================================================================
701 Standard_Integer bparallelmode(Draw_Interpretor& di, Standard_Integer n, const char** a)
705 Standard_Boolean isParallelOn = Draw::Atoi (a[1]) == 1;
706 if (isParallelOn == 1)
708 BOPAlgo_Algo::SetParallelMode(Standard_True);
709 di << "Parallel mode for boolean operations has been enabled";
713 BOPAlgo_Algo::SetParallelMode(Standard_False);
714 di << "Parallel mode for boolean operations has been disabled";
719 di << "Parallel mode state for boolean operations: "
720 << (BOPAlgo_Algo::GetParallelMode()? "enabled" : "disabled");
726 //=======================================================================
727 //function : mkvolume
729 //=======================================================================
730 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
732 const char* usage = "Usage: mkvolume r b1 b2 ... [-ni (do not intersect)] [-s (run in non parallel mode)]\n";
738 Standard_Boolean bToIntersect, bRunParallel;
739 Standard_Integer i, aNb;
742 bToIntersect = Standard_True;
743 bRunParallel = Standard_True;
745 if (!strcmp(a[n-1], "-ni")) {
746 bToIntersect = Standard_False;
749 else if (!strcmp(a[n-1], "-s")) {
750 bRunParallel = Standard_False;
754 if (!strcmp(a[n-2], "-ni")) {
755 bToIntersect = Standard_False;
758 else if (!strcmp(a[n-2], "-s")) {
759 bRunParallel = Standard_False;
765 di << "no shapes to process.\n";
770 BOPCol_ListOfShape aLS;
772 for (i = 2; i < aNb; ++i) {
773 aS = DBRep::Get(a[i]);
780 di << "no shapes to process.\n";
785 BOPAlgo_MakerVolume aMV;
786 aMV.SetArguments(aLS);
787 aMV.SetIntersect(bToIntersect);
788 aMV.SetRunParallel(bRunParallel);
791 if (aMV.ErrorStatus()) {
792 di << "Error status: " << aMV.ErrorStatus();
796 const TopoDS_Shape& aR = aMV.Shape();
798 DBRep::Set(a[1], aR);