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