请高手指点一下本人编的小程序
帮忙看一下我这个程序,刚一运行,就显示“当前范围内的声明重复”,这是在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
<font color="#ff0000"> Dim ptMin As Variant, ptMax As Variant<br/></font> Dim objText As AcadText<br/> Dim objMtext As AcadMText<br/> <br/> Dim i, j As Integer<br/> Dim quantity As Integer 'quantity为Mtext的行数<br/> Dim TextIndex() As Integer 'TextIndex()记录每行所在的位置<br/> Dim tmpStr As String<br/> <br/> <font color="#ff0000">Dim ptMin As Variant, ptMax As Variant</font> '获得多行文字的位置<br/> 这点问题是解决了,谢谢,虽然还是有问题,自己再找找 <p><font face="Verdana">你的创建选择集可能有问题,您可以看考下面的程序!<br/></font></p>
<p><font face="Verdana">''''''安全创建选择集<br/>On Error Resume Next<br/>Dim SSet As AcadSelectionSet<br/>If Not IsNull(docObj.SelectionSets.Item("Example")) Then<br/>Set SSet = docObj.SelectionSets.Item("Example")<br/>SSet.Delete '及时删除不用的选择集非常重要<br/>End If<br/>Set SSet = docObj.SelectionSets.Add("Example")<br/>''''''向选择集中添加实体<br/>SSet.Select acSelectionSetCrossing, ptMin, ptMax<br/>''''''将选择集中的实体添加到数组中<br/>Dim objCollection() As Object<br/>ReDim objCollection(SSet.Count - 1)<br/>Dim i As Integer<br/>For i = 0 To SSet.Count - 1<br/> Set objCollection(i) = SSet.Item(i)<br/>Next i</font></p> 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 '获得多行文字的位置
这个地方重复定义了嘛,
Dim ptMin As Variant, ptMax As Variant
Dim ptMin As Variant, ptMax As Variant '获得多行文字的位置
提醒一点,Mtext的textstring函数返回值包括格式符号你这样有些Mtext无法转成Text
页:
[1]