Reply

VB.NET and Solid Edge Occurrences

[ Edited ]

I am a 2.5 year programmer in vb.net, but new to solid edge. I am attempting to first, get a solid edge assembly and its properties. This works well and is pretty straightforward. Secondly, I am trying to traverse through its occurrences in order to get a full BOM from the document. The Occurrence count comes back with 9, so I know it is reading them. I have tried many of the examples that I've found on this site, and also from the Solid Edge programmers guide, to no avail. I always error on the "For Each objOccurrence In objOccurrences", objOccurrence returns NOTHING.

 

I am soon to give up and just use VB6, which I have working. But I'd rather not...

 

Any help would be appreciated, I thank you!

(Your site is very good!!)

Jennece

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim asm As SolidEdgeFramework.Application

' Connect to a running instance of Solid Edge.

asm = System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application")

If asm.ActiveEnvironment "Assembly" Then

' An assembly document is not open, notify user and then exit.

MsgBox("This program must be run with an assembly document open." & Chr(10) & Chr(13) & "Please open an assembly document and retry.")

Exit Sub

End If

 

' Get the assembly information by calling this recursive function.

ObtainAssemblyInformation(asm.ActiveDocument, 1)

 

End Sub

 

Sub ObtainAssemblyInformation(ByVal objDoc As SolidEdgeAssembly.AssemblyDocument, ByVal intLevel As Integer)

On Error Resume Next

Dim objOccurrence As SolidEdgeAssembly.Occurrence

Dim objOccurrences As SolidEdgeAssembly.Occurrences

objDoc.Activate()

objOccurrences = objDoc.Occurrences

Dim intc As Integer = objOccurrences.Count()

' Iterate through all of the parts in the current assembly.

 

For Each objOccurrence In objOccurrences

If Not objOccurrence Is Nothing Then

' Make sure it should be included in the BOM by checking the IncludeInBom flag.

If objOccurrence.IncludeInBom Then

' Find the object in the array, create if necessary

AddSolidEdgeClassItem(objDoc, objOccurrence, intLevel)

 

' If this is a subassembly, then recurse into it unless this is a top level parts listing.

If (objOccurrence.Subassembly) Then

ObtainAssemblyInformation(objOccurrence.OccurrenceDocument, intLevel + 1)

End If

End If

End If

Next

End Sub

 

Posted by: Jennece Wilson
Post date: 2/15/2007 1:51:49 AM

6 REPLIES

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

Hello,

 

For Each isn't possible with some Solid Edge object collections, try this instead:

 

For i As Integer = 1 To objOccurrences.Count

Dim objOccurrence As SolidEdgeAssembly.Occurrence = objOccurrences.Item(i)

...

 

 

Posted by: Wolfgang Kunert
Post date: 2/15/2007 2:11:35 AM

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

Go to my resources page and download the 2006 developer conference examples. Specifically look at Track 4, Demo 4. Here is the code.

 

 

Imports SolidEdgeAssembly

Imports SolidEdgeFramework

Imports System.Runtime.InteropServices

 

Module Module1

 

Sub Main()

Dim objApplication As SolidEdgeFramework.Application = Nothing

Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing

 

 

Try

objApplication = Marshal.GetActiveObject("SolidEdge.Application")

objAssembly = objApplication.ActiveDocument

ProcessLinks(objAssembly)

 

Console.WriteLine("Press Enter to continue.")

Console.In.ReadLine()

Catch ex As Exception

Console.WriteLine(ex.Message)

Console.WriteLine(ex.StackTrace)

Finally

If Not (objAssembly Is Nothing) Then

Marshal.ReleaseComObject(objAssembly)

End If

If Not (objApplication Is Nothing) Then

Marshal.ReleaseComObject(objApplication)

End If

End Try

End Sub

 

Sub ProcessLinks(ByVal objParent As SolidEdgeAssembly.AssemblyDocument)

Dim objOccurrences As SolidEdgeAssembly.Occurrences = Nothing

Dim objOccurrence As SolidEdgeAssembly.Occurrence = Nothing

 

Console.WriteLine(objParent.FullName)

 

Try

 

objOccurrences = objParent.Occurrences

If Not (objOccurrences Is Nothing) Then

For Each objOccurrence In objParent.Occurrences

ProcessLinks(objOccurrence, 1)

Next

End If

Catch ex As Exception

Throw ex

Finally

If Not (objOccurrence Is Nothing) Then

Marshal.ReleaseComObject(objOccurrence)

End If

If Not (objOccurrences Is Nothing) Then

