帮忙看一下我这个程序,刚一运行,就显示“当前范围内的声明重复”,这是在
AutoCAD中加载VBA的
谢谢了!-
- Option Explicit
- Public Sub MtextToText()
- On Error Resume Next
-
- Dim ptInsert As Variant
- Dim txtStr As String
- Dim height As Double
- Dim width As Double
-
- '选择多行文字*********************************************
- '安全创建选择集
- Dim SSet As AcadSelectionSet
- If Not IsNull(ThisDrawing.SelectionSets.Item("this")) Then
- Set SSet = ThisDrawing.SelectionSets.Item("this")
- SSet.Delete
- End If
- Set SSet = ThisDrawing.SelectionSets.Add("this")
-
- '定义过滤规则
- Dim filterType(0) As Integer
- Dim filterData(0) As Variant
- filterType(0) = 0
- filterData(0) = "Mtext"
-
- SSet.SelectOnScreen filterType, filterData
-
- '创建单行文字***************************************************************
- Dim ptMin As Variant, ptMax As Variant
- Dim objText As AcadText
- Dim objMtext As AcadMText
-
- Dim i, j As Integer
- Dim quantity As Integer 'quantity为Mtext的行数
- Dim TextIndex() As Integer 'TextIndex()记录每行所在的位置
- Dim tmpStr As String
-
- Dim ptMin As Variant, ptMax As Variant '获得多行文字的位置
-
- For Each objMtext In SSet
- '获得文字的主要参数
- objMtext.GetBoundingBox ptMin, ptMax
- txtStr = objMtext.TextString
- height = objMtext.height
- '找出Mtext共有几行
- quantity = 1
- For i = 1 To Len(Mtext)
- If Mid(Mtext, i, 1) = "\p" Then quantity = quantity + 1
- Next i
-
- '找出每行行首在Mtext的位置
- ReDim TextIndex(quantity)
- TextIndex(0) = 0
- For j = 1 To quantity
- For i = 1 To Len(Mtext)
- If Mid(Mtext, i, 1) = "\p" Then TextIndex(j) = i
- Next i
- Next j
- TextIndex(j) = i
-
- '将Mtext转换为多行Text文字
- For j = 0 To quantity - 1
- tmpStr = Mid(txtStr, TextIndex(j) + 1, TextIndex(i + 1) - TextIndex(i) - 1)
- ptInsert(0) = ptMin(0)
- ptInsert(1) = ptMin(1) + (i + 1) * (ptMax(1) - ptMin(1)) / quantity
- ptInsert(2) = ptMin(2)
-
- Set objText = ThisDrawing.ModelSpace.AddText(tmpStr, ptInsert, height)
-
- '调整单行文字的对齐方式
- objText.InsertionPoint = ptInsert
- objMtext.Delete '删除文字
- Next
-
- SSet.Delete
- End Sub
|