0023035: TKOpenGl redesign regression caused by destruction of global OpenGl_Display...
[occt.git] / src / QARicardo / QARicardo.cxx
1 // Created on: 2002-05-16
2 // Created by: QA Admin
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20
21
22 #include <QARicardo.hxx>
23
24 #include <Draw_Interpretor.hxx>
25 #include <DBRep.hxx>
26 #include <DrawTrSurf.hxx>
27 #include <AIS_InteractiveContext.hxx>
28 #include <ViewerTest.hxx>
29 #include <AIS_Shape.hxx>
30 #include <TopoDS_Shape.hxx>
31
32 #include <tcl.h>
33
34 #include <gp_Ax2.hxx>
35 #include <gp_Circ.hxx>
36 #include <gp_Pln.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeWire.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepGProp.hxx>
43 #include <BRepOffsetAPI_MakePipeShell.hxx>
44 #include <GC_MakeArcOfCircle.hxx>
45 #include <Geom_Curve.hxx>
46 #include <Geom_Plane.hxx>
47 #include <Law_Linear.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Wire.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <GProp_GProps.hxx>
56 #include <Standard_ErrorHandler.hxx>
57
58 //=======================================================================
59 //function :  OCC332
60 //purpose  : 
61 //=======================================================================
62 static Standard_Integer OCC332bug (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
63 {
64   // Used to Display Geometry or Topolgy
65   char name[255];
66   char *pname = name;
67   Standard_Boolean check = Standard_True;
68
69   // Set default arguments
70   double wall_thickness = 10.0;
71   double dia1 = 80.0;
72   double dia2 = 100.0;
73   double length = 400.0;
74   double major_radius = 280.0;
75
76   // Convert arguments
77   if (argc>1) wall_thickness = atof(argv[1]);
78   if (argc>2) dia1 = atof(argv[2]);
79   if (argc>3) dia2 = atof(argv[3]);
80   if (argc>4) major_radius = atof(argv[4]);
81   if (argc>5) length = atof(argv[5]);
82   double bend_angle = length/major_radius;
83
84   //if ((bend_angle >= M_PI)) {
85   if ((bend_angle >= M_PI)) {
86     di << "The arguments are invalid." << "\n";
87     return(TCL_ERROR);
88   }
89   di << "creating the shape for a bent tube" << "\n";
90
91   double radius_l = dia1/2.0;
92   double radius_r = dia2/2.0;
93
94   // SUPPORT:
95   // 1. There is no need to normalize the direction - it's done automatically
96   //gp_Ax2 origin(gp_Pnt(5000.0,-300.0, 1000.0),
97                 //gp_Dir(0.0, -1.0/sqrt(2.0), -1.0/sqrt(2.0)));
98   gp_Ax2 origin(gp_Pnt(5000.0,-300.0,1000.0),gp_Dir(0.0,-1.0,-1.0));
99
100   TopoDS_Face myFace;
101   TopoDS_Shape myShape, gasSolid;
102   TopoDS_Solid wallSolid;
103
104   // Construct a circle for the first face, on the xy-plane at the origin
105   gp_Pln circ1Plane(origin.Location(), origin.Direction());
106   gp_Circ faceCircle(origin, radius_l);
107   gp_Circ outFaceCircle(origin, radius_l+wall_thickness);
108
109   // Construct center for a circle to be the spine of
110   // revolution, on the xz-plane at x=major_radius
111   gp_Pnt circ_center = origin.Location().Translated(major_radius*origin.XDirection());
112
113   // This point will be the center of the second face.
114   // SUPPORT:
115   // - There is no need in this point - we'll use angle instead.
116   //gp_Pnt endPoint = origin.Location();
117   //endPoint.Translate(major_radius*(1.0-cos(bend_angle))*origin.XDirection()) ;
118   //endPoint.Translate((-major_radius*sin(bend_angle))*origin.Direction());
119
120   // Construct the plane for the second face to sit on.
121   // SUPPORT:
122   // - It is better to use rotation instead of explicit calculations
123   //gp_Pln circ2Plane = gce_MakePln(circ_center, endPoint,
124   //                              endPoint.Translated(major_radius*origin.YDirection())
125   //                              ).Value();
126   gp_Ax1 circ_axis(circ_center,origin.YDirection());
127   gp_Pln circ2Plane = circ1Plane.Rotated(circ_axis,bend_angle);
128
129   // The circle used for the spine.
130   // SUPPORT:
131   // - Use direction (-X) instead of (X) to obtain correct right-handed system.
132   //   It is very important to maintain correct orientation between spine
133   //   and circles axes.
134   //gp_Ax2 spineAxis(circ_center, origin.YDirection(), origin.XDirection());
135   gp_Ax2 spineAxis(circ_center, origin.YDirection(), -origin.XDirection());
136   gp_Circ circle(spineAxis, major_radius);
137
138   // SUPPORT:
139   // - There is no need to create 2nd circles - they will be created by MakePipeShell.
140   //gp_Ax2 circ2axis(endPoint, circ2Plane.Axis().Direction(), origin.YDirection());
141   //gp_Circ faceCircle2(circ2axis,radius_r);
142   //gp_Circ outFaceCircle2(circ2axis,radius_r+wall_thickness);
143
144   TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(faceCircle);
145   TopoDS_Wire Wire1_ = BRepBuilderAPI_MakeWire(E1).Wire();
146   
147   // Create the face at the near end for the wall solid, an annular ring.
148   TopoDS_Edge Eout1 = BRepBuilderAPI_MakeEdge(outFaceCircle);
149   TopoDS_Wire outerWire1_ = BRepBuilderAPI_MakeWire(Eout1).Wire();
150
151   // SUPPORT:
152   // - There is no need to create 2nd circles -
153   //   they will be created by MakePipeShell
154   //TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge(faceCircle2);
155   //TopoDS_Wire Wire2_ = BRepBuilderAPI_MakeWire(E2).Wire();
156   
157   // Create the face at the far end for the wall solid, an annular ring.
158   // SUPPORT:
159   // - There is no need to create 2nd circles -
160   //   they will be created by MakePipeShell
161   //TopoDS_Edge Eout2 = BRepBuilderAPI_MakeEdge(outFaceCircle2);
162   //TopoDS_Wire outerWire2_ = BRepBuilderAPI_MakeWire(Eout2).Wire();
163
164   // SUPPORT:
165   // - It is better to use bend angle calculated above
166   //Handle(Geom_Curve) SpineCurve = GC_MakeArcOfCircle(circle,
167   //                                                 endPoint,
168   //                                                 origin.Location(),
169   //                                                 Standard_True).Value();
170   Handle(Geom_Curve) SpineCurve = GC_MakeArcOfCircle(circle,
171                                                      0.0,
172                                                      bend_angle,
173                                                      Standard_True).Value();
174
175   // SUPPORT:
176   // - Use correct formula for scaling laws
177   Handle(Law_Linear) myLaw1 = new Law_Linear();
178   Handle(Law_Linear) myLaw2 = new Law_Linear();
179   //if ((radius_r - radius_l) < Precision::Confusion())
180   //{
181     //myLaw1->Set(SpineCurve->FirstParameter(), 1.0,
182                 //SpineCurve->LastParameter(), 1.0);
183     //myLaw2->Set(SpineCurve->FirstParameter(), 1.0,
184                 //SpineCurve->LastParameter(), 1.0);
185   //}
186   //else
187   //{
188     //myLaw1->Set(SpineCurve->FirstParameter(), radius_r/(radius_r-radius_l),
189                 //SpineCurve->LastParameter(), 1.0);
190     //myLaw2->Set(SpineCurve->FirstParameter(), (radius_r+wall_thickness)/(radius_r-radius_l),
191                 //SpineCurve->LastParameter(), 1.0);
192   //}
193   myLaw1->Set(SpineCurve->FirstParameter(),1.0,
194               SpineCurve->LastParameter(),radius_r/radius_l);
195   myLaw2->Set(SpineCurve->FirstParameter(),1.0,
196               SpineCurve->LastParameter(),(radius_r+wall_thickness)/(radius_l+wall_thickness));
197
198   BRepBuilderAPI_MakeFace mkFace;
199
200   BRepBuilderAPI_MakeEdge mkEdge;
201
202   mkEdge.Init(SpineCurve);
203   if (!mkEdge.IsDone()) return 0;
204   TopoDS_Wire SpineWire = BRepBuilderAPI_MakeWire(mkEdge.Edge()).Wire();
205
206   sprintf (name,"SpineWire");
207   DBRep::Set(name,SpineWire);
208
209   sprintf (name,"Wire1_");
210   DBRep::Set(name,Wire1_);
211
212   sprintf (name,"outerWire1_");
213   DBRep::Set(name,outerWire1_);
214
215   // SUPPORT:
216   // - There is no need to create 2nd circles
217   //sprintf (name,"Wire2_");
218   //DBRep::Set(name,Wire2_);
219   //sprintf (name,"outerWire2_");
220   //DBRep::Set(name,outerWire2_);
221
222   di.Eval("fit");
223
224   // SUPPORT:
225   // - There is no need in these vertices
226   //TopoDS_Vertex Location1, Location2;
227   //TopExp::Vertices(SpineWire, Location1, Location2);
228
229   // Make inner pipe shell
230   BRepOffsetAPI_MakePipeShell mkPipe1(SpineWire);
231   mkPipe1.SetTolerance(1.0e-8,1.0e-8,1.0e-6);
232   //mkPipe1.SetTransitionMode(BRepBuilderAPI_Transformed); // Default mode !!
233   mkPipe1.SetLaw(Wire1_, myLaw1/*, Location2*/, Standard_False, Standard_False);
234   mkPipe1.Build();
235   if (!mkPipe1.IsDone()) return 0;
236
237   // Make outer pipe shell
238   BRepOffsetAPI_MakePipeShell mkPipe2(SpineWire);
239   mkPipe2.SetTolerance(1.0e-8,1.0e-8,1.0e-6);
240   //mkPipe2.SetTransitionMode(BRepBuilderAPI_Transformed); // Default mode !!
241   mkPipe2.SetLaw(outerWire1_, myLaw2/*, Location2*/, Standard_False, Standard_False);
242   mkPipe2.Build();
243   if (!mkPipe2.IsDone()) return 0;
244
245   // Make face for first opening
246   Handle(Geom_Plane) Plane1 = new Geom_Plane(circ1Plane);
247   mkFace.Init(Plane1,Standard_False,Precision::Confusion());
248   // SUPPORT:
249   // - Use wires created by MakePipeShell
250   //mkFace.Add(TopoDS::Wire(outerWire1_));
251   //mkFace.Add(TopoDS::Wire(Wire1_.Reversed()));
252   mkFace.Add(TopoDS::Wire(mkPipe2.FirstShape()));
253   mkFace.Add(TopoDS::Wire(mkPipe1.FirstShape().Reversed()));
254   if (!mkFace.IsDone()) return 0;
255   TopoDS_Face Face1 = mkFace.Face();
256
257   // Make face for second opening
258   Handle(Geom_Plane) Plane2 = new Geom_Plane(circ2Plane);
259   mkFace.Init(Plane2,Standard_False,Precision::Confusion());
260   // SUPPORT:
261   // - Use wires created by MakePipeShell
262   //mkFace.Add(TopoDS::Wire(outerWire2_));
263   //mkFace.Add(TopoDS::Wire(Wire2_.Reversed()));
264   mkFace.Add(TopoDS::Wire(mkPipe2.LastShape()));
265   mkFace.Add(TopoDS::Wire(mkPipe1.LastShape().Reversed()));
266   if (!mkFace.IsDone()) return 0;
267   TopoDS_Face Face2 = mkFace.Face();
268
269   // Make tube
270   TopoDS_Shell TubeShell;
271   BRep_Builder B;
272   B.MakeShell(TubeShell);
273   TopExp_Explorer getFaces;
274   TopoDS_Face test_face;
275   getFaces.Init(mkPipe1.Shape(), TopAbs_FACE);
276   // SUPPORT:
277   // - In our case there should be only 1 pipe face
278   //while (getFaces.More())
279   //  {
280   //    test_face = TopoDS::Face(getFaces.Current());
281   //    Handle(Geom_Surface) S = BRep_Tool::Surface(test_face); 
282   //    GeomLib_IsPlanarSurface IsPl(S);
283   //    if (!IsPl.IsPlanar()) {
284   //    B.Add(TubeShell,getFaces.Current().Reversed());
285   //    }
286   //    getFaces.Next();
287   //  }
288   if (getFaces.More())
289     B.Add(TubeShell,getFaces.Current().Reversed());
290
291   // Grab the gas solid now that we've extracted the faces.
292   mkPipe1.MakeSolid();
293   gasSolid = mkPipe1.Shape();
294
295   sprintf (name,"gasSolid_");
296   DBRep::Set(name,gasSolid);
297
298   //getFaces.Clear();
299   getFaces.Init(mkPipe2.Shape(), TopAbs_FACE);
300   // SUPPORT:
301   // - In our case there should be only 1 pipe face
302   //while (getFaces.More())
303   //  {
304   //    B.Add(TubeShell,getFaces.Current());
305   //    getFaces.Next();
306   //  }
307   if (getFaces.More())
308     B.Add(TubeShell,getFaces.Current());
309
310   B.Add(TubeShell,Face1.Reversed());
311   B.Add(TubeShell,Face2);
312
313   B.MakeSolid(wallSolid);
314   B.Add(wallSolid,TubeShell);
315
316   sprintf (name,"wallSolid_");
317   DBRep::Set(name,wallSolid);
318
319   // Now calculated the volume of the outside tube.
320   GProp_GProps gprops;
321   BRepGProp::VolumeProperties(wallSolid, gprops);
322   di << "The wallSolid's volume is: " << gprops.Mass() << "\n";
323
324   if (check) {
325     if (!(BRepCheck_Analyzer(wallSolid).IsValid()))
326       di << "The TopoDS_Solid was checked, and it was invalid!" << "\n";
327     else
328       di << "The TopoDS_Solid was checked, and it was valid." << "\n";
329     if (!wallSolid.Closed())
330       di << "The TopoDS_Solid is not closed!" << "\n";
331     else
332       di << "The TopoDS_Solid is closed." << "\n";
333     if (!wallSolid.Checked())
334       di << "The TopoDS_Solid is not checked!" << "\n";
335     else
336       di << "The TopoDS_Solid has been checked." << "\n";
337     if (wallSolid.Infinite())
338       di << "The TopoDS_Solid is infinite!" << "\n";
339     else
340       di << "The TopoDS_Solid is finite." << "\n";
341   }
342
343   di << "The result is a ";
344   // Check to see if we have a solid
345   switch (wallSolid.ShapeType()) {
346   case (TopAbs_COMPOUND):
347     di << "TopAbs_COMPOUND" << "\n";
348     break;
349   case (TopAbs_COMPSOLID):
350     di << "TopAbs_COMPSOLID" << "\n";
351     break;
352   case (TopAbs_SOLID):
353     di << "TopAbs_SOLID" << "\n";
354     break;
355   case (TopAbs_SHELL):
356     di << "TopAbs_SHELL" << "\n";
357     break;
358   case (TopAbs_FACE):
359     di << "TopAbs_FACE" << "\n";
360     break;
361   case (TopAbs_WIRE):
362     di << "TopAbs_WIRE" << "\n";
363     break;
364   case (TopAbs_EDGE):
365     di << "TopAbs_EDGE" << "\n";
366     break;
367   case (TopAbs_VERTEX):
368     di << "TopAbs_VERTEX" << "\n";
369     break;
370   case (TopAbs_SHAPE):
371     di << "TopAbs_SHAPE" << "\n";
372   }
373   di << "Can we turn it into a solid? ";
374   try {
375     OCC_CATCH_SIGNALS
376     di << " yes" << "\n";
377   }
378   catch (Standard_TypeMismatch) {
379     di << " no" << "\n";
380   }
381
382   getFaces.Clear();
383   getFaces.Init(wallSolid, TopAbs_FACE);
384   int i =0;
385   while (getFaces.More())
386     {
387       i++;
388       sprintf(name,"Face%d",i);
389       di << "Face named " << name << "\n";
390       DBRep::Set(name,getFaces.Current());
391       getFaces.Next();
392     }
393
394   return 0;
395 }
396
397 #include <gce_MakePln.hxx>
398 #include <TopExp.hxx>
399 #include <BRepOffsetAPI_Sewing.hxx>
400 #include <BRepAlgoAPI_Fuse.hxx>
401 ///////#else
402 ///////#include <BRepAlgo_Fuse.hxx>
403 ///////#include <BRepAlgoAPI_Fuse.hxx>
404 #include <BRepAlgo_Fuse.hxx>
405
406 //=======================================================================
407 //function :  OCC544
408 //purpose  : 
409 //=======================================================================
410 static Standard_Integer OCC544 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
411 {
412   if(argc > 7) {
413     di << "Usage : " << argv[0] << " [[[[[wT [[[[d1 [[[d2 [[R [length [BRepAlgoAPI/BRepAlgo = 1/0]]]]]]" << "\n";
414     return 1;
415   }
416   Standard_Boolean IsBRepAlgoAPI = Standard_True;
417   if (argc == 7) {
418     Standard_Integer IsB = atoi(argv[6]);
419     if (IsB != 1) {
420       IsBRepAlgoAPI = Standard_False;
421 #if ! defined(BRepAlgo_def01)
422 //      di << "Error: There is not BRepAlgo_Fuse class" << "\n";
423 //      return 1;
424 #endif
425     }
426   }
427
428   // Used to Display Geometry or Topolgy
429   char name[255];
430   char *pname = name;
431   //bool check = true;
432   Standard_Boolean check = Standard_True;
433
434   // Set default arguments
435   double radius_l = 20.0;
436   double radius_r = 80.0;
437   // mkv 15.07.03 double bend_angle = M_PI/2.0;
438   double bend_angle = M_PI/2.0;
439
440   double major_rad = 280.0;
441   double wall_thickness = 10.0;
442                     
443
444   // Convert arguments
445   if (argc>1) radius_l = atof(argv[1]);
446   if (argc>2) radius_r = atof(argv[2]);
447   if (argc>3) bend_angle = atof(argv[3]);
448   if (argc>4) major_rad = atof(argv[4]);
449   if (argc>5) wall_thickness = atof(argv[5]);
450
451   // mkv 15.07.03 if ((bend_angle >= 2.0*M_PI)) {
452   if ((bend_angle >= 2.0*M_PI)) {
453     di << "The arguments are invalid." << "\n";
454     return(TCL_ERROR);
455   }
456   di << "creating the shape for a bent tube" << "\n";
457  
458   gp_Ax2 origin(gp_Pnt(500.0,-300.0, 100.0),
459                 gp_Dir(0.0, -1.0/sqrt(2.0), -1.0/sqrt(2.0)));
460
461   TopoDS_Face firstFace, lastFace;
462   TopoDS_Solid wallSolid, myShape;
463   // Construct a circle for the first face, on the xy-plane at the origin
464   gp_Pln circ1Plane(origin.Location(), origin.Direction());
465   gp_Circ faceCircle(origin, radius_l);
466   gp_Circ outFaceCircle(origin, radius_l+wall_thickness);
467
468   // Construct center for a circle to be the spine of
469   // revolution, on the xz-plane at x=major_rad
470   gp_Pnt circ_center = origin.Location().Translated(
471                                                     major_rad*origin.XDirection()
472                                                     );
473
474   // This point will be the center of the second face.
475   gp_Pnt endPoint = origin.Location();
476   endPoint.Translate(major_rad*(1.0-cos(bend_angle))*origin.XDirection()) ;
477   endPoint.Translate((-major_rad*sin(bend_angle))*origin.Direction());
478
479   // Construct the plane for the second face to sit on.
480   gp_Pln circ2Plane = gce_MakePln(circ_center, endPoint,
481                                   endPoint.Translated(major_rad*origin.YDirection())
482                                   ).Value();
483
484   // The circle used for the spine.
485   gp_Ax2 spineAxis(circ_center, origin.YDirection(), origin.XDirection());
486   gp_Circ circle(spineAxis, major_rad);
487
488   gp_Ax2 circ2axis(endPoint, circ2Plane.Axis().Direction(), origin.YDirection());
489   gp_Circ faceCircle2(circ2axis,radius_r);
490   gp_Circ outFaceCircle2(circ2axis,radius_r+wall_thickness);
491
492   TopoDS_Edge E1_1 = BRepBuilderAPI_MakeEdge(faceCircle, 0, M_PI);
493   TopoDS_Edge E1_2 = BRepBuilderAPI_MakeEdge(faceCircle, M_PI, 2.*M_PI);
494   TopoDS_Wire Wire1_ = BRepBuilderAPI_MakeWire(E1_1, E1_2);
495   
496   // Create the face at the near end for the wall solid, an annular ring.
497   TopoDS_Edge Eout1_1 = BRepBuilderAPI_MakeEdge(outFaceCircle, 0, M_PI);
498   TopoDS_Edge Eout1_2 = BRepBuilderAPI_MakeEdge(outFaceCircle, M_PI, 2.*M_PI);
499   TopoDS_Wire outerWire1_ = BRepBuilderAPI_MakeWire(Eout1_1, Eout1_2);
500  
501   TopoDS_Edge E2_1 = BRepBuilderAPI_MakeEdge(faceCircle2, 0, M_PI);
502   TopoDS_Edge E2_2 = BRepBuilderAPI_MakeEdge(faceCircle2, M_PI, 2.*M_PI);
503   TopoDS_Wire Wire2_ = BRepBuilderAPI_MakeWire(E2_1, E2_2);
504   
505   // Create the face at the far end for the wall solid, an annular ring.
506   TopoDS_Edge Eout2_1 = BRepBuilderAPI_MakeEdge(outFaceCircle2, 0, M_PI);
507   TopoDS_Edge Eout2_2 = BRepBuilderAPI_MakeEdge(outFaceCircle2, M_PI, 2.*M_PI);
508   TopoDS_Wire outerWire2_ = BRepBuilderAPI_MakeWire(Eout2_1, Eout2_2);
509
510   BRepBuilderAPI_MakeFace mkFace;
511
512   Handle(Geom_Curve) SpineCurve = GC_MakeArcOfCircle(circle,
513                                                      endPoint,
514                                                      origin.Location(),
515                                                      Standard_True).Value();
516   Handle(Law_Linear) myLaw = new Law_Linear();
517   Handle(Law_Linear) myLaw2 = new Law_Linear();
518
519   myLaw->Set(SpineCurve->FirstParameter(),
520              radius_r/radius_l,
521              SpineCurve->LastParameter(),
522              1.0);
523
524   myLaw2->Set(SpineCurve->FirstParameter(),
525               (radius_r+wall_thickness)/(radius_l+wall_thickness),
526               SpineCurve->LastParameter(),
527               1.0);
528
529   di << "SpineCurve->FirstParameter() is " << SpineCurve->FirstParameter() << "\n";
530   di << "SpineCurve->LastParameter() is " << SpineCurve->LastParameter() << "\n";
531   di << "Law1 Value at FirstParameter() is " << myLaw->Value(SpineCurve->FirstParameter()) << "\n";
532   di << "Law1 Value at LastParameter() is " << myLaw->Value(SpineCurve->LastParameter()) << "\n";
533   di << "radius_r / radius_l is " << radius_r/radius_l << "\n";
534
535   BRepBuilderAPI_MakeEdge mkEdge;
536
537   mkEdge.Init(SpineCurve);
538   if (!mkEdge.IsDone()) return TCL_ERROR;
539   TopoDS_Wire SpineWire = BRepBuilderAPI_MakeWire(mkEdge.Edge()).Wire();
540
541   sprintf (name,"SpineWire");
542   DBRep::Set(name,SpineWire);
543
544   sprintf (name,"Wire1_");
545   DBRep::Set(name,Wire1_);
546
547   sprintf (name,"outerWire1_");
548   DBRep::Set(name,outerWire1_);
549
550   sprintf (name,"Wire2_");
551   DBRep::Set(name,Wire2_);
552
553   sprintf (name,"outerWire2_");
554   DBRep::Set(name,outerWire2_);
555
556   di.Eval("fit");
557
558   TopoDS_Vertex Location1, Location2;
559
560   TopExp::Vertices(SpineWire, Location2, Location1);
561
562   sprintf (name,"Location1");
563   DBRep::Set(name,Location1);
564
565   sprintf (name,"Location2");
566   DBRep::Set(name,Location2);
567
568   // Make inner pipe shell
569   BRepOffsetAPI_MakePipeShell mkPipe1(SpineWire);
570   mkPipe1.SetTolerance(1.0e-8,1.0e-8,1.0e-6);
571   mkPipe1.SetTransitionMode(BRepBuilderAPI_Transformed);
572   mkPipe1.SetMode(Standard_False);
573   mkPipe1.SetLaw(Wire1_, myLaw, Location1, Standard_False, Standard_False);
574   mkPipe1.Build();
575   if (!mkPipe1.IsDone()) return TCL_ERROR;
576
577   // Make outer pipe shell
578   BRepOffsetAPI_MakePipeShell mkPipe2(SpineWire);
579   mkPipe2.SetTolerance(1.0e-8,1.0e-8,1.0e-6);
580   mkPipe2.SetTransitionMode(BRepBuilderAPI_Transformed);
581   mkPipe2.SetMode(Standard_False);
582   mkPipe2.SetLaw(outerWire1_, myLaw2, Location1, Standard_False, Standard_False);
583  mkPipe2.Build();
584   if (!mkPipe2.IsDone()) return TCL_ERROR;
585
586 //    sprintf(name,"w1-first");
587 //    DBRep::Set(name,mkPipe1.FirstShape());
588
589 //    sprintf(name,"w1-last");
590 //    DBRep::Set(name,mkPipe1.LastShape());
591
592 //    sprintf(name,"w2-first");
593 //    DBRep::Set(name,mkPipe2.FirstShape());
594
595 //    sprintf(name,"w2-last");
596 //    DBRep::Set(name,mkPipe2.LastShape());
597
598   BRepOffsetAPI_Sewing SewIt(1.0e-4);
599
600   // Make tube
601   TopExp_Explorer getFaces;
602   TopoDS_Face test_face;
603   getFaces.Init(mkPipe1.Shape(), TopAbs_FACE);
604   while (getFaces.More())
605     {
606       SewIt.Add(getFaces.Current().Reversed());
607       getFaces.Next();
608     }
609
610   // Make face for first opening
611   Handle(Geom_Plane) Plane1 = new Geom_Plane(circ1Plane);
612   mkFace.Init(Plane1,Standard_False,Precision::Confusion());
613   mkFace.Add(TopoDS::Wire(outerWire1_));
614   mkFace.Add(TopoDS::Wire(Wire1_.Reversed()));
615   if (!mkFace.IsDone()) return TCL_ERROR;
616   TopoDS_Face Face1 = mkFace.Face();
617
618   // Make face for second opening
619   Handle(Geom_Plane) Plane2 = new Geom_Plane(circ2Plane);
620   mkFace.Init(Plane2,Standard_False,Precision::Confusion());
621   mkFace.Add(TopoDS::Wire(outerWire2_));
622   mkFace.Add(TopoDS::Wire(Wire2_.Reversed()));
623   if (!mkFace.IsDone()) return TCL_ERROR;
624   TopoDS_Face Face2 = mkFace.Face();
625
626   // Grab the gas solid now that we've extracted the faces.
627   mkPipe1.MakeSolid();
628   myShape = TopoDS::Solid(mkPipe1.Shape());
629
630   getFaces.Clear();
631   getFaces.Init(mkPipe2.Shape(), TopAbs_FACE);
632   while (getFaces.More())
633     {
634       SewIt.Add(getFaces.Current());
635       getFaces.Next();
636     }
637
638   SewIt.Add(Face1.Reversed());
639   SewIt.Add(Face2);
640
641   SewIt.Perform();
642
643   di << "The result of the Sewing operation is a ";
644   // Check to see if we have a solid
645   switch (SewIt.SewedShape().ShapeType()) {
646   case (TopAbs_COMPOUND):
647     di << "TopAbs_COMPOUND" << "\n";
648     break;
649   case (TopAbs_COMPSOLID):
650     di << "TopAbs_COMPSOLID" << "\n";
651     break;
652   case (TopAbs_SOLID):
653     di << "TopAbs_SOLID" << "\n";
654     break;
655   case (TopAbs_SHELL):
656     di << "TopAbs_SHELL" << "\n";
657     break;
658   case (TopAbs_FACE):
659     di << "TopAbs_FACE" << "\n";
660     break;
661   case (TopAbs_WIRE):
662     di << "TopAbs_WIRE" << "\n";
663     break;
664   case (TopAbs_EDGE):
665     di << "TopAbs_EDGE" << "\n";
666     break;
667   case (TopAbs_VERTEX):
668     di << "TopAbs_VERTEX" << "\n";
669     break;
670   case (TopAbs_SHAPE):
671     di << "TopAbs_SHAPE" << "\n";
672   }
673
674   BRep_Builder B;
675
676   TopoDS_Shell TubeShell;
677   di << "Can we turn it into a shell? ";
678   try {
679     OCC_CATCH_SIGNALS
680     TubeShell = TopoDS::Shell(SewIt.SewedShape());
681     B.MakeSolid(wallSolid);
682     B.Add(wallSolid,TubeShell);
683     di << " yes" << "\n";
684   }
685   catch (Standard_TypeMismatch) {
686     di << "Can't convert to shell..." << "\n";
687     TopExp_Explorer getSol;
688     getSol.Init(SewIt.SewedShape(), TopAbs_SOLID);
689     if (getSol.More()) {
690       di << "First solid found in compound" << "\n";
691       wallSolid = TopoDS::Solid(getSol.Current());
692       TopoDS_Solid test_solid;
693       while (getSol.More())
694         {
695           di << "Next solid found in compound" << "\n";
696           getSol.Next();
697           test_solid = TopoDS::Solid(getSol.Current());
698 //////#if ! defined(BRepAlgoAPI_def01)
699 //////    BRepAlgoAPI_Fuse fuser(test_solid, wallSolid);
700 //////#else
701 //////    BRepAlgo_Fuse fuser(test_solid, wallSolid);
702 //////#endif
703 //////    fuser.Build();
704           if (IsBRepAlgoAPI) {
705             di << "BRepAlgoAPI_Fuse fuser(test_solid, wallSolid)" <<"\n";
706             BRepAlgoAPI_Fuse fuser(test_solid, wallSolid);
707             fuser.Build();
708             wallSolid = TopoDS::Solid(fuser.Shape());
709           } else {
710             di << "BRepAlgo_Fuse fuser(test_solid, wallSolid)" <<"\n";
711             BRepAlgo_Fuse fuser(test_solid, wallSolid);
712             fuser.Build();
713             wallSolid = TopoDS::Solid(fuser.Shape());
714           }
715 //////    wallSolid = TopoDS::Solid(fuser.Shape());
716         }
717     } else {
718       // Let's see if we can extract shells instead of solids.
719       TopExp_Explorer getShel;
720       getShel.Init(SewIt.SewedShape(), TopAbs_SHELL);
721       if (getShel.More()) {
722         di << "First shell found in compound" << "\n";
723         B.MakeSolid(wallSolid);
724         di << "B.Add(wallSolid,TopoDS::Shell(getShel.Current()));" << "\n";
725         int i = 1;
726         while (getShel.More())
727           {
728             di << "Next shell found in compound" << "\n";
729             di << "B.Add(wallSolid,TopoDS::Shell(getShel.Current()));" << "\n";
730             sprintf(name,"shell%d", i++);
731             DBRep::Set(name,getShel.Current());
732             B.Add(wallSolid,TopoDS::Shell(getShel.Current()));
733             getShel.Next();
734           }
735       }
736     }
737   }
738
739   sprintf(name,"result");
740   DBRep::Set(name,wallSolid);
741
742   // Now calculated the volume of the outside tube.
743   GProp_GProps gprops;
744   BRepGProp::VolumeProperties(wallSolid, gprops);
745   di << "The wallSolid's volume is: " << gprops.Mass() << "\n";
746
747   if (check) {
748     if (!(BRepCheck_Analyzer(wallSolid).IsValid()))
749       di << "The TopoDS_Solid was checked, and it was invalid!" << "\n";
750     else
751       di << "The TopoDS_Solid was checked, and it was valid." << "\n";
752     if (!wallSolid.Closed())
753       di << "The TopoDS_Solid is not closed!" << "\n";
754     else
755       di << "The TopoDS_Solid is closed." << "\n";
756     if (!wallSolid.Checked())
757       di << "The TopoDS_Solid is not checked!" << "\n";
758     else
759       di << "The TopoDS_Solid has been checked." << "\n";
760     if (wallSolid.Infinite())
761       di << "The TopoDS_Solid is infinite!" << "\n";
762     else
763       di << "The TopoDS_Solid is finite." << "\n";
764   }
765
766   di << "The result is a ";
767   // Check to see if we have a solid
768   switch (wallSolid.ShapeType()) {
769   case (TopAbs_COMPOUND):
770     di << "TopAbs_COMPOUND" << "\n";
771     break;
772   case (TopAbs_COMPSOLID):
773     di << "TopAbs_COMPSOLID" << "\n";
774     break;
775   case (TopAbs_SOLID):
776     di << "TopAbs_SOLID" << "\n";
777     break;
778   case (TopAbs_SHELL):
779     di << "TopAbs_SHELL" << "\n";
780     break;
781   case (TopAbs_FACE):
782     di << "TopAbs_FACE" << "\n";
783     break;
784   case (TopAbs_WIRE):
785     di << "TopAbs_WIRE" << "\n";
786     break;
787   case (TopAbs_EDGE):
788     di << "TopAbs_EDGE" << "\n";
789     break;
790   case (TopAbs_VERTEX):
791     di << "TopAbs_VERTEX" << "\n";
792     break;
793   case (TopAbs_SHAPE):
794     di << "TopAbs_SHAPE" << "\n";
795   }
796
797   return 0;
798 }
799
800 #include <BRepPrimAPI_MakeBox.hxx>
801 #include <BRepBndLib.hxx>
802 #include <TopTools_Array1OfShape.hxx>
803 #include <TColStd_Array1OfReal.hxx>
804 #include <BRepBuilderAPI_Copy.hxx>
805 #include <BRepAlgoAPI_Cut.hxx>
806 #include <BRepAlgoAPI_Common.hxx>
807 //////////#else
808 //////////#include <BRepAlgo_Cut.hxx>
809 //////////#include <BRepAlgo_Common.hxx>
810 //////////#include <BRepAlgoAPI_Cut.hxx>
811 //////////#include <BRepAlgoAPI_Common.hxx>
812 #include <BRepAlgo_Cut.hxx>
813 #include <BRepAlgo_Common.hxx>
814 #include <Precision.hxx>
815
816 static Standard_Integer OCC817 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
817 {
818   //////////if(argc != 3) {
819   //////////  cout << "Usage : " << argv[0] << " result mesh_delta" << endl;
820   //////////  return 1; 
821   //////////}
822   if(argc < 3 || argc > 4) {
823     di << "Usage : " << argv[0] << " result mesh_delta [BRepAlgoAPI/BRepAlgo = 1/0]" << "\n";
824     return 1;
825   }
826   Standard_Boolean IsBRepAlgoAPI = Standard_True;
827   if (argc == 4) {
828     Standard_Integer IsB = atoi(argv[3]);
829     if (IsB != 1) {
830       IsBRepAlgoAPI = Standard_False;
831 #if ! defined(BRepAlgo_def02)
832 //      di << "Error: There is not BRepAlgo_Cut class" << "\n";
833 //      return 1;
834 #endif
835 #if ! defined(BRepAlgo_def03)
836 //      di << "Error: There is not BRepAlgo_Common class" << "\n";
837 //      return 1;
838 #endif
839     }
840   }
841   
842   Standard_Real delt = 5.0*Precision::Confusion();
843   Standard_Real mesh_delt = atof(argv[2]);
844   if (mesh_delt <= 0.0)
845   {
846     di<<"Error: mesh_delta must be positive value"<<"\n";
847     return -1;
848   }
849
850   // Create outer box solid
851   gp_Pnt P(0,0,0);
852   TopoDS_Solid fullSolid = BRepPrimAPI_MakeBox(P, 30.0, 30.0, 30.0).Solid();
853
854   // Create inner box solid
855   P.SetX(10); P.SetY(10); P.SetZ(10);
856   TopoDS_Solid internalSolid = BRepPrimAPI_MakeBox(P, 10.0, 10.0, 10.0).Solid();
857
858   // Cut inner from outer
859 //////////#if ! defined(BRepAlgoAPI_def01)
860 //////////  BRepAlgoAPI_Cut cut( fullSolid, internalSolid );
861 //////////#else
862 //////////  BRepAlgo_Cut cut( fullSolid, internalSolid );
863 //////////#endif
864 //////////  TopoDS_Shape cut_shape = cut.Shape();
865 //////////  if ( !cut.IsDone() )
866 //////////  {
867 //////////    cout << "Error: Could not cut volumes" << endl;
868 //////////    return -1;
869 //////////  }
870
871   TopoDS_Shape cut_shape;
872   if (IsBRepAlgoAPI) {
873     di << "BRepAlgoAPI_Cut cut( fullSolid, internalSolid )" <<"\n";
874     BRepAlgoAPI_Cut cut( fullSolid, internalSolid );
875     cut_shape = cut.Shape();
876     if ( !cut.IsDone() )
877       {
878         di << "Error: Could not cut volumes" << "\n";
879         return -1;
880       }
881   } else {
882     di << "BRepAlgo_Cut cut( fullSolid, internalSolid )" <<"\n";
883     BRepAlgo_Cut cut( fullSolid, internalSolid );
884     cut_shape = cut.Shape();
885     if ( !cut.IsDone() )
886       {
887         di << "Error: Could not cut volumes" << "\n";
888         return -1;
889       }
890   }
891
892   // see if we have a solid
893   Standard_Integer found_solid = 0;
894   TopoDS_Solid cutSolid;
895   TopExp_Explorer Ex;
896   for (Ex.Init(cut_shape, TopAbs_SOLID); Ex.More(); Ex.Next())
897   {
898     TopoDS_Solid sol = TopoDS::Solid(Ex.Current());
899     if (!sol.IsNull()) { cutSolid = sol; found_solid++; }
900   }
901   if ( found_solid != 1 )
902   {
903     di << "Error: Cut operation produced " << found_solid << " solids" << "\n";
904     return -1;
905   }
906   DBRep::Set(argv[1],cutSolid);
907
908   // Calculate initial volume
909   GProp_GProps volumeVProps;
910   BRepGProp::VolumeProperties (cutSolid, volumeVProps);
911   di << "Info: Original volume  = " << volumeVProps.Mass() << "\n";
912
913   //
914   // build bounding box and calculate bounds for initial mesh
915   //
916   Bnd_Box bndBox;
917   BRepBndLib::Add( cutSolid, bndBox );
918   Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
919   bndBox.Get( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
920   Xmin -= delt;
921   Ymin -= delt;
922   Zmin -= delt;
923   Xmax += delt;
924   Ymax += delt;
925   Zmax += delt;
926   di<<"Info: Bounds\n  ("<<Xmin<<","<<Ymin<<","<<Zmin<<")\n  ("<<Xmax<<","<<Ymax<<","<<Zmax<<")"<<"\n";
927
928   // grid the bounding box
929   Standard_Integer NumXsubvolumes = (Xmax - Xmin) / mesh_delt; if (NumXsubvolumes <= 0) NumXsubvolumes = 1;
930   Standard_Integer NumYsubvolumes = (Ymax - Ymin) / mesh_delt; if (NumYsubvolumes <= 0) NumYsubvolumes = 1;
931   Standard_Integer NumZsubvolumes = (Zmax - Zmin) / mesh_delt; if (NumZsubvolumes <= 0) NumZsubvolumes = 1;
932   const Standard_Real StepX = (Xmax - Xmin) / NumXsubvolumes;
933   const Standard_Real StepY = (Ymax - Ymin) / NumYsubvolumes;
934   const Standard_Real StepZ = (Zmax - Zmin) / NumZsubvolumes;
935   const Standard_Integer NumSubvolumes = NumXsubvolumes * NumYsubvolumes * NumZsubvolumes;
936   di << "Info: NumSubvolumesX = " << NumXsubvolumes << "\n";
937   di << "Info: NumSubvolumesY = " << NumYsubvolumes << "\n";
938   di << "Info: NumSubvolumesZ = " << NumZsubvolumes << "\n";
939   di << "Info: NumSubvolumes = " << NumSubvolumes << "\n";
940
941   //
942   // construct initial mesh of cutSolid
943   //
944   TopTools_Array1OfShape SubvolumeSolid(0,NumSubvolumes-1);
945   TColStd_Array1OfReal SubvolumeVol(0,NumSubvolumes-1);
946   Standard_Real accumulatedVolume = 0.0;
947   Standard_Integer i, j, k, l = 0;
948   Standard_Real x = Xmin;
949   for ( i = 0; i < NumXsubvolumes; i++ )
950   {
951     Standard_Real y = Ymin;
952     for ( j = 0; j < NumYsubvolumes; j++ )
953     {
954       Standard_Real z = Zmin;
955       for ( k = 0; k < NumZsubvolumes; k++ )
956       {
957         P.SetX(x);
958         P.SetY(y);
959         P.SetZ(z);
960         TopoDS_Shape aSubvolume = BRepPrimAPI_MakeBox(P, StepX, StepY, StepZ).Solid();
961         di<<"Info: box b_"<<l<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" "<<StepX<<" "<<StepY<<" "<<StepZ<<"\n";
962         if ( aSubvolume.IsNull())
963         {
964           di << "Error: could not construct subvolume " << l << "\n";
965           return 1;
966         }
967         SubvolumeSolid.SetValue(l,aSubvolume);
968         GProp_GProps subvolumeVProps;
969         BRepGProp::VolumeProperties (SubvolumeSolid(l), subvolumeVProps);
970         const Standard_Real vol = subvolumeVProps.Mass();
971         di << "Info: original subvolume " << l << " volume = " << vol << "\n";
972         SubvolumeVol.SetValue(l,vol);
973         accumulatedVolume += vol;
974         l++;
975         z += StepZ;
976       }
977       y += StepY;
978     }
979     x += StepX;
980   }
981   di << "Info: Accumulated mesh volume = " << accumulatedVolume << "\n";
982
983   //  
984   // trim mesh to cutSolid
985   //
986   accumulatedVolume = 0.0;
987   for ( l = 0; l < NumSubvolumes; l++ )
988   {
989     TopoDS_Shape copySolid = BRepBuilderAPI_Copy(cutSolid).Shape();
990
991     // perform common
992 //////////#if ! defined(BRepAlgoAPI_def01)
993 //////////    BRepAlgoAPI_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l));
994 //////////#else
995 //////////    BRepAlgo_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l));
996 //////////#endif
997 //////////    if (!common.IsDone())
998 //////////    {
999 //////////      cout << "Error: could not construct a common solid " << l << endl;
1000 //////////      return 1;
1001 //////////    }
1002
1003     TopoDS_Shape aCommonShape;
1004     if (IsBRepAlgoAPI) {
1005       di << "BRepAlgoAPI_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l))" <<"\n";
1006       BRepAlgoAPI_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l));
1007       if (!common.IsDone())
1008         {
1009           di << "Error: could not construct a common solid " << l << "\n";
1010           return 1;
1011         }
1012       aCommonShape = common.Shape();
1013     } else {
1014       di << "BRepAlgo_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l))" <<"\n";
1015       BRepAlgo_Common common(copySolid/*cutSolid*/, SubvolumeSolid(l));
1016       if (!common.IsDone())
1017         {
1018           di << "Error: could not construct a common solid " << l << "\n";
1019           return 1;
1020         }
1021       aCommonShape = common.Shape();
1022     }
1023
1024     // see if we have a solid
1025     found_solid = 0;
1026     TopoDS_Shape commonShape;
1027     //////////for (Ex.Init(common.Shape(), TopAbs_SOLID); Ex.More(); Ex.Next())
1028     for (Ex.Init(aCommonShape, TopAbs_SOLID); Ex.More(); Ex.Next())
1029     {
1030       TopoDS_Solid sol = TopoDS::Solid(Ex.Current());
1031       if (!sol.IsNull()) { commonShape = sol; found_solid++; }
1032     }
1033     if ( found_solid != 1 )
1034     {
1035       di << "Info: Common operation " << l << " produced " << found_solid << " solids" << "\n";
1036     }
1037     else
1038     {
1039       SubvolumeSolid.SetValue(l,commonShape);
1040       GProp_GProps subvolumeVProps;
1041       BRepGProp::VolumeProperties (SubvolumeSolid(l), subvolumeVProps);
1042       const Standard_Real vol = subvolumeVProps.Mass();
1043       const Standard_Boolean err = (vol > SubvolumeVol(l)) || (vol <= 0.0);
1044       //cout << (err? "ERROR" : "Info") << ": final subvolume " << l << " volume = " << vol << endl;
1045       if (err)
1046         di << "ERROR" << ": final subvolume " << l << " volume = " << vol << "\n";
1047       else
1048         di << "Info" << ": final subvolume " << l << " volume = " << vol << "\n";
1049       accumulatedVolume += vol;
1050       if (err)
1051       {
1052         char astr[80];
1053         sprintf(astr,"e_%d",l);
1054         DBRep::Set(astr,commonShape);
1055       }
1056     }
1057   }
1058   di << "Info: Accumulated meshed volume = " << accumulatedVolume << "\n";
1059
1060   return 0;     
1061 }
1062
1063 void QARicardo::Commands(Draw_Interpretor& theCommands) {
1064   char *group = "QARicardo";
1065
1066   theCommands.Add ("OCC332", "OCC332 [wall_thickness [dia1 [dia2 [length [major_radius]]]]]", __FILE__, OCC332bug, group);
1067   //////theCommands.Add("OCC544", "OCC544 [[[[[wT [[[[d1 [[[d2 [[R [length]]]]]", __FILE__, OCC544, group);
1068   theCommands.Add("OCC544", "OCC544 [[[[[wT [[[[d1 [[[d2 [[R [length [BRepAlgoAPI/BRepAlgo = 1/0]]]]]]", __FILE__, OCC544, group);
1069   //////theCommands.Add("OCC817", "OCC817 result mesh_delta", __FILE__, OCC817, group);
1070   theCommands.Add("OCC817", "OCC817 result mesh_delta [BRepAlgoAPI/BRepAlgo = 1/0]", __FILE__, OCC817, group);
1071
1072   return;
1073 }