Cancel
Showing results for 
Search instead for 
Did you mean: 

How to get path and file name (Occurance Selectset)

Builder
Builder
 
Edge Developers,

As a matter of fact. 
I would like to select one account item in the assembly environment to call up the name and path of the selected part.
I think we can use this code, but...
I'm afraid VB6.0.
I don't know what to do with the VB.net code.

How to retrieve the path and file name of the selected item using the VB.net code.

Please help me.

 

 Count = objAsm.SelectSet.Count
        If Count = 0 Then
            name = objAsm.FullName
        Else
            For i = 1 To Count
                name = objAsm.SelectSet(i).object.OccurrenceDocument.FullName

 

objAsm.SelectSet(i)

I only work here and then it doesn't work.

.object.OccurrenceDocument.FullName  <---- Is this grammar in VB6?

 

How can I express the code above on VB.Net?

 

Option Infer On

Imports System
Imports System.Runtime.InteropServices

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim application As SolidEdgeFramework.Application = Nothing
        Dim assemblyDocument As SolidEdgeAssembly.AssemblyDocument = Nothing
        Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
        Dim occurrence As SolidEdgeAssembly.Occurrence = Nothing
        Dim objDocument As SolidEdgeFramework.SolidEdgeDocument = Nothing

        Dim Objselectset As SolidEdgeFramework.SelectSet = Nothing


        Dim count As String
        Dim name As String = Nothing
        Dim name2 As String = Nothing

        Try
            ' See "Handling 'Application is Busy' and 'Call was Rejected By Callee' errors" topic.
            'OleMessageFilter.Register()

            ' Attempt to connect to a running instance of Solid Edge.
            application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)
            objDocument = application.ActiveDocument


            If assemblyDocument IsNot Nothing Then
                occurrences = assemblyDocument.Occurrences

                count = assemblyDocument.SelectSet.Count

                If count = 0 Then
                    name = assemblyDocument.FullName
                Else
                    name = occurrence.OccurrenceFileName

                End If
            End If

        Catch ex As System.Exception
            Console.WriteLine(ex)
        Finally
            'OleMessageFilter.Unregister()
        End Try

    End Sub
End Class
10 REPLIES 10

Re: How to get path and file name (Occurance Selectset)

Gears Phenom Gears Phenom
Gears Phenom

@Hclee1 

 

I see an issue with

If assemblyDocument IsNot Nothing Then

as you have not previously set assemblyDocument.   Note you have set objDocument...  but then do not use it.

objDocument = application.ActiveDocument

 

Also,   replace 

name = occurrence.OccurrenceFileName

 with 

name = occurrences.Item(1).Name

 

 

Here is my snippet that works:

assemblyDocument = application.ActiveDocument

            If assemblyDocument IsNot Nothing Then
                occurrences = assemblyDocument.Occurrences

                count = assemblyDocument.SelectSet.Count

                If count = 0 Then
                    name = assemblyDocument.FullName
                Else
                    name = occurrences.Item(1).Name
                    name2 = occurrences.Item(1).PartFileName
                End If
            End If

            Console.WriteLine(name & " saved at: " & name2)
            Console.ReadKey(True)

HTH

Matt Johnson
Solid Edge Certified Professional
Solid Edge 2019 (SEEC) - Production
NX12 (Tc Integration)
Teamcenter 10.1.7

Re: How to get path and file name (Occurance Selectset)

Builder
Builder

Hi. @  
Thank you for your reply.

I've just tried it out, but...
This code does not work.

Am I missing something?

Re: How to get path and file name (Occurance Selectset)

Gears Phenom Gears Phenom
Gears Phenom

Odd,   Works for me... let's try the entire code.  this is a console app.   VS Community 2017 VB.net, SE2019

Option Infer On

Imports System
Imports System.Runtime.InteropServices
Module Module1

    Sub Main()
        Dim application As SolidEdgeFramework.Application = Nothing
        Dim assemblyDocument As SolidEdgeAssembly.AssemblyDocument = Nothing
        Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
        Dim occurrence As SolidEdgeAssembly.Occurrence = Nothing
        Dim objDocument As SolidEdgeFramework.SolidEdgeDocument = Nothing

        Dim Objselectset As SolidEdgeFramework.SelectSet = Nothing


        Dim count As String
        Dim name As String = Nothing
        Dim name2 As String = Nothing

        Try
            ' See "Handling 'Application is Busy' and 'Call was Rejected By Callee' errors" topic.
            'OleMessageFilter.Register()

            ' Attempt to connect to a running instance of Solid Edge.
            application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)
            objDocument = application.ActiveDocument


            assemblyDocument = application.ActiveDocument

            If assemblyDocument IsNot Nothing Then
                occurrences = assemblyDocument.Occurrences

                count = assemblyDocument.SelectSet.Count

                If count = 0 Then
                    name = assemblyDocument.FullName
                Else



                    name = occurrences.Item(1).Name
                    name2 = occurrences.Item(1).PartFileName
                End If
            End If

            Console.WriteLine(name & " saved at: " & name2)
            Console.ReadKey(True)


        Catch ex As System.Exception
            Console.WriteLine(ex)
        Finally
            ' eMessageFilter.Unregister()
        End Try
    End Sub