Marshal.ReleaseComObject(objOccurrences)

End If

End Try

 

End Sub

 

Sub ProcessLinks(ByVal objParent As SolidEdgeAssembly.Occurrence, ByVal level As Integer)

Dim objSubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing

Dim objSubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing

Dim i As Integer

 

For i = 0 To level

Console.Write(vbTab)

Next

 

Console.WriteLine(objParent.Name)

 

Try

objSubOccurrences = objParent.SubOccurrences

If Not (objSubOccurrences Is Nothing) Then

For Each objSubOccurrence In objParent.SubOccurrences

ProcessLinks(objSubOccurrence, level + 1)

Next

End If

Catch ex As Exception

Finally

If Not (objSubOccurrence Is Nothing) Then

Marshal.ReleaseComObject(objSubOccurrence)

End If

If Not (objSubOccurrences Is Nothing) Then

Marshal.ReleaseComObject(objSubOccurrences)

End If

End Try

End Sub

 

Sub ProcessLinks(ByVal objParent As SolidEdgeAssembly.SubOccurrence, ByVal level As Integer)

Dim objSubOccurrences As SolidEdgeAssembly.SubOccurrences = Nothing

Dim objSubOccurrence As SolidEdgeAssembly.SubOccurrence = Nothing

Dim i As Integer

 

For i = 0 To level

Console.Write(vbTab)

Next

 

Console.WriteLine(objParent.Name)

 

Try

objSubOccurrences = objParent.SubOccurrences

If Not (objSubOccurrences Is Nothing) Then

For Each objSubOccurrence In objParent.SubOccurrences

ProcessLinks(objSubOccurrence, level + 1)

Next

End If

Catch ex As Exception

Finally

If Not (objSubOccurrence Is Nothing) Then

Marshal.ReleaseComObject(objSubOccurrence)

End If

If Not (objSubOccurrences Is Nothing) Then

Marshal.ReleaseComObject(objSubOccurrences)

End If

End Try

End Sub

End Module

 

 

 

Posted by: Jason Newell
Post date: 2/15/2007 2:15:24 AM

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

Oh my gosh... I tried the for i = ... and it worked, came back with a valid

_com object. I am going to take a good look at the code example sent also.

I cannot tell you how amazed I am at the quick (not to mention accurate) help!

I have put your site as a favorite, and will try to help others if/when I can.

Thanks so much!

Jennece

 

P.S. Stayed at Red Rock Canyon SP for a week once, loved it so much I have always since wanted to move to Oklahome...

 

 

Posted by: Jennece Wilson
Post date: 2/15/2007 2:24:30 AM

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

Jennece,

 

I'm not sure if you've found it yet but you've got to check out my Solid Edge Spy application. It's a free product that I wrote for SE developers. It will help you quickly get up to speed on the Solid Edge runtime COM object model.

 

p.s. Glad your stay in Oklahoma was pleasant. I've traveled all over the U.S. and I can honestly say there is no other place I'd rather live.

 

Posted by: Jason Newell
Post date: 2/15/2007 3:11:33 PM

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

Hi Jennece,

 

It is possible to get For Each to work like this:

 

For Each objOccurrence In CType(objOccurrences, IEnumerable)

 

I sometimes use this to avoid messy code.

 

Cheers

Calum

 

Posted by: Calum McLellan
Post date: 2/15/2007 6:56:58 PM

RE: VB.NET and Solid Edge Occurrences

[ Edited ]

I'm trying to retrieve the face color from the parts in an Assembly with several levels of subassembies.

 

I saw another post that used for example:

oASM.Occurrences.Item(1).SubOccurrences.Item(8).ThisAsOccurrence.FaceStyle.stylename

This allowed me to retrieve the 1st level suboccurrences color.

Why doesn't this work for another level:

oASM.Occurrences.Item(1).SubOccurrences.Item(1).SubOccurrences.Item(14).ThisAsOccurrence.FaceStyle.stylename

and this so on

oASM.Occurrences.Item(1).SubOccurrences.Item(1).SubOccurrences.Item(14).SubOccurrences.Item(1).ThisAsOccurrence.FaceStyle.stylename

 

and I can retrieve their quantities using the same format:

oASM.Occurrences.Item(1).SubOccurrences.Item(1).SubOccurrences.Item(14).SubOccurrences.Item(1).ThisAsOccurrence.Quantity

 

Why can't I get deeper subassemblies face colors.

 

Posted by: Rita Ellis
Post date: 7/19/2011 4:06:40 PM