tanzh 发表于 2012-6-4 08:48:16

选择集遍历问题,运行不了求解释。

Sub test()
Dim sset As AcadSelectionSet
Dim fType(0) As Integer
Dim fData(0) As Variant
Dim mj As String
Dim element As AcadEntity
Dim dd As Variant

On Error Resume Next
If Not IsNull(ThisDrawing.SelectionSets.Item("example")) Then
   Set sset = ThisDrawing.SelectionSets.Item("example")
   sset.Delete
End If
Set sset = ThisDrawing.SelectionSets.Add("example")

fType(0) = 8
fData(0) = "test"
sset.Select acSelectionSetAll, , , fType, fData

Dim ptArrsX() As Double                '建立一个动态数组
ReDim ptArrsX((UBound(element.Coordinates) + 1) / 2 - 1)

Dim j As Integer
For Each element In sset

For j = 0 To ((UBound(element.Coordinates) + 1) / 2 - 1)
ptArrsX(j) = element.Coordinates(2 * j)   '用ptArrsX储存每个坐标的X值
Next j
dd = ptArrsX()
mj = element.Area
MsgBox mj & "," & dd(0)
Next

End Sub

上面的代码运行不了,请教高手指出错误在哪!




zkyzzy 发表于 2012-6-6 07:21:07

你要调试看是什么问题啊

dqb908 发表于 2012-9-27 13:04:20

Dim ptArrsX() As Double                '建立一个动态数组
ReDim ptArrsX((UBound(element.Coordinates) + 1) / 2 - 1)这句出错
没有给 element 赋值

修改下 应该是这样
Sub test()
Dim sset As AcadSelectionSet
Dim fType(0) As Integer
Dim fData(0) As Variant
Dim mj As String
Dim element As AcadEntity
Dim dd As Variant

On Error Resume Next
If Not IsNull(ThisDrawing.SelectionSets.Item("example")) Then
   Set sset = ThisDrawing.SelectionSets.Item("example")
   sset.Delete
End If
Set sset = ThisDrawing.SelectionSets.Add("example")

fType(0) = 8
fData(0) = "test"
sset.Select acSelectionSetAll, , , fType, fData

dim SSNum as integer
ssnum = sset.number

Dim ptArrsX() As Double                '建立一个动态数组
ReDim ptArrsX(1 to ssnum,10)

Dim j As Integer,k As Integer
For Each element In sset
k =0
For j = 0 To ((UBound(element.Coordinates) + 1) / 2 - 1) step 2
        ptArrsX(j,k) = element.Coordinates(j)   '用ptArrsX储存每个坐标的X值
        k=k+1
Next j
dd = ptArrsX()
mj = element.Area
MsgBox mj & "," & dd(0)
Next

End Sub

说明一下 数组 ReDim ptArrsX((UBound(element.Coordinates) + 1) / 2 - 1)

的 第二维 定义成这样是错误的 你的本意应该是 根据获取的实体 多义线 多段线之内的

的坐标点个数 来确定数组个数   建议先定义个最大值 避免出错

上面的代码没有调试 你试下

markc0826 发表于 2012-10-3 23:19:04

我记得element.Coordinates好像不能直接利用(印象中)
上次我先将element.Coordinates丢给varCoord再利用他来作运算...


dim varCord as variant
varCord=element.Coordinates
.......

tanzh 发表于 2012-10-29 00:13:46

四楼正解,谢了。前段时间忙,好久没逛明经通道了,谢谢大家的解答哈。
页: [1]
查看完整版本: 选择集遍历问题,运行不了求解释。