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]",
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 i, aNbCurves, aNbPoints;
564 Standard_Real anAppTol, aTolR;
565 TCollection_AsciiString aNm("c_"), aNp("p_");
567 anAppTol = 0.0000001;
568 aToApproxC3d = Standard_True;
569 aToApproxC2dOnS1 = Standard_False;
570 aToApproxC2dOnS2 = Standard_False;
573 if (!strcasecmp(a[3],"-2d")) {
574 aToApproxC2dOnS1 = Standard_True;
575 aToApproxC2dOnS2 = Standard_True;
577 else if (!strcasecmp(a[3],"-2d1")) {
578 aToApproxC2dOnS1 = Standard_True;
580 else if (!strcasecmp(a[3],"-2d2")) {
581 aToApproxC2dOnS2 = Standard_True;
584 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d/-2d1/-2d2 \n";
589 IntTools_FaceFace aFF;
591 aFF.SetParameters (aToApproxC3d,
596 aFF.Perform (aF1, aF2);
598 anIsDone=aFF.IsDone();
600 di << "Error: anIsDone=" << (Standard_Integer) anIsDone << "\n";
604 aFF.PrepareLines3D(Standard_False);
605 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
606 const IntTools_SequenceOfPntOn2Faces& aSPs = aFF.Points();
608 aNbCurves = aSCs.Length();
609 aNbPoints = aSPs.Length();
610 if (!aNbCurves && !aNbPoints) {
611 di << " has no 3d curves\n";
612 di << " has no 3d points\n";
616 aTolR=aFF.TolReached3d();
617 di << "Tolerance Reached=" << aTolR << "\n";
621 di << aNbCurves << " curve(s) found.\n";
623 for (i=1; i<=aNbCurves; i++) {
624 const IntTools_Curve& anIC=aSCs(i);
626 Handle (Geom_Curve) aC3D = anIC.Curve();
629 di << " has Null 3d curve# " << i << "\n";
633 TCollection_AsciiString anIndx(i), aNmx;
636 Standard_CString nameC = aNmx.ToCString();
638 DrawTrSurf::Set(nameC, aC3D);
641 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
642 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
644 if (!aPC1.IsNull() || !aPC2.IsNull()) {
647 if (!aPC1.IsNull()) {
648 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
649 pc1Nx = pc1N + anIndx;
650 Standard_CString nameC2d1 = pc1Nx.ToCString();
652 DrawTrSurf::Set(nameC2d1, aPC1);
656 if (!aPC2.IsNull()) {
657 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
658 pc2Nx = pc2N + anIndx;
659 Standard_CString nameC2d2 = pc2Nx.ToCString();
661 DrawTrSurf::Set(nameC2d2, aPC2);
663 if (!aPC1.IsNull()) {
676 di << aNbPoints << " point(s) found.\n";
678 for (i = 1; i <= aNbPoints; i++) {
679 const IntTools_PntOn2Faces& aPi = aSPs(i);
680 const gp_Pnt& aP = aPi.P1().Pnt();
682 TCollection_AsciiString anIndx(i), aNmx;
684 Standard_CString nameP = aNmx.ToCString();
686 DrawTrSurf::Set(nameP, aP);
694 //=======================================================================
695 //function : mkvolume
697 //=======================================================================
698 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
701 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni]\n";
703 di << " -c - use this option if the arguments are compounds\n";
704 di << " containing shapes that should be interfered;\n";
705 di << " -ni - use this option if the arguments should not be interfered;\n";
709 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
711 Standard_Boolean bToIntersect, bRunParallel, bCompounds;
715 BOPCol_ListOfShape aLS;
717 aTol = BOPTest_Objects::FuzzyValue();
718 bRunParallel = BOPTest_Objects::RunParallel();
720 bToIntersect = Standard_True;
721 bCompounds = Standard_False;
723 for (i = 2; i < n; ++i) {
724 aS = DBRep::Get(a[i]);
729 if (!strcmp(a[i], "-c")) {
730 bCompounds = Standard_True;
732 else if (!strcmp(a[i], "-ni")) {
733 bToIntersect = Standard_False;
739 di << "No shapes to process.\n";
744 // treat list of arguments for the case of compounds
745 if (bToIntersect && bCompounds) {
746 BOPCol_ListOfShape aLSx;
747 BOPCol_ListIteratorOfListOfShape aItLS;
749 aItLS.Initialize(aLS);
750 for (; aItLS.More(); aItLS.Next()) {
751 const TopoDS_Shape& aSx = aItLS.Value();
752 TopoDS_Iterator aItS(aSx);
753 for (; aItS.More(); aItS.Next()) {
754 const TopoDS_Shape& aSxS = aItS.Value();
763 BOPAlgo_MakerVolume aMV;
764 aMV.SetArguments(aLS);
765 aMV.SetIntersect(bToIntersect);
766 aMV.SetRunParallel(bRunParallel);
767 aMV.SetFuzzyValue(aTol);
770 if (aMV.ErrorStatus()) {
771 di << "Error status: " << aMV.ErrorStatus();
775 const TopoDS_Shape& aR = aMV.Shape();
777 DBRep::Set(a[1], aR);