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>
22 #include <NCollection_BaseAllocator.hxx>
23 #include <NCollection_IncAllocator.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Compound.hxx>
27 #include <BRep_Builder.hxx>
29 #include <BOPAlgo_PaveFiller.hxx>
30 #include <BOPAlgo_Operation.hxx>
31 #include <BOPAlgo_BOP.hxx>
32 #include <BOPAlgo_MakerVolume.hxx>
33 #include <BOPDS_DS.hxx>
34 #include <BOPTest_DrawableShape.hxx>
35 #include <BOPCol_ListOfShape.hxx>
37 #include <TCollection_AsciiString.hxx>
38 #include <IntTools_FaceFace.hxx>
39 #include <IntTools_Curve.hxx>
40 #include <DrawTrSurf.hxx>
41 #include <Draw_Color.hxx>
43 #include <BRepAlgoAPI_BooleanOperation.hxx>
44 #include <BRepAlgoAPI_Common.hxx>
45 #include <BRepAlgoAPI_Fuse.hxx>
46 #include <BRepAlgoAPI_Cut.hxx>
47 #include <BRepAlgoAPI_Section.hxx>
50 static BOPAlgo_PaveFiller* pPF=NULL;
54 Standard_Integer bopsmt(Draw_Interpretor& di,
57 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 bopnews (Draw_Interpretor&, Standard_Integer, const char**);
80 static Standard_Integer mkvolume (Draw_Interpretor&, Standard_Integer, const char**);
82 //=======================================================================
83 //function : BOPCommands
85 //=======================================================================
86 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
88 static Standard_Boolean done = Standard_False;
92 const char* g = "BOP commands";
95 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
96 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
97 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
98 theCommands.Add("bopcut" , "use bopcut" , __FILE__,bopcut, g);
99 theCommands.Add("boptuc" , "use boptuc" , __FILE__,boptuc, g);
100 theCommands.Add("bopsection", "use bopsection" , __FILE__,bopsection, g);
102 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
103 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
104 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
105 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
106 theCommands.Add("bsection", "Use >bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
107 __FILE__, bsection, g);
109 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d]", __FILE__, bopcurves, g);
110 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, g);
112 theCommands.Add("mkvolume", "make solids from set of shapes.\nmkvolume r b1 b2 ... [-ni (do not intersect)] [-s (run in non parallel mode)]", __FILE__, mkvolume , g);
115 //=======================================================================
118 //=======================================================================
119 Standard_Integer bop (Draw_Interpretor& di, Standard_Integer n, const char** a)
122 Standard_Integer iErr;
123 TopoDS_Shape aS1, aS2;
124 BOPCol_ListOfShape aLC;
127 di << " use bop Shape1 Shape2\n";
131 aS1=DBRep::Get(a[1]);
132 aS2=DBRep::Get(a[2]);
134 if (aS1.IsNull() || aS2.IsNull()) {
135 di << " null shapes are not allowed \n";
146 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
147 pPF=new BOPAlgo_PaveFiller(aAL);
149 pPF->SetArguments(aLC);
152 iErr=pPF->ErrorStatus();
154 Sprintf(buf, " ErrorStatus : %d\n", iErr);
161 //=======================================================================
162 //function : bopcommon
164 //=======================================================================
165 Standard_Integer bopcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
167 return bopsmt(di, n, a, BOPAlgo_COMMON);
169 //=======================================================================
172 //=======================================================================
173 Standard_Integer bopfuse(Draw_Interpretor& di, Standard_Integer n, const char** a)
175 return bopsmt(di, n, a, BOPAlgo_FUSE);
177 //=======================================================================
180 //=======================================================================
181 Standard_Integer bopcut(Draw_Interpretor& di, Standard_Integer n, const char** a)
183 return bopsmt(di, n, a, BOPAlgo_CUT);
185 //=======================================================================
188 //=======================================================================
189 Standard_Integer boptuc(Draw_Interpretor& di, Standard_Integer n, const char** a)
191 return bopsmt(di, n, a, BOPAlgo_CUT21);
193 //=======================================================================
194 //function : bopsection
196 //=======================================================================
197 Standard_Integer bopsection(Draw_Interpretor& di, Standard_Integer n, const char** a)
199 return bopsmt(di, n, a, BOPAlgo_SECTION);
201 //=======================================================================
204 //=======================================================================
205 Standard_Integer bopsmt(Draw_Interpretor& di,
208 const BOPAlgo_Operation aOp)
211 di << " use bopsmt r\n";
216 di << " prepare PaveFiller first\n";
220 if (pPF->ErrorStatus()) {
221 di << " PaveFiller has not been done\n";
226 Standard_Integer aNb, iErr;
229 const BOPCol_ListOfShape& aLC=pPF->Arguments();
232 Sprintf (buf, " wrong number of arguments %s\n", aNb);
237 const TopoDS_Shape& aS1=aLC.First();
238 const TopoDS_Shape& aS2=aLC.Last();
240 aBOP.AddArgument(aS1);
242 aBOP.SetOperation(aOp);
244 aBOP.PerformWithFiller(*pPF);
245 iErr=aBOP.ErrorStatus();
247 Sprintf(buf, " ErrorStatus : %d\n", iErr);
252 const TopoDS_Shape& aR=aBOP.Shape();
254 di << " null shape\n";
258 DBRep::Set(a[1], aR);
261 //=======================================================================
264 //=======================================================================
265 Standard_Integer bcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
267 return bsmt(di, n, a, BOPAlgo_COMMON);
269 //=======================================================================
272 //=======================================================================
273 Standard_Integer bfuse (Draw_Interpretor& di, Standard_Integer n, const char** a)
275 return bsmt(di, n, a, BOPAlgo_FUSE);
277 //=======================================================================
280 //=======================================================================
281 Standard_Integer bcut (Draw_Interpretor& di, Standard_Integer n, const char** a)
283 return bsmt(di, n, a, BOPAlgo_CUT);
285 //=======================================================================
288 //=======================================================================
289 Standard_Integer btuc (Draw_Interpretor& di, Standard_Integer n, const char** a)
291 return bsmt(di, n, a, BOPAlgo_CUT21);
293 //=======================================================================
294 //function : bsection
296 //=======================================================================
297 Standard_Integer bsection(Draw_Interpretor& di,
301 const char* usage = " Usage: bsection Result s1 s2 [-n2d/-n2d1/-n2d2] [-na]\n";
307 TopoDS_Shape aS1 = DBRep::Get(a[2]);
308 TopoDS_Shape aS2 = DBRep::Get(a[3]);
310 if (aS1.IsNull() || aS2.IsNull()) {
311 di << " Null shapes are not allowed \n";
315 Standard_Boolean bApp, bPC1, bPC2;
317 bApp = Standard_True;
318 bPC1 = Standard_True;
319 bPC2 = Standard_True;
321 Standard_Boolean isbadparameter = Standard_False;
324 const char* key1 = a[4];
325 const char* key2 = (n > 5) ? a[5] : NULL;
326 const char* pcurveconf = NULL;
328 if (key1 && (!strcasecmp(key1,"-n2d") || !strcasecmp(key1,"-n2d1") || !strcasecmp(key1,"-n2d2"))) {
332 if (!strcasecmp(key1,"-na")) {
333 bApp = Standard_False;
336 isbadparameter = Standard_True;
340 if(!strcasecmp(key2,"-na")) {
341 bApp = Standard_False;
344 isbadparameter = Standard_True;
348 if(!isbadparameter && pcurveconf) {
349 if (!strcasecmp(pcurveconf, "-n2d1")) {
350 bPC1 = Standard_False;
353 if (!strcasecmp(pcurveconf, "-n2d2")) {
354 bPC2 = Standard_False;
357 if (!strcasecmp(pcurveconf, "-n2d")) {
358 bPC1 = Standard_False;
359 bPC2 = Standard_False;
366 if(!isbadparameter) {
367 Standard_Integer iErr;
370 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
371 aSec.Approximation(bApp);
372 aSec.ComputePCurveOn1(bPC1);
373 aSec.ComputePCurveOn2(bPC2);
376 iErr=aSec.ErrorStatus();
377 if (!aSec.IsDone()) {
378 Sprintf(buf, " ErrorStatus : %d\n", iErr);
383 const TopoDS_Shape& aR=aSec.Shape();
385 di << " null shape\n";
388 DBRep::Set(a[1], aR);
396 //=======================================================================
399 //=======================================================================
400 Standard_Integer bsmt (Draw_Interpretor& di,
403 const BOPAlgo_Operation aOp)
406 Standard_Integer iErr;
407 TopoDS_Shape aS1, aS2;
408 BOPCol_ListOfShape aLC;
411 di << " use bx r s1 s2\n";
415 aS1=DBRep::Get(a[2]);
416 aS2=DBRep::Get(a[3]);
418 if (aS1.IsNull() || aS2.IsNull()) {
419 di << " null shapes are not allowed \n";
425 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
426 BOPAlgo_PaveFiller aPF(aAL);
428 aPF.SetArguments(aLC);
431 iErr=aPF.ErrorStatus();
433 Sprintf(buf, " ErrorStatus : %d\n", iErr);
438 BRepAlgoAPI_BooleanOperation* pBuilder=NULL;
440 if (aOp==BOPAlgo_COMMON) {
441 pBuilder=new BRepAlgoAPI_Common(aS1, aS2, aPF);
443 else if (aOp==BOPAlgo_FUSE) {
444 pBuilder=new BRepAlgoAPI_Fuse(aS1, aS2, aPF);
446 else if (aOp==BOPAlgo_CUT) {
447 pBuilder=new BRepAlgoAPI_Cut (aS1, aS2, aPF);
449 else if (aOp==BOPAlgo_CUT21) {
450 pBuilder=new BRepAlgoAPI_Cut(aS1, aS2, aPF, Standard_False);
453 iErr = pBuilder->ErrorStatus();
454 if (!pBuilder->IsDone()) {
455 Sprintf(buf, " ErrorStatus : %d\n", iErr);
459 const TopoDS_Shape& aR=pBuilder->Shape();
461 di << " null shape\n";
464 DBRep::Set(a[1], aR);
468 //=======================================================================
471 //=======================================================================
472 Standard_Integer bopnews (Draw_Interpretor& di,
477 di << " use bopnews -v[e,f]\n";
482 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
487 Standard_CString aText;
488 Standard_Integer i, i1, i2, iFound;
489 Draw_Color aTextColor(Draw_cyan);
491 Handle(BOPTest_DrawableShape) aDShape;
493 const BOPDS_PDS& pDS=pPF->PDS();
496 if (!strcmp (a[1], "-f")) {
499 else if (!strcmp (a[1], "-e")){
502 else if (!strcmp (a[1], "-v")){
506 di << " use bopnews -v[e,f]\n";
511 i1=pDS->NbSourceShapes();
513 for (i=i1; i<i2; ++i) {
514 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
515 if (aSI.ShapeType()==aT) {
516 const TopoDS_Shape& aS=aSI.Shape();
518 Sprintf (buf, "z%d", i);
520 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
521 Draw::Set (aText, aDShape);
523 Sprintf (buf, " z%d", i);
534 di << " not found\n";
540 //=======================================================================
541 //function : bopcurves
543 //=======================================================================
544 Standard_Integer bopcurves (Draw_Interpretor& di,
549 di << " use bopcurves F1 F2 [-2d]\n";
553 TopoDS_Shape S1 = DBRep::Get(a[1]);
554 TopoDS_Shape S2 = DBRep::Get(a[2]);
555 TopAbs_ShapeEnum aType;
557 if (S1.IsNull() || S2.IsNull()) {
558 di << " Null shapes is not allowed \n";
562 aType=S1.ShapeType();
563 if (aType != TopAbs_FACE) {
564 di << " Type mismatch F1\n";
567 aType=S2.ShapeType();
568 if (aType != TopAbs_FACE) {
569 di << " Type mismatch F2\n";
574 const TopoDS_Face& aF1=*(TopoDS_Face*)(&S1);
575 const TopoDS_Face& aF2=*(TopoDS_Face*)(&S2);
577 Standard_Boolean aToApproxC3d, aToApproxC2dOnS1, aToApproxC2dOnS2, anIsDone, bMake2dCurves;
578 Standard_Integer i, aNbCurves;
579 Standard_Real anAppTol, aTolR;
580 TCollection_AsciiString aNm("c_");
582 bMake2dCurves = Standard_False;
584 if (!strcasecmp(a[3],"-2d")) {
585 bMake2dCurves = Standard_True;
587 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d \n";
593 aToApproxC3d = Standard_True;
594 aToApproxC2dOnS1 = bMake2dCurves;
595 aToApproxC2dOnS2 = bMake2dCurves;
599 IntTools_FaceFace aFF;
601 aFF.SetParameters (aToApproxC3d,
606 aFF.Perform (aF1, aF2);
608 anIsDone=aFF.IsDone();
610 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
614 aFF.PrepareLines3D(Standard_False);
615 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
618 aTolR=aFF.TolReached3d();
619 di << "Tolerance Reached=" << aTolR << "\n";
621 aNbCurves=aSCs.Length();
623 di << " has no 3d curve\n";
628 di << aNbCurves << " curve(s) found.\n";
631 for (i=1; i<=aNbCurves; i++) {
632 const IntTools_Curve& anIC=aSCs(i);
634 Handle (Geom_Curve) aC3D = anIC.Curve();
637 di << " has Null 3d curve# " << i << "\n";
641 TCollection_AsciiString anIndx(i), aNmx;
644 Standard_CString nameC = aNmx.ToCString();
646 DrawTrSurf::Set(nameC, aC3D);
650 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
651 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
653 if (aPC1.IsNull() && aPC2.IsNull()) {
654 di << " \n has Null 2d curves# " << i << "\n";
659 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
660 pc2Nx = pc2N + anIndx;
661 Standard_CString nameC2d2 = pc2Nx.ToCString();
663 DrawTrSurf::Set(nameC2d2, aPC2);
664 di << "(" << nameC2d2 << ") ";
665 di << " \n Null first 2d curve of the curve #" << i << "\n";
668 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
669 pc1Nx = pc1N + anIndx;
670 Standard_CString nameC2d1 = pc1Nx.ToCString();
672 DrawTrSurf::Set(nameC2d1, aPC1);
673 di << "(" << nameC2d1;
677 di << ") \n Null second 2d curve of the curve #" << i << "\n";
680 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
681 pc2Nx = pc2N + anIndx;
682 Standard_CString nameC2d2 = pc2Nx.ToCString();
684 DrawTrSurf::Set(nameC2d2, aPC2);
685 di << ", " << nameC2d2 << ") ";
695 //=======================================================================
696 //function : mkvolume
698 //=======================================================================
699 Standard_Integer mkvolume(Draw_Interpretor& di, Standard_Integer n, const char** a)
701 const char* usage = "Usage: mkvolume r b1 b2 ... [-ni (do not intersect)] [-s (run in non parallel mode)]\n";
707 Standard_Boolean bToIntersect, bRunParallel;
708 Standard_Integer i, aNb;
711 bToIntersect = Standard_True;
712 bRunParallel = Standard_True;
714 if (!strcmp(a[n-1], "-ni")) {
715 bToIntersect = Standard_False;
718 else if (!strcmp(a[n-1], "-s")) {
719 bRunParallel = Standard_False;
723 if (!strcmp(a[n-2], "-ni")) {
724 bToIntersect = Standard_False;
727 else if (!strcmp(a[n-2], "-s")) {
728 bRunParallel = Standard_False;
734 di << "no shapes to process.\n";
739 BOPCol_ListOfShape aLS;
741 for (i = 2; i < aNb; ++i) {
742 aS = DBRep::Get(a[i]);
749 di << "no shapes to process.\n";
754 BOPAlgo_MakerVolume aMV;
755 aMV.SetArguments(aLS);
756 aMV.SetIntersect(bToIntersect);
757 aMV.SetRunParallel(bRunParallel);
760 if (aMV.ErrorStatus()) {
761 di << "Error status: " << aMV.ErrorStatus();
765 const TopoDS_Shape& aR = aMV.Shape();
767 DBRep::Set(a[1], aR);