Results 1 to 3 of 3

Thread: How to Delete Multiple Instances of a BlockReference?

  1. #1
    Member
    Join Date
    2006-11
    Posts
    21
    Login to Give a bone
    0

    Default How to Delete Multiple Instances of a BlockReference?

    I have some drawings with multiple insertions of an attributed block. I have pulled the attribute values from the block and want to delete all instances of the block from the drawing so I can insert a clean version of the block and update the attributes with the attribute values stored previously.

    The problem is I am havening trouble deleting more than one instance of the same block.

    Code snippet as follows:
    --------------------------------------------------------------------------
    For Each elem In ThisDrawing.ModelSpace
    ThisDrawing.PurgeAll
    If TypeOf elem Is AcadBlockReference Then
    Set aBRef = elem
    ' If StrComp(elem.EntityName, "AcDbBlockReference", 1) = 0 Then
    If aBRef.Name = "bdrtit" Or aBRef.Name = "bdrref" Or aBRef.Name = "BdrIssue" Or aBRef.Name = "bdrrev" Then
    aBRef.Delete
    End If
    End If
    Next elem
    --------------------------------------------------------------------------

    Any thoughts?

    Thanks,

    Brad

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,420
    Login to Give a bone
    0

    Default Re: How to Delete Multiple Instances of a BlockReference?

    Try these functions for getting a ss of blocks.

    Code:
    Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
    ' This routine does the error trapping neccessary for when you want to create a
    ' selectin set. It takes the set and the proposed name and either adds it to the selectionsets
    ' collection or sets it.
        On Error Resume Next
        Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
        If Err.Number <> 0 Then
            Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
            AddSelectionSet.Clear
        End If
    End Function
    
    Public Function GetSS_BlockName(BlockName As String) As AcadSelectionSet
        'creates a ss of blocks with the name supplied in the argument
        Dim s2 As AcadSelectionSet
        
        Set s2 = AddSelectionSet("ssBlocks")                ' create ss with a name
        s2.Clear                                        ' clear the set
        Dim intFtyp(3) As Integer                       ' setup for the filter
        Dim varFval(3) As Variant
        Dim varFilter1, varFilter2 As Variant
        intFtyp(0) = -4: varFval(0) = "<AND"
        intFtyp(1) = 0: varFval(1) = "INSERT"           ' get only blocks
        intFtyp(2) = 2: varFval(2) = BlockName          ' whose name is specified in argument
        intFtyp(3) = -4: varFval(3) = "AND>"
        varFilter1 = intFtyp: varFilter2 = varFval
        s2.Select acSelectionSetAll, , , varFilter1, varFilter2        ' do it
        Set GetSS_BlockName = s2
    
    End Function
    C:> ED WORKING....


    LinkedIn

  3. #3
    AUGI Addict MikeJarosz's Avatar
    Join Date
    2015-10
    Location
    New York NY
    Posts
    1,497
    Login to Give a bone
    0

    Default Re: How to Delete Multiple Instances of a BlockReference?

    I wrote practically the same thing several years ago. Some of this code I would do differently today, but this does work. This example was written for a large project where we decided after several hundred sheets were drawn that the door target had to be changed. What it does is make a selection set of all the door targets on the sheet, loop through the set extracting xyz, scale rotation and attributes. Then it deletes the door target and inserts the new one with the same attributes.

    It was designed to be called as a subroutine from a larger batch program that loads the sheets one at a time, runs the routine then saves the sheet.

    Code:
     
     '================================================================================================
    Sub ReplaceBlock()
    '================================================================================================
    Dim Ipt As Variant
    Dim RoomNumber As String
    Dim BldgLocation As String
    Dim DoorSequence As String
    Dim Attrib As Variant
    Dim Tag As Variant
    Dim Angle As Double
    Dim DrTar As AcadBlockReference
    Dim BlkRef As AcadBlockReference
    Const ScX As Double = 1#
    Const ScY As Double = 1#
    Const ScZ As Double = 1#
    Dim Target As String
    Target = "w:\a\blocks\door_tag_oval.dwg"
    Dim Sset As AcadSelectionSet
    Set Sset = ThisDrawing.SelectionSets.Add("DoorTarget")
    Dim Codes() As Integer
    ReDim Codes(2)
    Dim CodeValues() As Variant
    ReDim CodeValues(2)
    Codes(1) = 0
    Codes(2) = 2
    CodeValues(1) = "INSERT"
    CodeValues(2) = "door_tag" 'Block name
    Sset.Select acSelectionSetAll, , , Codes, CodeValues
    If Sset.Count = 0 Then Exit Sub
    For Each DrTar In Sset
        Ipt = DrTar.InsertionPoint
        Ipt(2) = 0#
        Angle = DrTar.Rotation
        RoomNumber = ""
        BldgLocation = ""
        DoorSequence = ""
     
        For Each Attrib In DrTar.GetAttributes
        With Attrib
            Select Case .TagString
            Case "RMNO"
                RoomNumber = Trim(.TextString)
            Case "LO"
                BldgLocation = Trim(.TextString)
            Case "DS"
                DoorSequence = Trim(.TextString)
            End Select
        End With
        Next Attrib
     
        If RoomNumber = "" Then RoomNumber = "----"
        If BldgLocation = "" Then BldgLocation = "--"
        If DoorSequence = "" Then DoorSequence = "--"
        DrTar.Delete
     
        'insert new block here and set attributes
        Set BlkRef = ThisDrawing.ModelSpace.InsertBlock(Ipt, Target, ScX, ScY, ScZ, Angle)
        For Each Tag In BlkRef.GetAttributes
            Select Case Tag.TagString
            Case "RMNO"
                Tag.TextString = RoomNumber
            Case "LO"
                Tag.TextString = BldgLocation
            Case "DS"
                Tag.TextString = DoorSequence
            End Select
        Next Tag
        BlkRef.Update
        Print #G, RoomNumber; Chr(9); BldgLocation; Chr(9); DoorSequence; Chr(9); _
            Ipt(0); Chr(9); Ipt(1); Chr(9); FileName
    Next DrTar
    ThisDrawing.PurgeAll
    ThisDrawing.PurgeAll
    ThisDrawing.SelectionSets("DoorTarget").Delete
    End Sub
    The redim() statements I learned from the Joe Sutphin book. It's not really neccessary. Just dim the array directly. Also, I was in the habit of setting option base 1. I don't do that anymore. Just remember you are always off by 1.

Similar Threads

  1. 2014: Multiple instances of a link
    By MikeJarosz in forum Revit Architecture - General
    Replies: 6
    Last Post: 2014-04-23, 09:36 PM
  2. I want to allow multiple instances in the same place
    By timsea81 in forum Revit MEP - General
    Replies: 3
    Last Post: 2010-10-27, 01:02 PM
  3. Multiple instances
    By jjanis in forum AutoCAD General
    Replies: 9
    Last Post: 2010-01-22, 10:54 PM
  4. Multiple Instances of ACLT2009
    By cad.192369 in forum AutoCAD LT - General
    Replies: 4
    Last Post: 2008-10-07, 10:28 AM
  5. Delete all instances of a specified block from the drawing
    By CADdancer in forum VBA/COM Interop
    Replies: 1
    Last Post: 2006-10-23, 06:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •