- <CommandMethod("AddBlockRefMenu")> _
- Public Sub AddBlockRefMenu()
- '定义一个ContextMenuExtension对象,用于表示快捷菜单
- Dim contextMenu As New ContextMenuExtension()
- '对于对象级别的快捷菜单,不必设置菜单名
- 'contextMenu.Title = "圆的快捷菜单"
- '添加一个名为"圆面积"的菜单项,用于在AutoCAD命令行上显示所选择的圆面积
- Dim miBlockUnload As New MenuItem("卸载")
- '为"圆面积"菜单项添加单击事件
- AddHandler miBlockUnload.Click, AddressOf miBlockUnload_Click
- '将"圆面积"菜单项添加到快捷菜单中
- contextMenu.MenuItems.Add(miBlockUnload)
- '为圆对象添加定义的快捷菜单
- Application.AddObjectContextMenuExtension(RXClass.GetClass(GetType(BlockReference)), contextMenu)
- End Sub
- Sub miBlockUnload_Click(ByVal sender As Object, ByVal e As EventArgs)
- Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
- Dim doc As Document = Application.DocumentManager.MdiActiveDocument
- Dim db As Database = doc.Database
- ' Dim bt as
- '获取当前的选择集对象
- Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
- Dim ss As SelectionSet = ed.SelectImplied().Value
- Using acLckDoc As DocumentLock = doc.LockDocument()
- Using trans As Transaction = db.TransactionManager.StartTransaction()
- '循环遍历选择集中的对象
- For Each id As ObjectId In ss.GetObjectIds()
- Dim obj As DBObject = trans.GetObject(id, OpenMode.ForRead)
- '如果所选择的对象是
- If TypeOf (obj) Is BlockReference Then
- '获取所选择的对象
- Dim br As BlockReference = trans.GetObject(id, OpenMode.ForRead) 'CType(obj, BlockReference)
- Dim btr As BlockTableRecord = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead)
- If (btr.IsFromExternalReference) Then
- Dim xrefId As ObjectId = br.BlockTableRecord
- If (Not xrefId = ObjectId.Null) Then
- acObjIdColl.Add(xrefId)
- 'db.DetachXref(xrefId)
- 'db.UnloadXrefs()
- ' ed.WriteMessage(vbCrLf & "Xref:" & btr.Name & " Unload!")
- End If
- End If
- '在命令行上显示圆面积信息
- 'ed.WriteMessage(vbCrLf & "圆面积为:" & circ.Area.ToString())
- End If
- Next
- db.UnloadXrefs(acObjIdColl)
- trans.Commit()
- End Using
- End Using
- End Sub
|