明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: glowz

[图元] vb.net 外部参照 unload 导致cad崩溃

  [复制链接]
 楼主| 发表于 2010-11-17 22:01:00 | 显示全部楼层
  1. <CommandMethod("AddBlockRefMenu")> _
  2. Public Sub AddBlockRefMenu()
  3. '定义一个ContextMenuExtension对象,用于表示快捷菜单
  4. Dim contextMenu As New ContextMenuExtension()
  5. '对于对象级别的快捷菜单,不必设置菜单名
  6. 'contextMenu.Title = "圆的快捷菜单"
  7. '添加一个名为"圆面积"的菜单项,用于在AutoCAD命令行上显示所选择的圆面积
  8. Dim miBlockUnload As New MenuItem("卸载")
  9. '为"圆面积"菜单项添加单击事件
  10. AddHandler miBlockUnload.Click, AddressOf miBlockUnload_Click
  11. '将"圆面积"菜单项添加到快捷菜单中
  12. contextMenu.MenuItems.Add(miBlockUnload)
  13. '为圆对象添加定义的快捷菜单
  14. Application.AddObjectContextMenuExtension(RXClass.GetClass(GetType(BlockReference)), contextMenu)
  15. End Sub
  16. Sub miBlockUnload_Click(ByVal sender As Object, ByVal e As EventArgs)
  17. Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  18. Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  19. Dim db As Database = doc.Database
  20. ' Dim bt as
  21. '获取当前的选择集对象
  22. Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
  23. Dim ss As SelectionSet = ed.SelectImplied().Value
  24. Using acLckDoc As DocumentLock = doc.LockDocument()
  25. Using trans As Transaction = db.TransactionManager.StartTransaction()
  26. '循环遍历选择集中的对象
  27. For Each id As ObjectId In ss.GetObjectIds()
  28. Dim obj As DBObject = trans.GetObject(id, OpenMode.ForRead)
  29. '如果所选择的对象是
  30. If TypeOf (obj) Is BlockReference Then
  31. '获取所选择的对象
  32. Dim br As BlockReference = trans.GetObject(id, OpenMode.ForRead) 'CType(obj, BlockReference)
  33. Dim btr As BlockTableRecord = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead)
  34. If (btr.IsFromExternalReference) Then
  35. Dim xrefId As ObjectId = br.BlockTableRecord
  36. If (Not xrefId = ObjectId.Null) Then
  37. acObjIdColl.Add(xrefId)
  38. 'db.DetachXref(xrefId)
  39. 'db.UnloadXrefs()
  40. ' ed.WriteMessage(vbCrLf & "Xref:" & btr.Name & " Unload!")
  41. End If
  42. End If
  43. '在命令行上显示圆面积信息
  44. 'ed.WriteMessage(vbCrLf & "圆面积为:" & circ.Area.ToString())
  45. End If
  46. Next
  47. db.UnloadXrefs(acObjIdColl)
  48. trans.Commit()
  49. End Using
  50. End Using
  51. End Sub
发表于 2015-5-29 14:58:04 | 显示全部楼层
做个标记,以后可能有用!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 11:48 , Processed in 0.147425 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表