End Module



Matt Johnson
Solid Edge Certified Professional
Solid Edge 2019 (SEEC) - Production
NX12 (Tc Integration)
Teamcenter 10.1.7

Re: How to get path and file name (Occurance Selectset)

Builder
Builder

@Johnson_BigMatt
The code you sent works well.

However, only the file name is imported at the higher level.
I would like to know the "path" of the account file I selected.

But your answer is precious.
I'll study more.

Thank you very much..

Re: How to get path and file name (Occurance Selectset)

Gears Phenom Gears Phenom
Gears Phenom

@Hclee1 

 

Note that in the prior code that if nothing was selected then we only set the "name" variable; thus that is all that was returned.

image.png

So to fix this we need to redefine "name" and add "name2"

Also, to return only the Path we need a new variable "sPath" as string.

Note that the AssyDoc has a "Path" Property that we can call directly, the Occurrences do not; so we must parse the path from the "FullName"

image.png

 

Here again is the full, modified code... 

Option Infer On

Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Module Module1

    Sub Main()
        Dim application As SolidEdgeFramework.Application = Nothing
        Dim assemblyDocument As SolidEdgeAssembly.AssemblyDocument = Nothing
        Dim occurrences As SolidEdgeAssembly.Occurrences = Nothing
        Dim occurrence As SolidEdgeAssembly.Occurrence = Nothing
        Dim objDocument As SolidEdgeFramework.SolidEdgeDocument = Nothing

        Dim Objselectset As SolidEdgeFramework.SelectSet = Nothing


        Dim count As String
        Dim name As String = Nothing
        Dim name2 As String = Nothing
        Dim sPath As String = Nothing



        Try
            ' See "Handling 'Application is Busy' and 'Call was Rejected By Callee' errors" topic.
            'OleMessageFilter.Register()

            ' Attempt to connect to a running instance of Solid Edge.
            application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)
            objDocument = application.ActiveDocument


            assemblyDocument = application.ActiveDocument

            If assemblyDocument IsNot Nothing Then
                occurrences = assemblyDocument.Occurrences

                count = assemblyDocument.SelectSet.Count

                If count = 0 Then
                    name = assemblyDocument.Name
                    name2 = assemblyDocument.FullName
                    sPath = assemblyDocument.Path
                Else
                    name = occurrences.Item(1).Name
                    name2 = occurrences.Item(1).PartFileName
                    sPath = Path.GetDirectoryName(name2)

                End If
            End If

            Console.WriteLine("File Name: " & name & vbCrLf & "Path: " & sPath & vbCrLf & "Full Name: " & name2)
            Console.ReadKey(True)


        Catch ex As System.Exception
            Console.WriteLine(ex)
        Finally
            ' eMessageFilter.Unregister()
        End Try
    End Sub

End Module



NOte the last thing you may need/want to do is trim the "Name" if an occurrence as this contains the occurrence number.  IE:  Filename:1

Matt Johnson
Solid Edge Certified Professional
Solid Edge 2019 (SEEC) - Production
NX12 (Tc Integration)
Teamcenter 10.1.7

Re: How to get path and file name (Occurance Selectset)

Builder
Builder

Hi @Johnson_BigMatt 

 

The code you gave me works well.
I wanted to select a part and find the path where it was located.

However, for parts included in the sub-assemblies:
The name and path of the sub-assemblies appear, not the part's name and path.

So the top question is this.

For i = 1 To Count
name = objAsm.SelectSet(i).OccurrenceFileName

If name = "" Then
name = objAsm.SelectSet(i).Object.OccurrenceDocument.FullName l = Len(name) dftname = Left(name, l - 3) + "dft" Set objDft = objApp.Documents.Open(dftname)

It's a darkened section.

 

Vb.net does not seem to support this code. 
I think this is the code used by VB6.
I'd like to know how to use it on VB.net
Can you help me?

Thank you.

Re: How to get path and file name (Occurance Selectset)

Gears Phenom Gears Phenom
Gears Phenom

Hello Hclee1,

 

Many years ago I wrote a macro to copy name of the selected part or subassembly to clipboard. I attach as example part of this macro. You can see in Button1_Click Sub how can you get name of the occurence.

 

Best regards

 

Peter Kaderasz

Highlighted

Re: How to get path and file name (Occurance Selectset)

Gears Phenom Gears Phenom
Gears Phenom

@Hclee1   I did not really look too much into the code, but simply modified yours to try to answer the initial questions...  but indeed you are correct.  

 

Upon Review, this is because the select set could be a part, sub assy, or sub-part w/in a sub assy each is a different type...   There will need to be additional logic to check the type before working with the item... 

Matt Johnson
Solid Edge Certified Professional
Solid Edge 2019 (SEEC) - Production
NX12 (Tc Integration)
Teamcenter 10.1.7

Re: How to get path and file name (Occurance Selectset)

Builder
Builder

@Johnson_BigMatt 

Thank you very much for your advice.
I will try to write it based on the code above.