Last entity created in drawing?

Last entity created in drawing?

Anonymous
Not applicable
697 Views
7 Replies
Message 1 of 8

Last entity created in drawing?

Anonymous
Not applicable
I want to return the last entity created in a drawing. I was thinking to use handles somehow. Are handle created in increasing order?
0 Likes
698 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
How about the last entity created in model space or a paper space layout? If so, just get the last element in the appropriate collection (using .Count - 1 as the index). -- http://www.caddzone.com AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005 http://www.acadxtabs.com "jwilkerson" wrote in message news:31298802.1109869226078.JavaMail.jive@jiveforum2.autodesk.com... >I want to return the last entity created in a drawing. I was thinking to use handles somehow. Are handle created in >increasing order?
0 Likes
Message 3 of 8

Anonymous
Not applicable
(entlast) ___ "jwilkerson" wrote in message news:31298802.1109869226078.JavaMail.jive@jiveforum2.autodesk.com... >I want to return the last entity created in a drawing.
0 Likes
Message 4 of 8

Anonymous
Not applicable
Jeff Mishler helped me with this code.
this code will find the last entity, regardless of space or layout. it will then zoom to the last objects bounding box coords.


[code]
'Find last entity added to drawing, zoom to location, and select

'http://discussion.autodesk.com/thread.jspa?messageID=4227426腢
' thanks Jeff Mishler, once again for the code help


Option Explicit

Private Const mconSmallestViewSize As Double = 100

Sub ZoomToLastEntity()
On Error Resume Next

Dim oLayout As AcadLayout
Dim oLayouts As AcadLayouts
Dim oLayoutToRestore As AcadLayout
Dim oEnt As AcadEntity
Dim oEntTemp As AcadEntity
Dim lEntHandle As Long
Dim ltemphandle As Long

Set oLayouts = ThisDrawing.Layouts
' Search through every layout (model and all tabs) for last entity created
For Each oLayout In oLayouts
Set oEntTemp = LastEntity(oLayout.Block)
If Not oEntTemp Is Nothing Then
ltemphandle = CLng("&H" & oEntTemp.Handle)
If ltemphandle > lEntHandle Then
lEntHandle = ltemphandle
Set oEnt = oEntTemp
Set oLayoutToRestore = oLayout
End If
End If
Next

If Not oLayoutToRestore Is Nothing Then
If ThisDrawing.ActiveLayout.Name <> oLayoutToRestore.Name Then ThisDrawing.ActiveLayout = oLayoutToRestore
'shouldn't be here! should always have at least one?
Else
MsgBox "ERROR finding layout object"
End If

'make sure we're in pspace in layout tab. don't want to zoom in a viewport
If oLayoutToRestore.Name <> "Model" Then
ThisDrawing.MSpace = False
End If


If Not oEnt Is Nothing Then
' Zoom to entity
ZoomToEntity oEnt
' display entity info
ThisDrawing.Utility.Prompt "Entity Type: " & oEnt.ObjectName & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Layer: " & oEnt.Layer & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Linetype: " & oEnt.Linetype & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Linetype scale: " & oEnt.LinetypeScale & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Lineweight: " & oEnt.Lineweight & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Layout: " & oLayoutToRestore.Name & vbCr
AcadApplication.Update
' select object

ThisDrawing.Utility.Prompt "Last object is selected"
AcadApplication.Update
Set oEnt = Nothing
ThisDrawing.SendCommand (".pselect" & vbCr & "last" & vbCr & vbCr)
'shouldn't be here.
Else
MsgBox "No ENTITY FOUND."
End If


Set oEntTemp = Nothing
Set oLayout = Nothing
Set oLayouts = Nothing
Set oLayoutToRestore = Nothing
If Err.Number <> 0 Then Err.Clear
End Sub

Private Sub ZoomToEntity(oEntityToZoomTo As AcadEntity)
Dim vLowerLeft As Variant
Dim vUpperRight As Variant
Dim vViewSize As Variant
Dim dblScaleFactor As Double

