从AutoCAD中提取明细栏和标题栏中的数据
在AutoCAD中的明细栏和标题栏通常是采用定义块然后添加相应的属性值。那么如何在AutoCAD中提取到这些属性值呢?下面是实现的方法(通过AutoCAD VBA实现的)。
-
获取当前Dwg文档中已定义的块
-block 命令查看用户定义的块。
VBA 代码实现。
Dim objBlock As AcadBlock
For i = 0 To ThisDrawing.Blocks.Count - 1
Set objBlock = ThisDrawing.Blocks.Item(i)
If objBlock.IsXRef = False And objBlock.IsLayout = False Then’用户定义的块
MsgBox objBlock.Name + " " + objBlock.ObjectName
End If
Next
-
获取块对应的属性值。
在AutoCAD中,块属性值一般都是放在AcDbBlockReference类的对象中,该对象的Name表示他属于的块名称,GetAttributes方法返回对象的属性,其中包含值。因此,可以在整个模型空间中查找AcDbBlockReference类型的对象,然后根据名称来分类。VBA实现代码如下:
Dim tempCount As Integer
tempCount = ThisDrawing.ModelSpace.Count’得到模型空间中的所有对象个数
For i = 0 To tempCount - 1
Set curAcadObject = ThisDrawing.ModelSpace.Item(i)
If curAcadObject.ObjectName = "AcDbBlockReference" Then
Dim ss As AcadBlockReference
Set ss = curAcadObject
If ss.HasAttributes = True Then
Dim aa() As Object ‘存放属性的数组
aa = ss.GetAttributes()
Dim cc As Integer
Dim strr As String
Dim oo As AcadAttributeReference‘属性对象
strr = ""
For cc = LBound(aa) To UBound(aa)
Set oo = aa(cc)
strr = strr + oo.TagString + ":" + oo.TextString + vbCrLf
Next
MsgBox strr, vbDefaultButton1, curAcadObject.Name
End If
End If
Next