有朋友提到的,如何在单行文字、多行文字、属性定义、属性引用(或参照)替换文字。
- Public Sub TextReplace()
- '创建选择集,名称为TextReplace
-
- Dim SSetObj As AcadSelectionSet
- On Error Resume Next
- Set SSetObj = ThisDrawing.SelectionSets("TextReplace")
- If Err Then
- Err.Clear
- Set SSetObj = ThisDrawing.SelectionSets.Add("TextReplace")
- End If
- SSetObj.Clear
- On Error GoTo 0
-
- '创建过滤器,仅选择Text(单行文本)、Mtext(多行文本)、AttDef(属性定义)
-
- '关于过滤器的详细使用资料,请参考有关书籍
-
- Dim fType(0 To 0) As Integer
- Dim fData(0 To 0) As Variant
- fType(0) = 0
- fData(0) = "Text,Mtext,AttDef"
-
- '选择全部的Text、Mtext、AttDef
-
- SSetObj.Select acSelectionSetAll, , , fType, fData
-
- Dim i As Integer
- If SSetObj.Count <> 0 Then
- For i = 0 To SSetObj.Count - 1
- '在Text和Mtext中显示的是TextString,在AttDef中显示的是TagString
- If TypeOf SSetObj(i) Is AcadAttribute Then
- If SSetObj(i).TagString = "ABC" Then SSetObj(i).TagString = "XYZ"
- Else
- If SSetObj(i).TextString = "ABC" Then SSetObj(i).TextString = "XYZ"
- End If
- Next
- End If
-
- '清空选择集中的数据,准备下一次操作
-
- SSetObj.Clear
-
- '创建过滤器,仅选择BlockRef(块引用)
-
- fType(0) = 0
- fData(0) = "Insert"
-
- '选择全部的BlockRef
-
- SSetObj.Select acSelectionSetAll, , , fType, fData
-
- Dim v As Variant
- Dim j As Integer
- If SSetObj.Count <> 0 Then
- For i = 0 To SSetObj.Count - 1
- If SSetObj(i).HasAttributes Then
- v = SSetObj(i).GetAttributes
- For j = 0 To UBound(v)
- '在BlockRef中的AttRef中显示的是TextString
- If v(j).TextString = "ABC" Then v(j).TextString = "XYZ"
- Next
- End If
- Next
- End If
-
- SSetObj.Clear
-
- SSetObj.Delete
-
- Set SSetObj = Nothing
- End Sub
|