System.InvalidCastException on AddTube

Valued Contributor
Valued Contributor

Hi,

 

I'm trying to make a macro to ease the creation of tubes in the XPresRoute module.

First thing to try is: does (and how)  the AddTube method works, so I made a test file.

UnluckilyI get this error message:

 

System.InvalidCastException
An unhandled exception of type 'System.InvalidCastException' occurred in Test.exe

Additional information: Unable to cast COM object of type 'System.__ComObject' to class type 'System.Array'.
Instances of types that represent COM components cannot be cast to types that do not represent COM components;
however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

 

First, create an empty assy and draw a few segments lines in the XpresRoute module.

Create an empty "Template" file: "C:\temp\TubeTemplate.par"

Run the code below.

It holds at the last statement, with the error message above.

 

It seems that there is some mismatch between the LineSegments object and what the Addtube method is expecting...

 

Pleae help,

Jan Maas

 

 

 

 

Imports System.Runtime.InteropServices
Imports SolidEdgeFramework
Imports SolidEdgeConstants

Public Class Form1

    Dim seApp As SolidEdgeFramework.Application = Nothing
    Dim seDoc As SolidEdgeFramework.SolidEdgeDocument = Nothing
    Dim sejAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
    Dim seOccurrences As SolidEdgeAssembly.Occurrences = Nothing
    Dim seOccurrence As SolidEdgeAssembly.Occurrence = Nothing
    Dim PartFileName As String = "C:\temp\tube.par"
    Dim TemplateFileName As String = "C:\temp\TubeTemplate.par"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        seApp = Marshal.GetActiveObject("SolidEdge.Application")
        seDoc = seApp.ActiveDocument
        seOccurrences = seDoc.Occurrences

        Dim TubeSegments As SolidEdgeAssembly.LineSegments = seDoc.LineSegments

        seOccurrence = seOccurrences.AddTube(TubeSegments:=TubeSegments, PartFileName:=PartFileName, TemplateFileName:=TemplateFileName)

    End Sub
End Class

2 REPLIES

Re: System.InvalidCastException on AddTube

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Hi,

first you use a LineSegments collection as first paramenter, which should be an array.

So you should create a system.array variable and copy all desired line elements it.

Second I'm not shure if you can omit the other parameter (even if they are declared as optional).

This depends on the implementation of the COM method in Solid Edge. Since there is no example code in the SDK help, you have to try it.

 

I have not own example, sorry.

regards

JB
VS2015, SE ST10

Re: System.InvalidCastException on AddTube

Valued Contributor
Valued Contributor

Hi JB,

 

Yes, this is the solution !

See below the changed code. (Bold)

 

Thanks,

Jan

 

Public Class Form1

    Dim seApp As SolidEdgeFramework.Application = Nothing
    Dim seDoc As SolidEdgeFramework.SolidEdgeDocument = Nothing
    Dim sejAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing
    Dim seOccurrences As SolidEdgeAssembly.Occurrences = Nothing
    Dim seOccurrence As SolidEdgeAssembly.Occurrence = Nothing
    Dim PartFileName As String = "C:\temp\tube.par"
    Dim TemplateFileName As String = "C:\temp\TubeTemplate.par"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        seApp = Marshal.GetActiveObject("SolidEdge.Application")
        seDoc = seApp.ActiveDocument
        seOccurrences = seDoc.Occurrences

        Dim TubeSegments As SolidEdgeAssembly.LineSegments = seDoc.LineSegments '

        Dim SA(2) As Object
        SA(0) = TubeSegments(0)
        SA(1) = TubeSegments(1)
        SA(2) = TubeSegments(2)

        seOccurrence = seOccurrences.AddTube(TubeSegments:=SA, PartFileName:=PartFileName, TemplateFileName:=TemplateFileName)

    End Sub