0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / QABugs / QABugs_17.cxx
1 // Created on: 2002-03-18
2 // Created by: QA Admin
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <stdio.h>
17
18 #include <QABugs.hxx>
19
20 #include <Draw.hxx>
21 #include <Draw_Interpretor.hxx>
22 #include <DBRep.hxx>
23 #include <DrawTrSurf.hxx>
24 #include <AIS_InteractiveContext.hxx>
25 #include <ViewerTest.hxx>
26 #include <ViewerTest_EventManager.hxx>
27 #include <AIS_Shape.hxx>
28 #include <TopoDS_Shape.hxx>
29
30 #include <Geom_Circle.hxx>
31 #include <Geom_Ellipse.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_BSplineSurface.hxx>
34 #include <gp_Pln.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <GeomAPI.hxx>
37 #include <Geom2dAdaptor_Curve.hxx>
38 #include <Geom2dGcc_QualifiedCurve.hxx>
39 #include <Geom2dGcc_Lin2d2Tan.hxx>
40 #include <Geom2d_Line.hxx>
41 #include <BRepBuilderAPI_MakeEdge.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <Precision.hxx>
44 #include <Geom2d_Circle.hxx>
45 #include <Geom2dGcc_QCurve.hxx>
46 #include <GccEnt_QualifiedCirc.hxx>
47 #include <Geom2dGcc_Lin2d2TanIter.hxx>
48 #include <BRepBuilderAPI_MakeWire.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Wire.hxx>
52 #include <BRep_Tool.hxx>
53 #include <gp_Circ.hxx>
54 #include <BRepOffsetAPI_MakePipeShell.hxx>
55 #include <AIS_Trihedron.hxx>
56 #include <Geom_Axis2Placement.hxx>
57 #include <V3d_View.hxx>
58 #include <BRepBuilderAPI_MakeVertex.hxx>
59 #include <PrsDim_LengthDimension.hxx>
60 #include <PrsDim_RadiusDimension.hxx>
61 #include <BRepPrimAPI_MakeBox.hxx>
62 #include <GeomAdaptor_Surface.hxx>
63 #include <Extrema_ExtPS.hxx>
64 #include <Geom_CartesianPoint.hxx>
65 #include <Geom2d_CartesianPoint.hxx>
66 #include <Geom2dGcc_Circ2d3Tan.hxx>
67 #include <Aspect_Window.hxx>
68
69 #include <TopoDS_Compound.hxx>
70 #include <TopoDS_CompSolid.hxx>
71 #include <HLRAlgo_Projector.hxx>
72 #include <Standard_ErrorHandler.hxx>
73 #include <Font_NameOfFont.hxx>
74
75 static Standard_Integer BUC60842 (Draw_Interpretor& di, Standard_Integer /*argc*/,const char ** /*argv*/)
76 {
77   Standard_Character abuf[16];
78   //Standard_Character * abuf = new Standard_Character[16];
79
80   Standard_CString st = abuf;
81
82   Handle(Geom_Circle) cir = new Geom_Circle (gp_Ax2 (gp_Pnt (823.687192, 502.366825, 478.960440), gp_Dir (0.173648, 0.984808, 0.000000), gp_Dir (-0.932169, 0.164367, -0.322560)), 50);
83   Handle(Geom_Ellipse) ell = new Geom_Ellipse (gp_Ax2 (gp_Pnt (1262.224429, 425.040878, 363.609716), gp_Dir (0.173648, 0.984808, 0.000000), gp_Dir (-0.932169, 0.164367, -0.322560)), 150, 100);
84   Handle(Geom_Plane) plne = new Geom_Plane (gp_Ax3 (gp_Ax2 (gp_Pnt (1262.224429, 425.040878, 363.609716), gp_Dir (0.173648, 0.984808, 0.000000), gp_Dir (-0.932169, 0.164367, -0.322560))));
85
86   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
87
88   gp_Pln pln = plne->Pln();
89   Handle(Geom2d_Curve) curve2d = GeomAPI::To2d (ell, pln);
90   Sprintf(abuf,"ell");
91   DrawTrSurf::Set(st,curve2d);
92   if(!aContext.IsNull()) {
93     Handle(AIS_Shape) aisp = 
94       new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(curve2d, pln)).Edge());
95     aContext->Display (aisp, Standard_False);
96   }
97
98   Handle(Geom2d_Curve) fromcurve2d = GeomAPI::To2d (cir, pln);
99   Sprintf(abuf,"cil");
100   DrawTrSurf::Set(st,fromcurve2d);
101   if(!aContext.IsNull()) {
102     Handle(AIS_Shape) aisp = 
103       new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(fromcurve2d, pln)).Edge());
104     aContext->Display (aisp, Standard_False);
105   }
106
107   Geom2dAdaptor_Curve acur (curve2d), afromcur (fromcurve2d);
108   Geom2dGcc_QualifiedCurve qcur (acur, GccEnt_outside);
109   Geom2dGcc_QualifiedCurve qfromcur (afromcur, GccEnt_outside);
110   Geom2dGcc_Lin2d2Tan lintan (qcur, qfromcur, 0.1, 0.0, 0.0);
111   Standard_Integer i=0;
112   for(i=0;i<lintan.NbSolutions();i++) {
113     Sprintf(abuf,"lintan_%d",i);
114     Handle(Geom2d_Line) glin = new Geom2d_Line(lintan.ThisSolution(i));
115     DrawTrSurf::Set(st,glin);
116     if(!aContext.IsNull()) {
117       Handle(AIS_Shape) aisp = 
118         new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(glin, pln)).Edge());
119       aContext->Display (aisp, Standard_False);
120     }
121   }
122   aContext->UpdateCurrentViewer();
123   di << " Is Done = \n" << (Standard_Integer) lintan.IsDone();
124   return 0;
125 }
126
127 static Standard_Integer BUC60843 (Draw_Interpretor& di, Standard_Integer argc,const char ** argv)
128 {
129   if (argc != 4 && argc != 5 && argc != 6 && argc != 7) {
130     di << "Usage : " << argv[0] << "result_shape name_of_circle name_of_curve [ par1 [ tol ] ]\n";
131     return 1;
132   }
133
134   Standard_Real par1 = 0.0, par2 = 0.0;
135   Standard_Real tol  = Precision::Angular();
136   if (argc >= 5)
137     par1 = Draw::Atof(argv[4]);
138   if (argc == 6)
139     par2 = Draw::Atof(argv[5]);
140   if (argc == 7)
141     tol = Draw::Atof(argv[6]);
142   Handle(Geom2d_Curve)  aCur2d1 = DrawTrSurf::GetCurve2d(argv[2]);
143   Handle(Geom2d_Curve)  aCur2d2 = DrawTrSurf::GetCurve2d(argv[3]);
144   if (aCur2d1.IsNull() || aCur2d2.IsNull()) {
145     di << "Faulty : not curves\n";
146     return 1;
147   }
148   Standard_Boolean c1IsCircle = Standard_False;
149   Handle(Geom2d_Circle) aCir2d = Handle(Geom2d_Circle)::DownCast(aCur2d1);
150   if (!aCir2d.IsNull()) {
151     c1IsCircle = Standard_True;
152     if (argc == 6) tol = Draw::Atof(argv[5]);
153   }
154   if(c1IsCircle) {
155     Geom2dAdaptor_Curve acur(aCur2d2);
156     Geom2dGcc_QCurve qcur(acur, GccEnt_unqualified);
157     GccEnt_QualifiedCirc qfromcur(aCir2d->Circ2d(), GccEnt_unqualified);
158     Geom2dGcc_Lin2d2TanIter lintan(qfromcur, qcur , par1, tol); 
159     if (lintan.IsDone()) {
160       gp_Lin2d lin = lintan.ThisSolution(); 
161       Handle(Geom2d_Line) glin = new Geom2d_Line(lin); 
162       DrawTrSurf::Set(argv[1], glin);
163     } else {
164       di << "Faulty : tangent line is not done.\n";
165     }
166   }
167   else {
168     Geom2dAdaptor_Curve acur1(aCur2d1);
169     Geom2dAdaptor_Curve acur2(aCur2d2);
170     Geom2dGcc_QCurve qcur1(acur1, GccEnt_unqualified);
171     Geom2dGcc_QCurve qcur2(acur2, GccEnt_unqualified);
172     Geom2dGcc_Lin2d2TanIter lintan(qcur1, qcur2 , par1, par2, tol); 
173     if (lintan.IsDone()) {
174       gp_Lin2d lin = lintan.ThisSolution(); 
175       Handle(Geom2d_Line) glin = new Geom2d_Line(lin); 
176       DrawTrSurf::Set(argv[1], glin);
177     } else {
178       di << "Faulty : tangent line is not done.\n";
179     }
180   }   
181   return 0;
182 }
183
184 static Standard_Integer BUC60970 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
185 {
186   if(argc!=3)
187   {
188     di << "Usage : " << argv[0] << " shape result\n";
189     return 1;
190   }
191
192   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
193   if(aContext.IsNull()) {
194     di << "use 'vinit' command before " << argv[0] << "\n";
195     return 1;
196   }
197
198   TopoDS_Shape aResult = DBRep::Get(argv[1]);
199
200   BRepBuilderAPI_MakeWire bRepSpineWire; 
201   TopExp_Explorer exp_(aResult, TopAbs_WIRE); 
202
203   Standard_Integer i = 0; 
204   for(;exp_.More();exp_.Next()){     
205     i++; 
206     bRepSpineWire.Add(TopoDS::Wire(exp_.Current())); 
207   } 
208
209   //printf("total no of wires are ............. %d\n", i); 
210   di << "total no of wires are ............. " << i << "\n"; 
211
212   TopoDS_Wire spineWire = bRepSpineWire.Wire(); 
213   aContext->Display (new AIS_Shape(spineWire), Standard_False);
214
215   DBRep::Set("slineW",spineWire);
216
217   TopExp_Explorer spineWireExp(spineWire, TopAbs_EDGE); 
218   Standard_Real first, last; 
219   Handle(Geom_Curve) curl_ = BRep_Tool::Curve(TopoDS::Edge(spineWireExp.Current()), first, last); 
220   gp_Pnt firstPnt; 
221   gp_Vec tanVec; 
222   curl_->D1(first, firstPnt, tanVec); 
223   tanVec.Normalize(); 
224   gp_Dir tanDir(tanVec.X(), tanVec.Y(), tanVec.Z()); 
225   gp_Ax2 gpAx2(firstPnt, tanDir); 
226   gp_Circ gpCirc(gpAx2, 2.5); 
227   BRepBuilderAPI_MakeWire aMWire(BRepBuilderAPI_MakeEdge(new Geom_Circle(gpCirc)).Edge());
228   TopoDS_Wire topoWire(aMWire); 
229   aContext->Display (new AIS_Shape(topoWire), Standard_False);
230
231   DBRep::Set("topoW",topoWire);
232
233   BRepOffsetAPI_MakePipeShell bRepPipe(spineWire);
234   bRepPipe.Add(topoWire); 
235   bRepPipe.Build(); 
236
237   aContext->Display (new AIS_Shape(bRepPipe.Shape()), Standard_True);
238
239   DBRep::Set(argv[2],bRepPipe.Shape());
240
241   return 0;
242 }
243
244 static Standard_Integer BUC60915_1(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
245 {
246   if (argc > 1) {
247     di<<"Function don't has parameters\n";
248     return 1;
249   }
250
251   if(ViewerTest::GetAISContext().IsNull())
252   {
253     di << "View was not created. Use 'vinit' command before "
254        << argv[0] << "\n";
255     return 1;
256   }
257   Handle(AIS_InteractiveContext) context = ViewerTest::GetAISContext();
258
259   //The following dimesion code has problems regarding arrow_size. The desired effect is not produced.
260   /***************************************/
261   //First view
262   /***************************************/
263   gp_Pnt p1 = gp_Pnt(602.51,50.,0.);
264   gp_Pnt p2 = gp_Pnt(602.51,200.,0.);
265   gp_Pnt p3 = gp_Pnt(102.51,200.,0.);
266   gp_Pnt p4 = gp_Pnt(102.51,170.,0.);
267   gp_Pnt p5 = gp_Pnt(502.51,170.,0.);
268   gp_Pnt p6 = gp_Pnt(502.51,80.,0.);
269   gp_Pnt p7 = gp_Pnt(102.51,80.,0.);
270   gp_Pnt p8 = gp_Pnt(102.51,50.,0.);
271   TopoDS_Vertex V1 = BRepBuilderAPI_MakeVertex(p1);
272   TopoDS_Vertex V2 = BRepBuilderAPI_MakeVertex(p2);
273   TopoDS_Vertex V3 = BRepBuilderAPI_MakeVertex(p3);
274   TopoDS_Vertex V4 = BRepBuilderAPI_MakeVertex(p4);
275   TopoDS_Vertex V5 = BRepBuilderAPI_MakeVertex(p5);
276   TopoDS_Vertex V6 = BRepBuilderAPI_MakeVertex(p6);
277   TopoDS_Vertex V7 = BRepBuilderAPI_MakeVertex(p7);
278   TopoDS_Vertex V8 = BRepBuilderAPI_MakeVertex(p8);
279   gp_Pnt plnpt(0, 0, 0);
280   gp_Dir plndir(0, 0, 1);
281   Handle(Geom_Plane) pln = new Geom_Plane(plnpt,plndir);
282   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
283   anAspect->MakeArrows3d (Standard_True);
284   anAspect->ArrowAspect()->SetAngle (M_PI/180.0 * 10.0);
285   /***************************************/
286   //dimension "L 502.51"
287   /***************************************/
288   Handle(PrsDim_LengthDimension) len = new PrsDim_LengthDimension(V2, V3, pln->Pln());
289   anAspect->ArrowAspect()->SetLength (30.0);
290   len->SetDimensionAspect (anAspect);
291   context->Display (len, Standard_False);
292   /***************************************/
293   //dimension "L 90"
294   /***************************************/
295   Handle(PrsDim_LengthDimension) len1 = new PrsDim_LengthDimension(V7, V4, pln->Pln());
296   len1->SetDimensionAspect (anAspect);
297   len1->SetFlyout (30.0);
298   anAspect->ArrowAspect()->SetLength (100.0);
299   context->Display (len1, Standard_False);
300   /***************************************/
301   //dimension "L 150"
302   /***************************************/
303   Handle(PrsDim_LengthDimension) len2 = new PrsDim_LengthDimension(V1, V2, pln->Pln());
304   len2->SetDimensionAspect (anAspect);
305   context->Display (len2, Standard_False);
306   /***************************************/
307   //dimension "R 88.58"
308   /***************************************/
309   gp_Circ cir = gp_Circ(gp_Ax2(gp_Pnt(191.09, -88.58, 0), gp_Dir(0, 0, 1)), 88.58);
310   TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(cir,gp_Pnt(191.09,0,0.),gp_Pnt(191.09,-177.16,0.) );
311   Handle(PrsDim_RadiusDimension) dim1 = new PrsDim_RadiusDimension(E1);
312   dim1->SetDimensionAspect (anAspect);
313   context->Display (dim1, Standard_False);
314   /***************************************/
315   //dimension "R 43.80"
316   /***************************************/
317   gp_Circ cir1 = gp_Circ(gp_Ax2(gp_Pnt(191.09, -88.58, 0), gp_Dir(0, 0, 1)), 43.80);
318   TopoDS_Edge E_cir1 = BRepBuilderAPI_MakeEdge(cir1);
319   dim1 = new PrsDim_RadiusDimension(E_cir1);
320   anAspect->ArrowAspect()->SetLength (60.0);
321   dim1->SetDimensionAspect (anAspect);
322   context->Display (dim1, Standard_False);
323   /***************************************/
324   //dimension "R 17.86"
325   /***************************************/
326   gp_Circ cir2 = gp_Circ(gp_Ax2(gp_Pnt(566.11, -88.58, 0), gp_Dir(0, 0, -1)), 17.86);
327   TopoDS_Edge E_cir2 = BRepBuilderAPI_MakeEdge(cir2);
328   dim1 = new PrsDim_RadiusDimension(E_cir2);
329   anAspect->ArrowAspect()->SetLength (40.0);
330   dim1->SetDimensionAspect (anAspect);
331   context->Display (dim1, Standard_True);
332
333   return 0;
334 }
335
336 static Standard_Integer  OCC138 (Draw_Interpretor& di, Standard_Integer /*argc*/, const char ** argv)
337 {
338   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
339   if(aContext.IsNull()) { 
340     di << "use 'vinit' command before " << argv[0] << "\n";
341     return 1;
342   }
343
344   BRepPrimAPI_MakeBox box1(gp_Pnt(0, 0, 0),  gp_Pnt(100, 100, 100));
345   BRepPrimAPI_MakeBox box2(gp_Pnt(120, 120, 120),  gp_Pnt(300, 300,300));
346   BRepPrimAPI_MakeBox box3(gp_Pnt(320, 320, 320),  gp_Pnt(500, 500,500));
347
348   Handle(AIS_InteractiveObject) ais1 = new AIS_Shape(box1.Shape());
349   Handle(AIS_InteractiveObject) ais2 = new AIS_Shape(box2.Shape());
350   Handle(AIS_InteractiveObject) ais3 = new AIS_Shape(box3.Shape());
351
352   aContext->Display (ais1, Standard_False);
353   aContext->Display (ais2, Standard_False);
354   aContext->Display (ais3, Standard_False);
355
356   aContext->AddOrRemoveSelected (ais1, Standard_False);
357   aContext->AddOrRemoveSelected (ais2, Standard_False);
358   aContext->AddOrRemoveSelected (ais3, Standard_False);
359
360   di << "\n No of currents = " << aContext->NbSelected();
361
362   aContext->InitSelected();
363
364   int count = 1;
365   while(aContext->MoreSelected())
366   {
367     di << "\n count is = " << count++;
368     Handle(AIS_InteractiveObject) ais = aContext->SelectedInteractive();
369     aContext->AddOrRemoveSelected (ais, Standard_False);
370     aContext->InitSelected();
371   }
372
373   aContext->UpdateCurrentViewer();
374
375   return 0; 
376 }
377
378 static int geom_get_2Dpt_from_3Dpt(const gp_Pnt& pnt3d, const gp_Pln& pln, gp_Pnt2d& pnt2d)
379
380   int ret = 0; 
381   Handle(Geom_Plane) gpln = new Geom_Plane(pln); 
382   GeomAdaptor_Surface adsur(gpln); 
383   Extrema_ExtPS extps(pnt3d, adsur, 0.001, 0.001); 
384   if( extps.IsDone() ) 
385   { 
386     int index;
387     for(index=1; index<= extps.NbExt(); index++ ) 
388     { 
389       Extrema_POnSurf pons = extps.Point(index); 
390       Standard_Real U, V; 
391       pons.Parameter(U, V); 
392       pnt2d.SetCoord(U, V); 
393       ret = 1; 
394     } 
395   } 
396   return ret;
397
398
399 static Standard_Integer OCC353 (Draw_Interpretor& di, Standard_Integer , const char ** )
400 {
401   gp_Ax2 ax21( gp_Pnt(100,0,0), gp_Dir(0,0,1) );
402   Handle(Geom_Circle) h_cir1 = new Geom_Circle( ax21, 25 );
403
404   gp_Ax2 ax22( gp_Pnt(-100,0,0), gp_Dir(0,0,1) );
405   Handle(Geom_Circle) h_cir2 = new Geom_Circle( ax22, 25 );
406
407   gp_Pln refpln( gp_Pnt(0,0,0), gp_Dir(0,0,1) );
408   Handle(Geom2d_Curve) cir2d1 = GeomAPI::To2d(h_cir1, refpln);
409   Handle(Geom2d_Curve) cir2d2 = GeomAPI::To2d(h_cir2, refpln);
410
411   Geom2dAdaptor_Curve adop1(cir2d1);
412   Geom2dAdaptor_Curve adop2(cir2d2);
413
414   Geom2dGcc_QualifiedCurve qcur1(adop1, GccEnt_enclosing);
415   Geom2dGcc_QualifiedCurve qcur2(adop2, GccEnt_enclosing);
416
417   Handle(Geom_CartesianPoint) h_carpt = new Geom_CartesianPoint(0,175,0);
418
419   gp_Pnt pt3d = h_carpt->Pnt();
420   gp_Pnt2d pt2d;
421   geom_get_2Dpt_from_3Dpt(pt3d, refpln, pt2d);
422
423   Standard_CString st;
424   st = "cir2d1";
425   DrawTrSurf::Set(st,cir2d1);
426   st = "cir2d2";
427   DrawTrSurf::Set(st,cir2d2);
428   st = "pt2d";
429   DrawTrSurf::Set(st,pt2d);
430
431   Handle(Geom2d_CartesianPoint) pt = new Geom2d_CartesianPoint(pt2d);
432   Geom2dGcc_Circ2d3Tan sol( qcur1, qcur2, pt, 0.001, 0.0, 0.0);
433
434   int res = 0;
435   Standard_Character buf[10];
436   buf[0] = '\0';
437
438   if( sol.IsDone() ) {
439     res = sol.NbSolutions();
440     for(Standard_Integer i=1;i<=res;i++) {
441       Handle(Geom2d_Circle) aC = new Geom2d_Circle(sol.ThisSolution(i));
442       Sprintf(buf,"Result_%d",i);
443       st = buf;
444       DrawTrSurf::Set(st,aC);
445     }
446   }
447   else
448     di << "\n Faulty: no solutions found ";
449   //printf("\n Faulty: no solutions found ");
450
451   //printf("\n Num of solutions are %d ", res );
452   di << "\n Num of solutions are " << res << " ";
453
454   return 0;
455 }
456
457 static Standard_Integer OCC280 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
458 {
459   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
460   if(aContext.IsNull()) 
461   { 
462     di << "use 'vinit' command before " << argv[0] << "\n";
463     return -1;
464   }
465   if ( argc != 3) {
466     di << "ERROR : Usage : " << argv[0] << " hlr=0/1 setsurfecedetail=0/1; set perspecrive view\n";
467     return 1;
468   }
469
470   Standard_Integer HLR = Draw::Atoi(argv[1]);
471   if (HLR != 0) {
472     HLR = 1;
473   }
474
475   TCollection_AsciiString anOldName = ViewerTest::GetCurrentViewName();
476   Handle(V3d_Viewer) aViewer = ViewerTest::GetViewerFromContext();
477   aViewer->SetDefaultTypeOfView (V3d_PERSPECTIVE);
478   Handle(Aspect_Window) asp = ViewerTest::CurrentView()->Window();
479   Handle(V3d_View) aNewView = aViewer->CreateView();
480   ViewerTest::CurrentView (aNewView);
481   TCollection_AsciiString aNewName=anOldName + "_new";
482   ViewerTest::InitViewName(aNewName,ViewerTest::CurrentView());
483   aNewView->SetWindow (asp);
484   if (!asp->IsMapped()) asp->Map();
485   aNewView->Redraw();
486   ViewerTest::RemoveView(anOldName,false);
487   ViewerTest::UnsetEventManager();
488   ViewerTest::SetEventManager (new ViewerTest_EventManager (aNewView, ViewerTest::GetAISContext()));
489
490   if (HLR == 1)
491   {
492     di << "HLR\n";
493     ViewerTest::CurrentView()->SetComputedMode (Standard_True);
494   }
495
496   return 0;
497 }
498
499 static Standard_Integer  OCC138LC (Draw_Interpretor& di, Standard_Integer /*argc*/, const char ** argv)
500 {
501   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
502   if(aContext.IsNull()) { 
503     di << "use 'vinit' command before " << argv[0] << "\n";
504     return 1;
505   }
506
507   BRepPrimAPI_MakeBox box1(gp_Pnt(0, 0, 0),  gp_Pnt(100, 100, 100));
508   BRepPrimAPI_MakeBox box2(gp_Pnt(120, 120, 120),  gp_Pnt(300, 300,300));
509   BRepPrimAPI_MakeBox box3(gp_Pnt(320, 320, 320),  gp_Pnt(500, 500,500));
510
511   Handle(AIS_InteractiveObject) ais1 = new AIS_Shape(box1.Shape());
512   Handle(AIS_InteractiveObject) ais2 = new AIS_Shape(box2.Shape());
513   Handle(AIS_InteractiveObject) ais3 = new AIS_Shape(box3.Shape());
514
515   aContext->Display (ais1, Standard_False);
516   aContext->Display (ais2, Standard_False);
517   aContext->Display (ais3, Standard_False);
518
519   aContext->AddOrRemoveSelected (ais1, Standard_False);
520   aContext->AddOrRemoveSelected (ais2, Standard_False);
521   aContext->AddOrRemoveSelected (ais3, Standard_False);
522
523   di << "\n No of selected = " << aContext->NbSelected();
524
525   aContext->InitSelected();
526
527   int count = 1;
528   while(aContext->MoreSelected())
529   {
530     di << "\n count is = %d" << count++;
531     Handle(AIS_InteractiveObject) ais = aContext->SelectedInteractive();
532     aContext->AddOrRemoveSelected (ais, Standard_False);
533     aContext->InitSelected();
534   }
535
536   aContext->UpdateCurrentViewer();
537
538   return 0; 
539 }
540
541 #include <BRepBndLib.hxx>
542 #include <Draw.hxx>
543 //=======================================================================
544 //function : OCC566
545 //purpose  : 
546 //=======================================================================
547 static Standard_Integer OCC566(Draw_Interpretor& di,Standard_Integer n,const char ** a)
548 {
549   if (n < 2) {
550     di<<"Usage: " << a[0] << " shape [ xmin ymin zmin xmax ymax zmax]\n";
551     return 1;
552   }
553   TopoDS_Shape S = DBRep::Get(a[1]);
554   if (S.IsNull()) return 1;
555   Bnd_Box B;
556   BRepBndLib::AddClose(S,B);
557   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
558   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
559   di << axmin<<" "<< aymin<<" "<< azmin<<" "<< axmax<<" "<< aymax<<" "<< azmax;
560   if (n >= 8) {
561     Draw::Set(a[2],axmin) ;
562     Draw::Set(a[3],aymin) ;
563     Draw::Set(a[4],azmin) ;
564     Draw::Set(a[5],axmax) ;
565     Draw::Set(a[6],aymax) ;
566     Draw::Set(a[7],azmax) ;
567   }
568   return 0;
569 }
570
571 #include <BRepFilletAPI_MakeFillet.hxx>
572 //=======================================================================
573 //function : OCC570
574 //purpose  : 
575 //=======================================================================
576 static Standard_Integer OCC570 (Draw_Interpretor& di, Standard_Integer argc,const char ** argv)
577 {
578   if (argc < 2) {
579     di<<"Usage: " << argv[0] << " result\n";
580     return 1;
581   }
582
583   BRepPrimAPI_MakeBox mkBox(100.,100.,100.);
584   TopoDS_Shape aBox = mkBox.Shape();
585
586   TopExp_Explorer aExp;
587   aExp.Init(aBox,TopAbs_WIRE);
588   if (aExp.More())
589   {
590     TopoDS_Shape aWire = aExp.Current();
591
592     aExp.Init(aWire,TopAbs_EDGE);
593     TopoDS_Edge e1 = TopoDS::Edge(aExp.Current()); aExp.Next();
594     TopoDS_Edge e2 = TopoDS::Edge(aExp.Current()); aExp.Next();
595     TopoDS_Edge e3 = TopoDS::Edge(aExp.Current()); aExp.Next();
596     TopoDS_Edge e4 = TopoDS::Edge(aExp.Current());
597
598     try
599     {
600       OCC_CATCH_SIGNALS
601         BRepFilletAPI_MakeFillet mkFillet(aBox);
602       mkFillet.SetContinuity(GeomAbs_C1,.001);
603
604       // Setup variable fillet data
605       TColgp_Array1OfPnt2d t_pnt(1,4);
606       t_pnt.SetValue(1,gp_Pnt2d(0.0,5.0));
607       t_pnt.SetValue(2,gp_Pnt2d(0.3,15.0));
608       t_pnt.SetValue(3,gp_Pnt2d(0.7,15.0));
609       t_pnt.SetValue(4,gp_Pnt2d(1.0,5.0));
610
611       // HERE:
612       // It is impossible to build fillet if at least one edge
613       // with variable radius is added!!! If all are constant, everything is ok.
614       mkFillet.Add(t_pnt,e1);
615       mkFillet.Add(5.0,e2);
616       mkFillet.Add(t_pnt,e3);
617       mkFillet.Add(5.0,e4);
618
619       mkFillet.Build();
620       TopoDS_Shape aFinalShape = mkFillet.Shape();
621
622       DBRep::Set(argv[1],aFinalShape);
623     }
624     catch(Standard_Failure const&)
625     {
626       di << argv[0] << ": Exception in fillet\n";
627       return 2;
628     }
629   }
630
631   return 0;
632 }
633
634 #include <Law_Interpol.hxx>
635
636 static Standard_Real t3d = 1.e-4;
637 static Standard_Real t2d = 1.e-5;
638 static Standard_Real ta  = 1.e-2;
639 static Standard_Real fl  = 1.e-3;
640 static Standard_Real tapp_angle = 1.e-2;
641 static GeomAbs_Shape blend_cont = GeomAbs_C1;
642
643 static BRepFilletAPI_MakeFillet* Rake = 0;
644 static char name[100];
645
646 static void printtolblend(Draw_Interpretor& di)
647 {
648   di<<"tolerance ang : "<<ta<<"\n";
649   di<<"tolerance 3d  : "<<t3d<<"\n";
650   di<<"tolerance 2d  : "<<t2d<<"\n";
651   di<<"fleche        : "<<fl<<"\n";
652
653   di<<"tolblend "<<ta<<" "<<t3d<<" "<<t2d<<" "<<fl<<"\n";
654 }
655
656 static Standard_Integer MKEVOL(Draw_Interpretor& di, 
657                                Standard_Integer narg, 
658                                const char ** a)
659 {
660   if(Rake != 0) {delete Rake; Rake = 0;}
661   printtolblend(di);
662   if (narg < 3) return 1;
663   TopoDS_Shape V = DBRep::Get(a[2]);
664   Rake = new BRepFilletAPI_MakeFillet(V);
665   Rake->SetParams(ta,t3d,t2d,t3d,t2d,fl);
666   Rake->SetContinuity(blend_cont, tapp_angle);
667   if (narg == 4) {
668     ChFi3d_FilletShape FSh = ChFi3d_Rational;
669     if (!strcasecmp(a[3], "Q")) {
670       FSh = ChFi3d_QuasiAngular;
671     }
672     else if (!strcasecmp(a[3], "P")) {
673       FSh = ChFi3d_Polynomial;
674     }
675     Rake->SetFilletShape(FSh);
676   }
677   strcpy(name, a[1]);
678   return 0;
679 }
680
681 static Standard_Integer UPDATEVOL(Draw_Interpretor& di, 
682                                   Standard_Integer narg, 
683                                   const char ** a)
684 {
685   if(Rake == 0){
686     di << "MakeFillet non initialise\n";
687     return 1 ;
688   }
689   if(narg%2 != 0 || narg < 4) return 1;
690   TColgp_Array1OfPnt2d uandr(1,(narg/2)-1);
691   Standard_Real Rad, Par;
692   TopoDS_Shape aLocalEdge(DBRep::Get(a[1],TopAbs_EDGE));
693   TopoDS_Edge E = TopoDS::Edge(aLocalEdge);
694   for (Standard_Integer ii = 1; ii <= (narg/2)-1; ii++){
695     Par = Draw::Atof(a[2*ii]);
696     Rad = Draw::Atof(a[2*ii + 1]);
697     uandr.ChangeValue(ii).SetCoord(Par,Rad);
698   }
699   //HELPDESK: Add law creation
700   const Standard_Boolean aLawMode = !strcmp(a[0],"OCC570evollaw");
701   if (aLawMode)
702   {
703     di<<"INFO: Using law to define variable fillet\n";
704     Handle(Law_Interpol) law = new Law_Interpol;
705     law->Set(uandr);
706     Rake->Add(law,E);
707   }
708   else
709     Rake->Add(uandr,E);
710
711   return 0;
712 }
713
714 static Standard_Integer BUILDEVOL(Draw_Interpretor& di,
715                                   Standard_Integer, 
716                                   const char **)
717 {
718   if(Rake == 0){
719     di << "MakeFillet non initialise\n";
720     return 1 ;
721   }
722   Rake->Build();
723   if(Rake->IsDone()){
724     TopoDS_Shape result = Rake->Shape();
725     DBRep::Set(name,result);
726     if(Rake != 0) {delete Rake; Rake = 0;}
727     return 0;
728   }
729   if(Rake != 0) {delete Rake; Rake = 0;}
730   return 1;
731 }
732
733 #include <TColGeom_SequenceOfCurve.hxx>
734 #include <Geom_TrimmedCurve.hxx>
735 #include <GeomFill_NSections.hxx>
736 #include <BRepBuilderAPI_MakeFace.hxx>
737 //=======================================================================
738 //function : OCC606
739 //purpose  : 
740 //=======================================================================
741 static Standard_Integer OCC606 ( Draw_Interpretor& di, Standard_Integer n, const char ** a )
742 {
743   if (n != 3 && n != 4)
744   {
745     di << "Usage : " << a[0] << " result shape [-t]\n";
746     return 0;
747   }
748
749   Standard_Boolean TrimMode = (n == 4);
750   if (TrimMode) di<<"INFO: Using trimmed curves...\n";
751   else di<<"INFO: Using non trimmed curves...\n";
752
753   TopoDS_Shape S = DBRep::Get(a[2]);
754
755   TopExp_Explorer t_exp(S,TopAbs_EDGE);
756   TColGeom_SequenceOfCurve n_curves1;
757   TColStd_SequenceOfReal np;
758   Standard_Real param = 5.0;
759
760   for (;t_exp.More();t_exp.Next())
761   {
762     Standard_Real f,l;
763     Handle(Geom_Curve) h_cur = BRep_Tool::Curve(TopoDS::Edge(t_exp.Current()),f,l);
764     if (!h_cur.IsNull())
765     {
766       if (TrimMode) h_cur = new Geom_TrimmedCurve(h_cur,f,l);
767
768       n_curves1.Append(h_cur);
769       np.Append(param);
770       param += 1.0;
771     }
772   }
773
774   if (n_curves1.Length()>1 && !np.IsEmpty())
775   {
776     try
777     {
778       OCC_CATCH_SIGNALS
779         GeomFill_NSections b_surface1(n_curves1, np);
780       Handle(Geom_BSplineSurface) result_surf1 = b_surface1.BSplineSurface();
781       if (!result_surf1.IsNull())
782       {
783         BRepBuilderAPI_MakeFace b_face1(result_surf1, Precision::Confusion());
784         TopoDS_Face bsp_face1 = b_face1.Face();
785         DBRep::Set(a[1],bsp_face1);
786       }
787     }
788     catch(Standard_Failure const&)
789     {
790       di<<"ERROR: Exception in GeomFill_NSections\n";
791     }
792   }
793
794
795   return 0;
796 }
797
798 //=======================================================================
799 //function : OCC813
800 //purpose  : 
801 //=======================================================================
802 static Standard_Integer OCC813 (Draw_Interpretor& di, Standard_Integer argc,const char ** argv)
803 {
804   if (argc < 3)
805   {
806     di << "Usage : " << argv[0] << " U V\n";
807     return 1;
808   }
809
810   Standard_CString str;
811   Standard_Real U = Draw::Atof(argv[1]);
812   Standard_Real V = Draw::Atof(argv[2]);
813
814   //Between ellipse and point:
815
816   Handle(Geom_Ellipse) ell = new Geom_Ellipse(gp_Ax2(gp_Pnt(1262.224429,425.040878,363.609716),
817     gp_Dir(0.173648,0.984808,0.000000),
818     gp_Dir(-0.932169,0.164367,-0.322560)), 150, 100);
819   Handle(Geom_Plane) plne = new Geom_Plane(gp_Ax3(gp_Ax2(gp_Pnt(1262.224429,425.040878,363.609716),
820     gp_Dir(0.173648,0.984808,0.000000),
821     gp_Dir(-0.932169,0.164367,-0.322560))) );
822
823   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
824
825   gp_Pnt2d pt2d(U,V);
826   gp_Pln pln = plne->Pln();
827
828   str = "OCC813_pnt"; DrawTrSurf::Set(str,pt2d);
829
830   Handle(Geom2d_Curve) curve2d = GeomAPI::To2d(ell,pln);
831   Geom2dAdaptor_Curve acur(curve2d);
832   Geom2dGcc_QualifiedCurve qcur(acur, GccEnt_outside);
833
834   str = "OCC813_ell"; DrawTrSurf::Set(str,curve2d);
835   if(!aContext.IsNull()) {
836     Handle(AIS_Shape) aisp = 
837       new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(curve2d, pln)).Edge());
838     aContext->Display (aisp, Standard_False);
839   }
840
841   //This does not give any solutions.
842   Geom2dGcc_Lin2d2Tan lintan(qcur, pt2d, 0.1);
843   di<<"OCC813 nb of solutions = "<<lintan.NbSolutions()<<"\n";
844
845   Standard_Character abuf[16];
846   Standard_CString st = abuf;
847
848   Standard_Integer i;
849   for(i=1;i<=lintan.NbSolutions();i++) {
850     Sprintf(abuf,"lintan_%d",i);
851     Handle(Geom2d_Line) glin = new Geom2d_Line(lintan.ThisSolution(i));
852     DrawTrSurf::Set(st,glin);
853     if(!aContext.IsNull()) {
854       Handle(AIS_Shape) aisp = 
855         new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(glin, pln)).Edge());
856       aContext->Display (aisp, Standard_False);
857     }
858   }
859
860   if (!aContext.IsNull())
861   {
862     aContext->UpdateCurrentViewer();
863   }
864
865   return 0;
866 }
867
868 //=======================================================================
869 //function : OCC814
870 //purpose  : 
871 //=======================================================================
872 static Standard_Integer OCC814 (Draw_Interpretor& di, Standard_Integer argc,const char ** argv)
873 {
874   if (argc > 1)
875   {
876     di << "Usage : " << argv[0] << "\n";
877     return 1;
878   }
879
880   Standard_CString str;
881
882   //Between Ellipse and Circle:
883
884   Handle(Geom_Circle) cir = new Geom_Circle(gp_Ax2(gp_Pnt(823.687192,502.366825,478.960440),
885     gp_Dir(0.173648,0.984808,0.000000),
886     gp_Dir(-0.932169,0.164367,-0.322560)), 50);
887   Handle(Geom_Ellipse) ell = new Geom_Ellipse(gp_Ax2(gp_Pnt(1262.224429,425.040878,363.609716),
888     gp_Dir(0.173648,0.984808,0.000000),
889     gp_Dir(-0.932169,0.164367,-0.322560)), 150, 100);
890   Handle(Geom_Plane) plne = new Geom_Plane(gp_Ax3(gp_Ax2(gp_Pnt(1262.224429,425.040878,363.609716),
891     gp_Dir(0.173648,0.984808,0.000000),
892     gp_Dir(-0.932169,0.164367,-0.322560))) );
893
894   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
895
896   gp_Pln pln = plne->Pln();
897   Handle(Geom2d_Curve) curve2d = GeomAPI::To2d(ell, pln);
898   Handle(Geom2d_Curve) fromcurve2d = GeomAPI::To2d(cir, pln);
899
900   str = "OCC814_cir"; DrawTrSurf::Set(str,curve2d);
901   str = "OCC814_ell"; DrawTrSurf::Set(str,fromcurve2d);
902   if(!aContext.IsNull()) {
903     Handle(AIS_Shape) aisp = 
904       new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(curve2d, pln)).Edge());
905     aContext->Display (aisp, Standard_False);
906   }
907   if(!aContext.IsNull()) {
908     Handle(AIS_Shape) aisp = 
909       new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(fromcurve2d, pln)).Edge());
910     aContext->Display (aisp, Standard_False);
911   }
912
913   Geom2dAdaptor_Curve acur(curve2d), afromcur(fromcurve2d);
914
915   Geom2dGcc_QualifiedCurve qcur(acur, GccEnt_outside) ;
916   Geom2dGcc_QualifiedCurve qfromcur(afromcur, GccEnt_outside) ;
917
918   //This does not give any solutions.
919   Geom2dGcc_Lin2d2Tan lintan(qcur, qfromcur, 0.1);
920   di<<"OCC814 nb of solutions = "<<lintan.NbSolutions()<<"\n";
921
922   Standard_Character abuf[16];
923   Standard_CString st = abuf;
924
925   Standard_Integer i;
926   for(i=1;i<=lintan.NbSolutions();i++) {
927     Sprintf(abuf,"lintan_%d",i);
928     Handle(Geom2d_Line) glin = new Geom2d_Line(lintan.ThisSolution(i));
929     DrawTrSurf::Set(st,glin);
930     if(!aContext.IsNull()) {
931       Handle(AIS_Shape) aisp = 
932         new AIS_Shape (BRepBuilderAPI_MakeEdge(GeomAPI::To3d(glin, pln)).Edge());
933       aContext->Display (aisp, Standard_False);
934     }
935   }
936
937   if (!aContext.IsNull())
938   {
939     aContext->UpdateCurrentViewer();
940   }
941
942   return 0;
943 }
944
945 #include <ShapeAnalysis_Wire.hxx>
946 #include <IntRes2d_SequenceOfIntersectionPoint.hxx>
947 #include <TColgp_SequenceOfPnt.hxx>
948 #include <ShapeFix_Wire.hxx>
949 //=======================================================================
950 //function : OCC884
951 //purpose  : 
952 //=======================================================================
953 static Standard_Integer OCC884 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
954 {
955   if (argc < 3 || argc > 5)
956   {
957     di << "Usage : " << argv[0] << " result shape [toler [maxtoler]]\n";
958     return 1;
959   }
960
961   TopoDS_Shape shape = DBRep::Get(argv[2]); //read the shape
962
963   TopExp_Explorer exp(shape, TopAbs_EDGE);
964   if (!exp.More())
965   {
966     di<<"Error: no edge found\n";
967     return 1;
968   }
969
970   BRep_Builder builder;
971   TopoDS_Wire wire;
972   builder.MakeWire(wire);
973   builder.Add(wire, TopoDS::Edge(exp.Current()));
974
975   // HelpDesk: Create planar face if possible
976   TopoDS_Face face = BRepBuilderAPI_MakeFace(wire,Standard_True);
977
978   Handle(ShapeAnalysis_Wire) advWA = new ShapeAnalysis_Wire;
979   advWA->Load(wire);
980   advWA->SetFace(face);
981   advWA->SetPrecision(0.001);
982
983   IntRes2d_SequenceOfIntersectionPoint points2d;
984   TColgp_SequenceOfPnt points3d;
985
986   di << "Info: CheckSelfIntersectingEdge = " << (Standard_Integer) advWA->CheckSelfIntersectingEdge(1,points2d,points3d) << "\n";
987
988   ShapeExtend_Status status = ShapeExtend_FAIL1;
989   if (advWA->StatusSelfIntersection(status))
990     di << "Info: No P Curve found in the edge\n";
991
992   status = ShapeExtend_FAIL2; 
993   if (advWA->StatusSelfIntersection(status))
994     di << "Info: No Vertices found in the edge\n";
995
996   status = ShapeExtend_DONE1; 
997   if (advWA->StatusSelfIntersection(status))
998     di << "Info: Self-intersection found in the edge\n";
999
1000   Standard_Integer i, num = points2d.Length();
1001   di << "Info: No. of self-intersection points : " << num << "\n";
1002
1003   char str[80];
1004   Standard_CString aName = str;
1005   for (i = 1; i <= num; ++i)
1006   { 
1007     gp_Pnt pt = points3d(i); 
1008     di << "Info: Intersecting pt : (" << pt.X() << ", " << pt.Y() << ", " << pt.Z() << ")\n";
1009     Sprintf(str,"p_%d",i);
1010     DrawTrSurf::Set(aName,pt);
1011   }
1012
1013   Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1014   sfw->Load(wire);
1015   sfw->SetFace(face);
1016
1017   if (argc > 3) sfw->SetPrecision(Draw::Atof(argv[3])/*0.1*/);
1018   if (argc > 4) sfw->SetMaxTolerance(Draw::Atof(argv[4]));
1019   di << "Info: Precision is set to " << sfw->Precision() << "\n";
1020   di << "Info: MaxTolerance is set to " << sfw->MaxTolerance() << "\n";
1021
1022   sfw->ModifyTopologyMode() = 1;
1023   sfw->ModifyGeometryMode() = 1; 
1024   sfw->FixSelfIntersectingEdgeMode() = 1;
1025   //printf("Info: FixSelfIntersection = %d\n",sfw->FixSelfIntersection());
1026   di << "Info: FixSelfIntersection = " << (Standard_Integer) sfw->FixSelfIntersection() << "\n";
1027
1028   DBRep::Set(argv[1],sfw->Wire());
1029
1030   status = ShapeExtend_OK;
1031   if (sfw->StatusSelfIntersection(status))
1032     di << "Info: ShapeExtend_OK : No intersection found\n";
1033   //printf("Info: ShapeExtend_OK : No intersection found\n");
1034
1035   status = ShapeExtend_FAIL1;
1036   if (sfw->StatusSelfIntersection(status))
1037     di << "Info: ShapeExtend_FAIL1 : analysis failed (edge has no pcurve, or no vertices etc.)\n";
1038
1039   status = ShapeExtend_FAIL2;
1040   if (sfw->StatusSelfIntersection(status))
1041     di << "Info: ShapeExtend_FAIL2 : self-intersection was found, but not fixed because of limit of increasing tolerance (MaxTolerance)\n";
1042
1043   status = ShapeExtend_FAIL3;
1044   if (sfw->StatusSelfIntersection(status))
1045     di << "Info: ShapeExtend_FAIL3 : intercestion of non adjacent edges found, but not fixed because of limit of increasing tolerance (MaxTolerance)\n";
1046
1047   status = ShapeExtend_DONE1;
1048   if (sfw->StatusSelfIntersection(status))
1049     di << "Info: ShapeExtend_DONE1 : tolerance of vertex was increased to fix self-intersection\n";
1050
1051   status = ShapeExtend_DONE2;
1052   if (sfw->StatusSelfIntersection(status))
1053     di << "Info: ShapeExtend_DONE2 : vertex was moved to fix self-intersection\n";
1054
1055   status = ShapeExtend_DONE3;
1056   if (sfw->StatusSelfIntersection(status))
1057     di << "Info: ShapeExtend_DONE3 : some edges were removed because of intersection\n";
1058
1059   status = ShapeExtend_DONE4;
1060   if (sfw->StatusSelfIntersection(status))
1061     di << "Info: ShapeExtend_DONE4 : pcurve(s) was(were) modified\n";
1062
1063   status = ShapeExtend_DONE5;
1064   if (sfw->StatusSelfIntersection(status))
1065     di << "Info: ShapeExtend_DONE5 : non adjacent intersection fixed by increasing tolerance of vertex(vertices)\n";
1066
1067   status = ShapeExtend_DONE6;
1068   if (sfw->StatusSelfIntersection(status))
1069     di << "Info: ShapeExtend_DONE6 : tolerance of edge was increased to hide intersection\n";
1070
1071   status = ShapeExtend_DONE7;
1072   if (sfw->StatusSelfIntersection(status))
1073     di << "Info: ShapeExtend_DONE7 : range of some edges was decreased to avoid intersection\n";
1074
1075   return 0;
1076 }
1077
1078 #include <Graphic3d_MaterialAspect.hxx>
1079 #include <Prs3d_Drawer.hxx>
1080 #include <Prs3d_ShadingAspect.hxx>
1081 //=======================================================================
1082 //function : OCC1174_1
1083 //purpose  : 
1084 //=======================================================================
1085 static Standard_Integer OCC1174_1 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1086 {
1087   if (argc != 2)
1088   {
1089     di << "Usage : " << argv[0] << " shape\n";
1090     return 1;
1091   }
1092
1093   Handle(AIS_InteractiveContext) anAISContext = ViewerTest::GetAISContext();
1094   if(anAISContext.IsNull())
1095   {
1096     di << "use 'vinit' command before " << argv[0] << "\n";
1097     return 1;
1098   }
1099
1100   TopoDS_Shape aShape = DBRep::Get(argv[1]);
1101
1102   Handle(AIS_Shape) anAisIO = new AIS_Shape(aShape);
1103
1104   Quantity_Color aColF(0.0, 0.4, 0.0, Quantity_TOC_sRGB);
1105   Quantity_Color aColB(0.0, 0.0, 0.6, Quantity_TOC_sRGB);
1106
1107   Handle(Prs3d_Drawer) aDrawer = anAisIO->Attributes();
1108   Handle(Prs3d_ShadingAspect) aShadingAspect = aDrawer->ShadingAspect();
1109
1110   Graphic3d_MaterialAspect aFront = aShadingAspect->Material(Aspect_TOFM_FRONT_SIDE);
1111   aFront.SetAmbientColor(aColF);
1112   aFront.SetDiffuseColor(aColF);
1113   aFront.SetSpecularColor(aColF);
1114   aFront.SetEmissiveColor(Quantity_NOC_BLACK);
1115   aFront.SetTransparency(0.0);
1116   aShadingAspect->SetMaterial(aFront,Aspect_TOFM_FRONT_SIDE);
1117
1118   Graphic3d_MaterialAspect aBack = aShadingAspect->Material(Aspect_TOFM_BACK_SIDE);
1119   aBack.SetAmbientColor(aColB);
1120   aBack.SetDiffuseColor(aColB);
1121   aBack.SetSpecularColor(aColB);
1122   aBack.SetEmissiveColor(Quantity_NOC_BLACK);
1123   aBack.SetTransparency(0.0);
1124   aShadingAspect->SetMaterial(aBack,Aspect_TOFM_BACK_SIDE);
1125
1126   aDrawer->SetShadingAspect (aShadingAspect);
1127
1128   anAISContext->Display (anAisIO, 1, 0, Standard_True);
1129
1130   Standard_Real r, g, b; 
1131   aShadingAspect->Color(Aspect_TOFM_FRONT_SIDE).Values(r,g,b, Quantity_TOC_sRGB);
1132   di << "Info: color on front side (" << r << "," << g << "," << b << ")\n";
1133   aShadingAspect->Color(Aspect_TOFM_BACK_SIDE).Values(r,g,b, Quantity_TOC_sRGB);
1134   di << "Info: color on back side (" << r << "," << g << "," << b << ")\n";
1135
1136   return 0;
1137 }
1138
1139 //=======================================================================
1140 //function : OCC1174_2
1141 //purpose  : 
1142 //=======================================================================
1143 static Standard_Integer OCC1174_2 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1144 {
1145   if (argc != 2)
1146   {
1147     di << "Usage : " << argv[0] << " shape\n";
1148     return 1;
1149   }
1150
1151   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1152   if(AISContext.IsNull()) 
1153   {
1154     di << "use 'vinit' command before " << argv[0] << "\n";
1155     return 1;
1156   }
1157
1158   TopoDS_Shape sh = DBRep::Get(argv[1]);
1159
1160   Handle(AIS_Shape) ais = new AIS_Shape(sh); 
1161   AISContext->Display (ais, 1, 0, Standard_False);
1162   AISContext->SetMaterial (ais, Graphic3d_NOM_SHINY_PLASTIC, Standard_False);
1163
1164   Quantity_Color colf(0.0, 0.4, 0.0, Quantity_TOC_sRGB);
1165   Quantity_Color colb(0.0, 0.0, 0.6, Quantity_TOC_sRGB);
1166   Handle(Prs3d_ShadingAspect) sa = ais->Attributes()->ShadingAspect(); 
1167
1168   Graphic3d_MaterialAspect front = sa->Material(Aspect_TOFM_FRONT_SIDE); 
1169   front.SetAmbientColor(colf); 
1170   front.SetDiffuseColor(colf); 
1171   front.SetSpecularColor(colf); 
1172   front.SetEmissiveColor(Quantity_NOC_BLACK);
1173   front.SetTransparency (0.4f);
1174   sa->SetMaterial(front,Aspect_TOFM_FRONT_SIDE); 
1175
1176   Graphic3d_MaterialAspect back = sa->Material(Aspect_TOFM_BACK_SIDE); 
1177   back.SetAmbientColor(colb); 
1178   back.SetDiffuseColor(colb); 
1179   back.SetSpecularColor(colb); 
1180   back.SetEmissiveColor(Quantity_NOC_BLACK);
1181   back.SetTransparency (0.2f);
1182   sa->SetMaterial(back,Aspect_TOFM_BACK_SIDE); 
1183
1184   AISContext->Redisplay (ais, 1, 0);
1185
1186   return 0;
1187 }
1188
1189 #include <TopoDS_Solid.hxx>
1190 #include <BRepFeat_MakeDPrism.hxx>
1191 //=======================================================================
1192 //function : OCCN1
1193 //purpose  : FEATURE OPERATION (BOSS and SLOT)
1194 //=======================================================================
1195 static Standard_Integer OCCN1 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1196 {
1197   if (argc != 4)
1198   {
1199     di << "Usage : " << argv[0] << " angle fuse(1 for boss / 0 for slot) length\n";
1200     return 1;
1201   }
1202
1203   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
1204   if(aContext.IsNull()) 
1205   {
1206     di << "use 'vinit' command before " << argv[0] << "\n";
1207     return 1;
1208   }
1209   Standard_Real    angle  = Draw::Atof(argv[1]);
1210   Standard_Integer fuse   = Draw::Atoi(argv[2]);
1211   Standard_Real    length = Draw::Atof(argv[3]);
1212
1213   BRepBuilderAPI_MakeEdge edge1(gp_Pnt(0, 0, 0), gp_Pnt(50, 0, 0));
1214   BRepBuilderAPI_MakeEdge edge2(gp_Pnt(50, 0, 0), gp_Pnt(50, 50, 0));
1215   BRepBuilderAPI_MakeEdge edge3(gp_Pnt(50, 50, 0), gp_Pnt(0, 50, 0));
1216   BRepBuilderAPI_MakeEdge edge4(gp_Pnt(0, 50, 0), gp_Pnt(0, 0, 0));
1217   TopoDS_Edge ted1 = edge1.Edge();
1218   TopoDS_Edge ted2 = edge2.Edge();
1219   TopoDS_Edge ted3 = edge3.Edge();
1220   TopoDS_Edge ted4 = edge4.Edge();
1221
1222   BRepBuilderAPI_MakeWire wire(ted1, ted2, ted3, ted4);
1223   TopoDS_Wire twire = wire.Wire();
1224
1225   BRepBuilderAPI_MakeFace face(twire);
1226   TopoDS_Face tface = face.Face();
1227   ////////Handle(AIS_Shape) face_ais = new AIS_Shape( tface );
1228   ////////aContext->Display(face_ais);
1229
1230   TopoDS_Solid box;
1231   BRepPrimAPI_MakeBox block(gp_Pnt(-50, -50, -150), 150, 150, 100);
1232   box = block.Solid();
1233
1234   TopoDS_Face top_face;
1235   top_face = block.TopFace();
1236
1237   BRepFeat_MakeDPrism slot(box, tface, top_face, angle, fuse,
1238     Standard_True);
1239
1240   slot.Perform(length);
1241   TopoDS_Shape S2 = slot.Shape();
1242
1243   DBRep::Set("OCCN1_tface",tface);
1244   DBRep::Set("OCCN1_slot",S2);
1245
1246   return 0;
1247 }
1248
1249 #include <BRepPrimAPI_MakeCylinder.hxx>
1250 #include <BRepPrimAPI_MakeSphere.hxx>
1251 #include <BRepAlgoAPI_Section.hxx>
1252 //=======================================================================
1253 //function : OCCN2
1254 //purpose  : BOOLEAN OPERATION
1255 //=======================================================================
1256 static Standard_Integer OCCN2 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1257 {
1258   if (argc > 2) {
1259     di << "Usage : " << argv[0] << "\n";
1260     return 1;
1261   }
1262
1263   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
1264   if(aContext.IsNull()) 
1265   {
1266     di << "use 'vinit' command before " << argv[0] << "\n";
1267     return 1;
1268   }
1269
1270   BRepPrimAPI_MakeCylinder cylinder(50,200);
1271   TopoDS_Shape cylinder_sh = cylinder.Shape();
1272
1273   BRepPrimAPI_MakeSphere sphere(gp_Pnt(60,0,100),50);
1274   TopoDS_Shape sphere_sh = sphere.Shape();
1275
1276   di << "BRepAlgoAPI_Section section(cylinder_sh, sphere_sh)\n";
1277   BRepAlgoAPI_Section section(cylinder_sh, sphere_sh);
1278   if (!section.IsDone()){
1279     di << "Error performing intersection: not done.\n";
1280   }
1281   const TopoDS_Shape& shape = section.Shape();
1282
1283   DBRep::Set("OCCN2_cylinder",cylinder_sh);
1284   DBRep::Set("OCCN2_sphere",sphere_sh);
1285   DBRep::Set("OCCN2_section",shape);
1286
1287   return 0;
1288 }
1289
1290 #include <TColgp_Array1OfPnt.hxx>
1291 #include <Geom_BezierCurve.hxx>
1292
1293 static Standard_Integer OCC2569 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1294 {
1295   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
1296   if(aContext.IsNull()) 
1297   {
1298     di << "use 'vinit' command before " << argv[0] << "\n";
1299     return 1;
1300   }
1301   if(argc != 3) {
1302     di << "Usage : " << argv[0] << " nbpoles result\n";
1303     return 1;
1304   }
1305
1306   int poles=Draw::Atoi(argv[1]); 
1307
1308   TColgp_Array1OfPnt arr(1, poles); 
1309   for(int i=1; i<=poles; i++) 
1310     arr.SetValue(i, gp_Pnt(i+10, i*2+20, i*3+45)); 
1311
1312   Handle(Geom_BezierCurve) bez = new Geom_BezierCurve(arr); 
1313   if(bez.IsNull()) {
1314     di << "\n The curve is not created.\n";
1315   } else {
1316     di << "\n Degree = " << bez->Degree() << "\n";   
1317   }
1318   TopoDS_Edge sh = BRepBuilderAPI_MakeEdge(bez).Edge(); 
1319   Handle(AIS_Shape) ais = new AIS_Shape(sh); 
1320   aContext->Display (ais, Standard_True);
1321   DrawTrSurf::Set(argv[2],bez);
1322   return 0;
1323 }
1324
1325 #include <TopTools_IndexedMapOfShape.hxx>
1326 #include <TopExp.hxx>
1327
1328 static Standard_Integer OCC1642 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1329 {
1330   if (argc != 10) {
1331     di<<"Usage: " << argv[0] << " FinalWare FinalFace InitWare InitFace shape FixReorder FixDegenerated FixConnected FixSelfIntersection\n";
1332     return 1;
1333   }
1334
1335   TopoDS_Shape shape = DBRep::Get(argv[5]);
1336
1337   TopExp_Explorer exp(shape, TopAbs_WIRE);
1338   TopoDS_Shape wire = exp.Current();
1339
1340   Handle(ShapeAnalysis_Wire) advWA = new ShapeAnalysis_Wire;
1341   advWA->Load(TopoDS::Wire(wire));
1342
1343   DBRep::Set(argv[3],wire);
1344
1345   TopoDS_Face face =
1346     BRepBuilderAPI_MakeFace(TopoDS::Wire(wire),Standard_True);
1347
1348   DBRep::Set(argv[4],face);
1349
1350   advWA->SetFace(face);
1351   Standard_Real precision_to_ana = 0.0001;
1352   advWA->SetPrecision(precision_to_ana);
1353
1354   TopTools_IndexedMapOfShape M;
1355   TopExp::MapShapes(wire, TopAbs_EDGE, M);
1356
1357   int j= 1;
1358   for(j= 1; j<=M.Extent(); ++j) {
1359     Standard_Integer num=1;
1360     IntRes2d_SequenceOfIntersectionPoint points2d;
1361     TColgp_SequenceOfPnt points3d;
1362
1363     std::cout << "\n j =" << j << ",  CheckSelfIntersectingEdge = " <<
1364       advWA->CheckSelfIntersectingEdge(j, points2d, points3d);
1365
1366
1367     ShapeExtend_Status status = ShapeExtend_FAIL1;
1368     if(advWA->StatusSelfIntersection(status))
1369
1370       status = ShapeExtend_FAIL2;
1371     if(advWA->StatusSelfIntersection(status))
1372       di << "\n No Vertices found in the edge";
1373
1374     status = ShapeExtend_DONE1;
1375     if(advWA->StatusSelfIntersection(status))
1376       di << "\n Self-intersection found in the edge";
1377
1378     num = points2d.Length();
1379     di << "\n No. of self-intersecting edges : " << num;
1380
1381     for(int i=1; i<=num; ++i) {
1382       gp_Pnt pt = points3d(i);
1383       di << "\n Intersecting pt : (" << pt.X() << ", " << pt.Y()<< ", " << pt.Z() << ")";
1384     }
1385
1386   }
1387
1388   Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1389   sfw->Load(TopoDS::Wire(wire));
1390   sfw->SetFace(face);
1391   sfw->SetPrecision(Precision::Confusion());
1392
1393   sfw->FixReorderMode() = 1;
1394   sfw->ClosedWireMode() = 1;
1395   sfw->FixDegeneratedMode() = 1;
1396   sfw->FixConnectedMode() = 1;
1397   sfw->ModifyTopologyMode() = 1;
1398   sfw->ModifyGeometryMode() = 1;
1399   sfw->FixSelfIntersectionMode() = 1;
1400   sfw->FixSelfIntersectingEdgeMode() = 1;
1401   sfw->FixIntersectingEdgesMode() = 1;
1402   sfw->FixNonAdjacentIntersectingEdgesMode() = 1;
1403   sfw->FixEdgeCurvesMode() = 1;
1404
1405   sfw->ModifyRemoveLoopMode() = 1;
1406   sfw->SetMaxTolerance(1.0);
1407
1408   di << "\n FixReorder = " << (Standard_Integer) sfw->FixReorder();
1409   di << "\n FixDegenerated = " << (Standard_Integer) sfw->FixDegenerated();
1410   di << "\n FixConnected = " << (Standard_Integer) sfw->FixConnected();
1411   di << "\n";
1412   di << "\n FixSelfIntersection = " << (Standard_Integer) sfw->FixSelfIntersection();
1413   di << "\n";
1414
1415   Draw::Set(argv[6],sfw->FixReorder()) ;
1416   Draw::Set(argv[7],sfw->FixDegenerated()) ;
1417   Draw::Set(argv[8],sfw->FixConnected()) ;
1418   Draw::Set(argv[9],sfw->FixSelfIntersection()) ;
1419
1420   ShapeExtend_Status status = ShapeExtend_OK;
1421   if(sfw->StatusSelfIntersection(status))
1422     di << "\n ShapeExtend_OK : No intersection found";
1423
1424   status = ShapeExtend_FAIL1;
1425   if(sfw->StatusSelfIntersection(status))
1426     di << "\n ShapeExtend_FAIL1 : analysis failed (edge has no pcurve,or no vertices etc.)";
1427
1428   status = ShapeExtend_FAIL2;
1429   if(sfw->StatusSelfIntersection(status))
1430     di << "\n ShapeExtend_FAIL2 : self-intersection was found, but not fixed because of limit of increasing tolerance (MaxTolerance)";
1431
1432   status = ShapeExtend_FAIL3;
1433   if(sfw->StatusSelfIntersection(status))
1434     di << "\n ShapeExtend_FAIL3 : intercestion of non adjacent edges found, but not fixed because of limit of increasing tolerance (MaxTolerance)";
1435
1436   status = ShapeExtend_DONE1;
1437   if(sfw->StatusSelfIntersection(status))
1438     di << "\n ShapeExtend_DONE1 : tolerance of vertex was increased to fix self-intersection";
1439
1440   status = ShapeExtend_DONE2;
1441   if(sfw->StatusSelfIntersection(status))
1442     di << "\n ShapeExtend_DONE2 : vertex was moved to fix self-intersection";
1443
1444   status = ShapeExtend_DONE3;
1445   if(sfw->StatusSelfIntersection(status))
1446     di << "\n ShapeExtend_DONE3 : some edges were removed because of intersection";
1447
1448   status = ShapeExtend_DONE4;
1449   if(sfw->StatusSelfIntersection(status))
1450     di << "\n ShapeExtend_DONE4 : pcurve(s) was(were) modified";
1451
1452   status = ShapeExtend_DONE5;
1453   if(sfw->StatusSelfIntersection(status))
1454     di << "\n ShapeExtend_DONE5 : non adjacent intersection fixed by increasing tolerance of vertex(vertices)";
1455
1456   status = ShapeExtend_DONE6;
1457   if(sfw->StatusSelfIntersection(status))
1458     di << "\n ShapeExtend_DONE6 : tolerance of edge was increased to hide intersection";
1459
1460   status = ShapeExtend_DONE7;
1461   if(sfw->StatusSelfIntersection(status))
1462     di << "\n ShapeExtend_DONE7 : range of some edges was decreased to avoid intersection";
1463
1464   TopoDS_Wire finalwire = sfw->Wire();
1465
1466   DBRep::Set(argv[1],finalwire);
1467
1468
1469   advWA->Load(TopoDS::Wire(finalwire));
1470
1471   TopoDS_Face fface =
1472     BRepBuilderAPI_MakeFace(TopoDS::Wire(finalwire),Standard_True);
1473
1474   DBRep::Set(argv[2],fface);
1475
1476   advWA->SetFace(fface);
1477
1478   TopTools_IndexedMapOfShape fM;
1479   TopExp::MapShapes(finalwire, TopAbs_EDGE, fM);
1480
1481   for(j= 1; j<=fM.Extent(); ++j) {
1482     Standard_Integer num=1;
1483     IntRes2d_SequenceOfIntersectionPoint points2d;
1484     TColgp_SequenceOfPnt points3d;
1485
1486     di << "\n j =" << j << ",  CheckSelfIntersectingEdge = " <<
1487       (Standard_Integer) advWA->CheckSelfIntersectingEdge(j, points2d, points3d);
1488
1489
1490     status = ShapeExtend_FAIL1;
1491     if(advWA->StatusSelfIntersection(status))
1492
1493       status = ShapeExtend_FAIL2;
1494     if(advWA->StatusSelfIntersection(status))
1495       di << "\n No Vertices found in the edge";
1496
1497     status = ShapeExtend_DONE1;
1498     if(advWA->StatusSelfIntersection(status))
1499       di << "\n Self-intersection found in the edge";
1500
1501     num = points2d.Length();
1502     di << "\n No. of self-intersecting edges : " << num;
1503
1504     for(int i=1; i<=num; ++i) {
1505       gp_Pnt pt = points3d(i);
1506       di << "\n Intersecting pt : (" << pt.X() << ", " << pt.Y() << ", " << pt.Z() << ")";
1507     }
1508
1509   }
1510
1511   di << "\n";
1512
1513   return 0;
1514 }
1515
1516 void QABugs::Commands_17(Draw_Interpretor& theCommands) {
1517   const char *group = "QABugs";
1518
1519   theCommands.Add ("BUC60842", "BUC60842", __FILE__, BUC60842, group);
1520   theCommands.Add ("BUC60843", "BUC60843 result_shape name_of_circle name_of_curve [ par1 [ tol ] ]", __FILE__, BUC60843, group);
1521   theCommands.Add ("BUC60970", "BUC60970 shape result", __FILE__, BUC60970, group);
1522   theCommands.Add ("BUC60915", "BUC60915", __FILE__, BUC60915_1, group);
1523   theCommands.Add ("OCC138", "OCC138", __FILE__, OCC138, group);
1524   theCommands.Add ("OCC353","OCC353",__FILE__,OCC353,group);
1525   theCommands.Add ("OCC280","OCC280 hlr=0/1 setsurfecedetail=0/1; set perspecrive view",__FILE__,OCC280,group);
1526   theCommands.Add ("OCC138LC", "OCC138LC", __FILE__, OCC138LC, group);
1527   theCommands.Add ("OCC566", "OCC566 shape [ xmin ymin zmin xmax ymax zmax] ; print bounding box", __FILE__, OCC566, group);
1528   theCommands.Add ("OCC570", "OCC570 result", __FILE__, OCC570, group);
1529
1530   theCommands.Add("OCC570mkevol",
1531     "OCC570mkevol result object (then use updatevol) [R/Q/P]; mkevol",
1532     __FILE__, MKEVOL,group);
1533   theCommands.Add("OCC570updatevol",
1534     "OCC570updatevol edge u1 rad1 u2 rad2 ...; updatevol",
1535     __FILE__, UPDATEVOL,group);
1536   theCommands.Add("OCC570updatevollaw",
1537     "OCC570updatevollaw edge u1 rad1 u2 rad2 ...; updatevollaw",
1538     __FILE__, UPDATEVOL,group);
1539   theCommands.Add("OCC570buildevol",
1540     "OCC570buildevol; end of the evol fillet computation",
1541     __FILE__, BUILDEVOL,group);
1542
1543   theCommands.Add ("OCC606", "OCC606 result shape [-t]", __FILE__, OCC606, group);
1544
1545   theCommands.Add ("OCC813", "OCC813 U V", __FILE__, OCC813, group);
1546   theCommands.Add ("OCC814", "OCC814", __FILE__, OCC814, group);
1547   theCommands.Add ("OCC884", "OCC884 result shape [toler [maxtoler]]", __FILE__, OCC884, group);
1548
1549   theCommands.Add ("OCC1174_1", "OCC1174_1 shape", __FILE__, OCC1174_1, group);
1550   theCommands.Add ("OCC1174_2", "OCC1174_2 shape", __FILE__, OCC1174_2, group);
1551
1552   theCommands.Add ("OCCN1", "OCCN1 angle fuse(1 for boss / 0 for slot) length", __FILE__, OCCN1, group);
1553   theCommands.Add ("OCCN2", "OCCN2", __FILE__, OCCN2, group);
1554
1555   theCommands.Add ("OCC2569", "OCC2569 nbpoles result", __FILE__, OCC2569, group);
1556
1557   theCommands.Add ("OCC1642", "OCC1642 FinalWare FinalFace InitWare InitFace shape FixReorder FixDegenerated FixConnected FixSelfIntersection", __FILE__, OCC1642, group);
1558
1559   return;
1560 }