1 // Created on: 2000-03-16
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <BOPTest.ixx>
27 #include <NCollection_BaseAllocator.hxx>
28 #include <NCollection_IncAllocator.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <BRep_Builder.hxx>
34 #include <BOPAlgo_PaveFiller.hxx>
35 #include <BOPAlgo_Operation.hxx>
36 #include <BOPAlgo_BOP.hxx>
37 #include <BOPDS_DS.hxx>
38 #include <BOPTest_DrawableShape.hxx>
39 #include <BOPCol_ListOfShape.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <IntTools_FaceFace.hxx>
43 #include <IntTools_Curve.hxx>
44 #include <DrawTrSurf.hxx>
45 #include <Draw_Color.hxx>
47 #include <BRepAlgoAPI_BooleanOperation.hxx>
48 #include <BRepAlgoAPI_Common.hxx>
49 #include <BRepAlgoAPI_Fuse.hxx>
50 #include <BRepAlgoAPI_Cut.hxx>
51 #include <BRepAlgoAPI_Section.hxx>
54 static BOPAlgo_PaveFiller* pPF=NULL;
58 Standard_Integer bopsmt(Draw_Interpretor& di,
61 const BOPAlgo_Operation aOp);
63 Standard_Integer bsmt (Draw_Interpretor& di,
66 const BOPAlgo_Operation aOp);
68 static Standard_Integer bop (Draw_Interpretor&, Standard_Integer, const char**);
69 static Standard_Integer bopsection(Draw_Interpretor&, Standard_Integer, const char**);
70 static Standard_Integer boptuc (Draw_Interpretor&, Standard_Integer, const char**);
71 static Standard_Integer bopcut (Draw_Interpretor&, Standard_Integer, const char**);
72 static Standard_Integer bopfuse (Draw_Interpretor&, Standard_Integer, const char**);
73 static Standard_Integer bopcommon (Draw_Interpretor&, Standard_Integer, const char**);
75 static Standard_Integer bsection (Draw_Interpretor&, Standard_Integer, const char**);
76 static Standard_Integer btuc (Draw_Interpretor&, Standard_Integer, const char**);
77 static Standard_Integer bcut (Draw_Interpretor&, Standard_Integer, const char**);
78 static Standard_Integer bfuse (Draw_Interpretor&, Standard_Integer, const char**);
79 static Standard_Integer bcommon (Draw_Interpretor&, Standard_Integer, const char**);
81 static Standard_Integer bopcurves (Draw_Interpretor&, Standard_Integer, const char**);
82 static Standard_Integer bopnews (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 = "BOP 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" , __FILE__,bopcut, g);
101 theCommands.Add("boptuc" , "use boptuc" , __FILE__,boptuc, g);
102 theCommands.Add("bopsection", "use bopsection" , __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", __FILE__, bopcurves, g);
112 theCommands.Add("bopnews", "use bopnews -v[e,f]" , __FILE__, bopnews, 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\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;
578 Standard_Integer i, aNbCurves;
579 Standard_Real anAppTol, aTolR;
580 TCollection_AsciiString aNm("c_");
582 aToApproxC3d=Standard_True;
583 aToApproxC2dOnS1=Standard_False;
584 aToApproxC2dOnS2=Standard_False;
588 IntTools_FaceFace aFF;
590 aFF.SetParameters (aToApproxC3d,
595 aFF.Perform (aF1, aF2);
597 anIsDone=aFF.IsDone();
599 di << " anIsDone=" << (Standard_Integer) anIsDone << "\n";
603 aFF.PrepareLines3D();
604 const IntTools_SequenceOfCurves& aSCs=aFF.Lines();
607 aTolR=aFF.TolReached3d();
608 di << "Tolerance Reached=" << aTolR << "\n";
610 aNbCurves=aSCs.Length();
612 di << " has no 3d curve\n";
616 for (i=1; i<=aNbCurves; i++) {
617 const IntTools_Curve& anIC=aSCs(i);
619 Handle (Geom_Curve) aC3D=anIC.Curve();
622 di << " has Null 3d curve# " << i << "%d\n";
626 TCollection_AsciiString anIndx(i), aNmx;
628 Standard_CString name= aNmx.ToCString();
629 DrawTrSurf::Set(name, aC3D);