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 <BOPDS_DS.hxx>
33 #include <BOPTest_DrawableShape.hxx>
34 #include <BOPCol_ListOfShape.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <IntTools_FaceFace.hxx>
38 #include <IntTools_Curve.hxx>
39 #include <DrawTrSurf.hxx>
40 #include <Draw_Color.hxx>
42 #include <BRepAlgoAPI_BooleanOperation.hxx>
43 #include <BRepAlgoAPI_Common.hxx>
44 #include <BRepAlgoAPI_Fuse.hxx>
45 #include <BRepAlgoAPI_Cut.hxx>
46 #include <BRepAlgoAPI_Section.hxx>
49 static BOPAlgo_PaveFiller* pPF=NULL;
53 Standard_Integer bopsmt(Draw_Interpretor& di,
56 const BOPAlgo_Operation aOp);
58 Standard_Integer bsmt (Draw_Interpretor& di,
61 const BOPAlgo_Operation aOp);
63 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
64 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
65 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
66 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
67 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
68 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
70 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
71 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
73 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
74 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bopnews (Draw_Interpretor&, Standard_Integer, const char**);
79 //=======================================================================
80 //function : BOPCommands
82 //=======================================================================
83 void BOPTest::BOPCommands(Draw_Interpretor& theCommands)
85 static Standard_Boolean done = Standard_False;
89 const char* g = "BOP commands";
92 theCommands.Add("bop" , "use bop s1 s2" , __FILE__, bop, g);
93 theCommands.Add("bopcommon" , "use bopcommon r" , __FILE__, bopcommon, g);
94 theCommands.Add("bopfuse" , "use bopfuse r" , __FILE__,bopfuse, g);
95 theCommands.Add("bopcut" , "use bopcut" , __FILE__,bopcut, g);
96 theCommands.Add("boptuc" , "use boptuc" , __FILE__,boptuc, g);
97 theCommands.Add("bopsection", "use bopsection" , __FILE__,bopsection, g);
99 theCommands.Add("bcommon" , "use bcommon r s1 s2" , __FILE__,bcommon, g);
100 theCommands.Add("bfuse" , "use bfuse r s1 s2" , __FILE__,bfuse, g);
101 theCommands.Add("bcut" , "use bcut r s1 s2" , __FILE__,bcut, g);
102 theCommands.Add("btuc" , "use btuc r s1 s2" , __FILE__,btuc, g);
103 theCommands.Add("bsection", "Use >bsection r s1 s2 [-n2d/-n2d1/-n2d2] [-na]",
104 __FILE__, bsection, g);
106 theCommands.Add("bopcurves", "use bopcurves F1 F2 [-2d]", __FILE__, bopcurves, g);
107 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, g);
110 //=======================================================================
113 //=======================================================================
114 Standard_Integer bop (Draw_Interpretor& di, Standard_Integer n, const char** a)
117 Standard_Integer iErr;
118 TopoDS_Shape aS1, aS2;
119 BOPCol_ListOfShape aLC;
122 di << " use bop Shape1 Shape2\n";
126 aS1=DBRep::Get(a[1]);
127 aS2=DBRep::Get(a[2]);
129 if (aS1.IsNull() || aS2.IsNull()) {
130 di << " null shapes are not allowed \n";
141 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
142 pPF=new BOPAlgo_PaveFiller(aAL);
144 pPF->SetArguments(aLC);
147 iErr=pPF->ErrorStatus();
149 Sprintf(buf, " ErrorStatus : %d\n", iErr);
156 //=======================================================================
157 //function : bopcommon
159 //=======================================================================
160 Standard_Integer bopcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
162 return bopsmt(di, n, a, BOPAlgo_COMMON);
164 //=======================================================================
167 //=======================================================================
168 Standard_Integer bopfuse(Draw_Interpretor& di, Standard_Integer n, const char** a)
170 return bopsmt(di, n, a, BOPAlgo_FUSE);
172 //=======================================================================
175 //=======================================================================
176 Standard_Integer bopcut(Draw_Interpretor& di, Standard_Integer n, const char** a)
178 return bopsmt(di, n, a, BOPAlgo_CUT);
180 //=======================================================================
183 //=======================================================================
184 Standard_Integer boptuc(Draw_Interpretor& di, Standard_Integer n, const char** a)
186 return bopsmt(di, n, a, BOPAlgo_CUT21);
188 //=======================================================================
189 //function : bopsection
191 //=======================================================================
192 Standard_Integer bopsection(Draw_Interpretor& di, Standard_Integer n, const char** a)
194 return bopsmt(di, n, a, BOPAlgo_SECTION);
196 //=======================================================================
199 //=======================================================================
200 Standard_Integer bopsmt(Draw_Interpretor& di,
203 const BOPAlgo_Operation aOp)
206 di << " use bopsmt r\n";
211 di << " prepare PaveFiller first\n";
215 if (pPF->ErrorStatus()) {
216 di << " PaveFiller has not been done\n";
221 Standard_Integer aNb, iErr;
224 const BOPCol_ListOfShape& aLC=pPF->Arguments();
227 Sprintf (buf, " wrong number of arguments %s\n", aNb);
232 const TopoDS_Shape& aS1=aLC.First();
233 const TopoDS_Shape& aS2=aLC.Last();
235 aBOP.AddArgument(aS1);
237 aBOP.SetOperation(aOp);
239 aBOP.PerformWithFiller(*pPF);
240 iErr=aBOP.ErrorStatus();
242 Sprintf(buf, " ErrorStatus : %d\n", iErr);
247 const TopoDS_Shape& aR=aBOP.Shape();
249 di << " null shape\n";
253 DBRep::Set(a[1], aR);
256 //=======================================================================
259 //=======================================================================
260 Standard_Integer bcommon (Draw_Interpretor& di, Standard_Integer n, const char** a)
262 return bsmt(di, n, a, BOPAlgo_COMMON);
264 //=======================================================================
267 //=======================================================================
268 Standard_Integer bfuse (Draw_Interpretor& di, Standard_Integer n, const char** a)
270 return bsmt(di, n, a, BOPAlgo_FUSE);
272 //=======================================================================
275 //=======================================================================
276 Standard_Integer bcut (Draw_Interpretor& di, Standard_Integer n, const char** a)
278 return bsmt(di, n, a, BOPAlgo_CUT);
280 //=======================================================================
283 //=======================================================================
284 Standard_Integer btuc (Draw_Interpretor& di, Standard_Integer n, const char** a)
286 return bsmt(di, n, a, BOPAlgo_CUT21);
288 //=======================================================================
289 //function : bsection
291 //=======================================================================
292 Standard_Integer bsection(Draw_Interpretor& di,
296 const char* usage = " Usage: bsection Result s1 s2 [-n2d/-n2d1/-n2d2] [-na]\n";
302 TopoDS_Shape aS1 = DBRep::Get(a[2]);
303 TopoDS_Shape aS2 = DBRep::Get(a[3]);
305 if (aS1.IsNull() || aS2.IsNull()) {
306 di << " Null shapes are not allowed \n";
310 Standard_Boolean bApp, bPC1, bPC2;
312 bApp = Standard_True;
313 bPC1 = Standard_True;
314 bPC2 = Standard_True;
316 Standard_Boolean isbadparameter = Standard_False;
319 const char* key1 = a[4];
320 const char* key2 = (n > 5) ? a[5] : NULL;
321 const char* pcurveconf = NULL;
323 if (key1 && (!strcasecmp(key1,"-n2d") || !strcasecmp(key1,"-n2d1") || !strcasecmp(key1,"-n2d2"))) {
327 if (!strcasecmp(key1,"-na")) {
328 bApp = Standard_False;
331 isbadparameter = Standard_True;
335 if(!strcasecmp(key2,"-na")) {
336 bApp = Standard_False;
339 isbadparameter = Standard_True;
343 if(!isbadparameter && pcurveconf) {
344 if (!strcasecmp(pcurveconf, "-n2d1")) {
345 bPC1 = Standard_False;
348 if (!strcasecmp(pcurveconf, "-n2d2")) {
349 bPC2 = Standard_False;
352 if (!strcasecmp(pcurveconf, "-n2d")) {
353 bPC1 = Standard_False;
354 bPC2 = Standard_False;
361 if(!isbadparameter) {
362 Standard_Integer iErr;
365 BRepAlgoAPI_Section aSec(aS1, aS2, Standard_False);
366 aSec.Approximation(bApp);
367 aSec.ComputePCurveOn1(bPC1);
368 aSec.ComputePCurveOn2(bPC2);
371 iErr=aSec.ErrorStatus();
372 if (!aSec.IsDone()) {
373 Sprintf(buf, " ErrorStatus : %d\n", iErr);
378 const TopoDS_Shape& aR=aSec.Shape();
380 di << " null shape\n";
383 DBRep::Set(a[1], aR);
391 //=======================================================================
394 //=======================================================================
395 Standard_Integer bsmt (Draw_Interpretor& di,
398 const BOPAlgo_Operation aOp)
401 Standard_Integer iErr;
402 TopoDS_Shape aS1, aS2;
403 BOPCol_ListOfShape aLC;
406 di << " use bx r s1 s2\n";
410 aS1=DBRep::Get(a[2]);
411 aS2=DBRep::Get(a[3]);
413 if (aS1.IsNull() || aS2.IsNull()) {
414 di << " null shapes are not allowed \n";
420 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
421 BOPAlgo_PaveFiller aPF(aAL);
423 aPF.SetArguments(aLC);
426 iErr=aPF.ErrorStatus();
428 Sprintf(buf, " ErrorStatus : %d\n", iErr);
433 BRepAlgoAPI_BooleanOperation* pBuilder=NULL;
435 if (aOp==BOPAlgo_COMMON) {
436 pBuilder=new BRepAlgoAPI_Common(aS1, aS2, aPF);
438 else if (aOp==BOPAlgo_FUSE) {
439 pBuilder=new BRepAlgoAPI_Fuse(aS1, aS2, aPF);
441 else if (aOp==BOPAlgo_CUT) {
442 pBuilder=new BRepAlgoAPI_Cut (aS1, aS2, aPF);
444 else if (aOp==BOPAlgo_CUT21) {
445 pBuilder=new BRepAlgoAPI_Cut(aS1, aS2, aPF, Standard_False);
448 iErr = pBuilder->ErrorStatus();
449 if (!pBuilder->IsDone()) {
450 Sprintf(buf, " ErrorStatus : %d\n", iErr);
454 const TopoDS_Shape& aR=pBuilder->Shape();
456 di << " null shape\n";
459 DBRep::Set(a[1], aR);
463 //=======================================================================
466 //=======================================================================
467 Standard_Integer bopnews (Draw_Interpretor& di,
472 di << " use bopnews -v[e,f]\n";
477 di << " Prepare BOPAlgo_PaveFiller first >bop S1 S2\n";
482 Standard_CString aText;
483 Standard_Integer i, i1, i2, iFound;
484 Draw_Color aTextColor(Draw_cyan);
486 Handle(BOPTest_DrawableShape) aDShape;
488 const BOPDS_PDS& pDS=pPF->PDS();
491 if (!strcmp (a[1], "-f")) {
494 else if (!strcmp (a[1], "-e")){
497 else if (!strcmp (a[1], "-v")){
501 di << " use bopnews -v[e,f]\n";
506 i1=pDS->NbSourceShapes();
508 for (i=i1; i<i2; ++i) {
509 const BOPDS_ShapeInfo& aSI=pDS->ShapeInfo(i);
510 if (aSI.ShapeType()==aT) {
511 const TopoDS_Shape& aS=aSI.Shape();
513 Sprintf (buf, "z%d", i);
515 aDShape=new BOPTest_DrawableShape (aS, aText, aTextColor);
516 Draw::Set (aText, aDShape);
518 Sprintf (buf, " z%d", i);
529 di << " not found\n";
535 //=======================================================================
536 //function : bopcurves
538 //=======================================================================
539 Standard_Integer bopcurves (Draw_Interpretor& di,
544 di << " use bopcurves F1 F2 [-2d]\n";
548 TopoDS_Shape S1 = DBRep::Get(a[1]);
549 TopoDS_Shape S2 = DBRep::Get(a[2]);
550 TopAbs_ShapeEnum aType;
552 if (S1.IsNull() || S2.IsNull()) {
553 di << " Null shapes is not allowed \n";
557 aType=S1.ShapeType();
558 if (aType != TopAbs_FACE) {
559 di << " Type mismatch F1\n";
562 aType=S2.ShapeType();
563 if (aType != TopAbs_FACE) {
564 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, bMake2dCurves;
573 Standard_Integer i, aNbCurves;
574 Standard_Real anAppTol, aTolR;
575 TCollection_AsciiString aNm("c_");
577 bMake2dCurves = Standard_False;
579 if (!strcasecmp(a[3],"-2d")) {
580 bMake2dCurves = Standard_True;
582 di << "Wrong key. To build 2d curves use: bopcurves F1 F2 -2d \n";
588 aToApproxC3d = Standard_True;
589 aToApproxC2dOnS1 = bMake2dCurves;
590 aToApproxC2dOnS2 = bMake2dCurves;
594 IntTools_FaceFace aFF;
596 aFF.SetParameters (aToApproxC3d,
601 aFF.Perform (aF1, aF2);
603 anIsDone=aFF.IsDone();
605 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
609 aFF.PrepareLines3D(Standard_False);
610 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
613 aTolR=aFF.TolReached3d();
614 di << "Tolerance Reached=" << aTolR << "\n";
616 aNbCurves=aSCs.Length();
618 di << " has no 3d curve\n";
623 di << aNbCurves << " curve(s) found.\n";
626 for (i=1; i<=aNbCurves; i++) {
627 const IntTools_Curve& anIC=aSCs(i);
629 Handle (Geom_Curve) aC3D = anIC.Curve();
632 di << " has Null 3d curve# " << i << "\n";
636 TCollection_AsciiString anIndx(i), aNmx;
639 Standard_CString nameC = aNmx.ToCString();
641 DrawTrSurf::Set(nameC, aC3D);
645 Handle(Geom2d_Curve) aPC1 = anIC.FirstCurve2d();
646 Handle(Geom2d_Curve) aPC2 = anIC.SecondCurve2d();
648 if (aPC1.IsNull() && aPC2.IsNull()) {
649 di << " \n has Null 2d curves# " << i << "\n";
654 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
655 pc2Nx = pc2N + anIndx;
656 Standard_CString nameC2d2 = pc2Nx.ToCString();
658 DrawTrSurf::Set(nameC2d2, aPC2);
659 di << "(" << nameC2d2 << ") ";
660 di << " \n Null first 2d curve of the curve #" << i << "\n";
663 TCollection_AsciiString pc1N("c2d1_"), pc1Nx;
664 pc1Nx = pc1N + anIndx;
665 Standard_CString nameC2d1 = pc1Nx.ToCString();
667 DrawTrSurf::Set(nameC2d1, aPC1);
668 di << "(" << nameC2d1;
672 di << ") \n Null second 2d curve of the curve #" << i << "\n";
675 TCollection_AsciiString pc2N("c2d2_"), pc2Nx;
676 pc2Nx = pc2N + anIndx;
677 Standard_CString nameC2d2 = pc2Nx.ToCString();
679 DrawTrSurf::Set(nameC2d2, aPC2);
680 di << ", " << nameC2d2 << ") ";