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>
20 #include <TCollection_AsciiString.hxx>
23 #include <DrawTrSurf.hxx>
24 #include <Draw_Color.hxx>
27 #include <NCollection_BaseAllocator.hxx>
28 #include <NCollection_IncAllocator.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Iterator.hxx>
34 #include <BRep_Builder.hxx>
36 #include <IntTools_FaceFace.hxx>
37 #include <IntTools_Curve.hxx>
39 #include <BOPCol_ListOfShape.hxx>
41 #include <BOPDS_DS.hxx>
43 #include <BOPAlgo_PaveFiller.hxx>
44 #include <BOPAlgo_Operation.hxx>
45 #include <BOPAlgo_BOP.hxx>
46 #include <BOPAlgo_MakerVolume.hxx>
47 #include <BOPAlgo_Section.hxx>
49 #include <BRepAlgoAPI_BooleanOperation.hxx>
50 #include <BRepAlgoAPI_Common.hxx>
51 #include <BRepAlgoAPI_Fuse.hxx>
52 #include <BRepAlgoAPI_Cut.hxx>
53 #include <BRepAlgoAPI_Section.hxx>
55 #include <BOPTest_DrawableShape.hxx>
56 #include <BOPTest_Objects.hxx>
59 static BOPAlgo_PaveFiller* pPF=NULL;
63 Standard_Integer bopsmt(Draw_Interpretor& di,
66 const BOPAlgo_Operation aOp);
69 Standard_Integer bsmt (Draw_Interpretor& di,
72 const BOPAlgo_Operation aOp);
74 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
79 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
81 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
82 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
83 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
84 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
85 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
87 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
88 static Standard_Integer bopnews (Draw_Interpretor&, Standard_Integer, const char**);
89 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
91 //=======================================================================
92 //function : BOPCommands
94 //=======================================================================
95 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
97 static Standard_Boolean done = Standard_False;
101 const char* g = "BOPTest commands";
104 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
105 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
106 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
107 theCommands.Add("bopcut" , "use bopcut r" , __FILE__,bopcut, g);
108 theCommands.Add("boptuc" , "use boptuc r" , __FILE__,boptuc, g);
109 theCommands.Add("bopsection", "use bopsection r" , __FILE__,bopsection, g);
111 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
112 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
113 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
114 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
115 theCommands.Add("bsection", "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
116 __FILE__, bsection, g);
118 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d/-2d1/-2d2]",
119 __FILE__, bopcurves, g);
120 theCommands.Add("bopnews" , "use bopnews -v[e,f]" , __FILE__, bopnews, g);
121 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni]",
122 __FILE__, mkvolume , g);
125 //=======================================================================
128 //=======================================================================
129 Standard_Integer bop(Draw_Interpretor& di,
134 Standard_Boolean bRunParallel;
135 Standard_Integer iErr;
137 TopoDS_Shape aS1, aS2;
138 BOPCol_ListOfShape aLC;
141 di << " use bop s1 s2 \n";
145 aS1=DBRep::Get(a[1]);
146 aS2=DBRep::Get(a[2]);
148 if (aS1.IsNull() || aS2.IsNull()) {
149 di << " null shapes are not allowed \n";
153 aTol=BOPTest_Objects::FuzzyValue();
154 bRunParallel=BOPTest_Objects::RunParallel();
163 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
164 pPF=new BOPAlgo_PaveFiller(aAL);
166 pPF->SetArguments(aLC);
167 pPF->SetFuzzyValue(aTol);
168 pPF->SetRunParallel(bRunParallel);
171 iErr=pPF->ErrorStatus();
173 Sprintf(buf, " ErrorStatus : %d\n", iErr);
180 //=======================================================================
181 //function : bopcommon
183 //=======================================================================
184 Standard_Integer bopcommon (Draw_Interpretor& di,
188 return bopsmt(di, n, a, BOPAlgo_COMMON);
190 //=======================================================================
193 //=======================================================================
194 Standard_Integer bopfuse(Draw_Interpretor& di,
198 return bopsmt(di, n, a, BOPAlgo_FUSE);
200 //=======================================================================
203 //=======================================================================
204 Standard_Integer bopcut(Draw_Interpretor& di,
208 return bopsmt(di, n, a, BOPAlgo_CUT);
210 //=======================================================================
213 //=======================================================================
214 Standard_Integer boptuc(Draw_Interpretor& di,
218 return bopsmt(di, n, a, BOPAlgo_CUT21);
220 //=======================================================================
223 //=======================================================================
224 Standard_Integer bopsmt(Draw_Interpretor& di,
227 const BOPAlgo_Operation aOp)
230 di << " use bopsmt r\n";
235 di << " prepare PaveFiller first\n";
239 if (pPF->ErrorStatus()) {
240 di << " PaveFiller has not been done\n";
245 Standard_Boolean bRunParallel;
246 Standard_Integer aNb, iErr;
249 const BOPCol_ListOfShape& aLC=pPF->Arguments();
252 Sprintf (buf, " wrong number of arguments %s\n", aNb);
257 bRunParallel=BOPTest_Objects::RunParallel();
259 const TopoDS_Shape& aS1=aLC.First();
260 const TopoDS_Shape& aS2=aLC.Last();
262 aBOP.AddArgument(aS1);
264 aBOP.SetOperation(aOp);
265 aBOP.SetRunParallel (bRunParallel);
267 aBOP.PerformWithFiller(*pPF);
268 iErr=aBOP.ErrorStatus();
270 Sprintf(buf, " ErrorStatus : %d\n", iErr);
275 const TopoDS_Shape& aR=aBOP.Shape();
277 di << " null shape\n";
281 DBRep::Set(a[1], aR);
284 //=======================================================================
285 //function : bopsection
287 //=======================================================================
288 Standard_Integer bopsection(Draw_Interpretor& di,
293 di << " use bopsection r\n";
298 di << " prepare PaveFiller first\n";
302 if (pPF->ErrorStatus()) {
303 di << " PaveFiller has not been done\n";
308 Standard_Boolean bRunParallel;
309 Standard_Integer aNb, iErr;
310 BOPAlgo_Section aBOP;
312 const BOPCol_ListOfShape& aLC=pPF->Arguments();
315 Sprintf (buf, " wrong number of arguments %s\n", aNb);
320 bRunParallel=BOPTest_Objects::RunParallel();
322 const TopoDS_Shape& aS1=aLC.First();
323 const TopoDS_Shape& aS2=aLC.Last();
325 aBOP.AddArgument(aS1);
326 aBOP.AddArgument(aS2);
327 aBOP.SetRunParallel (bRunParallel);
329 aBOP.PerformWithFiller(*pPF);
330 iErr=aBOP.ErrorStatus();
332 Sprintf(buf, " ErrorStatus : %d\n", iErr);
337 const TopoDS_Shape& aR=aBOP.Shape();
339 di << " null shape\n";
343 DBRep::Set(a[1], aR);
346 //=======================================================================
349 //=======================================================================
350 Standard_Integer bcommon (Draw_Interpretor& di,
354 return bsmt(di, n, a, BOPAlgo_COMMON);
356 //=======================================================================
359 //=======================================================================
360 Standard_Integer bfuse (Draw_Interpretor& di,
364 return bsmt(di, n, a, BOPAlgo_FUSE);
366 //=======================================================================
369 //=======================================================================
370 Standard_Integer bcut (Draw_Interpretor& di,
374 return bsmt(di, n, a, BOPAlgo_CUT);
376 //=======================================================================
379 //=======================================================================
380 Standard_Integer btuc (Draw_Interpretor& di,
384 return bsmt(di, n, a, BOPAlgo_CUT21);
386 //=======================================================================
387 //function : bsection
389 //=======================================================================
390 Standard_Integer bsection(Draw_Interpretor& di,
395 di << "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]\n";
399 TopoDS_Shape aS1, aS2;
401 aS1=DBRep::Get(a[2]);
402 aS2=DBRep::Get(a[3]);
403 if (aS1.IsNull() || aS2.IsNull()) {
404 di << " Null shapes are not allowed \n";
409 Standard_Boolean bRunParallel, bApp, bPC1, bPC2;
410 Standard_Integer i, iErr;
413 bApp = Standard_True;
414 bPC1 = Standard_True;
415 bPC2 = Standard_True;
416 aTol = BOPTest_Objects::FuzzyValue();
417 bRunParallel = BOPTest_Objects::RunParallel();
419 for (i = 4; i < n; ++i) {
420 if (!strcmp(a[i], "-n2d")) {
421 bPC1 = Standard_False;
422 bPC2 = Standard_False;
424 else if (!strcmp(a[i], "-n2d1")) {
425 bPC1 = Standard_False;
427 else if (!strcmp(a[i], "-n2d2")) {
428 bPC2 = Standard_False;
430 else if (!strcmp(a[i], "-na")) {
431 bApp = Standard_False;
435 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
437 aSec.Approximation(bApp);
438 aSec.ComputePCurveOn1(bPC1);
439 aSec.ComputePCurveOn2(bPC2);
441 aSec.SetFuzzyValue(aTol);
442 aSec.SetRunParallel(bRunParallel);
445 iErr=aSec.ErrorStatus();
446 if (!aSec.IsDone()) {
447 Sprintf(buf, " ErrorStatus : %d\n", iErr);
452 const TopoDS_Shape& aR=aSec.Shape();
454 di << " null shape\n";
457 DBRep::Set(a[1], aR);
460 //=======================================================================
463 //=======================================================================
464 Standard_Integer bsmt (Draw_Interpretor& di,
467 const BOPAlgo_Operation aOp)
470 Standard_Boolean bRunParallel;
471 Standard_Integer iErr;
472 TopoDS_Shape aS1, aS2;
473 BOPCol_ListOfShape aLC;
477 di << " use bx r s1 s2\n";
481 aS1=DBRep::Get(a[2]);
482 aS2=DBRep::Get(a[3]);
484 if (aS1.IsNull() || aS2.IsNull()) {
485 di << " null shapes are not allowed \n";
491 aTol=BOPTest_Objects::FuzzyValue();
492 bRunParallel = BOPTest_Objects::RunParallel();
494 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
496 //---------------------------------------------------------------
497 BOPAlgo_PaveFiller aPF(aAL);
499 aPF.SetArguments(aLC);
500 aPF.SetFuzzyValue(aTol);
501 aPF.SetRunParallel(bRunParallel);
504 iErr=aPF.ErrorStatus();
506 Sprintf(buf, " ErrorStatus : %d\n", iErr);
511 //---------------------------------------------------------------
512 BOPAlgo_BOP aBOP(aAL);
514 aBOP.AddArgument(aS1);
516 aBOP.SetOperation(aOp);
517 aBOP.SetRunParallel(bRunParallel);
519 aBOP.PerformWithFiller(aPF);
521 iErr=aBOP.ErrorStatus();
523 Sprintf(buf, " ErrorStatus : %d\n", iErr);
527 const TopoDS_Shape& aR=aBOP.Shape();
529 di << " null shape\n";
533 DBRep::Set(a[1], aR);
536 //=======================================================================
539 //=======================================================================
540 Standard_Integer bopnews (Draw_Interpretor& di,
545 di << " use bopnews -v[e,f]\n";
550 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
555 Standard_CString aText;
556 Standard_Integer i, i1, i2, iFound;
557 Draw_Color aTextColor(Draw_cyan);
559 Handle(BOPTest_DrawableShape) aDShape;
561 const BOPDS_PDS& pDS=pPF->PDS();
564 if (!strcmp (a[1], "-f")) {
567 else if (!strcmp (a[1], "-e")){
570 else if (!strcmp (a[1], "-v")){
574 di << " use bopnews -v[e,f]\n";
579 i1=pDS->NbSourceShapes();
581 for (i=i1; i<i2; ++i) {
582 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
583 if (aSI.ShapeType()==aT) {
584 const TopoDS_Shape& aS=aSI.Shape();
586 Sprintf (buf, "z%d", i);
588 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
589 Draw::Set (aText, aDShape);
591 Sprintf (buf, " z%d", i);
602 di << " not found\n";
607 //=======================================================================
608 //function : bopcurves
610 //=======================================================================
611 Standard_Integer bopcurves (Draw_Interpretor& di,
616 di << " use bopcurves F1 F2 [-2d/-2d1/-2d2]\n";
620 TopoDS_Shape S1 = DBRep::Get(a[1]);
621 TopoDS_Shape S2 = DBRep::Get(a[2]);
622 TopAbs_ShapeEnum aType;
624 if (S1.IsNull() || S2.IsNull()) {
625 di << " Null shapes is not allowed \n";
629 aType=S1.ShapeType();
630 if (aType != TopAbs_FACE) {
631 di << " Type mismatch F1\n";
634 aType=S2.ShapeType();
635 if (aType != TopAbs_FACE) {
636 di << " Type mismatch F2\n";
640 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
641 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
643 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone;
644 Standard_Integer i, aNbCurves;
645 Standard_Real anAppTol, aTolR;
646 TCollection_AsciiString aNm("c_");
648 anAppTol = 0.0000001;
649 aToApproxC3d = Standard_True;
650 aToApproxC2dOnS1 = Standard_False;
651 aToApproxC2dOnS2 = Standard_False;
654 if (!strcasecmp(a[3],"-2d")) {
655 aToApproxC2dOnS1 = Standard_True;
656 aToApproxC2dOnS2 = Standard_True;
658 else if (!strcasecmp(a[3],"-2d1")) {
659 aToApproxC2dOnS1 = Standard_True;
661 else if (!strcasecmp(a[3],"-2d2")) {
662 aToApproxC2dOnS2 = Standard_True;
665 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d/-2d1/-2d2 \n";
670 IntTools_FaceFace aFF;
672 aFF.SetParameters (aToApproxC3d,
677 aFF.Perform (aF1, aF2);
679 anIsDone=aFF.IsDone();
681 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
685 aFF.PrepareLines3D(Standard_False);
686 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
689 aTolR=aFF.TolReached3d();
690 di << "Tolerance Reached=" << aTolR << "\n";
692 aNbCurves=aSCs.Length();
694 di << " has no 3d curve\n";
699 di << aNbCurves << " curve(s) found.\n";
702 for (i=1; i<=aNbCurves; i++) {
703 const IntTools_Curve& anIC=aSCs(i);
705 Handle (Geom_Curve) aC3D = anIC.Curve();
708 di << " has Null 3d curve# " << i << "\n";
712 TCollection_AsciiString anIndx(i), aNmx;
715 Standard_CString nameC = aNmx.ToCString();
717 DrawTrSurf::Set(nameC, aC3D);
720 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
721 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
723 if (!aPC1.IsNull() || !aPC2.IsNull()) {
726 if (!aPC1.IsNull()) {
727 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
728 pc1Nx = pc1N + anIndx;
729 Standard_CString nameC2d1 = pc1Nx.ToCString();
731 DrawTrSurf::Set(nameC2d1, aPC1);
735 if (!aPC2.IsNull()) {
736 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
737 pc2Nx = pc2N + anIndx;
738 Standard_CString nameC2d2 = pc2Nx.ToCString();
740 DrawTrSurf::Set(nameC2d2, aPC2);
742 if (!aPC1.IsNull()) {
754 //=======================================================================
755 //function : mkvolume
757 //=======================================================================
758 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
761 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni]\n";
763 di << " -c - use this option if the arguments are compounds\n";
764 di << " containing shapes that should be interfered;\n";
765 di << " -ni - use this option if the arguments should not be interfered;\n";
769 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
771 Standard_Boolean bToIntersect, bRunParallel, bCompounds;
775 BOPCol_ListOfShape aLS;
777 aTol = BOPTest_Objects::FuzzyValue();
778 bRunParallel = BOPTest_Objects::RunParallel();
780 bToIntersect = Standard_True;
781 bCompounds = Standard_False;
783 for (i = 2; i < n; ++i) {
784 aS = DBRep::Get(a[i]);
789 if (!strcmp(a[i], "-c")) {
790 bCompounds = Standard_True;
792 else if (!strcmp(a[i], "-ni")) {
793 bToIntersect = Standard_False;
799 di << "No shapes to process.\n";
804 // treat list of arguments for the case of compounds
805 if (bToIntersect && bCompounds) {
806 BOPCol_ListOfShape aLSx;
807 BOPCol_ListIteratorOfListOfShape aItLS;
809 aItLS.Initialize(aLS);
810 for (; aItLS.More(); aItLS.Next()) {
811 const TopoDS_Shape& aSx = aItLS.Value();
812 TopoDS_Iterator aItS(aSx);
813 for (; aItS.More(); aItS.Next()) {
814 const TopoDS_Shape& aSxS = aItS.Value();
823 BOPAlgo_MakerVolume aMV;
824 aMV.SetArguments(aLS);
825 aMV.SetIntersect(bToIntersect);
826 aMV.SetRunParallel(bRunParallel);
827 aMV.SetFuzzyValue(aTol);
830 if (aMV.ErrorStatus()) {
831 di << "Error status: " << aMV.ErrorStatus();
835 const TopoDS_Shape& aR = aMV.Shape();
837 DBRep::Set(a[1], aR);