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()
68 setName ("Conversion to BSpline curves and surfaces");
70 myNbFuncs = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
71 myNbSamples = myNbFuncs;
75 //////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////
79 void Convert_Presentation::DoSample()
81 ((COCCDemoApp*) AfxGetApp())->SetSampleName (L"Convert");
82 ((COCCDemoApp*) AfxGetApp())->SetSamplePath (L"..\\..\\10_Convert");
83 getAISContext()->EraseAll();
84 if (myIndex >=0 && myIndex < myNbFuncs)
85 (this->*SampleFuncs[myIndex])();
88 //================================================================
89 // Function : Convert_Presentation::drawSurfaceAndItsBSpline
91 //================================================================
92 void Convert_Presentation::drawSurfaceAndItsBSpline(Handle_Geom_Surface theSurface,
93 const Standard_CString theName,
94 TCollection_AsciiString& theText)
96 TCollection_AsciiString aTitle ("Converting ");
98 aTitle += " to BSpline surface";
101 " Handle_Geom_BSplineSurface aBSplineSurface = " EOL
102 " GeomConvert::SurfaceToBSplineSurface(aSurface);" EOL;
104 setResultTitle (aTitle.ToCString());
105 setResultText (theText.ToCString());
107 drawSurface (theSurface, SurfaceColor);
109 if (WAIT_A_LITTLE) return;
111 Handle_Geom_BSplineSurface aBSplineSurface = GeomConvert::SurfaceToBSplineSurface(theSurface);
113 _ASSERTE(!aBSplineSurface.IsNull());
115 drawSurface (aBSplineSurface, BSplineSurfaceColor);
118 //================================================================
119 // Function : Convert_Presentation::drawCurveAndItsBSpline
121 //================================================================
122 void Convert_Presentation::drawCurveAndItsBSpline(Handle_Geom_Curve theCurve,
123 const Standard_CString theName,
124 TCollection_AsciiString& theText)
126 TCollection_AsciiString aTitle ("Converting ");
128 aTitle += " to BSpline curve";
131 " Handle_Geom_BSplineCurve aBSpline = " EOL
132 " GeomConvert::CurveToBSplineCurve(aCurve);" EOL;
134 setResultTitle (aTitle.ToCString());
135 setResultText (theText.ToCString());
137 drawCurve (theCurve, CurveColor);
139 if (WAIT_A_LITTLE) return;
141 Handle_Geom_BSplineCurve aBSpline = GeomConvert::CurveToBSplineCurve(theCurve);
143 drawCurve (aBSpline, BSplineColor);
147 //////////////////////////////////////////////////////////////////////
149 //////////////////////////////////////////////////////////////////////
151 //================================================================
152 // Function : Convert_Presentation::sampleCircle
154 //================================================================
155 void Convert_Presentation::sampleCircle()
157 gp_Pnt aOrigin (0,0,0);
159 gp_Ax2 aAxis (aOrigin, aDir);
160 Standard_Real aRadius = 300;
161 Handle_Geom_Circle aCurve = new Geom_Circle (aAxis, aRadius);
163 TCollection_AsciiString aText (
164 " gp_Pnt aOrigin (0,0,0);" EOL
165 " gp_Dir aDir (1,0,0);" EOL
166 " gp_Ax2 aAxis (aOrigin, aDir);" EOL
167 " Standard_Real aRadius = 300;" EOL
168 " Handle(Geom_Circle) aCurve = new Geom_Circle (aAxis, aRadius);" EOL
170 drawCurveAndItsBSpline (aCurve, "Circle", aText);
173 //================================================================
174 // Function : Convert_Presentation::sampleEllipse
176 //================================================================
177 void Convert_Presentation::sampleEllipse()
179 gp_Pnt aOrigin (0,0,0);
181 gp_Ax2 aMajorAxis (aOrigin, aDir);
182 Standard_Real aMajorRadius = 300;
183 Standard_Real aMinorRadius = 150;
184 Handle(Geom_Ellipse) aCurve =
185 new Geom_Ellipse (aMajorAxis, aMajorRadius, aMinorRadius);
187 TCollection_AsciiString aText (
188 " gp_Pnt aOrigin (0,0,0);" EOL
189 " gp_Dir aDir (1,0,0);" EOL
190 " gp_Ax2 aAxis (aOrigin, aDir);" EOL
191 " Standard_Real aMajorRadius = 300;" EOL
192 " Standard_Real aMinorRadius = 150;" EOL
193 " Handle(Geom_Ellipse) aCurve = " EOL
194 " new Geom_Ellipse (aAxis, aMajorRadius, aMinorRadius);" EOL
196 drawCurveAndItsBSpline (aCurve, "Ellipse", aText);
199 //================================================================
200 // Function : Convert_Presentation::sampleBezier
202 //================================================================
203 void Convert_Presentation::sampleBezier()
205 TCollection_AsciiString aText (
206 " Standard_Real aPolesCoords[][3] = {" EOL
207 " {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
208 " {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
209 " {7,0,0},{8,0,0},{8,1,0}" EOL
211 " TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*2));" EOL
213 " for (Standard_Integer i=1; i <= aPoles.Upper(); i++)" EOL
214 " aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*100, " EOL
215 " aPolesCoords[i-1][1]*100, " EOL
216 " aPolesCoords[i-1][2]*100);" EOL
218 " Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);" EOL
221 Standard_Real aPolesCoords[][3] = {
222 {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},
223 {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},
224 {7,0,0},{8,0,0},{8,1,0}
226 TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*3));
228 for (Standard_Integer i=1; i <= aPoles.Upper(); i++)
229 aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*150-500,
230 aPolesCoords[i-1][1]*150,
231 aPolesCoords[i-1][2]*150);
233 Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);
235 drawCurveAndItsBSpline (aCurve, "BezierCurve", aText);
238 //================================================================
239 // Function : Convert_Presentation::sampleBezierSurface
241 //================================================================
242 void Convert_Presentation::sampleBezierSurface()
244 getAISContext()->EraseAll();
246 Standard_CString aName = "BezierSurface";
247 // Create a BezierSurface
248 TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points
249 TColStd_Array2OfReal aWeights(1,2,1,4);
250 // initializing array of points
251 aPoles.SetValue(1,1,gp_Pnt(0,10,0)); aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));
252 aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));
253 aPoles.SetValue(2,1,gp_Pnt(0,0,0)); aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));
254 aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3)); aPoles.SetValue(2,4,gp_Pnt(10,0,0));
256 for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)
257 for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)
258 aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);
259 //initializing array of weights
260 aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);
261 aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);
262 aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);
263 aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);
264 Handle(Geom_BezierSurface) aSurface =
265 new Geom_BezierSurface(aPoles, aWeights);
267 TCollection_AsciiString aText (
268 " // Create a BezierSurface" EOL
269 " TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points" EOL
270 " TColStd_Array2OfReal aWeights(1,2,1,4);" EOL
271 " // initializing array of points" EOL
272 " aPoles.SetValue(1,1,gp_Pnt(0,10,0)); aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));" EOL
273 " aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));" EOL
274 " aPoles.SetValue(2,1,gp_Pnt(0,0,0)); aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));" EOL
275 " aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3)); aPoles.SetValue(2,4,gp_Pnt(10,0,0)); " EOL
276 " // scaling poles" EOL
277 " for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)" EOL
278 " for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)" EOL
279 " aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);" EOL
280 " //initializing array of weights" EOL
281 " aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);" EOL
282 " aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);" EOL
283 " aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);" EOL
284 " aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);" EOL
285 " Handle(Geom_BezierSurface) aSurface =" EOL
286 " new Geom_BezierSurface(aPoles, aWeights);" EOL
289 drawSurfaceAndItsBSpline (aSurface, aName, aText);
292 //================================================================
293 // Function : OCCDemo_Presentation::sampleCylindricalSurface
295 //================================================================
296 void Convert_Presentation::sampleCylindricalSurface()
298 getAISContext()->EraseAll();
300 Standard_CString aName = "Cylindrical Surface";
301 TCollection_AsciiString aText (
302 " // creating an axis parallel to Y axis" EOL
303 " gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL EOL
305 " // creating a cylindrical surface along anAx with radius = 100" EOL
306 " Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);" EOL EOL
308 " // only finit surfaces can be converted to BSpline surfaces, " EOL
309 " // cylindrical surface is infinite, it must be trimmed" EOL
310 " Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
311 " new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
313 // creating an axis parallel to Y axis
314 gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
316 // creating a cylindrical surface along anAx with radius = 4
317 Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);
319 // only finit surfaces can be converted to BSpline surfaces,
320 // cylindrical surface is infinite, it must be trimmed
321 Handle(Geom_RectangularTrimmedSurface) aSurface =
322 new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*M_PI, -1000, 1000, Standard_True, Standard_True);
324 drawSurfaceAndItsBSpline(aSurface, aName, aText);
327 //================================================================
328 // Function : OCCDemo_Presentation::sampleRevolSurface
330 //================================================================
331 void Convert_Presentation::sampleRevolSurface()
335 TranslateView(-176.84682, -102.12892);
336 SetViewScale(0.69326);
338 getAISContext()->EraseAll();
340 Standard_CString aName = "Surface of revolution";
341 TCollection_AsciiString aText (
342 " // creating a curve for revolution. Let it be a Bezier curve." EOL
343 " Handle(Geom_BezierCurve) aBezierCurve;" EOL EOL
345 " // array of the bezier curve poles" EOL
346 " TColgp_Array1OfPnt aPoles(1,4);" EOL
347 " // array of the poles' weights" EOL
348 " TColStd_Array1OfReal aWeights(1,4);" EOL EOL
350 " aPoles(1) = gp_Pnt(0, 0, 0); aWeights(1) = 1;" EOL
351 " aPoles(2) = gp_Pnt(150, 250, 0); aWeights(2) =75;" EOL
352 " aPoles(3) = gp_Pnt(350, 150, 0); aWeights(3) =120;" EOL
353 " aPoles(4) = gp_Pnt(500, 500, 0); aWeights(4) = 1;" EOL EOL
355 " // creating a bezier curve" EOL
356 " aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);" EOL EOL
358 " // creating a surface of revolution of the bezier curve around Y axis" EOL
359 " gp_Ax1 anAx(gp_Pnt(0, 0, 0), gp_Dir(0,1,0));" EOL
360 " Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);" EOL
363 // array of the bezier curve poles
364 TColgp_Array1OfPnt aPoles(1,4);
365 // array of the poles' weights
366 TColStd_Array1OfReal aWeights(1,4);
368 aPoles(1) = gp_Pnt(0, 0, 0); aWeights(1) = 1;
369 aPoles(2) = gp_Pnt(150, 250, 0); aWeights(2) =75;
370 aPoles(3) = gp_Pnt(350, 150, 0); aWeights(3) =120;
371 aPoles(4) = gp_Pnt(500, 500, 0); aWeights(4) = 1;
373 Handle(Geom_BezierCurve) aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);
374 drawCurve(aBezierCurve);
376 // creating a surface of revolution of the bezier curve around Y axis
377 gp_Ax1 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
378 Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);
380 drawSurfaceAndItsBSpline (aSurface, aName, aText);
384 //================================================================
385 // Function : Convert_Presentation::sampleToroidalSurface
387 //================================================================
388 void Convert_Presentation::sampleToroidalSurface()
390 getAISContext()->EraseAll();
392 Standard_CString aName = "Toroidal surface";
393 TCollection_AsciiString aText (
394 " // creating an axis parallel to Y axis" EOL
395 " gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL
396 " // creating a toroidal surface with major radius = 240 and minor radius = 120" EOL
397 " Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);" EOL);
399 // creating an axis parallel to Y axis
400 gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
401 // creating a toroidal surface with major radius = 240 and minor radius = 120
402 Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);
404 drawSurfaceAndItsBSpline(aSurface, aName, aText);
407 //================================================================
408 // Function : Convert_Presentation::sampleConicalSurface
410 //================================================================
411 void Convert_Presentation::sampleConicalSurface()
413 getAISContext()->EraseAll();
415 Standard_CString aName = "Conical surface";
416 TCollection_AsciiString aText (
417 " // creating an axis parallel to Z axis" EOL
418 " gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); " EOL
419 " // creating a conical surface with base radius = 10 and angle = 20 deg" EOL
420 " Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,PI/9., 10);" EOL EOL
422 " // only finit surfaces can be converted to BSpline surfaces, " EOL
423 " // conical surface is infinite, it must be trimmed" EOL
424 " Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
425 " new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
427 // creating an axis parallel to Z axis
428 gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1));
429 // creating a conical surface with base radius = 10 and angle = 20 deg
430 Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,M_PI/9., 10);
432 // only finit surfaces can be converted to BSpline surfaces,
433 // conical surface is infinite, it must be trimmed
434 Handle(Geom_RectangularTrimmedSurface) aSurface =
435 new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*M_PI, -1000, 1000, Standard_True, Standard_True);
437 drawSurfaceAndItsBSpline(aSurface, aName, aText);
440 //================================================================
441 // Function : Convert_Presentation::sampleSphericalSurface
443 //================================================================
444 void Convert_Presentation::sampleSphericalSurface()
446 getAISContext()->EraseAll();
448 Standard_CString aName = "Spherical surface";
449 TCollection_AsciiString aText (
450 "// creating an axis parallel to Z axis" EOL
451 "gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1));" EOL
452 "// creating a spherical surface with radius = 300" EOL
453 "Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);" EOL);
455 // creating an axis parallel to Z axis
456 gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1));
457 // creating a spherical surface with radius = 300
458 Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);
460 drawSurfaceAndItsBSpline(aSurface, aName, aText);