AutoCAD.Internal.Utils.ZoomObjects(True) ‘Z O
Imports ZwSoft.ZwCAD.Runtime
Imports ZwSoft.ZwCAD.DatabaseServicesImports ZwSoft.ZwCAD.Geometry
Imports ZwSoft.ZwCAD.EditorInput
Imports ZwSoft.ZwCAD.ApplicationServices
Public Class Commands
<CommandMethod("ZoomToObjects")> _
Public Shared Sub ZoomToObjects()
Dim ZcDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim ZcDb As Database = ZcDoc.Database
Dim ZcEd As Editor = ZcDoc.Editor
Dim result As PromptSelectionResult = ZcEd.GetSelection()
Dim entExt As Extents3d = Nothing
Dim entMaxpoints As Point2dCollection = New Point2dCollection
Dim entMinpoints As Point2dCollection = New Point2dCollection
If result.Status = PromptStatus.OK AndAlso result.Value.Count > 0 Then
Dim ids As ObjectId() = result.Value.GetObjectIds()
Using lockDocument As DocumentLock = ZcDoc.LockDocument
Using ZcTrans As Transaction = ZcDb.TransactionManager.StartTransaction()
For Each id As ObjectId In ids
Dim ent As Entity = ZcTrans.GetObject(id, OpenMode.ForRead)
entExt = ent.Bounds.GetValueOrDefault()
entMaxpoints.Add(New Point2d(entExt.MaxPoint.X, entExt.MaxPoint.Y))
entMinpoints.Add(New Point2d(entExt.MinPoint.X, entExt.MinPoint.Y))
Next
End Using
End Using
Dim minPt As Point2d = New Point2d
Dim maxPt As Point2d = New Point2d
If ids.Count > 1 Then
MinOrMaxPt(entMinpoints, "min", minPt)
MinOrMaxPt(entMaxpoints, "max", maxPt)
Else
minPt = entMinpoints(0)
maxPt = entMaxpoints(0)
End If
Dim NewView As ViewTableRecord = New ViewTableRecord()
NewView.CenterPoint = minPt + ((maxPt - minPt) / 2.0)
NewView.Height = maxPt.Y - minPt.Y
NewView.Width = maxPt.X - minPt.X
ZcEd.SetCurrentView(NewView)
End If
End Sub
Private Shared Sub MinOrMaxPt(ByVal pts As Point2dCollection, ByVal flag As String, ByRef Pt As Point2d)
Dim temMinX As Double = pts(0).X, temMinY As Double = pts(0).Y
Dim temMaxX As Double = pts(0).X, temMaxY As Double = pts(0).Y
If flag = "min" Then
For Each subPt As Point2d In pts
If subPt.X < temMinX Then
temMinX = subPt.X
End If
If subPt.Y < temMinY Then
temMinY = subPt.Y
End If
Next
Pt = New Point2d(temMinX, temMinY)
ElseIf flag = "max" Then
For Each subPt As Point2d In pts
If subPt.X > temMaxX Then
temMaxX = subPt.X
End If
If subPt.Y > temMaxY Then
temMaxY = subPt.Y
End If
Next
Pt = New Point2d(temMaxX, temMaxY)
End If
End Sub
End Class