本帖最后由 史巴燕 于 2020-2-24 23:42 编辑
附件说明:
1、项目源文件在附件1中,引用的AutoCAD是2014版的(2014版的相关dll文件在“\Question\Question\bin\Debug”路径中),编程语言是vb.net,源文件专门针对这个问题精简了代码,很小的小程序。2、包含动态块的dwg文件在附件2中。
问题描述:我在CAD里面定义并插入了一个块参照(动态块),它除了包含两个常规的块属性外还有一个动态块的专有属性“可见性1”,但是在代码中DynamicBlockReferencePropertyCollection.Count的值输出为0(即动态块专有属性的个数为0),且它的isDynamicBlock属性是False(即程序认为该块参照不是动态块),想问下问题出在哪?
代码如下(也可以下载附件中的源文件和dwg文件调试):
- Imports Autodesk.AutoCAD.ApplicationServices
- Imports Autodesk.AutoCAD.DatabaseServices
- Imports Autodesk.AutoCAD.EditorInput
- Imports Autodesk.AutoCAD.Runtime
- Public NotInheritable Class MyAutoCADTools2014
- '该项目引用的是AutoCAD2014版的.net库
- ''' <summary>
- ''' 输出选择的块实体(包括动态块)的所有属性名称
- ''' </summary>
- <CommandMethod("SBBC")>
- 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
- ''' <summary>
- ''' 根据ObjectID返回实体,做这个函数是因为不知道为什么使用objectId.getObject()方法要出错
- ''' </summary>
- ''' <param name="objId">对象的ObjectId</param>
- ''' <returns></returns>
- 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
动态块的专有属性截图:
|