明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4129|回复: 11

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

  [复制链接]
发表于 2010-11-16 21:47:00 | 显示全部楼层 |阅读模式

 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 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
                        db.UnloadXrefs(acObjIdColl)
                        trans.Commit()
                    End If
                  
  End If
            Next
        End Using

 楼主| 发表于 2010-11-16 21:53:00 | 显示全部楼层

vb.net 外部参照 unload 导致cad崩溃

运行到db.UnloadXrefs(acObjIdColl) 就崩溃了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2010-11-16 21:58:00 | 显示全部楼层

其他的没仔细看,但这里应该在For循环外吧

                        db.UnloadXrefs(acObjIdColl)
                        trans.Commit()

 楼主| 发表于 2010-11-16 22:53:00 | 显示全部楼层

vb.net 外部参照 unload 导致cad崩溃

我是想做个右键的快捷菜单 ,一般的选择后执行 我试了试没有问题 ,用在右键的快捷菜单就崩溃了


  1. <CommandMethod("AddBlockRefMenu")> _
  2. Public Sub AddBlockRefMenu()
  3. '定义一个ContextMenuExtension对象,用于表示快捷菜单
  4. Dim contextMenu As New ContextMenuExtension()
  5. '对于对象级别的快捷菜单,不必设置菜单名
  6. Dim miBlockUnload As New MenuItem("卸载")
  7. AddHandler miBlockUnload.Click, AddressOf miBlockUnload_Click
  8. contextMenu.MenuItems.Add(miBlockUnload)
  9. Application.AddObjectContextMenuExtension(RXClass.GetClass(GetType(BlockReference)), contextMenu)
  10. End Sub
  11. Sub miBlockUnload_Click(ByVal sender As Object, ByVal e As EventArgs)
  12. Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  13. Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  14. Dim db As Database = doc.Database
  15. ' Dim bt as
  16. '获取当前的选择集对象
  17. Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
  18. Dim ss As SelectionSet = ed.SelectImplied().Value
  19. Using trans As Transaction = db.TransactionManager.StartTransaction()
  20. '循环遍历选择集中的对象
  21. For Each id As ObjectId In ss.GetObjectIds()
  22. Dim obj As DBObject = trans.GetObject(id, OpenMode.ForRead)
  23. '如果所选择的对象是
  24. If TypeOf (obj) Is BlockReference Then
  25. '获取所选择的对象
  26. Dim br As BlockReference = trans.GetObject(id, OpenMode.ForRead) 'CType(obj, BlockReference)
  27. Dim btr As BlockTableRecord = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead)
  28. If (btr.IsFromExternalReference) Then
  29. Dim xrefId As ObjectId = br.BlockTableRecord
  30. If (Not xrefId = ObjectId.Null) Then
  31. acObjIdColl.Add(xrefId)
  32. 'db.DetachXref(xrefId)
  33. 'db.UnloadXrefs()
  34. ' ed.WriteMessage(vbCrLf & "Xref:" & btr.Name & " Unload!")
  35. End If
  36. End If
  37. End If
  38. Next
  39. db.UnloadXrefs(acObjIdColl)
  40. trans.Commit()
  41. End Using
  42. End Sub
  43. End Class
 楼主| 发表于 2010-11-16 22:57:00 | 显示全部楼层

vb.net 外部参照 unload 导致cad崩溃

顺便再问问
  1. Dim acTypValAr(0) As TypedValue
  2. acTypValAr.SetValue(New TypedValue(DxfCode.Start, "BlockReference"), 0)
好像不能过滤到块参照,过滤块参照应该在.net怎么用呢
发表于 2010-11-16 23:00:00 | 显示全部楼层

"Insert"

 

右键菜单的话,文档应该加锁的吧

 楼主| 发表于 2010-11-17 08:19:00 | 显示全部楼层
谢谢 飞狐版主 能详细说下文档应该怎么加锁吗
发表于 2010-11-17 21:08:00 | 显示全部楼层

Using doc.LockDocument()

 

 

End Using

 楼主| 发表于 2010-11-17 21:59:00 | 显示全部楼层

vb.net 外部参照 unload 导致cad崩溃

谢谢 飞狐版主  可以做出来 哈哈

 楼主| 发表于 2010-11-17 22:01:00 | 显示全部楼层

vb.net 外部参照 unload 导致cad崩溃

完整的代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 11:50 , Processed in 0.167959 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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