Get File path of selected item

Get File path of selected item

pball
Mentor Mentor
4,048 Views
9 Replies
Message 1 of 10

Get File path of selected item

pball
Mentor
Mentor

I'm looking to get the file path of a selected part/assembly while inside of an assembly. I think I found something that can get it but the example is just different enough I can't figure it out and I don't have time to play with it. There also might be an easier way I couldn't find while searching.

 

This is the example code that seems to be able to do loads of things including get the full path, if you know what you're doing.

http://modthemachine.typepad.com/my_weblog/2008/10/program-prototyping.html

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Accepted solutions (1)
4,049 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
I'm not sure if you meant that you need to get the path in your own program or if you were trying to create a utility that would do this for you. If you just want a utility that will do this for you and if you are running Inventor 2013 or later there is a nice app in the Inventor App Exchange that does this.
http://apps.exchange.autodesk.com/INVNTOR/Detail/Index?id=appstore.exchange.autodesk.com%3ainventorf...
0 Likes
Message 3 of 10

pball
Mentor
Mentor

That isn't really what I'm looking for. I want the full path of a select part/assembly inside of an assembly using VBA code.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 10

nagwani
Alumni
Alumni

Hi There,

 

The sample VBA code below gets the path of selected occurrence in an assembly document.

 

Hope this helps!

 

-Ishwar N

 

Sub TestOccPath()
    Dim odoc As AssemblyDocument
    Set odoc = ThisApplication.ActiveDocument
    
    Dim obj As Object
    Set obj = odoc.SelectSet.Item(1)
    
    Dim occ As ComponentOccurrence
    Set occ = obj
    Dim spath As String
    spath = getOccPath(occ)
End Sub

Public Function getOccPath(ByVal m_occ As Inventor.ComponentOccurrence) As String
    Dim m_name As String
    If m_occ.OccurrencePath.Count = 1 Then
        ' The occurenace path is name of parent assembly + current componnet
        getOccPath = m_occ.Name
        Exit Function
    Else
        'build up occurrent path in case of nested component
        Dim m_occtemp As Inventor.ComponentOccurrence
        For Each m_occtemp In m_occ.OccurrencePath
            m_name = m_name + m_occtemp.Name + "\"
        Next
        Mid(m_name, Len(m_name)) = " " 'replace last "\" with space
        getOccPath = Trim(m_name)
        Exit Function
    End If
End Function

 

 

0 Likes
Message 5 of 10

GeorgK
Advisor
Advisor

Sub get_Path()
    Dim FullFileName As String
    Dim FilePath As String
    FullFileName = ThisApplication.ActiveDocument.FullFileName
    FilePath = Left(FullFileName, InStrRev(FullFileName, "\"))
    ThisApplication.Caption = FullFileName  'FilePath
    MsgBox "File Path: " & FilePath, vbOKOnly, FilePath
    MsgBox "Full File Name: " & FullFileName, vbOKOnly, FullFileName
    Variable = InputBox("Path", "Input", FullFileName, 1, 1)
   
    Shell "explorer.exe /e, """ & FilePath & """", vbNormalFocus
    
   Shell "Explorer.exe /e,/select, """ & FullFileName & """", vbNormalFocus
End Sub

Message 6 of 10

pball
Mentor
Mentor

nagwani

 

Your code is returning the name in the browser tree which I don't trust since it can wrong and that doesn't help with the path. Also it doesn't appear to work if you are editing a subassembly.

 

GeorgK

 

I'm not quite sure what your code does.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 7 of 10

nagwani
Alumni
Alumni
Accepted solution

HI,

 

It seems tha you want full path of selected occurrence. The sample code shown below demonstrates same.

 

Regards,

-Ishwar N

 

Sub geSelectedPath()
Dim obj As Object
Set obj = ThisApplication.ActiveDocument.SelectSet.Item(1)
If (TypeOf obj Is ComponentOccurrence) Then
   Dim occ As ComponentOccurrence
   Set occ = obj
   Debug.Print occ.Definition.Document.FullFileName
End If
End Sub

0 Likes
Message 8 of 10

pball
Mentor
Mentor

Thanks, that's exactly what I'm looking for.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 9 of 10

Anonymous
Not applicable

how to use the path that is printed using debug.print

 

and open that file?

 

 

0 Likes
Message 10 of 10

pball
Mentor
Mentor
Sub openselected()
  Dim obj As Object
  Set obj = ThisApplication.ActiveDocument.SelectSet.Item(1)
  If (TypeOf obj Is ComponentOccurrence) Then
     Dim occ As ComponentOccurrence
     Set occ = obj
     Set odrawdoc = ThisApplication.Documents.Open(occ.Definition.Document.FullFileName, True)
     odrawdoc.Activate
  End If
End Sub

 I replaced the debug.print with the commands to open the drawing. The first new line opens the drawing and the second line activates it.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes