Detach Xref

Discussion in 'AutoCAD' started by Pat Bjork, Apr 13, 2004.

  1. Pat Bjork

    Pat Bjork Guest

    I am trying to detatch all xrefs in the current drawing. I am using the
    following code but I am getting an error. The error I get is an Automation
    error - Unspecified Error. This code will detach one xref. I think the error
    happens when the program hits the last block in curBlock. When I hit debug
    it highlights the line "Next curBlock". Can anyone tell me how to get around
    this, or provide different code to get this done. Thanks in advance,

    Pat

    Dim curBlock As AcadBlock
    Dim myBlocks As AcadBlocks

    Set myBlocks = ThisDrawing.Blocks
    For Each curBlock In myBlocks
    If curBlock.IsXRef Then

    Debug.Print curBlock.name
    curBlock.Detach

    End If

    Next curBlock
     
    Pat Bjork, Apr 13, 2004
    #1
  2. Pat Bjork

    Ed Jobe Guest

    No, that wouldn't be it. For Each is looking for the last one. Try setting a
    breakpoint and stepping through the code to get a better idea. Watch the
    values of variables. Is one of the xrefs on a locked/frozen layer?

    BTW, you don't need to set the block collection to a var, just:
    For Each curBlock In ThisDrawing.Blocks
    You might do it if you were going to be referring to the collection again
    later, though.
     
    Ed Jobe, Apr 13, 2004
    #2
  3. Pat Bjork

    Pat Bjork Guest

    Ed, thanks for the quick reply. It still does not work. If I take out the
    line that reads "curBlock.detach" it will run fine. If I leave that line in
    I get the error. Any thoughts?

    Thanks
     
    Pat Bjork, Apr 13, 2004
    #3
  4. Pat Bjork

    Ed Jobe Guest

    I gave you one. Don't just run the code, debug it. Here's what I mean. In
    the vbaide, make sure you have the Debug toolbar showing. Click in the left
    margin of the code module at the location of the For loop. This will place a
    breakpoint at that location, marked by a dot in the margin and highlighting
    the line. Now start execution by clicking on the Run button on the Debug
    toobar and the execution will break at that line. Step through the code one
    line at a time by clicking on the Step Into button (F8). Watch the value of
    curBlock.Name (look in the Locals window or hover your mouse over the var)
    through each iteration. Note the name of the block that is being processed
    when the error occurs. Find out why *that* block is causing problems. It may
    be on a locked/frozen layer. When you find out what the problem is, you can
    include logic in your code to exclude them from the loop.
     
    Ed Jobe, Apr 13, 2004
    #4
  5. Pat Bjork

    pkirill Guest

    Are any xrefs nested? What I found was that if you detach an xref that has
    nested xrefs in it, the nested xrefs stay in the block list until your loop
    is done. And since they cannot be detached because either a) they are nested
    and can only be detached from the parent, and b) because the parent has
    already been detached and thus the nested xrefs are detached but still
    resident in the drawing database.

    You have to put in some error correction that kicks in if you try to detach
    an xref that is not there or cannot be detached. When it hits an arror,
    refresh the block list and try again. This may be clunky, but it works for
    me...

    For Each blkMyBlock In dwg.Blocks
    On Error Resume Next 'GoTo MOVEON
    If blkMyBlock.IsXRef Then
    If Err Then GoTo MOVEON1
    dwg.Blocks.Item(blkMyBlock.Name).Detach
    End If
    MOVEON1: Next blkMyBlock
     
    pkirill, Apr 14, 2004
    #5
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.