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 <BOPCol_ListOfShape.hxx>
23 #include <BOPDS_DS.hxx>
24 #include <BOPTest.hxx>
25 #include <BOPTest_Objects.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRepAlgoAPI_BooleanOperation.hxx>
28 #include <BRepAlgoAPI_Common.hxx>
29 #include <BRepAlgoAPI_Cut.hxx>
30 #include <BRepAlgoAPI_Fuse.hxx>
31 #include <BRepAlgoAPI_Section.hxx>
34 #include <DrawTrSurf.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom_Curve.hxx>
37 #include <IntTools_Curve.hxx>
38 #include <IntTools_FaceFace.hxx>
39 #include <IntTools_PntOn2Faces.hxx>
40 #include <NCollection_BaseAllocator.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Iterator.hxx>
44 #include <TopoDS_Shape.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 __FILE__, bsection, g);
107 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d/-2d1/-2d2] [-p u1 v1 u2 v2]",
108 __FILE__, bopcurves, g);
109 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni]",
110 __FILE__, mkvolume , g);
113 //=======================================================================
116 //=======================================================================
117 Standard_Integer bop(Draw_Interpretor& di,
122 Standard_Boolean bRunParallel;
123 Standard_Integer iErr;
125 TopoDS_Shape aS1, aS2;
126 BOPCol_ListOfShape aLC;
129 di << " use bop s1 s2 \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";
141 aTol=BOPTest_Objects::FuzzyValue();
142 bRunParallel=BOPTest_Objects::RunParallel();
151 Handle(NCollection_BaseAllocator)aAL=
152 NCollection_BaseAllocator::CommonBaseAllocator();
153 pPF=new BOPAlgo_PaveFiller(aAL);
155 pPF->SetArguments(aLC);
156 pPF->SetFuzzyValue(aTol);
157 pPF->SetRunParallel(bRunParallel);
160 iErr=pPF->ErrorStatus();
162 Sprintf(buf, " ErrorStatus : %d\n", iErr);
169 //=======================================================================
170 //function : bopcommon
172 //=======================================================================
173 Standard_Integer bopcommon (Draw_Interpretor& di,
177 return bopsmt(di, n, a, BOPAlgo_COMMON);
179 //=======================================================================
182 //=======================================================================
183 Standard_Integer bopfuse(Draw_Interpretor& di,
187 return bopsmt(di, n, a, BOPAlgo_FUSE);
189 //=======================================================================
192 //=======================================================================
193 Standard_Integer bopcut(Draw_Interpretor& di,
197 return bopsmt(di, n, a, BOPAlgo_CUT);
199 //=======================================================================
202 //=======================================================================
203 Standard_Integer boptuc(Draw_Interpretor& di,
207 return bopsmt(di, n, a, BOPAlgo_CUT21);
209 //=======================================================================
212 //=======================================================================
213 Standard_Integer bopsmt(Draw_Interpretor& di,
216 const BOPAlgo_Operation aOp)
219 di << " use bopsmt r\n";
224 di << " prepare PaveFiller first\n";
228 if (pPF->ErrorStatus()) {
229 di << " PaveFiller has not been done\n";
234 Standard_Boolean bRunParallel;
235 Standard_Integer aNb, iErr;
238 const BOPCol_ListOfShape& aLC=pPF->Arguments();
241 Sprintf (buf, " wrong number of arguments %s\n", aNb);
246 bRunParallel=BOPTest_Objects::RunParallel();
248 const TopoDS_Shape& aS1=aLC.First();
249 const TopoDS_Shape& aS2=aLC.Last();
251 aBOP.AddArgument(aS1);
253 aBOP.SetOperation(aOp);
254 aBOP.SetRunParallel (bRunParallel);
256 aBOP.PerformWithFiller(*pPF);
257 iErr=aBOP.ErrorStatus();
259 Sprintf(buf, " ErrorStatus : %d\n", iErr);
264 const TopoDS_Shape& aR=aBOP.Shape();
266 di << " null shape\n";
270 DBRep::Set(a[1], aR);
273 //=======================================================================
274 //function : bopsection
276 //=======================================================================
277 Standard_Integer bopsection(Draw_Interpretor& di,
282 di << " use bopsection r\n";
287 di << " prepare PaveFiller first\n";
291 if (pPF->ErrorStatus()) {
292 di << " PaveFiller has not been done\n";
297 Standard_Boolean bRunParallel;
298 Standard_Integer aNb, iErr;
299 BOPAlgo_Section aBOP;
301 const BOPCol_ListOfShape& aLC=pPF->Arguments();
304 Sprintf (buf, " wrong number of arguments %s\n", aNb);
309 bRunParallel=BOPTest_Objects::RunParallel();
311 const TopoDS_Shape& aS1=aLC.First();
312 const TopoDS_Shape& aS2=aLC.Last();
314 aBOP.AddArgument(aS1);
315 aBOP.AddArgument(aS2);
316 aBOP.SetRunParallel (bRunParallel);
318 aBOP.PerformWithFiller(*pPF);
319 iErr=aBOP.ErrorStatus();
321 Sprintf(buf, " ErrorStatus : %d\n", iErr);
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";
398 Standard_Boolean bRunParallel, bApp, bPC1, bPC2;
399 Standard_Integer i, iErr;
402 bApp = Standard_True;
403 bPC1 = Standard_True;
404 bPC2 = Standard_True;
405 aTol = BOPTest_Objects::FuzzyValue();
406 bRunParallel = BOPTest_Objects::RunParallel();
408 for (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);
434 iErr=aSec.ErrorStatus();
435 if (!aSec.IsDone()) {
436 Sprintf(buf, " ErrorStatus : %d\n", iErr);
441 const TopoDS_Shape& aR=aSec.Shape();
443 di << " null shape\n";
446 DBRep::Set(a[1], aR);
449 //=======================================================================
452 //=======================================================================
453 Standard_Integer bsmt (Draw_Interpretor& di,
456 const BOPAlgo_Operation aOp)
459 Standard_Boolean bRunParallel;
460 Standard_Integer iErr;
461 TopoDS_Shape aS1, aS2;
462 BOPCol_ListOfShape aLC;
466 di << " use bx r s1 s2\n";
470 aS1=DBRep::Get(a[2]);
471 aS2=DBRep::Get(a[3]);
473 if (aS1.IsNull() || aS2.IsNull()) {
474 di << " null shapes are not allowed \n";
480 aTol=BOPTest_Objects::FuzzyValue();
481 bRunParallel = BOPTest_Objects::RunParallel();
483 Handle(NCollection_BaseAllocator)aAL=
484 NCollection_BaseAllocator::CommonBaseAllocator();
486 //---------------------------------------------------------------
487 BOPAlgo_PaveFiller aPF(aAL);
489 aPF.SetArguments(aLC);
490 aPF.SetFuzzyValue(aTol);
491 aPF.SetRunParallel(bRunParallel);
494 iErr=aPF.ErrorStatus();
496 Sprintf(buf, " ErrorStatus : %d\n", iErr);
501 //---------------------------------------------------------------
502 BOPAlgo_BOP aBOP(aAL);
504 aBOP.AddArgument(aS1);
506 aBOP.SetOperation(aOp);
507 aBOP.SetRunParallel(bRunParallel);
509 aBOP.PerformWithFiller(aPF);
511 iErr=aBOP.ErrorStatus();
513 Sprintf(buf, " ErrorStatus : %d\n", iErr);
517 const TopoDS_Shape& aR=aBOP.Shape();
519 di << " null shape\n";
523 DBRep::Set(a[1], aR);
526 //=======================================================================
527 //function : bopcurves
529 //=======================================================================
530 Standard_Integer bopcurves (Draw_Interpretor& di,
535 di << " use bopcurves F1 F2 [-2d/-2d1/-2d2]\n";
539 TopoDS_Shape S1 = DBRep::Get(a[1]);
540 TopoDS_Shape S2 = DBRep::Get(a[2]);
541 TopAbs_ShapeEnum aType;
543 if (S1.IsNull() || S2.IsNull()) {
544 di << " Null shapes is not allowed \n";
548 aType=S1.ShapeType();
549 if (aType != TopAbs_FACE) {
550 di << " Type mismatch F1\n";
553 aType=S2.ShapeType();
554 if (aType != TopAbs_FACE) {
555 di << " Type mismatch F2\n";
559 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
560 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
562 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone;
563 Standard_Integer aNbCurves, aNbPoints;
564 Standard_Real anAppTol, aTolR;
565 IntSurf_ListOfPntOn2S aListOfPnts;
566 TCollection_AsciiString aNm("c_"), aNp("p_");
568 anAppTol = 0.0000001;
569 aToApproxC3d = Standard_True;
570 aToApproxC2dOnS1 = Standard_False;
571 aToApproxC2dOnS2 = Standard_False;
574 for(Standard_Integer i = 3; i < n; i++)
576 if (!strcasecmp(a[i],"-2d")) {
577 aToApproxC2dOnS1 = Standard_True;
578 aToApproxC2dOnS2 = Standard_True;
580 else if (!strcasecmp(a[i],"-2d1")) {
581 aToApproxC2dOnS1 = Standard_True;
583 else if (!strcasecmp(a[i],"-2d2")) {
584 aToApproxC2dOnS2 = Standard_True;
586 else if (!strcasecmp(a[i],"-p")) {
588 const Standard_Real aU1 = Draw::Atof(a[++i]);
589 const Standard_Real aV1 = Draw::Atof(a[++i]);
590 const Standard_Real aU2 = Draw::Atof(a[++i]);
591 const Standard_Real aV2 = Draw::Atof(a[++i]);
593 aPt.SetValue(aU1, aV1, aU2, aV2);
594 aListOfPnts.Append(aPt);
597 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 [-2d/-2d1/-2d2] [-p u1 v1 u2 v2]\n";
604 IntTools_FaceFace aFF;
606 aFF.SetParameters (aToApproxC3d,
610 aFF.SetList(aListOfPnts);
612 aFF.Perform (aF1, aF2);
614 anIsDone=aFF.IsDone();
616 di << "Error: anIsDone=" << (Standard_Integer) anIsDone << "\n";
620 aFF.PrepareLines3D(Standard_False);
621 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
622 const IntTools_SequenceOfPntOn2Faces& aSPs = aFF.Points();
624 aNbCurves = aSCs.Length();
625 aNbPoints = aSPs.Length();
626 if (!aNbCurves && !aNbPoints) {
627 di << " has no 3d curves\n";
628 di << " has no 3d points\n";
632 aTolR=aFF.TolReached3d();
633 di << "Tolerance Reached=" << aTolR << "\n";
637 di << aNbCurves << " curve(s) found.\n";
639 for (Standard_Integer i=1; i<=aNbCurves; i++) {
640 const IntTools_Curve& anIC=aSCs(i);
642 Handle (Geom_Curve) aC3D = anIC.Curve();
645 di << " has Null 3d curve# " << i << "\n";
649 TCollection_AsciiString anIndx(i), aNmx;
652 Standard_CString nameC = aNmx.ToCString();
654 DrawTrSurf::Set(nameC, aC3D);
657 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
658 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
660 if (!aPC1.IsNull() || !aPC2.IsNull()) {
663 if (!aPC1.IsNull()) {
664 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
665 pc1Nx = pc1N + anIndx;
666 Standard_CString nameC2d1 = pc1Nx.ToCString();
668 DrawTrSurf::Set(nameC2d1, aPC1);
672 if (!aPC2.IsNull()) {
673 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
674 pc2Nx = pc2N + anIndx;
675 Standard_CString nameC2d2 = pc2Nx.ToCString();
677 DrawTrSurf::Set(nameC2d2, aPC2);
679 if (!aPC1.IsNull()) {
692 di << aNbPoints << " point(s) found.\n";
694 for (Standard_Integer i = 1; i <= aNbPoints; i++) {
695 const IntTools_PntOn2Faces& aPi = aSPs(i);
696 const gp_Pnt& aP = aPi.P1().Pnt();
698 TCollection_AsciiString anIndx(i), aNmx;
700 Standard_CString nameP = aNmx.ToCString();
702 DrawTrSurf::Set(nameP, aP);
710 //=======================================================================
711 //function : mkvolume
713 //=======================================================================
714 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
717 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni]\n";
719 di << " -c - use this option if the arguments are compounds\n";
720 di << " containing shapes that should be interfered;\n";
721 di << " -ni - use this option if the arguments should not be interfered;\n";
725 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
727 Standard_Boolean bToIntersect, bRunParallel, bCompounds;
731 BOPCol_ListOfShape aLS;
733 aTol = BOPTest_Objects::FuzzyValue();
734 bRunParallel = BOPTest_Objects::RunParallel();
736 bToIntersect = Standard_True;
737 bCompounds = Standard_False;
739 for (i = 2; i < n; ++i) {
740 aS = DBRep::Get(a[i]);
745 if (!strcmp(a[i], "-c")) {
746 bCompounds = Standard_True;
748 else if (!strcmp(a[i], "-ni")) {
749 bToIntersect = Standard_False;
755 di << "No shapes to process.\n";
760 // treat list of arguments for the case of compounds
761 if (bToIntersect && bCompounds) {
762 BOPCol_ListOfShape aLSx;
763 BOPCol_ListIteratorOfListOfShape aItLS;
765 aItLS.Initialize(aLS);
766 for (; aItLS.More(); aItLS.Next()) {
767 const TopoDS_Shape& aSx = aItLS.Value();
768 TopoDS_Iterator aItS(aSx);
769 for (; aItS.More(); aItS.Next()) {
770 const TopoDS_Shape& aSxS = aItS.Value();
779 BOPAlgo_MakerVolume aMV;
780 aMV.SetArguments(aLS);
781 aMV.SetIntersect(bToIntersect);
782 aMV.SetRunParallel(bRunParallel);
783 aMV.SetFuzzyValue(aTol);
786 if (aMV.ErrorStatus()) {
787 di << "Error status: " << aMV.ErrorStatus();
791 const TopoDS_Shape& aR = aMV.Shape();
793 DBRep::Set(a[1], aR);