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 <BOPDS_DS.hxx>
23 #include <BOPTest.hxx>
24 #include <BOPTest_Objects.hxx>
25 #include <BRep_Builder.hxx>
26 #include <BRepAlgoAPI_BooleanOperation.hxx>
27 #include <BRepAlgoAPI_Common.hxx>
28 #include <BRepAlgoAPI_Cut.hxx>
29 #include <BRepAlgoAPI_Fuse.hxx>
30 #include <BRepAlgoAPI_Section.hxx>
33 #include <DrawTrSurf.hxx>
34 #include <Geom2d_Curve.hxx>
35 #include <Geom_Curve.hxx>
36 #include <IntTools_Curve.hxx>
37 #include <IntTools_FaceFace.hxx>
38 #include <IntTools_PntOn2Faces.hxx>
39 #include <NCollection_BaseAllocator.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Iterator.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopTools_ListOfShape.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 "Builds section between shapes. Options:\n"
106 "-n2d/-n2d1/-n2d2 - disable the PCurve construction;\n"
107 "-na - disables the approximation of the section curves.\n",
108 __FILE__, bsection, g);
110 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d/-2d1/-2d2] "
111 "[-p u1 v1 u2 v2 (to add start points] [-v (for extended output)]",
112 __FILE__, bopcurves, g);
113 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni] [-ai]",
114 __FILE__, mkvolume , g);
117 //=======================================================================
120 //=======================================================================
121 Standard_Integer bop(Draw_Interpretor& di,
125 Standard_Boolean bRunParallel, bNonDestructive;
127 TopoDS_Shape aS1, aS2;
128 TopTools_ListOfShape aLC;
131 di << " use bop s1 s2 \n";
135 aS1=DBRep::Get(a[1]);
136 aS2=DBRep::Get(a[2]);
138 if (aS1.IsNull() || aS2.IsNull()) {
139 di << " null shapes are not allowed \n";
143 aTol=BOPTest_Objects::FuzzyValue();
144 bRunParallel=BOPTest_Objects::RunParallel();
145 bNonDestructive = BOPTest_Objects::NonDestructive();
146 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
155 Handle(NCollection_BaseAllocator)aAL=
156 NCollection_BaseAllocator::CommonBaseAllocator();
157 pPF=new BOPAlgo_PaveFiller(aAL);
159 pPF->SetArguments(aLC);
160 pPF->SetFuzzyValue(aTol);
161 pPF->SetRunParallel(bRunParallel);
162 pPF->SetNonDestructive(bNonDestructive);
164 pPF->SetUseOBB(BOPTest_Objects::UseOBB());
167 BOPTest::ReportAlerts(pPF->GetReport());
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->HasErrors()) {
231 di << " PaveFiller has not been done\n";
236 Standard_Boolean bRunParallel;
237 Standard_Integer aNb;
240 const TopTools_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);
257 aBOP.SetCheckInverted(BOPTest_Objects::CheckInverted());
259 aBOP.PerformWithFiller(*pPF);
260 BOPTest::ReportAlerts(aBOP.GetReport());
261 if (aBOP.HasErrors()) {
265 const TopoDS_Shape& aR=aBOP.Shape();
267 di << " null shape\n";
271 DBRep::Set(a[1], aR);
274 //=======================================================================
275 //function : bopsection
277 //=======================================================================
278 Standard_Integer bopsection(Draw_Interpretor& di,
283 di << " use bopsection r\n";
288 di << " prepare PaveFiller first\n";
292 if (pPF->HasErrors()) {
293 di << " PaveFiller has not been done\n";
298 Standard_Boolean bRunParallel;
299 Standard_Integer aNb;
300 BOPAlgo_Section aBOP;
302 const TopTools_ListOfShape& aLC=pPF->Arguments();
305 Sprintf (buf, " wrong number of arguments %s\n", aNb);
310 bRunParallel=BOPTest_Objects::RunParallel();
312 const TopoDS_Shape& aS1=aLC.First();
313 const TopoDS_Shape& aS2=aLC.Last();
315 aBOP.AddArgument(aS1);
316 aBOP.AddArgument(aS2);
317 aBOP.SetRunParallel (bRunParallel);
318 aBOP.SetCheckInverted(BOPTest_Objects::CheckInverted());
320 aBOP.PerformWithFiller(*pPF);
321 BOPTest::ReportAlerts(aBOP.GetReport());
322 if (aBOP.HasErrors()) {
326 const TopoDS_Shape& aR=aBOP.Shape();
328 di << " null shape\n";
332 DBRep::Set(a[1], aR);
335 //=======================================================================
338 //=======================================================================
339 Standard_Integer bcommon (Draw_Interpretor& di,
343 return bsmt(di, n, a, BOPAlgo_COMMON);
345 //=======================================================================
348 //=======================================================================
349 Standard_Integer bfuse (Draw_Interpretor& di,
353 return bsmt(di, n, a, BOPAlgo_FUSE);
355 //=======================================================================
358 //=======================================================================
359 Standard_Integer bcut (Draw_Interpretor& di,
363 return bsmt(di, n, a, BOPAlgo_CUT);
365 //=======================================================================
368 //=======================================================================
369 Standard_Integer btuc (Draw_Interpretor& di,
373 return bsmt(di, n, a, BOPAlgo_CUT21);
375 //=======================================================================
376 //function : bsection
378 //=======================================================================
379 Standard_Integer bsection(Draw_Interpretor& di,
384 di << "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]\n";
388 TopoDS_Shape aS1, aS2;
390 aS1=DBRep::Get(a[2]);
391 aS2=DBRep::Get(a[3]);
392 if (aS1.IsNull() || aS2.IsNull()) {
393 di << " Null shapes are not allowed \n";
397 Standard_Boolean bRunParallel, bNonDestructive, bApp, bPC1, bPC2;
400 bApp = Standard_True;
401 bPC1 = Standard_True;
402 bPC2 = Standard_True;
403 aTol = BOPTest_Objects::FuzzyValue();
404 bRunParallel = BOPTest_Objects::RunParallel();
405 bNonDestructive = BOPTest_Objects::NonDestructive();
406 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
408 for (Standard_Integer i = 4; i < n; ++i) {
409 if (!strcmp(a[i], "-n2d")) {
410 bPC1 = Standard_False;
411 bPC2 = Standard_False;
413 else if (!strcmp(a[i], "-n2d1")) {
414 bPC1 = Standard_False;
416 else if (!strcmp(a[i], "-n2d2")) {
417 bPC2 = Standard_False;
419 else if (!strcmp(a[i], "-na")) {
420 bApp = Standard_False;
424 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
426 aSec.Approximation(bApp);
427 aSec.ComputePCurveOn1(bPC1);
428 aSec.ComputePCurveOn2(bPC2);
430 aSec.SetFuzzyValue(aTol);
431 aSec.SetRunParallel(bRunParallel);
432 aSec.SetNonDestructive(bNonDestructive);
434 aSec.SetUseOBB(BOPTest_Objects::UseOBB());
438 if (aSec.HasWarnings()) {
439 Standard_SStream aSStream;
440 aSec.DumpWarnings(aSStream);
444 if (!aSec.IsDone()) {
445 Standard_SStream aSStream;
446 aSec.DumpErrors(aSStream);
451 const TopoDS_Shape& aR=aSec.Shape();
453 di << " null shape\n";
456 DBRep::Set(a[1], aR);
459 //=======================================================================
462 //=======================================================================
463 Standard_Integer bsmt (Draw_Interpretor& di,
466 const BOPAlgo_Operation aOp)
468 Standard_Boolean bRunParallel, bNonDestructive;
469 TopoDS_Shape aS1, aS2;
470 TopTools_ListOfShape aLC;
474 di << " use bx r s1 s2\n";
478 aS1=DBRep::Get(a[2]);
479 aS2=DBRep::Get(a[3]);
481 if (aS1.IsNull() || aS2.IsNull()) {
482 di << " null shapes are not allowed \n";
488 aTol=BOPTest_Objects::FuzzyValue();
489 bRunParallel = BOPTest_Objects::RunParallel();
490 bNonDestructive = BOPTest_Objects::NonDestructive();
491 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
493 Handle(NCollection_BaseAllocator)aAL=
494 NCollection_BaseAllocator::CommonBaseAllocator();
496 //---------------------------------------------------------------
497 BOPAlgo_PaveFiller aPF(aAL);
499 aPF.SetArguments(aLC);
500 aPF.SetFuzzyValue(aTol);
501 aPF.SetRunParallel(bRunParallel);
502 aPF.SetNonDestructive(bNonDestructive);
504 aPF.SetUseOBB(BOPTest_Objects::UseOBB());
507 BOPTest::ReportAlerts(aPF.GetReport());
508 if (aPF.HasErrors()) {
512 //---------------------------------------------------------------
513 BOPAlgo_BOP aBOP(aAL);
515 aBOP.AddArgument(aS1);
517 aBOP.SetOperation(aOp);
518 aBOP.SetRunParallel(bRunParallel);
519 aBOP.SetCheckInverted(BOPTest_Objects::CheckInverted());
521 aBOP.PerformWithFiller(aPF);
522 BOPTest::ReportAlerts(aBOP.GetReport());
523 if (aBOP.HasErrors()) {
526 const TopoDS_Shape& aR=aBOP.Shape();
528 di << " null shape\n";
532 DBRep::Set(a[1], aR);
535 //=======================================================================
536 //function : bopcurves
538 //=======================================================================
539 Standard_Integer bopcurves (Draw_Interpretor& di,
544 di << "Usage: bopcurves F1 F2 [-2d/-2d1/-2d2] "
545 "[-p u1 v1 u2 v2 (to add start points] [-v (for extended output)]\n";
549 TopoDS_Shape S1 = DBRep::Get(a[1]);
550 TopoDS_Shape S2 = DBRep::Get(a[2]);
551 TopAbs_ShapeEnum aType;
553 if (S1.IsNull() || S2.IsNull()) {
554 di << " Null shapes are not allowed \n";
558 aType=S1.ShapeType();
559 if (aType != TopAbs_FACE) {
560 di << " Type mismatch F1\n";
563 aType=S2.ShapeType();
564 if (aType != TopAbs_FACE) {
565 di << " Type mismatch F2\n";
569 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
570 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
572 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone;
573 Standard_Integer aNbCurves, aNbPoints;
574 Standard_Real anAppTol;
575 IntSurf_ListOfPntOn2S aListOfPnts;
576 TCollection_AsciiString aNm("c_"), aNp("p_");
578 anAppTol = 0.0000001;
579 aToApproxC3d = Standard_True;
580 aToApproxC2dOnS1 = Standard_False;
581 aToApproxC2dOnS2 = Standard_False;
584 Standard_Boolean bExtOut = Standard_False;
585 for(Standard_Integer i = 3; i < n; i++)
587 if (!strcasecmp(a[i],"-2d")) {
588 aToApproxC2dOnS1 = Standard_True;
589 aToApproxC2dOnS2 = Standard_True;
591 else if (!strcasecmp(a[i],"-2d1")) {
592 aToApproxC2dOnS1 = Standard_True;
594 else if (!strcasecmp(a[i],"-2d2")) {
595 aToApproxC2dOnS2 = Standard_True;
597 else if (!strcasecmp(a[i],"-p")) {
599 const Standard_Real aU1 = Draw::Atof(a[++i]);
600 const Standard_Real aV1 = Draw::Atof(a[++i]);
601 const Standard_Real aU2 = Draw::Atof(a[++i]);
602 const Standard_Real aV2 = Draw::Atof(a[++i]);
604 aPt.SetValue(aU1, aV1, aU2, aV2);
605 aListOfPnts.Append(aPt);
607 else if (!strcasecmp(a[i],"-v")) {
608 bExtOut = Standard_True;
611 di << "Wrong key.\n";
612 di << "To build 2d curves use one of the following keys: -2d/-2d1/-2d2\n";
613 di << "To add start points use the following key: -p u1 v1 u2 v2\n";
614 di << "For extended output use the following key: -v\n";
619 IntTools_FaceFace aFF;
621 aFF.SetParameters (aToApproxC3d,
625 aFF.SetList(aListOfPnts);
627 aFF.Perform (aF1, aF2);
629 anIsDone=aFF.IsDone();
631 di << "Error: Intersection failed\n";
635 aFF.PrepareLines3D(Standard_False);
636 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
637 const IntTools_SequenceOfPntOn2Faces& aSPs = aFF.Points();
639 aNbCurves = aSCs.Length();
640 aNbPoints = aSPs.Length();
641 if (!aNbCurves && !aNbPoints) {
642 di << " has no 3d curves\n";
643 di << " has no 3d points\n";
649 Standard_Real aTolR = 0.;
651 // find maximal tolerance
652 for (Standard_Integer i = 1; i <= aNbCurves; i++) {
653 const IntTools_Curve& anIC = aSCs(i);
654 if (aTolR < anIC.Tolerance()) {
655 aTolR = anIC.Tolerance();
658 di << "Tolerance Reached=" << aTolR << "\n";
661 di << aNbCurves << " curve(s) found.\n";
663 for (Standard_Integer i=1; i<=aNbCurves; i++) {
664 const IntTools_Curve& anIC=aSCs(i);
666 Handle (Geom_Curve) aC3D = anIC.Curve();
669 di << " has Null 3d curve# " << i << "\n";
673 TCollection_AsciiString anIndx(i), aNmx;
676 Standard_CString nameC = aNmx.ToCString();
678 DrawTrSurf::Set(nameC, aC3D);
681 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
682 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
684 if (!aPC1.IsNull() || !aPC2.IsNull()) {
687 if (!aPC1.IsNull()) {
688 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
689 pc1Nx = pc1N + anIndx;
690 Standard_CString nameC2d1 = pc1Nx.ToCString();
692 DrawTrSurf::Set(nameC2d1, aPC1);
696 if (!aPC2.IsNull()) {
697 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
698 pc2Nx = pc2N + anIndx;
699 Standard_CString nameC2d2 = pc2Nx.ToCString();
701 DrawTrSurf::Set(nameC2d2, aPC2);
703 if (!aPC1.IsNull()) {
712 di << "\nTolerance: " << anIC.Tolerance() << "\n";
713 di << "Tangential tolerance: " << anIC.TangentialTolerance() << "\n";
724 di << aNbPoints << " point(s) found.\n";
726 for (Standard_Integer i = 1; i <= aNbPoints; i++) {
727 const IntTools_PntOn2Faces& aPi = aSPs(i);
728 const gp_Pnt& aP = aPi.P1().Pnt();
730 TCollection_AsciiString anIndx(i), aNmx;
732 Standard_CString nameP = aNmx.ToCString();
734 DrawTrSurf::Set(nameP, aP);
742 //=======================================================================
743 //function : mkvolume
745 //=======================================================================
746 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
749 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni] [-ai]\n";
751 di << " -c - use this option if the arguments are compounds\n";
752 di << " containing shapes that should be interfered;\n";
753 di << " -ni - use this option if the arguments should not be interfered;\n";
754 di << " -ai - use this option to avoid internal for solids shapes in the result.\n";
758 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
760 Standard_Boolean bToIntersect, bRunParallel, bNonDestructive;
761 Standard_Boolean bCompounds, bAvoidInternal;
765 TopTools_ListOfShape aLS;
767 aTol = BOPTest_Objects::FuzzyValue();
768 bRunParallel = BOPTest_Objects::RunParallel();
769 bNonDestructive = BOPTest_Objects::NonDestructive();
770 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
772 bToIntersect = Standard_True;
773 bCompounds = Standard_False;
774 bAvoidInternal = Standard_False;
776 for (i = 2; i < n; ++i) {
777 aS = DBRep::Get(a[i]);
782 if (!strcmp(a[i], "-c")) {
783 bCompounds = Standard_True;
785 else if (!strcmp(a[i], "-ni")) {
786 bToIntersect = Standard_False;
788 else if (!strcmp(a[i], "-ai")) {
789 bAvoidInternal = Standard_True;
795 di << "No shapes to process.\n";
800 // treat list of arguments for the case of compounds
801 if (bToIntersect && bCompounds) {
802 TopTools_ListOfShape aLSx;
803 TopTools_ListIteratorOfListOfShape aItLS;
805 aItLS.Initialize(aLS);
806 for (; aItLS.More(); aItLS.Next()) {
807 const TopoDS_Shape& aSx = aItLS.Value();
808 TopoDS_Iterator aItS(aSx);
809 for (; aItS.More(); aItS.Next()) {
810 const TopoDS_Shape& aSxS = aItS.Value();
819 BOPAlgo_MakerVolume aMV;
820 aMV.SetArguments(aLS);
821 aMV.SetIntersect(bToIntersect);
822 aMV.SetRunParallel(bRunParallel);
823 aMV.SetFuzzyValue(aTol);
824 aMV.SetNonDestructive(bNonDestructive);
825 aMV.SetAvoidInternalShapes(bAvoidInternal);
827 aMV.SetUseOBB(BOPTest_Objects::UseOBB());
830 BOPTest::ReportAlerts(aMV.GetReport());
831 if (aMV.HasErrors()) {
835 const TopoDS_Shape& aR = aMV.Shape();
837 DBRep::Set(a[1], aR);