vb.net get group extents
Imports System.Collections.Generic
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Namespace GroupExtents
Public Shared Class TransactionExtensions
' A simple extension method that aggregates the extents of any entities
' passed in (via their ObjectIds)
<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetExtents(tr As Transaction, ids As ObjectId()) As Extents3d
Dim ext As New Extents3d()
For Each id As Object In ids
Dim ent As Object = TryCast(tr.GetObject(id, OpenMode.ForRead), Entity)
If ent IsNot Nothing Then
ext.AddExtents(ent.GeometricExtents)
End If
Next
Return ext
End Function
End Class
Public Class Commands
<CommandMethod("GE")> _
Public Sub GroupExtents()
Dim doc As Object = Application.DocumentManager.MdiActiveDocument
Dim db As Object = doc.Database
Dim ed As Object = doc.Editor
Using tr As Object = db.TransactionManager.StartTransaction()
' Get the group dictionary from the drawing
Dim gd As DBDictionary = CType(tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead), DBDictionary)
If gd.Count = 0 Then
ed.WriteMessage(vbLf & "No groups found in drawing.")
Else
' List the groups in the drawing with an index
Dim groupNames As New List(Of String)(gd.Count)
ed.WriteMessage(vbLf & "Groups:")
Dim i As Integer = 0
For Each entry As Object In gd
i += 1
ed.WriteMessage(vbLf & "{0}. {1}", i, entry.Key)
groupNames.Add(entry.Key)
Next
' Ask the user to select a group number
Dim pio As New PromptIntegerOptions(vbLf & "Enter group index")
pio.AllowNegative = False
pio.AllowZero = False
pio.DefaultValue = 1
pio.LowerLimit = 1
pio.UpperLimit = i
Dim pir As Object = ed.GetInteger(pio)
If pir.Status = PromptStatus.OK Then
' Get the selected group
Dim grp As Object = TryCast(tr.GetObject(CType(gd(groupNames(pir.Value - 1)), ObjectId), OpenMode.ForRead), Group)
If grp IsNot Nothing Then
' Call our extension method to get the extents of the group's
' referenced objects
Dim ext As Object = tr.GetExtents(grp.GetAllEntityIds())
' Print the information for the user
ed.WriteMessage(vbLf & "Group's extents are from {0} to {1}.", ext.MinPoint, ext.MaxPoint)
End If
End If
End If
' Commit the transaction
tr.Commit()
End Using
End Sub
End Class
End Namespace
只能在模块中定义扩展方法
欢迎关注微信公众账号ByCAD