' get bounding box of object
oEntityToZoomTo.GetBoundingBox vLowerLeft, vUpperRight
' zoom window with bounding box coords
ZoomWindow vLowerLeft, vUpperRight

' get new view size
vViewSize = ThisDrawing.GetVariable("VIEWSIZE")

'model space resize
If ThisDrawing.ActiveSpace = acModelSpace Then
' if object is small, zoom out a bit
If vViewSize < mconSmallestViewSize Then
dblScaleFactor = vViewSize / mconSmallestViewSize
AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative
End If
' paper space (for us) tends to be much different. our view size is rarely over 30
Else
If vViewSize < mconSmallestViewSize Then
dblScaleFactor = vViewSize / 10
AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative
End If
End If


End Sub

Public Function LastEntity(container As AcadBlock) As AcadEntity
On Error Resume Next
Set LastEntity = container(container.Count - 1)
If Err.Number <> 0 Then Err.Clear
End Function


[/code]
0 Likes
Message 5 of 8

Anonymous
Not applicable
> Option Explicit > > Private Const mconSmallestViewSize As Double = 100 > > Sub ZoomToLastEntity() > On Error Resume Next > > Dim oLayout As AcadLayout > Dim oLayouts As AcadLayouts > Dim oLayoutToRestore As AcadLayout > Dim oEnt As AcadEntity > Dim oEntTemp As AcadEntity > Dim lEntHandle As Long > Dim ltemphandle As Long > > Set oLayouts = ThisDrawing.Layouts > ' Search through every layout (model and all tabs) for last entity > created > For Each oLayout In oLayouts > Set oEntTemp = LastEntity(oLayout.Block) > If Not oEntTemp Is Nothing Then > ltemphandle = CLng("&H" & oEntTemp.Handle) > If ltemphandle > lEntHandle Then > lEntHandle = ltemphandle > Set oEnt = oEntTemp > Set oLayoutToRestore = oLayout > End If > End If > Next > > If Not oLayoutToRestore Is Nothing Then > If ThisDrawing.ActiveLayout.Name <> oLayoutToRestore.Name Then > ThisDrawing.ActiveLayout = oLayoutToRestore > 'shouldn't be here! should always have at least one? > Else > MsgBox "ERROR finding layout object" > End If > > 'make sure we're in pspace in layout tab. don't want to zoom in a > viewport > If oLayoutToRestore.Name <> "Model" Then > ThisDrawing.MSpace = False > End If > > > If Not oEnt Is Nothing Then > ' Zoom to entity > ZoomToEntity oEnt > ' display entity info > ThisDrawing.Utility.Prompt "Entity Type: " & oEnt.ObjectName & vbCr > AcadApplication.Update > ThisDrawing.Utility.Prompt "Layer: " & oEnt.Layer & vbCr > AcadApplication.Update > ThisDrawing.Utility.Prompt "Linetype: " & oEnt.Linetype & vbCr > AcadApplication.Update > ThisDrawing.Utility.Prompt "Linetype scale: " & oEnt.LinetypeScale > & vbCr > AcadApplication.Update > ThisDrawing.Utility.Prompt "Lineweight: " & oEnt.Lineweight & vbCr > AcadApplication.Update > ThisDrawing.Utility.Prompt "Layout: " & oLayoutToRestore.Name & > vbCr > AcadApplication.Update > ' select object > > ThisDrawing.Utility.Prompt "Last object is selected" > AcadApplication.Update > Set oEnt = Nothing > ThisDrawing.SendCommand (".pselect" & vbCr & "last" & vbCr & vbCr) > 'shouldn't be here. > Else > MsgBox "No ENTITY FOUND." > End If > > > Set oEntTemp = Nothing > Set oLayout = Nothing > Set oLayouts = Nothing > Set oLayoutToRestore = Nothing > If Err.Number <> 0 Then Err.Clear > End Sub > > Private Sub ZoomToEntity(oEntityToZoomTo As AcadEntity) > Dim vLowerLeft As Variant > Dim vUpperRight As Variant > Dim vViewSize As Variant > Dim dblScaleFactor As Double > > ' get bounding box of object > oEntityToZoomTo.GetBoundingBox vLowerLeft, vUpperRight > ' zoom window with bounding box coords > ZoomWindow vLowerLeft, vUpperRight > > ' get new view size > vViewSize = ThisDrawing.GetVariable("VIEWSIZE") > > 'model space resize > If ThisDrawing.ActiveSpace = acModelSpace Then > ' if object is small, zoom out a bit > If vViewSize < mconSmallestViewSize Then > dblScaleFactor = vViewSize / mconSmallestViewSize > AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative > End If > ' paper space (for us) tends to be much different. our view size is > rarely over 30 > Else > If vViewSize < mconSmallestViewSize Then > dblScaleFactor = vViewSize / 10 > AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative > End If > End If > > > End Sub > > Public Function LastEntity(container As AcadBlock) As AcadEntity > On Error Resume Next > Set LastEntity = container(container.Count - 1) > If Err.Number <> 0 Then Err.Clear > End Function > > > [/code] WOW! Now I know VBA is better than lisp! And all this time I've been using (entlast) . . . ;-) jb
0 Likes
Message 6 of 8

