Access a block from the name?

Discussion in 'AutoCAD' started by GARYNTX, Aug 30, 2004.

  1. GARYNTX

    GARYNTX Guest

    I'm trying to access a block via by entering the name. I have a couple of problems that I don't know how to fix.

    There is always just one instance of the block in the drawing. So I figured that I could just create a selection set and then access the first item.

    First when I run the program and get the selection set I havent figured out how to get the first item.
    Second is when I try to run the program again it say the selection set exists and then crashes....so somehow I need to remove the selection set when I am finished. I tried ss.clear but that doesn't seem to work. attached is the program that I am trying to fix. Any help would be greatly appreciated.


    Sub Test()
    Dim fType(3) As Integer, fData(3), BlockName
    Dim ss As AcadSelectionSet, oBlk As AcadBlockReference
    BlockName = "4036F1-ST_L-ST2-B" '

    Set ss = ThisDrawing.SelectionSets.Add("ss6")
    fType(0) = 0: fData(0) = "INSERT"
    fType(1) = 2: fData(1) = BlockName
    ss.Select acSelectionSetAll, , , fType, fData

    'I though you could get the block this way
    Set oBlk = ss(1)

    'I also thought that you could clear the selection set here
    ss.Clear


    End Sub
     
    GARYNTX, Aug 30, 2004
    #1
  2. GARYNTX

    Ed Jobe Guest

    You can't add an ss if the name already exists. This is due to the fact that
    ss's are stored in a collection, which is indexed and you can't have
    duplicate keys in the index. Here are some functions I wrote that should fix
    both your problems.

    Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
    ' This routine does the error trapping neccessary for creating selection
    sets.
    On Error Resume Next
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
    Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
    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
    To get at the objects in a selectionset, use the Item method. All
    collections have a Count property and an Item method.
    Dim blk As AcadBlockReference
    If ss.Count > 0 Then
    Set blk = ss.Item(0) 'zero based index
    End If

    Or use a For Each structure

    For Each blk In ss
    'you can assume each object in the ss is a blk insert
    'because you filtered it. Otherwise use a var of type AcadEntity.
    'do something with the blk.
    Next blk

    --
    ----
    Ed
    ----
    problems that I don't know how to fix.
    figured that I could just create a selection set and then access the first
    item.
    exists and then crashes....so somehow I need to remove the selection set
    when I am finished. I tried ss.clear but that doesn't seem to work. attached
    is the program that I am trying to fix. Any help would be greatly
    appreciated.
     
    Ed Jobe, Aug 30, 2004
    #2
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.