Reply

InvalidCastException

Hello everyone,

 

I am trying to get the list of all variables from variable list that are connected to an excel list, but i keep getting an exception "Unable to cast COM object of type 'System.__ComObject' to class type "SolidEdgeFramework.variable".

 

here is my code:

 

Dim seApp As SolidEdgeFramework.Application

Dim seDocs As SolidEdgeFramework.Documents

Dim ASMdoc As SolidEdgeAssembly.AssemblyDocument

Dim seVariables As SolidEdgeFramework.Variables = Nothing

Dim objVarList As SolidEdgeFramework.VariableList = Nothing

Dim seVariable As SolidEdgeFramework.variable = Nothing

 

 

Try

OleMessageFilter.Register()

seApp = CType(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)

Catch ex As Exception

If Err.Number <> 0 Then

Err.Clear()

seApp = CType(CreateObject("SolidEdge.Application"), SolidEdgeFramework.Application)

End If

Finally

seDocs = seApp.Documents

End Try

 

ASMdoc = CType(seDocs.Open(FileName), SolidEdgeAssembly.AssemblyDocument)

seVariables = CType(ASMdoc.Variables, SolidEdgeFramework.Variables)

objVarList = CType(seVariables.Query("*"), SolidEdgeFramework.VariableList)

 

For n = 1 To objVarList.Count

Try

seVariable = CType(objVarList.item(n), SolidEdgeFramework.variable) 'here I am getting an exception

Formula = seVariable.Formula

VarName = seVariable.Name

If Formula <> "" AndAlso Formula.Substring(0, 1) = "@" Then

xlWb.Range("A" & k).Value = VarName

xlWb.Range("B" & k).Value = Formula

xlWb.Range("C" & k).Value = FileName

k = k + 1

End If

Catch ex As Exception

 

End Try

 

System.Windows.Forms.Application.DoEvents()

 

 

Does someone have an idea what is wrong with my code?.. Any help is highly appreciated...

 

Best regards, salwadora

 

Posted by: Salwadora
Post date: 2/4/2013 5:01:35 AM

3 REPLIES

InvalidCastException

Salwadora,

 

the objects in the VariableList may have be of 3 different types (at least these 3 I have already seen):

* variable

* Dimension

* AsmRefPlane (only in Assembly environment)

It is quite probable, that your object is one of the second or third type!!

 

Posted by: Martin Bernhard
Post date: 2/4/2013 5:03:01 PM

InvalidCastException

Here is the answer. I wrote this quick so it doesn't necessarily represent best practices but you'll get the idea. Working with variables suck!

 

Also note my comments at the top of the code.

 

 

[code type=vbnet]

Imports System.Runtime.InteropServices

 

' .NET Framework 4

' Embed Interop Types = False

Module Module1

 

Sub Main()

Dim objApplication As SolidEdgeFramework.Application = Nothing

Dim objAssemblyDocument As SolidEdgeAssembly.AssemblyDocument = Nothing

Dim objVariables As SolidEdgeFramework.Variables = Nothing

Dim objVariablesList As SolidEdgeFramework.VariableList = Nothing

Dim i As Integer

 

objApplication = CType(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)

objAssemblyDocument = CType(objApplication.ActiveDocument, SolidEdgeAssembly.AssemblyDocument)

objVariables = CType(objAssemblyDocument.Variables, SolidEdgeFramework.Variables)

objVariablesList = objVariables.Query("*")

 

For i = 1 To objVariablesList.Count

Dim objVariableListItem As Object

Dim objectType As SolidEdgeConstants.ObjectType

 

objVariableListItem = objVariablesList.Item(i)

 

' .Type property is the key to the whole thing.

' Maps to SolidEdgeConstants.ObjectType.

objectType = objVariableListItem.Type

 

Dim objDimension As SolidEdgeFrameworkSupport.Dimension = Nothing

Dim objVariable As SolidEdgeFramework.variable = Nothing

 

Select Case objectType

Case SolidEdgeConstants.ObjectType.igDimension

objDimension = CType(objVariableListItem, SolidEdgeFrameworkSupport.Dimension)

Case SolidEdgeConstants.ObjectType.igVariable

objVariable = CType(objVariableListItem, SolidEdgeFramework.variable)

End Select

Next

End Sub

 

End Module

[/code]

 

Posted by: Jason Newell
Post date: 2/5/2013 2:24:09 AM

InvalidCastException

Thank you both so much, the solution worked perfectly for me Smiley Happy

You were absolutely right, I was getting an exception whenever I tried to make a variable object out of dimension. How could I miss that?

 

Guys, you are the best!

 

salwadora

 

Posted by: Salwadora
Post date: 2/5/2013 3:43:41 AM