Anonymous
Not applicable
> > WOW! Now I know VBA is better than lisp! And all this time I've been > using (entlast) . . . ;-) > > jb Just to show that I learn from my posts here, Thanks Tony!, here's something that works pretty well for me. First a lisp function is required to be defined in the current drawing: ************* (defun *entlast* () (cdr (assoc 5 (entget (entlast)))) ) ************* Then the VBA function: ************* Public Function entLast() As AcadEntity Dim vl As Object Dim vlFun As Object Dim Result As String Set vl = ThisDrawing.Application.GetInterfaceObject("VL.Application.1") Set vlFun = vl.ActiveDocument.Functions Result = vlFun.Item("*entlast*") Set entLast = ThisDrawing.HandleToObject(Result) End Function ************ And to test it: ************ Sub testme() Dim ent As AcadEntity Set ent = entLast Debug.Print ent.ObjectName End Sub
0 Likes
Message 7 of 8

Anonymous
Not applicable
And to eliminate the need for a seperate lisp that must be loaded: Public Function entLast() As AcadEntity Dim vl As Object Dim vlFun As Object Dim Result As String Set vl = ThisDrawing.Application.GetInterfaceObject("VL.Application.1") Set vlFun = vl.ActiveDocument.Functions On Error GoTo err_Here Result = vlFun.Item("*jmm:entlast*") GoTo exit_Here err_Here: Select Case Err.Number Case Is = 2000 ThisDrawing.SendCommand "(defun *jmm:entlast* ()(cdr (assoc 5 (entget (entlast))))) " Resume Case Else Set entLast = Nothing Exit Function End Select exit_Here: Set entLast = ThisDrawing.HandleToObject(Result) End Function -- Jeff check out www.cadvault.com "Jeff Mishler" wrote in message news:4227a657_3@newsprd01... > > >> WOW! Now I know VBA is better than lisp! And all this time I've been >> using (entlast) . . . ;-) >> >> jb >
0 Likes
Message 8 of 8

