Reply

C# Casting Face to Cylinder

[ Edited ]

 

Greetings,

I'm working with Solid Edge v20 using C#. Initially, I'm trying to develop a program to dump the contents of a file. (BTW, I would like to donate this program to this web site.) I have to admit that I'm new to C# and not proficient in the Microsoft technologies.

I'm having trouble casting a face to a cylinder. What I've tried is:

                    SolidEdgeGeometry.Cylinder cylinder = (SolidEdgeGeometry.Cylinder)face;

The runtime exception message is: Unable to cast COM object of type 'System.__ComObject' to interface type 'SolidEdgeGeometry.Cylinder'. This operation failed because the QueryInterface call on

the COM component for the interface with IID '{F925AA83-F74F-11D0-BC6A-0800360D1403}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Any suggestions?

 

This is the code I've got so far:

        static void DumpFace(SolidEdgeGeometry.Face face)

        {

            Console.WriteLine("\tface area: " + face.Area);

            object geometry = face.Geometry;

            SolidEdgeGeometry.GNTTypePropertyConstants faceType = (SolidEdgeGeometry.GNTTypePropertyConstants)geometry.GetType().InvokeMember("Type", BindingFlags.GetProperty, null, geometry, null);

            Console.WriteLine("\tface type: " + faceType);

            switch (faceType)

            {

                case SolidEdgeGeometry.GNTTypePropertyConstants.igBSplineSurface:

                    break;

                case SolidEdgeGeometry.GNTTypePropertyConstants.igCylinder:

                    SolidEdgeGeometry.Cylinder cylinder = (SolidEdgeGeometry.Cylinder)face;

                    System.Array basePoint = new double[3] { 0.0, 0.0, 0.0 };

                    basePoint.Initialize();

                    System.Array axisVector = new double[3] { 0.0, 0.0, 0.0 };

                    axisVector.Initialize();

                    double radius;

                    cylinder.GetCylinderData(ref basePoint, ref axisVector, out radius);

                    Console.WriteLine("\tbasePoint: (" + basePoint.GetValue(0) + ", " + basePoint.GetValue(1) + ", " + basePoint.GetValue(2) + ")");

                    Console.WriteLine("\taxisVector: (" + axisVector.GetValue(0) + ", " + axisVector.GetValue(1) + ", " + axisVector.GetValue(2) + ")");

                    Console.WriteLine("\tradius=" + radius);

                    break;

                case SolidEdgeGeometry.GNTTypePropertyConstants.igPlane:

                    System.Array parameters = new double[3] { 0.0, 0.0, 0.0 };

                    parameters.Initialize();

                    System.Array normals = new double[3] { 0.0, 0.0, 0.0 };

                    normals.Initialize();

                    face.GetNormal(1, ref parameters, ref normals);

                    Console.WriteLine("\tNormal: (" + normals.GetValue(0) + ", " + normals.GetValue(1) + ", " + normals.GetValue(2) + ")");

                    break;

                case SolidEdgeGeometry.GNTTypePropertyConstants.igSphere:

                    break;

                case SolidEdgeGeometry.GNTTypePropertyConstants.igTorus:

                    break;

                default:

                    Console.WriteLine("\tface type: Unknown");

                    break;

            }

        }

TIA,

Bob Monat

 

Posted by: Robert Monat
Post date: 4/11/2009 12:00:09 PM

4 REPLIES

RE: C# Casting Face to Cylinder

[ Edited ]

 

Bob,

I suggest usingSmiley FrustratedolidEdgeGeometry.Cylinder cylinder = face as SolidEdgeGeometry.Cylinder;if (cylinder != null){}

rather than:

SolidEdgeGeometry.Cylinder cylinder = (SolidEdgeGeometry.Cylinder)face;

As to why it's failing, I'm not sure. If you could share a part file with me that has this face type, I'll take a look at it.

 

Posted by: Jason Newell
Post date: 4/17/2009 6:50:09 PM

RE: C# Casting Face to Cylinder

[ Edited ]

 

Jason,

 

Thanks for your help.

 

I tried the code change you suggested but to no avail. An exception is not produced but the code does not perform as expected.

 

The part is attached. It is one distributed by Solid Edge in the installation Training folder.

(Learning to use the form!)

 

Bob

 

Posted by: Robert Monat
Post date: 4/18/2009 12:33:13 PM

RE: C# Casting Face to Cylinder

[ Edited ]

Bob,

 

I think that I see what's going on in your code. You're getting the 'Type' from the 'face.Geometry' property then you try to cast the 'face' object to that object. I believe that you will want to try and cast the 'face.Geometry' object instead.

 

If you still have trouble, send me some more detailed demo code so that I can try to reproduce it.

 

Posted by: Jason Newell
Post date: 4/19/2009 11:48:43 AM

RE: C# Casting Face to Cylinder

[ Edited ]

I tried the same thing with VB and get the same exception. Unfortunately, the examples are for VB6, not 2003 nor 2005. From the Cylinder.GetCylinderData example:

 

' Getting the Cylinder data for a Cylinder object

Set objBody = objDoc.Models(1).Body

Set objCylndr = objBody.Shells(1).Faces(3).Geometry

Call objCylndr.GetCylinderData(BasePoint:=dblCylBasePt, _

AxisVector:=dblCylAxVect, Radius:=dblCylRadius)

 

How should this code be written for VB?

 

Posted by: Robert Monat
Post date: 5/4/2009 9:41:00 AM