Find Last Entity with Layouts

Discussion in 'AutoCAD' started by Oberer, Jan 3, 2005.

  1. Oberer

    Oberer Guest

    After searching the NG and google, I've not found any code that seems to work with layouts.

    (I'd like to zoom to the last entity added to the db, regardless of space or layout)

    Here's what I've found so far:
    (both seem to work in MS, and IF you're on the tab that has the last entity, it's ok.

    Any Ideas?
    TIA

    '-----------------------------------------------------
    'LAST ENTITY EXAMPLE #1
    '-----------------------------------------------------

    Public Function LastEntity(container As AcadBlock) As AcadEntity
    Set LastEntity = container(container.Count - 1)
    End Function


    Sub test1()
    Dim oEnt As AcadEntity
    Dim vLowerLeft As Variant
    Dim vUpperRight As Variant

    If ThisDrawing.GetVariable("TILEMODE") = 1 Then
    Set oEnt = LastEntity(ThisDrawing.ModelSpace)
    Else
    Set oEnt = LastEntity(ThisDrawing.PaperSpace)
    End If

    oEnt.Highlight True
    oEnt.GetBoundingBox vLowerLeft, vUpperRight
    ZoomWindow vLowerLeft, vUpperRight

    End Sub

    '-----------------------------------------------------




    '-----------------------------------------------------
    'LAST ENTITY EXAMPLE #2
    '-----------------------------------------------------
    Public Function EntLast() As Object

    'returns the last object created

    Dim lastObj(0 To 1) As Object
    Dim lngObj(0 To 1) As Long

    'get the handle of the last object in model space
    Set lastObj(acModelSpace) = ThisDrawing.ModelSpace(ThisDrawing.ModelSpace.Count - 1)
    lngObj(acModelSpace) = CLng("&H" & lastObj(acModelSpace).Handle)

    'get the handle of the last object in paper space
    Set lastObj(acPaperSpace) = ThisDrawing.PaperSpace(ThisDrawing.PaperSpace.Count - 1)
    lngObj(acPaperSpace) = CLng("&H" & lastObj(acPaperSpace).Handle)

    If lngObj(acPaperSpace) > lngObj(acModelSpace) Then
    Set EntLast = lastObj(acPaperSpace)
    Else
    Set EntLast = lastObj(acModelSpace)
    End If

    Set lastObj(0) = Nothing
    Set lastObj(1) = Nothing

    End Function

    Sub test()
    Dim oEnt As AcadEntity
    Dim vLowerLeft As Variant
    Dim vUpperRight As Variant

    Set oEnt = EntLast

    oEnt.Highlight True
    oEnt.GetBoundingBox vLowerLeft, vUpperRight
    ZoomWindow vLowerLeft, vUpperRight

    Set oEnt = Nothing
    End Sub

    '-----------------------------------------------------
     
    Oberer, Jan 3, 2005
    #1
  2. Oberer

    Jeff Mishler Guest

    How about this?
    Code:
    Sub test()
    Dim layout As AcadLayout
    Dim entLast As AcadEntity
    Dim entTemp As AcadEntity
    Dim entHand As Long
    Dim temphand As Long
    
    For Each layout In ThisDrawing.Layouts
    Set entTemp = LastEntity(layout.Block)
    temphand = CLng("&H" & entTemp.Handle)
    If temphand > entHand Then
    entHand = temphand
    Set entLast = entTemp
    End If
    Next
    Debug.Print entLast.ObjectName
    End Sub
    
    Public Function LastEntity(container As AcadBlock) As AcadEntity
    Set LastEntity = container(container.Count - 1)
    End Function
    
     
    Jeff Mishler, Jan 3, 2005
    #2
  3. Oberer

    Oberer Guest

    Jeff, thanks again. I've posted the updated code. I was really hoping to always have a way to view the last added object, regardless of location.

    Code:
    
    'Zooms to last entity created
    'Thanks Jeff Mishler !
    
    Option Explicit
    
    
    Sub ZoomToLastEntity()
    
    
    
    
    Dim oLayout As AcadLayout
    Dim oLayoutToRestore As AcadLayout
    Dim oEnt As AcadEntity
    Dim oEntTemp As AcadEntity
    Dim lEntHandle As Long
    Dim ltemphandle As Long
    Dim vLowerLeft As Variant
    Dim vUpperRight As Variant
    
    
    ' Search through every layout (model and all tabs) for last entity created
    For Each oLayout In ThisDrawing.Layouts
    Set oEntTemp = LastEntity(oLayout.Block)
    ltemphandle = CLng("&H" & oEntTemp.Handle)
    If ltemphandle > lEntHandle Then
    lEntHandle = ltemphandle
    Set oEnt = oEntTemp
    Set oLayoutToRestore = oLayout
    End If
    Next
    
    'Debug.Print oEnt.ObjectName
    
    ThisDrawing.ActiveLayout = oLayoutToRestore
    oEnt.Highlight True
    
    oEnt.GetBoundingBox vLowerLeft, vUpperRight
    ZoomWindow vLowerLeft, vUpperRight
    
    Set oEnt = Nothing
    Set oEntTemp = Nothing
    Set oLayout = Nothing
    Set oLayoutToRestore = Nothing
    
    End Sub
    
    Public Function LastEntity(container As AcadBlock) As AcadEntity
    Set LastEntity = container(container.Count - 1)
    End Function
    
     
    Oberer, Jan 3, 2005
    #3
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.