Anonymous
Not applicable
Tony has the right idea. Dim oEnt as AcadEntity Set oEnt = ThisDrawing.ModelSpace(ThisDrawing.ModelSpace.Count - 1) ' (for ModelSpace) "James Buzbee" wrote in message news:42279e8a$1_3@newsprd01... > > Option Explicit > > > > Private Const mconSmallestViewSize As Double = 100 > > > > Sub ZoomToLastEntity() > > On Error Resume Next > > > > Dim oLayout As AcadLayout > > Dim oLayouts As AcadLayouts > > Dim oLayoutToRestore As AcadLayout > > Dim oEnt As AcadEntity > > Dim oEntTemp As AcadEntity > > Dim lEntHandle As Long > > Dim ltemphandle As Long > > > > Set oLayouts = ThisDrawing.Layouts > > ' Search through every layout (model and all tabs) for last entity > > created > > For Each oLayout In oLayouts > > Set oEntTemp = LastEntity(oLayout.Block) > > If Not oEntTemp Is Nothing Then > > ltemphandle = CLng("&H" & oEntTemp.Handle) > > If ltemphandle > lEntHandle Then > > lEntHandle = ltemphandle > > Set oEnt = oEntTemp > > Set oLayoutToRestore = oLayout > > End If > > End If > > Next > > > > If Not oLayoutToRestore Is Nothing Then > > If ThisDrawing.ActiveLayout.Name <> oLayoutToRestore.Name Then > > ThisDrawing.ActiveLayout = oLayoutToRestore > > 'shouldn't be here! should always have at least one? > > Else > > MsgBox "ERROR finding layout object" > > End If > > > > 'make sure we're in pspace in layout tab. don't want to zoom in a > > viewport > > If oLayoutToRestore.Name <> "Model" Then > > ThisDrawing.MSpace = False > > End If > > > > > > If Not oEnt Is Nothing Then > > ' Zoom to entity > > ZoomToEntity oEnt > > ' display entity info > > ThisDrawing.Utility.Prompt "Entity Type: " & oEnt.ObjectName & vbCr > > AcadApplication.Update > > ThisDrawing.Utility.Prompt "Layer: " & oEnt.Layer & vbCr > > AcadApplication.Update > > ThisDrawing.Utility.Prompt "Linetype: " & oEnt.Linetype & vbCr > > AcadApplication.Update > > ThisDrawing.Utility.Prompt "Linetype scale: " & oEnt.LinetypeScale > > & vbCr > > AcadApplication.Update > > ThisDrawing.Utility.Prompt "Lineweight: " & oEnt.Lineweight & vbCr > > AcadApplication.Update > > ThisDrawing.Utility.Prompt "Layout: " & oLayoutToRestore.Name & > > vbCr > > AcadApplication.Update > > ' select object > > > > ThisDrawing.Utility.Prompt "Last object is selected" > > AcadApplication.Update > > Set oEnt = Nothing > > ThisDrawing.SendCommand (".pselect" & vbCr & "last" & vbCr & vbCr) > > 'shouldn't be here. > > Else > > MsgBox "No ENTITY FOUND." > > End If > > > > > > Set oEntTemp = Nothing > > Set oLayout = Nothing > > Set oLayouts = Nothing > > Set oLayoutToRestore = Nothing > > If Err.Number <> 0 Then Err.Clear > > End Sub > > > > Private Sub ZoomToEntity(oEntityToZoomTo As AcadEntity) > > Dim vLowerLeft As Variant > > Dim vUpperRight As Variant > > Dim vViewSize As Variant > > Dim dblScaleFactor As Double > > > > ' get bounding box of object > > oEntityToZoomTo.GetBoundingBox vLowerLeft, vUpperRight > > ' zoom window with bounding box coords > > ZoomWindow vLowerLeft, vUpperRight > > > > ' get new view size > > vViewSize = ThisDrawing.GetVariable("VIEWSIZE") > > > > 'model space resize > > If ThisDrawing.ActiveSpace = acModelSpace Then > > ' if object is small, zoom out a bit > > If vViewSize < mconSmallestViewSize Then > > dblScaleFactor = vViewSize / mconSmallestViewSize > > AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative > > End If > > ' paper space (for us) tends to be much different. our view size is > > rarely over 30 > > Else > > If vViewSize < mconSmallestViewSize Then > > dblScaleFactor = vViewSize / 10 > > AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative > > End If > > End If > > > > > > End Sub > > > > Public Function LastEntity(container As AcadBlock) As AcadEntity > > On Error Resume Next > > Set LastEntity = container(container.Count - 1) > > If Err.Number <> 0 Then Err.Clear > > End Function > > > > > > [/code] > > WOW! Now I know VBA is better than lisp! And all this time I've been using > (entlast) . . . ;-) > > jb > >
0 Likes