Find Last Entity with Layouts
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
Find Last Entity with Layouts

 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA
Author Message
Oberer
Guest





Posted: Mon Jan 03, 2005 8:19 pm    Post subject: Find Last Entity with Layouts Reply with quote

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

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

Back to top
Jeff Mishler
Guest





Posted: Tue Jan 04, 2005 12:07 am    Post subject: Re: Find Last Entity with Layouts Reply with quote

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
check out www.cadvault.com
"Oberer" <nospam@address.withheld> wrote in message
news:26895776.1104765615698.JavaMail.jive@jiveforum1.autodesk.com...
Quote:
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

'-----------------------------------------------------
Back to top
Oberer
Guest





Posted: Tue Jan 04, 2005 2:41 am    Post subject: Re: Find Last Entity with Layouts Reply with quote

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


Back to top
 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Powered by phpBB