Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / occtdemo / Convert / Convert_Presentation.cpp
1 // Convert_Presentation.cpp: implementation of the Convert_Presentation class.
2 // Conversion of elementary geometry to BSpline curves and surfaces
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "Convert_Presentation.h"
7
8 #include <Quantity_Color.hxx>
9
10 #include <gp_Dir.hxx>
11 #include <gp_Ax2.hxx>
12
13 #include <TColgp_Array1OfPnt.hxx>
14 #include <TColStd_Array1OfReal.hxx>
15 #include <TColgp_Array2OfPnt.hxx>
16 #include <TColStd_Array2OfReal.hxx>
17
18 #include <GeomConvert.hxx>
19
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_Circle.hxx>
22 #include <Geom_Ellipse.hxx>
23 #include <Geom_BezierCurve.hxx>
24 #include <Geom_BSplineCurve.hxx>
25 #include <Geom_SphericalSurface.hxx>
26 #include <Geom_CylindricalSurface.hxx>
27 #include <Geom_RectangularTrimmedSurface.hxx>
28 #include <Geom_SurfaceOfRevolution.hxx>
29 #include <Geom_ToroidalSurface.hxx>
30 #include <Geom_ConicalSurface.hxx>
31
32
33 // Initialization of global variable with an instance of this class
34 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Convert_Presentation;
35
36 // Initialization of array of samples
37 const Convert_Presentation::PSampleFuncType Convert_Presentation::SampleFuncs[] =
38 {
39   &Convert_Presentation::sampleCircle,
40   &Convert_Presentation::sampleEllipse,
41   &Convert_Presentation::sampleBezier,
42   &Convert_Presentation::sampleBezierSurface,
43   &Convert_Presentation::sampleCylindricalSurface,
44   &Convert_Presentation::sampleRevolSurface,
45   &Convert_Presentation::sampleToroidalSurface,
46   &Convert_Presentation::sampleConicalSurface,
47   &Convert_Presentation::sampleSphericalSurface
48 };
49
50 // Colors of objects
51 static const Quantity_Color CurveColor       (1,1,0, Quantity_TOC_RGB);      // yellow
52 static const Quantity_Color SurfaceColor     (1,1,0, Quantity_TOC_RGB);      // yellow
53 static const Quantity_Color BSplineColor     (1,0.647,0, Quantity_TOC_RGB);  // orange
54 static const Quantity_Color BSplineSurfaceColor (0,0,1, Quantity_TOC_RGB);   // blue
55
56 #ifdef WNT
57  #define EOL "\r\n"
58 #else
59  #define EOL "\n"
60 #endif
61
62 //////////////////////////////////////////////////////////////////////
63 // Construction/Destruction
64 //////////////////////////////////////////////////////////////////////
65
66 Convert_Presentation::Convert_Presentation()
67 {
68   FitMode = true;
69   setName ("Conversion to BSpline curves and surfaces");
70   myIndex = 0;
71   myNbFuncs = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
72   myNbSamples = myNbFuncs;
73 }
74
75 //////////////////////////////////////////////////////////////////////
76 // Sample execution
77 //////////////////////////////////////////////////////////////////////
78
79 void Convert_Presentation::DoSample()
80 {
81   getAISContext()->EraseAll();
82   if (myIndex >=0 && myIndex < myNbFuncs)
83     (this->*SampleFuncs[myIndex])();
84 }
85
86
87 //================================================================
88 // Function : Convert_Presentation::drawSurfaceAndItsBSpline
89 // Purpose  : 
90 //================================================================
91 void Convert_Presentation::drawSurfaceAndItsBSpline(Handle_Geom_Surface theSurface, 
92                                                     const Standard_CString theName, 
93                                                     TCollection_AsciiString& theText)
94 {
95   TCollection_AsciiString aTitle ("Converting ");
96   aTitle += theName;
97   aTitle += " to BSpline surface";
98
99   theText += EOL
100     "  Handle_Geom_BSplineSurface aBSplineSurface = " EOL
101     "    GeomConvert::SurfaceToBSplineSurface(aSurface);" EOL;
102
103   setResultTitle (aTitle.ToCString());
104   setResultText (theText.ToCString());
105
106   drawSurface (theSurface, SurfaceColor);
107
108   if (WAIT_A_LITTLE) return;
109
110   Handle_Geom_BSplineSurface aBSplineSurface = GeomConvert::SurfaceToBSplineSurface(theSurface);
111
112   _ASSERTE(!aBSplineSurface.IsNull());
113
114   drawSurface (aBSplineSurface, BSplineSurfaceColor);
115 }
116
117 //================================================================
118 // Function : Convert_Presentation::drawCurveAndItsBSpline
119 // Purpose  : 
120 //================================================================
121 void Convert_Presentation::drawCurveAndItsBSpline(Handle_Geom_Curve theCurve, 
122                                                   const Standard_CString theName, 
123                                                   TCollection_AsciiString& theText)
124 {
125   TCollection_AsciiString aTitle ("Converting ");
126   aTitle += theName;
127   aTitle += " to BSpline curve";
128
129   theText += EOL
130     "  Handle_Geom_BSplineCurve aBSpline = " EOL
131     "    GeomConvert::CurveToBSplineCurve(aCurve);" EOL;
132
133   setResultTitle (aTitle.ToCString());
134   setResultText (theText.ToCString());
135   
136   drawCurve (theCurve, CurveColor);
137   
138   if (WAIT_A_LITTLE) return;
139
140   Handle_Geom_BSplineCurve aBSpline = GeomConvert::CurveToBSplineCurve(theCurve);
141
142   drawCurve (aBSpline, BSplineColor);
143 }
144
145
146 //////////////////////////////////////////////////////////////////////
147 // Sample functions
148 //////////////////////////////////////////////////////////////////////
149
150 //================================================================
151 // Function : Convert_Presentation::sampleCircle
152 // Purpose  : 
153 //================================================================
154 void Convert_Presentation::sampleCircle()
155 {
156   gp_Pnt aOrigin (0,0,0);
157   gp_Dir aDir (1,0,0);
158   gp_Ax2 aAxis (aOrigin, aDir);
159   Standard_Real aRadius = 300;
160   Handle_Geom_Circle aCurve = new Geom_Circle (aAxis, aRadius);
161
162   TCollection_AsciiString aText (
163     "  gp_Pnt aOrigin (0,0,0);" EOL
164     "  gp_Dir aDir (1,0,0);" EOL
165     "  gp_Ax2 aAxis (aOrigin, aDir);" EOL
166     "  Standard_Real aRadius = 300;" EOL
167     "  Handle(Geom_Circle) aCurve = new Geom_Circle (aAxis, aRadius);" EOL
168     );
169   drawCurveAndItsBSpline (aCurve, "Circle", aText);
170 }
171
172 //================================================================
173 // Function : Convert_Presentation::sampleEllipse
174 // Purpose  : 
175 //================================================================
176 void Convert_Presentation::sampleEllipse()
177 {
178   gp_Pnt aOrigin (0,0,0);
179   gp_Dir aDir (1,0,0);
180   gp_Ax2 aMajorAxis (aOrigin, aDir);
181   Standard_Real aMajorRadius = 300;
182   Standard_Real aMinorRadius = 150;
183   Handle(Geom_Ellipse) aCurve = 
184     new Geom_Ellipse (aMajorAxis, aMajorRadius, aMinorRadius);
185
186   TCollection_AsciiString aText (
187     "  gp_Pnt aOrigin (0,0,0);" EOL
188     "  gp_Dir aDir (1,0,0);" EOL
189     "  gp_Ax2 aAxis (aOrigin, aDir);" EOL
190     "  Standard_Real aMajorRadius = 300;" EOL
191     "  Standard_Real aMinorRadius = 150;" EOL
192     "  Handle(Geom_Ellipse) aCurve = " EOL
193     "    new Geom_Ellipse (aAxis, aMajorRadius, aMinorRadius);" EOL
194     );
195   drawCurveAndItsBSpline (aCurve, "Ellipse", aText);
196 }
197
198 //================================================================
199 // Function : Convert_Presentation::sampleBezier
200 // Purpose  : 
201 //================================================================
202 void Convert_Presentation::sampleBezier()
203 {
204   TCollection_AsciiString aText (
205     "  Standard_Real aPolesCoords[][3] = {" EOL
206     "    {0,0,0},{0,1,0},{1,1,0},{1,2,0},{2,2,0},{2,1,0},{3,1,0},{3,0,0},{2,0,0},{2,-1,0}," EOL
207     "    {3,-1,0},{3,-2,0},{4,-2,0},{4,-1,0},{5,-1,0},{5,0,0},{6,0,0},{6,-1,0},{7,-1,0}," EOL
208     "    {7,0,0},{8,0,0},{8,1,0}" EOL
209     "  };" EOL
210     "  TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*2));" EOL
211     " " EOL
212     "  for (Standard_Integer i=1; i <= aPoles.Upper(); i++)" EOL
213     "    aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*100, " EOL
214     "                        aPolesCoords[i-1][1]*100, " EOL
215     "                        aPolesCoords[i-1][2]*100);" EOL
216     "  " EOL
217     "  Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);" EOL
218     );
219
220   Standard_Real aPolesCoords[][3] = {
221     {0,0,0},{0,1,0},{1,1,0},{1,2,0},{2,2,0},{2,1,0},{3,1,0},{3,0,0},{2,0,0},{2,-1,0},
222     {3,-1,0},{3,-2,0},{4,-2,0},{4,-1,0},{5,-1,0},{5,0,0},{6,0,0},{6,-1,0},{7,-1,0},
223     {7,0,0},{8,0,0},{8,1,0}
224   };
225   TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*3));
226  
227   for (Standard_Integer i=1; i <= aPoles.Upper(); i++)
228     aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*150-500, 
229                         aPolesCoords[i-1][1]*150, 
230                         aPolesCoords[i-1][2]*150);
231   
232   Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);
233
234   drawCurveAndItsBSpline (aCurve, "BezierCurve", aText);
235 }
236
237 //================================================================
238 // Function : Convert_Presentation::sampleBezierSurface
239 // Purpose  : 
240 //================================================================
241 void Convert_Presentation::sampleBezierSurface()
242 {
243   getAISContext()->EraseAll();
244
245   Standard_CString aName = "BezierSurface";
246   // Create a BezierSurface
247   TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points
248   TColStd_Array2OfReal aWeights(1,2,1,4);
249   // initializing array of points
250   aPoles.SetValue(1,1,gp_Pnt(0,10,0));     aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));
251   aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));
252   aPoles.SetValue(2,1,gp_Pnt(0,0,0));      aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));
253   aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3));  aPoles.SetValue(2,4,gp_Pnt(10,0,0));  
254   // scaling poles
255   for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)
256     for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)
257       aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);
258   //initializing array of weights
259   aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);
260   aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);
261   aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);
262   aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);
263   Handle(Geom_BezierSurface) aSurface =
264     new Geom_BezierSurface(aPoles, aWeights);
265
266   TCollection_AsciiString aText (
267     "  // Create a BezierSurface" EOL
268     "  TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points" EOL
269     "  TColStd_Array2OfReal aWeights(1,2,1,4);" EOL
270     "  // initializing array of points" EOL
271     "  aPoles.SetValue(1,1,gp_Pnt(0,10,0));     aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));" EOL
272     "  aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));" EOL
273     "  aPoles.SetValue(2,1,gp_Pnt(0,0,0));      aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));" EOL
274     "  aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3));  aPoles.SetValue(2,4,gp_Pnt(10,0,0));  " EOL
275     "  // scaling poles" EOL
276     "  for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)" EOL
277     "    for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)" EOL
278     "      aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);" EOL
279     "  //initializing array of weights" EOL
280     "  aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);" EOL
281     "  aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);" EOL
282     "  aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);" EOL
283     "  aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);" EOL
284     "  Handle(Geom_BezierSurface) aSurface =" EOL
285     "    new Geom_BezierSurface(aPoles, aWeights);" EOL
286     );
287
288   drawSurfaceAndItsBSpline (aSurface, aName, aText);
289 }
290
291 //================================================================
292 // Function : OCCDemo_Presentation::sampleCylindricalSurface
293 // Purpose  : 
294 //================================================================
295 void Convert_Presentation::sampleCylindricalSurface()
296 {
297   getAISContext()->EraseAll();
298
299   Standard_CString aName = "Cylindrical Surface";
300   TCollection_AsciiString aText (
301     "  // creating an axis parallel to Y axis" EOL
302     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL EOL
303
304     "  // creating a cylindrical surface along anAx with radius = 100" EOL
305     "  Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);" EOL EOL
306
307     "  // only finit surfaces can be converted to BSpline surfaces, " EOL
308     "  // cylindrical surface is infinite, it must be trimmed" EOL
309     "  Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
310     "    new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
311
312   // creating an axis parallel to Y axis
313   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
314
315   // creating a cylindrical surface along anAx with radius = 4
316   Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);
317
318   // only finit surfaces can be converted to BSpline surfaces, 
319   // cylindrical surface is infinite, it must be trimmed
320   Handle(Geom_RectangularTrimmedSurface) aSurface = 
321     new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);
322   
323   drawSurfaceAndItsBSpline(aSurface, aName, aText);
324 }
325
326 //================================================================
327 // Function : OCCDemo_Presentation::sampleRevolSurface
328 // Purpose  : 
329 //================================================================
330 void Convert_Presentation::sampleRevolSurface()
331 {
332   FitMode=false;
333   ResetView();
334   SetViewCenter(176.84682,102.12892);
335   SetViewScale(0.69326);
336
337   getAISContext()->EraseAll();
338
339   Standard_CString aName = "Surface of revolution";
340   TCollection_AsciiString aText (
341     "  // creating a curve for revolution.  Let it be a Bezier curve." EOL
342     "  Handle(Geom_BezierCurve) aBezierCurve;" EOL EOL
343
344     "  // array of the bezier curve poles" EOL
345     "  TColgp_Array1OfPnt aPoles(1,4);" EOL
346     "  // array of the poles' weights" EOL
347     "  TColStd_Array1OfReal aWeights(1,4);" EOL EOL
348
349     "  aPoles(1) = gp_Pnt(0, 0, 0);      aWeights(1) = 1;" EOL
350     "  aPoles(2) = gp_Pnt(150, 250, 0);  aWeights(2) =75;" EOL
351     "  aPoles(3) = gp_Pnt(350, 150, 0);  aWeights(3) =120;" EOL
352     "  aPoles(4) = gp_Pnt(500, 500, 0);  aWeights(4) = 1;" EOL EOL
353
354     "  // creating a bezier curve" EOL
355     "  aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);" EOL EOL
356
357     "  // creating a surface of revolution of the bezier curve around Y axis" EOL
358     "  gp_Ax1 anAx(gp_Pnt(0, 0, 0), gp_Dir(0,1,0));" EOL
359     "  Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);" EOL
360     );
361   
362   // array of the bezier curve poles
363   TColgp_Array1OfPnt aPoles(1,4);
364   // array of the poles' weights
365   TColStd_Array1OfReal aWeights(1,4);
366
367   aPoles(1) = gp_Pnt(0, 0, 0);      aWeights(1) = 1;
368   aPoles(2) = gp_Pnt(150, 250, 0);  aWeights(2) =75;
369   aPoles(3) = gp_Pnt(350, 150, 0);  aWeights(3) =120;
370   aPoles(4) = gp_Pnt(500, 500, 0);  aWeights(4) = 1;
371
372   Handle(Geom_BezierCurve) aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);
373   drawCurve(aBezierCurve);
374
375   // creating a surface of revolution of the bezier curve around Y axis
376   gp_Ax1 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
377   Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);
378
379   drawSurfaceAndItsBSpline (aSurface, aName, aText);
380   FitMode=true;
381 }
382
383 //================================================================
384 // Function : Convert_Presentation::sampleToroidalSurface
385 // Purpose  : 
386 //================================================================
387 void Convert_Presentation::sampleToroidalSurface()
388 {
389   getAISContext()->EraseAll();
390
391   Standard_CString aName = "Toroidal surface";
392   TCollection_AsciiString aText (
393     "  // creating an axis parallel to Y axis" EOL
394     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL
395     "  // creating a toroidal surface with major radius = 240 and minor radius = 120" EOL
396     "  Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);" EOL);
397
398   // creating an axis parallel to Y axis 
399   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0)); 
400   // creating a toroidal surface with major radius = 240 and minor radius = 120
401   Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);
402
403   drawSurfaceAndItsBSpline(aSurface, aName, aText);
404 }
405
406 //================================================================
407 // Function : Convert_Presentation::sampleConicalSurface
408 // Purpose  : 
409 //================================================================
410 void Convert_Presentation::sampleConicalSurface()
411 {
412   getAISContext()->EraseAll();
413
414   Standard_CString aName = "Conical surface";
415   TCollection_AsciiString aText (
416     "  // creating an axis parallel to Z axis" EOL
417     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); " EOL
418     "  // creating a conical surface with base radius = 10 and angle = 20 deg" EOL
419     "  Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,PI/9., 10);" EOL EOL
420
421     "  // only finit surfaces can be converted to BSpline surfaces, " EOL
422     "  // conical surface is infinite, it must be trimmed" EOL
423     "  Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
424     "    new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
425
426   // creating an axis parallel to Z axis 
427   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); 
428   // creating a conical surface with base radius = 10 and angle = 20 deg
429   Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,PI/9., 10);
430
431   // only finit surfaces can be converted to BSpline surfaces, 
432   // conical surface is infinite, it must be trimmed
433   Handle(Geom_RectangularTrimmedSurface) aSurface = 
434     new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);
435
436   drawSurfaceAndItsBSpline(aSurface, aName, aText);
437 }
438
439 //================================================================
440 // Function : Convert_Presentation::sampleSphericalSurface
441 // Purpose  : 
442 //================================================================
443 void Convert_Presentation::sampleSphericalSurface()
444 {
445   getAISContext()->EraseAll();
446
447   Standard_CString aName = "Spherical surface";
448   TCollection_AsciiString aText (
449     "// creating an axis parallel to Z axis" EOL
450     "gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1));" EOL
451     "// creating a spherical surface with radius = 300" EOL
452     "Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);" EOL);
453
454   // creating an axis parallel to Z axis 
455   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); 
456   // creating a spherical surface with radius = 300
457   Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);
458
459   drawSurfaceAndItsBSpline(aSurface, aName, aText);
460 }
461