【求助】vb.net二次开发时,在CAD中选择了动态块,但其IsDynamicBlock属性却是false?
我在CAD里面定义并插入了一个块参照(动态块),它除了包含两个常规的块属性外还有一个动态块的专有属性“可见性1”,但是运行后该动态块的DynamicBlockReferencePropertyCollection.Count的值输出为0(即动态块专有属性的个数为0),且它的isDynamicBlock属性是False(即程序认为该块参照不是动态块),想问下问题出在哪?这里是代码,引用的是2014版的,编程语言是vb.net。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Public NotInheritable Class MyAutoCADTools2014
'该项目引用的是AutoCAD2014版的.net库
'''
''' 输出选择的块实体(包括动态块)的所有属性名称
'''
Public Shared Sub SBBC() 'SetBlockByCoordinate
'---------------这一段代码是输出常规的块参照的属性-------------------------
Dim ed As Editor = Core.Application.DocumentManager.MdiActiveDocument.Editor
Dim peo As PromptEntityResult = ed.GetEntity("请选择一个块实体")
Dim objId As ObjectId = peo.ObjectId
Dim ent As Entity = getEntityByObjectId(peo.ObjectId)
Do Until ent.GetType.Name = "BlockReference" Or peo.Status = PromptStatus.Cancel Or peo.Status = PromptStatus.None
ed.WriteMessage("选择的不是块实体")
peo = ed.GetEntity("请选择一个块实体")
ent = getEntityByObjectId(peo.ObjectId) 'getEntityByObjectId是我自定义的函数,代码见后面的“其他”
Loop
Dim br As BlockReference = CType(ent, BlockReference)
Dim ar As AttributeReference
Dim arNames As String = ""
For i As Integer = 0 To br.AttributeCollection.Count - 1
ar = CType(getEntityByObjectId(br.AttributeCollection.Item(i)), AttributeReference)
If i = 0 Then arNames = ar.Tag Else arNames = arNames & vbCrLf & ar.Tag
Next
ed.WriteMessage(vbCrLf & vbCrLf & "------该块实体包含的属性------" & vbCrLf & arNames)
'---------------这一段代码是输出常规的块参照的属性-------------------------
'--------这一段代码用来调试检测动态块的相关属性,问题就在这里,跟动态块相关的代码都不起作用--------
ed.WriteMessage(vbCrLf & "------检测该块是否是动态块------" & vbCrLf)
ed.WriteMessage(br.IsDynamicBlock)
ed.WriteMessage(vbCrLf & "------输出动态块的属性的个数------" & vbCrLf)
ed.WriteMessage(br.DynamicBlockReferencePropertyCollection.Count)
ed.WriteMessage(vbCrLf & "------输出动态块的所有属性的名称------")
arNames = ""
For Each dbrp As DynamicBlockReferenceProperty In br.DynamicBlockReferencePropertyCollection
arNames = arNames & vbCrLf
Next
'--------这一段代码用来调试检测动态块的相关属性,问题就在这里,跟动态块相关的代码都不起作用--------
End Sub
#Region "其他"
Public Class Initialize
Public Sub New()
End Sub
End Class
'''
''' 根据ObjectID返回实体,做这个函数是因为不知道为什么使用objectId.getObject()方法要出错
'''
''' 对象的ObjectId
'''
Public Shared Function getEntityByObjectId(ByVal objId As ObjectId) As Entity
Dim ent As Entity
Using tr As Transaction = objId.Database.TransactionManager.StartTransaction
ent = CType(tr.GetObject(objId, OpenMode.ForRead), Entity)
tr.Commit()
End Using
Return ent
End Function
#End Region
End Class
我自己找到解决方法了,国外的一个网友也提过这个问题,答案就是不能把引用动态块专有属性的相关代码放在transaction代码段的外面,而我的代码中单独定义了一个getEntityByObjectID函数,在该函数返回entity对象后就关闭了transaction,所以再检测返回的entity对象是否为动态块时系统就会认为它不是动态块了。 可以把transaction 作为getEntityByObjectId函数的一个参数
页:
[1]