Iterate through ALL External References

Discussion in 'AutoCAD' started by MickyV, May 28, 2004.

  1. MickyV

    MickyV Guest

    G'Day guys,

    I'm making a macro that needs to access all xrefs in a drawing, both in ModelSpace and PaperSpace. At the moment, my code to do this is as below:

    Dim objEnt as AcadEntity
    Dim objRef as AcadExternalReference

    For Each objEnt in ThisDrawing.ModelSpace
    If TypeOf objEnt Is AcadExternalReference Then

    'Work on the xref

    End If
    Next objEnt

    For Each objEnt in ThisDrawing.PaperSpace
    If TypeOf objEnt Is AcadExternalReference Then

    'Work on the xref

    End If
    Next objEnt

    Question: Is there a better way to grab and work on each xref? I don't like repeating my code, and I figure that grabbing every object in the drawing is overkill, when I'm only interested in Xrefs.

    Thanks,
    Mick.
     
    MickyV, May 28, 2004
    #1
  2. MickyV

    Jeff Mishler Guest

    Get the block insertions in a Selection Set, then determine if the insert is
    and Xref....

    Sub xrefWork()
    Dim objSS As AcadSelectionSet
    Dim objSSets As AcadSelectionSets
    Dim objBlk As AcadBlockReference
    Dim iType(0) As Integer
    Dim vData(0) As Variant
    Set objSSets = ThisDrawing.SelectionSets
    On Error Resume Next
    objSSets.Item("XREFS").Delete
    On Error GoTo 0
    Set objSS = objSSets.Add("XREFS")
    iType(0) = 0
    vData(0) = "INSERT"
    objSS.Select acSelectionSetAll, filtertype:=iType, filterdata:=vData
    For Each objBlk In objSS
    If TypeOf objBlk Is AcadExternalReference Then
    'Do your work on the xref here
    End If
    Next objBlk
    End Sub

    Jeff

    ModelSpace and PaperSpace. At the moment, my code to do this is as below:
    like repeating my code, and I figure that grabbing every object in the
    drawing is overkill, when I'm only interested in Xrefs.
     
    Jeff Mishler, May 28, 2004
    #2
  3. MickyV

    Jeff Mishler Guest

    Well, he didn't ask for nested xrefs. He specifically stated that he was
    working with xrefs inserted in Modelspace and Paperspace.

    However, to be more specific and to list ALL xrefs, including nested, you
    could use this:
    Dim blk As AcadBlock
    For Each blk In ThisDrawing.Blocks
    If blk.IsXRef = True Then Debug.Print blk.Name
    Next

    but you still need the inserts to obtain the path information, which means
    for nested xrefs you will still need to iterate through every object in the
    xref database.

    Jeff

    BTW, Matt, could you please set your newsreader to post in plain text? Your
    html posts are overly large for a 1 sentence post (your reply added 6k to my
    reply which was only 2k) and there are still some newsreaders that don't
    read html. Thanks.
     
    Jeff Mishler, May 28, 2004
    #3
  4. Be warned ! Iterating thru all xrefs and nested xrefs
    (and their nested xrefs...and) this way, can be painfully slow !
     
    Jorge Jimenez, May 28, 2004
    #4
  5. The Blocks property of the AcadDocument class returns a collection of
    AcadBlockDefinition objects.

    Each of those objects has an .IsXref property which returns true if the
    block definition is an external reference.

    You could traverse the blocks collection, testing that property. If you
    get a positive response, you can append the name of the block to a
    comma-delimited string.

    Once done, create a filtered selection set and you'll have every
    instance of an external reference inside a single selection set object,
    making it very easy to traverse and query your xrefs.
     
    Frank Oquendo, May 28, 2004
    #5
  6. MickyV

    Jeff Mishler Guest

    Hi Matt,
    Actually it's best to at least leave a portion of the message you are
    responding to so that someone reading your message can see what you are
    responding to.....especially since not all messages get 'synched' between
    servers.

    As for a 'couple of K', it's not that it bugs me it's a sum of the
    following:
    1. Internet newsgroup etiquette has always asked that messages be plain text
    (do a Google search if you doubt this)
    2. not everyone has DSL or Cable access. Just this group alone in the last
    week has generated 300+ posts, of which more than 90% were made in plain
    text. Now if everyone hade made these posts in html, and IF the average
    increase per post were 6K, that would've increased the total download for
    all of these messages by 1.6 megs. Now taking this further, if there are an
    average of 30 people that download all messages each week, that means that
    the adesk server needs to provide an EXTRA 48 megs of bandwidth, just for
    ONE group that is by far not the even one of the most used groups. This also
    increases the storage space that ADESK must provide which leads to
    "newsgroup maintenance" days where older messages are deleted to make room
    for the new ones. So I think of it as more of a courtesy to others, AND
    yourself, than anything else.
    3. As I said before, not all newsreaders handle html posts. And even those
    that do have an option to turn of html in meassages to avoid
    virus/ad-ware/spy-ware from inflicting their crud their computers.
    5. I just remembered that this has all been brought to your attention before
    <*shrug*>, oh well.....

    Just tryin' to help on my little ol' modem that has a hard time connecting
    at 33k or above,
    Jeff
     
    Jeff Mishler, May 28, 2004
    #6
  7. Depending on the type of drawing, there is a high probability
    that the number of blocks in the drawing is many times more
    than the number of layers.

    This means that iterating thru the layers to get the xrefs block name
    should be much faster.

    Also, it seems to me, that using a selection set on an nested Xref
    will not yield any insertions for the current drawing.
     
    Jorge Jimenez, May 28, 2004
    #7
  8. You could always compare the various collection
    counts and then branch to the faster algor.

    :)

     
    michael puckett, May 28, 2004
    #8
  9. Yes, that is a good idea !

    --
    Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica


     
    Jorge Jimenez, May 28, 2004
    #9
  10. MickyV

    MickyV Guest

    Crikey! You take an extra long weekend, and when you get back, posts have sprouted like mushrooms!

    You're right Jeff. I'm not after any nested xrefs. You're code works a treat.

    Much appreciated,
    Mick.
     
    MickyV, Jun 1, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.