1 // Convert_Presentation.cpp: implementation of the Convert_Presentation class.
2 // Conversion of elementary geometry to BSpline curves and surfaces
3 //////////////////////////////////////////////////////////////////////
6 #include "Convert_Presentation.h"
8 #include <Quantity_Color.hxx>
13 #include <TColgp_Array1OfPnt.hxx>
14 #include <TColStd_Array1OfReal.hxx>
15 #include <TColgp_Array2OfPnt.hxx>
16 #include <TColStd_Array2OfReal.hxx>
18 #include <GeomConvert.hxx>
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>
33 // Initialization of global variable with an instance of this class
34 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Convert_Presentation;
36 // Initialization of array of samples
37 const Convert_Presentation::PSampleFuncType Convert_Presentation::SampleFuncs[] =
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
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
62 //////////////////////////////////////////////////////////////////////
63 // Construction/Destruction
64 //////////////////////////////////////////////////////////////////////
66 Convert_Presentation::Convert_Presentation()
69 setName ("Conversion to BSpline curves and surfaces");
71 myNbFuncs = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
72 myNbSamples = myNbFuncs;
75 //////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////
79 void Convert_Presentation::DoSample()
81 getAISContext()->EraseAll();
82 if (myIndex >=0 && myIndex < myNbFuncs)
83 (this->*SampleFuncs[myIndex])();
87 //================================================================
88 // Function : Convert_Presentation::drawSurfaceAndItsBSpline
90 //================================================================
91 void Convert_Presentation::drawSurfaceAndItsBSpline(Handle(Geom_Surface) theSurface,
92 const Standard_CString theName,
93 TCollection_AsciiString& theText)
95 TCollection_AsciiString aTitle ("Converting ");
97 aTitle += " to BSpline surface";
100 " Handle(Geom_BSplineSurface) aBSplineSurface = " EOL
101 " GeomConvert::SurfaceToBSplineSurface(aSurface);" EOL;
103 setResultTitle (aTitle.ToCString());
104 setResultText (theText.ToCString());
106 drawSurface (theSurface, SurfaceColor);
108 if (WAIT_A_LITTLE) return;
110 Handle(Geom_BSplineSurface) aBSplineSurface = GeomConvert::SurfaceToBSplineSurface(theSurface);
112 _ASSERTE(!aBSplineSurface.IsNull());
114 drawSurface (aBSplineSurface, BSplineSurfaceColor);
117 //================================================================
118 // Function : Convert_Presentation::drawCurveAndItsBSpline
120 //================================================================
121 void Convert_Presentation::drawCurveAndItsBSpline(Handle(Geom_Curve) theCurve,
122 const Standard_CString theName,
123 TCollection_AsciiString& theText)
125 TCollection_AsciiString aTitle ("Converting ");
127 aTitle += " to BSpline curve";
130 " Handle(Geom_BSplineCurve) aBSpline = " EOL
131 " GeomConvert::CurveToBSplineCurve(aCurve);" EOL;
133 setResultTitle (aTitle.ToCString());
134 setResultText (theText.ToCString());
136 drawCurve (theCurve, CurveColor);
138 if (WAIT_A_LITTLE) return;
140 Handle(Geom_BSplineCurve) aBSpline = GeomConvert::CurveToBSplineCurve(theCurve);
142 drawCurve (aBSpline, BSplineColor);
146 //////////////////////////////////////////////////////////////////////
148 //////////////////////////////////////////////////////////////////////
150 //================================================================
151 // Function : Convert_Presentation::sampleCircle
153 //================================================================
154 void Convert_Presentation::sampleCircle()
156 gp_Pnt aOrigin (0,0,0);
158 gp_Ax2 aAxis (aOrigin, aDir);
159 Standard_Real aRadius = 300;
160 Handle(Geom_Circle) aCurve = new Geom_Circle (aAxis, aRadius);
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
169 drawCurveAndItsBSpline (aCurve, "Circle", aText);
172 //================================================================
173 // Function : Convert_Presentation::sampleEllipse
175 //================================================================
176 void Convert_Presentation::sampleEllipse()
178 gp_Pnt aOrigin (0,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);
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
195 drawCurveAndItsBSpline (aCurve, "Ellipse", aText);
198 //================================================================
199 // Function : Convert_Presentation::sampleBezier
201 //================================================================
202 void Convert_Presentation::sampleBezier()
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
210 " TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*2));" 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
217 " Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);" EOL
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}
225 TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*3));
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);
232 Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);
234 drawCurveAndItsBSpline (aCurve, "BezierCurve", aText);
237 //================================================================
238 // Function : Convert_Presentation::sampleBezierSurface
240 //================================================================
241 void Convert_Presentation::sampleBezierSurface()
243 getAISContext()->EraseAll();
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));
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);
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
288 drawSurfaceAndItsBSpline (aSurface, aName, aText);
291 //================================================================
292 // Function : OCCDemo_Presentation::sampleCylindricalSurface
294 //================================================================
295 void Convert_Presentation::sampleCylindricalSurface()
297 getAISContext()->EraseAll();
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
304 " // creating a cylindrical surface along anAx with radius = 100" EOL
305 " Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);" EOL EOL
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);
312 // creating an axis parallel to Y axis
313 gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
315 // creating a cylindrical surface along anAx with radius = 4
316 Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);
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);
323 drawSurfaceAndItsBSpline(aSurface, aName, aText);
326 //================================================================
327 // Function : OCCDemo_Presentation::sampleRevolSurface
329 //================================================================
330 void Convert_Presentation::sampleRevolSurface()
334 SetViewCenter(176.84682,102.12892);
335 SetViewScale(0.69326);
337 getAISContext()->EraseAll();
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
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
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
354 " // creating a bezier curve" EOL
355 " aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);" EOL EOL
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
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);
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;
372 Handle(Geom_BezierCurve) aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);
373 drawCurve(aBezierCurve);
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);
379 drawSurfaceAndItsBSpline (aSurface, aName, aText);
383 //================================================================
384 // Function : Convert_Presentation::sampleToroidalSurface
386 //================================================================
387 void Convert_Presentation::sampleToroidalSurface()
389 getAISContext()->EraseAll();
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);
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);
403 drawSurfaceAndItsBSpline(aSurface, aName, aText);
406 //================================================================
407 // Function : Convert_Presentation::sampleConicalSurface
409 //================================================================
410 void Convert_Presentation::sampleConicalSurface()
412 getAISContext()->EraseAll();
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
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);
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);
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);
436 drawSurfaceAndItsBSpline(aSurface, aName, aText);
439 //================================================================
440 // Function : Convert_Presentation::sampleSphericalSurface
442 //================================================================
443 void Convert_Presentation::sampleSphericalSurface()
445 getAISContext()->EraseAll();
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);
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);
459 drawSurfaceAndItsBSpline(aSurface, aName, aText);