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_DrawableShape.hxx>
26 #include <BOPTest_Objects.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepAlgoAPI_BooleanOperation.hxx>
29 #include <BRepAlgoAPI_Common.hxx>
30 #include <BRepAlgoAPI_Cut.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
35 #include <Draw_Color.hxx>
36 #include <DrawTrSurf.hxx>
37 #include <Geom2d_Curve.hxx>
38 #include <Geom_Curve.hxx>
39 #include <IntTools_Curve.hxx>
40 #include <IntTools_FaceFace.hxx>
41 #include <IntTools_PntOn2Faces.hxx>
42 #include <NCollection_BaseAllocator.hxx>
43 #include <NCollection_IncAllocator.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <TopoDS_Iterator.hxx>
47 #include <TopoDS_Shape.hxx>
52 static BOPAlgo_PaveFiller* pPF=NULL;
56 Standard_Integer bopsmt(Draw_Interpretor& di,
59 const BOPAlgo_Operation aOp);
62 Standard_Integer bsmt (Draw_Interpretor& di,
65 const BOPAlgo_Operation aOp);
67 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
68 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
69 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
70 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
71 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
74 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
80 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
81 static Standard_Integer bopnews (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 = "BOPTest 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 r" , __FILE__,bopcut, g);
101 theCommands.Add("boptuc" , "use boptuc r" , __FILE__,boptuc, g);
102 theCommands.Add("bopsection", "use bopsection r" , __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/-2d1/-2d2]",
112 __FILE__, bopcurves, g);
113 theCommands.Add("bopnews" , "use bopnews -v[e,f]" , __FILE__, bopnews, g);
114 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-c] [-ni]",
115 __FILE__, mkvolume , g);
118 //=======================================================================
121 //=======================================================================
122 Standard_Integer bop(Draw_Interpretor& di,
127 Standard_Boolean bRunParallel;
128 Standard_Integer iErr;
130 TopoDS_Shape aS1, aS2;
131 BOPCol_ListOfShape aLC;
134 di << " use bop s1 s2 \n";
138 aS1=DBRep::Get(a[1]);
139 aS2=DBRep::Get(a[2]);
141 if (aS1.IsNull() || aS2.IsNull()) {
142 di << " null shapes are not allowed \n";
146 aTol=BOPTest_Objects::FuzzyValue();
147 bRunParallel=BOPTest_Objects::RunParallel();
156 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
157 pPF=new BOPAlgo_PaveFiller(aAL);
159 pPF->SetArguments(aLC);
160 pPF->SetFuzzyValue(aTol);
161 pPF->SetRunParallel(bRunParallel);
164 iErr=pPF->ErrorStatus();
166 Sprintf(buf, " ErrorStatus : %d\n", iErr);
173 //=======================================================================
174 //function : bopcommon
176 //=======================================================================
177 Standard_Integer bopcommon (Draw_Interpretor& di,
181 return bopsmt(di, n, a, BOPAlgo_COMMON);
183 //=======================================================================
186 //=======================================================================
187 Standard_Integer bopfuse(Draw_Interpretor& di,
191 return bopsmt(di, n, a, BOPAlgo_FUSE);
193 //=======================================================================
196 //=======================================================================
197 Standard_Integer bopcut(Draw_Interpretor& di,
201 return bopsmt(di, n, a, BOPAlgo_CUT);
203 //=======================================================================
206 //=======================================================================
207 Standard_Integer boptuc(Draw_Interpretor& di,
211 return bopsmt(di, n, a, BOPAlgo_CUT21);
213 //=======================================================================
216 //=======================================================================
217 Standard_Integer bopsmt(Draw_Interpretor& di,
220 const BOPAlgo_Operation aOp)
223 di << " use bopsmt r\n";
228 di << " prepare PaveFiller first\n";
232 if (pPF->ErrorStatus()) {
233 di << " PaveFiller has not been done\n";
238 Standard_Boolean bRunParallel;
239 Standard_Integer aNb, iErr;
242 const BOPCol_ListOfShape& aLC=pPF->Arguments();
245 Sprintf (buf, " wrong number of arguments %s\n", aNb);
250 bRunParallel=BOPTest_Objects::RunParallel();
252 const TopoDS_Shape& aS1=aLC.First();
253 const TopoDS_Shape& aS2=aLC.Last();
255 aBOP.AddArgument(aS1);
257 aBOP.SetOperation(aOp);
258 aBOP.SetRunParallel (bRunParallel);
260 aBOP.PerformWithFiller(*pPF);
261 iErr=aBOP.ErrorStatus();
263 Sprintf(buf, " ErrorStatus : %d\n", iErr);
268 const TopoDS_Shape& aR=aBOP.Shape();
270 di << " null shape\n";
274 DBRep::Set(a[1], aR);
277 //=======================================================================
278 //function : bopsection
280 //=======================================================================
281 Standard_Integer bopsection(Draw_Interpretor& di,
286 di << " use bopsection r\n";
291 di << " prepare PaveFiller first\n";
295 if (pPF->ErrorStatus()) {
296 di << " PaveFiller has not been done\n";
301 Standard_Boolean bRunParallel;
302 Standard_Integer aNb, iErr;
303 BOPAlgo_Section aBOP;
305 const BOPCol_ListOfShape& aLC=pPF->Arguments();
308 Sprintf (buf, " wrong number of arguments %s\n", aNb);
313 bRunParallel=BOPTest_Objects::RunParallel();
315 const TopoDS_Shape& aS1=aLC.First();
316 const TopoDS_Shape& aS2=aLC.Last();
318 aBOP.AddArgument(aS1);
319 aBOP.AddArgument(aS2);
320 aBOP.SetRunParallel (bRunParallel);
322 aBOP.PerformWithFiller(*pPF);
323 iErr=aBOP.ErrorStatus();
325 Sprintf(buf, " ErrorStatus : %d\n", iErr);
330 const TopoDS_Shape& aR=aBOP.Shape();
332 di << " null shape\n";
336 DBRep::Set(a[1], aR);
339 //=======================================================================
342 //=======================================================================
343 Standard_Integer bcommon (Draw_Interpretor& di,
347 return bsmt(di, n, a, BOPAlgo_COMMON);
349 //=======================================================================
352 //=======================================================================
353 Standard_Integer bfuse (Draw_Interpretor& di,
357 return bsmt(di, n, a, BOPAlgo_FUSE);
359 //=======================================================================
362 //=======================================================================
363 Standard_Integer bcut (Draw_Interpretor& di,
367 return bsmt(di, n, a, BOPAlgo_CUT);
369 //=======================================================================
372 //=======================================================================
373 Standard_Integer btuc (Draw_Interpretor& di,
377 return bsmt(di, n, a, BOPAlgo_CUT21);
379 //=======================================================================
380 //function : bsection
382 //=======================================================================
383 Standard_Integer bsection(Draw_Interpretor& di,
388 di << "use bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na] [tol]\n";
392 TopoDS_Shape aS1, aS2;
394 aS1=DBRep::Get(a[2]);
395 aS2=DBRep::Get(a[3]);
396 if (aS1.IsNull() || aS2.IsNull()) {
397 di << " Null shapes are not allowed \n";
402 Standard_Boolean bRunParallel, bApp, bPC1, bPC2;
403 Standard_Integer i, iErr;
406 bApp = Standard_True;
407 bPC1 = Standard_True;
408 bPC2 = Standard_True;
409 aTol = BOPTest_Objects::FuzzyValue();
410 bRunParallel = BOPTest_Objects::RunParallel();
412 for (i = 4; i < n; ++i) {
413 if (!strcmp(a[i], "-n2d")) {
414 bPC1 = Standard_False;
415 bPC2 = Standard_False;
417 else if (!strcmp(a[i], "-n2d1")) {
418 bPC1 = Standard_False;
420 else if (!strcmp(a[i], "-n2d2")) {
421 bPC2 = Standard_False;
423 else if (!strcmp(a[i], "-na")) {
424 bApp = Standard_False;
428 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
430 aSec.Approximation(bApp);
431 aSec.ComputePCurveOn1(bPC1);
432 aSec.ComputePCurveOn2(bPC2);
434 aSec.SetFuzzyValue(aTol);
435 aSec.SetRunParallel(bRunParallel);
438 iErr=aSec.ErrorStatus();
439 if (!aSec.IsDone()) {
440 Sprintf(buf, " ErrorStatus : %d\n", iErr);
445 const TopoDS_Shape& aR=aSec.Shape();
447 di << " null shape\n";
450 DBRep::Set(a[1], aR);
453 //=======================================================================
456 //=======================================================================
457 Standard_Integer bsmt (Draw_Interpretor& di,
460 const BOPAlgo_Operation aOp)
463 Standard_Boolean bRunParallel;
464 Standard_Integer iErr;
465 TopoDS_Shape aS1, aS2;
466 BOPCol_ListOfShape aLC;
470 di << " use bx r s1 s2\n";
474 aS1=DBRep::Get(a[2]);
475 aS2=DBRep::Get(a[3]);
477 if (aS1.IsNull() || aS2.IsNull()) {
478 di << " null shapes are not allowed \n";
484 aTol=BOPTest_Objects::FuzzyValue();
485 bRunParallel = BOPTest_Objects::RunParallel();
487 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
489 //---------------------------------------------------------------
490 BOPAlgo_PaveFiller aPF(aAL);
492 aPF.SetArguments(aLC);
493 aPF.SetFuzzyValue(aTol);
494 aPF.SetRunParallel(bRunParallel);
497 iErr=aPF.ErrorStatus();
499 Sprintf(buf, " ErrorStatus : %d\n", iErr);
504 //---------------------------------------------------------------
505 BOPAlgo_BOP aBOP(aAL);
507 aBOP.AddArgument(aS1);
509 aBOP.SetOperation(aOp);
510 aBOP.SetRunParallel(bRunParallel);
512 aBOP.PerformWithFiller(aPF);
514 iErr=aBOP.ErrorStatus();
516 Sprintf(buf, " ErrorStatus : %d\n", iErr);
520 const TopoDS_Shape& aR=aBOP.Shape();
522 di << " null shape\n";
526 DBRep::Set(a[1], aR);
529 //=======================================================================
532 //=======================================================================
533 Standard_Integer bopnews (Draw_Interpretor& di,
538 di << " use bopnews -v[e,f]\n";
543 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
548 Standard_CString aText;
549 Standard_Integer i, i1, i2, iFound;
550 Draw_Color aTextColor(Draw_cyan);
552 Handle(BOPTest_DrawableShape) aDShape;
554 const BOPDS_PDS& pDS=pPF->PDS();
557 if (!strcmp (a[1], "-f")) {
560 else if (!strcmp (a[1], "-e")){
563 else if (!strcmp (a[1], "-v")){
567 di << " use bopnews -v[e,f]\n";
572 i1=pDS->NbSourceShapes();
574 for (i=i1; i<i2; ++i) {
575 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
576 if (aSI.ShapeType()==aT) {
577 const TopoDS_Shape& aS=aSI.Shape();
579 Sprintf (buf, "z%d", i);
581 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
582 Draw::Set (aText, aDShape);
584 Sprintf (buf, " z%d", i);
595 di << " not found\n";
600 //=======================================================================
601 //function : bopcurves
603 //=======================================================================
604 Standard_Integer bopcurves (Draw_Interpretor& di,
609 di << " use bopcurves F1 F2 [-2d/-2d1/-2d2]\n";
613 TopoDS_Shape S1 = DBRep::Get(a[1]);
614 TopoDS_Shape S2 = DBRep::Get(a[2]);
615 TopAbs_ShapeEnum aType;
617 if (S1.IsNull() || S2.IsNull()) {
618 di << " Null shapes is not allowed \n";
622 aType=S1.ShapeType();
623 if (aType != TopAbs_FACE) {
624 di << " Type mismatch F1\n";
627 aType=S2.ShapeType();
628 if (aType != TopAbs_FACE) {
629 di << " Type mismatch F2\n";
633 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
634 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
636 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone;
637 Standard_Integer i, aNbCurves, aNbPoints;
638 Standard_Real anAppTol, aTolR;
639 TCollection_AsciiString aNm("c_"), aNp("p_");
641 anAppTol = 0.0000001;
642 aToApproxC3d = Standard_True;
643 aToApproxC2dOnS1 = Standard_False;
644 aToApproxC2dOnS2 = Standard_False;
647 if (!strcasecmp(a[3],"-2d")) {
648 aToApproxC2dOnS1 = Standard_True;
649 aToApproxC2dOnS2 = Standard_True;
651 else if (!strcasecmp(a[3],"-2d1")) {
652 aToApproxC2dOnS1 = Standard_True;
654 else if (!strcasecmp(a[3],"-2d2")) {
655 aToApproxC2dOnS2 = Standard_True;
658 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d/-2d1/-2d2 \n";
663 IntTools_FaceFace aFF;
665 aFF.SetParameters (aToApproxC3d,
670 aFF.Perform (aF1, aF2);
672 anIsDone=aFF.IsDone();
674 di << "Error: anIsDone=" << (Standard_Integer) anIsDone << "\n";
678 aFF.PrepareLines3D(Standard_False);
679 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
680 const IntTools_SequenceOfPntOn2Faces& aSPs = aFF.Points();
682 aNbCurves = aSCs.Length();
683 aNbPoints = aSPs.Length();
684 if (!aNbCurves && !aNbPoints) {
685 di << " has no 3d curves\n";
686 di << " has no 3d points\n";
690 aTolR=aFF.TolReached3d();
691 di << "Tolerance Reached=" << aTolR << "\n";
695 di << aNbCurves << " curve(s) found.\n";
697 for (i=1; i<=aNbCurves; i++) {
698 const IntTools_Curve& anIC=aSCs(i);
700 Handle (Geom_Curve) aC3D = anIC.Curve();
703 di << " has Null 3d curve# " << i << "\n";
707 TCollection_AsciiString anIndx(i), aNmx;
710 Standard_CString nameC = aNmx.ToCString();
712 DrawTrSurf::Set(nameC, aC3D);
715 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
716 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
718 if (!aPC1.IsNull() || !aPC2.IsNull()) {
721 if (!aPC1.IsNull()) {
722 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
723 pc1Nx = pc1N + anIndx;
724 Standard_CString nameC2d1 = pc1Nx.ToCString();
726 DrawTrSurf::Set(nameC2d1, aPC1);
730 if (!aPC2.IsNull()) {
731 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
732 pc2Nx = pc2N + anIndx;
733 Standard_CString nameC2d2 = pc2Nx.ToCString();
735 DrawTrSurf::Set(nameC2d2, aPC2);
737 if (!aPC1.IsNull()) {
750 di << aNbPoints << " point(s) found.\n";
752 for (i = 1; i <= aNbPoints; i++) {
753 const IntTools_PntOn2Faces& aPi = aSPs(i);
754 const gp_Pnt& aP = aPi.P1().Pnt();
756 TCollection_AsciiString anIndx(i), aNmx;
758 Standard_CString nameP = aNmx.ToCString();
760 DrawTrSurf::Set(nameP, aP);
768 //=======================================================================
769 //function : mkvolume
771 //=======================================================================
772 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
775 di << "Usage: mkvolume r b1 b2 ... [-c] [-ni]\n";
777 di << " -c - use this option if the arguments are compounds\n";
778 di << " containing shapes that should be interfered;\n";
779 di << " -ni - use this option if the arguments should not be interfered;\n";
783 const char* usage = "Type mkvolume without arguments for the usage of the command.\n";
785 Standard_Boolean bToIntersect, bRunParallel, bCompounds;
789 BOPCol_ListOfShape aLS;
791 aTol = BOPTest_Objects::FuzzyValue();
792 bRunParallel = BOPTest_Objects::RunParallel();
794 bToIntersect = Standard_True;
795 bCompounds = Standard_False;
797 for (i = 2; i < n; ++i) {
798 aS = DBRep::Get(a[i]);
803 if (!strcmp(a[i], "-c")) {
804 bCompounds = Standard_True;
806 else if (!strcmp(a[i], "-ni")) {
807 bToIntersect = Standard_False;
813 di << "No shapes to process.\n";
818 // treat list of arguments for the case of compounds
819 if (bToIntersect && bCompounds) {
820 BOPCol_ListOfShape aLSx;
821 BOPCol_ListIteratorOfListOfShape aItLS;
823 aItLS.Initialize(aLS);
824 for (; aItLS.More(); aItLS.Next()) {
825 const TopoDS_Shape& aSx = aItLS.Value();
826 TopoDS_Iterator aItS(aSx);
827 for (; aItS.More(); aItS.Next()) {
828 const TopoDS_Shape& aSxS = aItS.Value();
837 BOPAlgo_MakerVolume aMV;
838 aMV.SetArguments(aLS);
839 aMV.SetIntersect(bToIntersect);
840 aMV.SetRunParallel(bRunParallel);
841 aMV.SetFuzzyValue(aTol);
844 if (aMV.ErrorStatus()) {
845 di << "Error status: " << aMV.ErrorStatus();
849 const TopoDS_Shape& aR = aMV.Shape();
851 DBRep::Set(a[1], aR);