本帖最后由 willj 于 2023-9-12 10:20 编辑
- Sub SetAttributeValue()
- Dim objAcadApp As Object
- Dim objAcadDoc As Object
- Dim objBlockRef As Object
- Dim objBlockRefs As Object
- Dim strBlockName As String
- Dim strAttributeName As String
- Dim strAttributeValue As String
- Dim blnBlockRefFound As Boolean
- '连接到AutoCAD应用程序
- On Error Resume Next
- Set objAcadApp = GetObject(, "AutoCAD.Application")
- If objAcadApp Is Nothing Then
- Set objAcadApp = CreateObject("AutoCAD.Application")
- End If
- On Error GoTo 0
- '检查是否连接到AutoCAD
- If objAcadApp Is Nothing Then
- MsgBox "无法连接到AutoCAD"
- Exit Sub
- End If
- '获取当前打开的图档
- Set objAcadDoc = objAcadApp.ActiveDocument
- '检查是否打开了图档
- If objAcadDoc Is Nothing Then
- MsgBox "未打开图档"
- Exit Sub
- End If
- '获取指定块参照对象的块名称、属性名称和属性值
- strBlockName = "测试模块"
- strAttributeName = "宽度"
- strAttributeValue = "1400"
- '遍历所有块参照对象并查找指定块参照对象
- For Each objBlockRef In objBlockRefs
- If objBlockRef.Name = strBlockName Then
- blnBlockRefFound = True
- '获取块参照对象的所有属性
- Dim vAtt As Variant
- vAtt = objBlockRef.GetAttributes
- If IsArray(vAtt) Then
- '遍历属性集合并查找标签(TagString)等于指定块名称(BlName)的属性
- Dim i As Integer
- For i = LBound(vAtt) To UBound(vAtt)
- If vAtt(i).TagString = strAttributeName Then
- '设置属性值(TextString)为指定的值(ValueStr)
- vAtt(i).TextString = strAttributeValue
- Exit For
- End If
- Next i
- End If
- Exit For
- End If
- Next objBlockRef
- '提示块参照对象是否存在
- If blnBlockRefFound Then
- MsgBox "块参照对象存在"
- Else
- MsgBox "块参照对象不存在"
- End If
- End Sub
思路感觉是这样了。就是还有个报错不知道什么情况
|