+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Senior Member pefi's Avatar
    Using
    AutoCAD 2005
    Join Date
    Jul 2006
    Location
    Cork, Ireland
    Posts
    166

    Default How get last inserted block? [vba]

    Registered forum members do not see this ad.

    Hi lads! (and not only lads I think...)

    Code:
    ThisDrawing.SendCommand "-insert" & vbCr & BlockType & vbCr & ptPoint(0) & "," & ptPoint(1) & vbCr & vbCr & vbCr & vbCr ' insert block
          
    Set ent = ThisDrawing.ModelSpace.Item(ThisDrawing.ModelSpace.Count-1)
    Should I have hope that ent will point to my inserted block _always_ or maybe is another way to do it?
    I need insert block and edit attributes (values from excel sheet). It will work in loop so everytime I need to edit only last inserted block (cannot use search-by-names to find block cause I'm using many times the same block)

    Cheers,
    Przemo

  2. #2
    Senior Member kpblc's Avatar
    Using
    AutoCAD 2005
    Join Date
    May 2006
    Location
    Russia, St-Petersburg
    Posts
    317

    Default

    I think the better way will be something like this:
    Code:
    Sub test()
    Dim ins_pt(2) As Double
    Dim ins_bl As AcadBlockReference
      ins_pt(0) = 0#: ins_pt(1) = 0#: ins_pt(2) = 0#
      Set ins_bl = ThisDrawing.ModelSpace.InsertBlock(ins_pt, "rte", 1#, 1#, 1#, 0#)
    End Sub
    After you get ins_bl you can edit attributes of this reference
    All I say is only my opinion.

  3. #3
    Full Member
    Using
    AutoCAD 2008
    Join Date
    Apr 2006
    Location
    Los Angeles
    Posts
    65

    Default

    I use .Select acSelectionSetLast and it works.
    The important thing to do is check for a user escape or pan and exit the sub before setting the Ent, else with either method you will end up messing with a previously inserted entity

    Function Panned() As Boolean
    Dim varCancel As Variant
    varCancel = ThisDrawing.GetVariable("LASTPROMPT")
    If varCancel = "Specify rotation angle: 0" Then
    'Debug.Print GetAsyncKeyState(VK_MBUTTON)
    If GetAsyncKeyState(VK_MBUTTON) < 0 Then
    Panned = True
    End If
    Else
    'Missed the pick, send them back!
    Panned = False
    End If
    End Function

  4. #4
    Senior Member pefi's Avatar
    Using
    AutoCAD 2005
    Join Date
    Jul 2006
    Location
    Cork, Ireland
    Posts
    166

    Default

    Thanks lads!
    As usual great replies! And it's not "dirty coding" like mine...
    Przemo

  5. #5
    Forum Newbie
    Using
    AutoCAD 2007
    Join Date
    Sep 2006
    Posts
    1

    Default This will return the handle for the last entity of type block ref

    Code:
     
    Function lastenthandle() As String
        Dim entity As AcadObject
        Dim count As Variant
        ' set count to all items in modelspace collection
        count = ThisDrawing.ModelSpace.count
        Set entity = ThisDrawing.ModelSpace.Item(count - 1)
        ' check if entity is a block if not count down until it is
        While count > 0
        If StrComp(entity.EntityName, "AcDbBlockReference", 1) = 0 Then
            If entity.HasAttributes Then
                lastenthandle = entity.handle
                Exit Function
            End If
        End If
            count = count - 1
            Set entity = ThisDrawing.ModelSpace.Item(count)
        Wend
    End Function

  6. #6
    Senior Member pefi's Avatar
    Using
    AutoCAD 2005
    Join Date
    Jul 2006
    Location
    Cork, Ireland
    Posts
    166

    Default

    Thanks for that!
    Now I've got 4 different ways to get last block! I love this forum!
    Przemo

Bookmarks

Posting Permissions

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