如何检查一个图元是否包含在当前工作集

用.net开发Autocad时,自己写的命令在Refedit中使用时,

工作集外被选中的物体也会被修改,

这时就先需要判断一个物体是否包含在当前工作集,

然后决定要不要对其进行修改。

原文链接:Résolu : How to check if entity is part of "Working Set"? - Autodesk Community - AutoCAD

原文中使用扩展方法,在使用时出来些问题,改为类后就没有问题了,

首先需要定义两个类:

Public Class MyDocumentExtensions
    Public Shared Function WorkSetHas(ByVal doc As acapps.Document, ByVal entityId As ObjectId, ByVal Optional includingErased As Boolean = True) As Boolean
        Dim id As ObjectId = acapps.Application.LongTransactionManager.CurrentLongTransactionFor(doc)

        If Not id.IsNull Then

            Using trans = New ReadOnlyTransaction()
                Dim ltr As LongTransaction = CType(trans.GetObject(id, OpenMode.ForRead), LongTransaction)
                Return ltr.WorkSetHas(entityId, includingErased)
            End Using
        End If

        Return False
    End Function
End Class

Public Class ReadOnlyTransaction
    Inherits OpenCloseTransaction

    Protected Overrides Sub DeleteUnmanagedObject()
        Commit()
        MyBase.DeleteUnmanagedObject()
    End Sub

    Public Overrides Sub Abort()
        Commit()
    End Sub
End Class

在使用时先通过CAD系统变量确定是否正在Refedit命令中,

如果是,再检查指定物体是否在当前工作集

 Dim RefeditName As String = AcApps.Application.GetSystemVariable("REFEDITNAME")
 If RefeditName <> "" Then
    If MyDocumentExtensions.WorkSetHas(doc, id, False) Then                                            
       ‘在这里对物体进行修改’

    End If
 End If

 

posted @ 2023-06-22 11:54  rf8862  阅读(17)  评论(0编辑  收藏  举报