Reference Files and XRef Paths

by Mark Stefanchuk, CADmanage.com

I was looking through some old emails recently and found this questions from Adrian. “How do I get the reference file path of a reference file?”

Why would you need the path? Good question. You might want to audit projects to make sure users are using standard naming conventions. You may need to include reference file names and paths in the border for cross referencing. Or, maybe, as is the case with MicroStation you want to know if the file is located in the project path or if it’s coming from another folder – another project or a common files folder like you might have for standard borders.

So, how do we do it? Well, you could open the reference dialog and hover your mouse over the file name until the pop-up window shows you the file specification, or you can write a small app.

In MicroStation we get the reference file path and file name using the designfile object with fullname. In this example, I’m just sending the name to the immediate window.

Sub reftest()
    Dim att As Attachment
    For Each att In ActiveModelReference.Attachments
        Debug.Print att.DesignFile.FullName
    Next
End Sub

AutoCAD looks a little different, because we have to search the list of blocks in the file and filter out the ones that are XREFS.

Sub xreftest()
    Dim oBlk As AcadBlock
    For Each oBlk In ThisDrawing.Blocks
        If oBlk.IsXRef Then
            Debug.Print oBlk.Path
        End If
    Next
End Sub

Printing to the immediate window is a quick and easy solution for you, but if you want your users to be able to cut and paste give them a list box or dump the results to at text file.

A listbox approach might look like this.

And the code for this is essentially the same, except you put the for each into the userform_initialize event and add items to the listbox instead of printing it to the immediate window.

 Load frmRefList
 frmRefList.Show
 frmRefList.lstXRefs.Clear
 Dim att As Attachment
 For Each att In ActiveModelReference.Attachments
    frmRefList.lstXRefs.AddItem att.DesignFile.FullName
 Next

Finally, if you want your users to be able to copy the list to the clipboard then you will have to provide a copy button because CTRL-C does not work on the list contents. This example shows you how to use a DataObject to copy the list. Put the code into a button click event and you’ll be good to go.

 Dim s As String, i As Integer, oData As DataObject
 For i = 0 To Me.lstXRefs.ListCount - 1
    If Len(Trim(Me.lstXRefs.List(i))) > 0 Then
        s = s + Trim(Me.lstXRefs.List(i)) + vbCrLf
    End If
 Next i
 Set oData = New DataObject
 oData.Clear
 oData.SetText Trim(s)
 oData.PutInClipboard

Code download is available on our CADgurus page.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.

4 responses to “Reference Files and XRef Paths”

  1. Lorys Lea says :

    mark pretty good coding … I tried this myself a while ago it worked great at home but the security restrictions at work just ruined its functions… cant wait to try yours at home and at work… would it be possible to modify your code to show 1 if the ref file is on or off and more importantly 2 if it is a nested ref file and show the nested paths..
    The reasons for listing is say you want to work on a drawing that may it self be referenced into several other drawings you would want to know the impact of editing this drawing is going to have on other drawings… so my old vba couldnt do the nesting but it could do whole folder contents and list the xref paths of the all the drgs in the same folder and save to txt file ie say all my electrical drgs about 200 all live in the same folder but some a refed into each other and may even have circular nested refs like one large over all schematic consists of several other smaller schematics ie sub stations 1, 2 ,3 4 wiring but they are part of the overall network and use the ref attachments to make the overall schematic… so get my point? If I find my old mvba I’ll send it to you

    • cadmanage says :

      I’ve never had problems with security when running VBA. Unfortunately I know only enough about network security to get me in trouble. My understanding though, is that if the app is running in the parent program’s object space you will be ok. Now if the VBA is stored on a network server versus the client machine there’s no telling what security agents are running through your corporate network.

      Regarding reference file settings – in MicroStation you can access the properties of each attachment – display, locate, snap, and so on. There is also a DisplayAsNested property. With XRefs in AutoCAD the properties are limited to those that can be extracted from the block object – name, size, status. In this case maybe you can use a MicroStation VBA to process the attachment information of a DWG – worth exploring.

      • Lorys Lea says :

        what do oyu add to the vba in microstation so it can list the nested ref files as nested?

      • cadmanage says :

        Hi Lorys,

        You have to do this with a recursive call. Like this…

        Sub reftest()
        Dim att As Attachment, att_nest As Attachment
        For Each att In ActiveModelReference.Attachments
        processAttachments att
        Next
        End Sub
        Sub processAttachments(om As Attachment)
        Dim att As Attachment
        If om.Attachments.Count > 0 Then
        For Each att In om.Attachments
        processAttachments att
        Next
        Else
        Debug.Print om.DesignFile.FullName
        End If
        End Sub

        The code loops through the reference attachments of each reference attachment until it runs out. I suspect that it’s possible to have a case where two files reference each other, which would result in an infinite loop – so, you probably want to test for situations where reference name is the same as the master file in order to skip this case.

        Regards,
        Mark

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: