本人刚学vba不久,编了一个随机画正多边形的命令,运行出错,求大神指点
Sub polygon()Dim myselect(1 To 300) As AcadEntity '定义选择集数组
Dim num As Integer
Dim pnt As Variant
Dim lpnt As Variant
num = 5 '正多边形边数
Dim fpnt(0 To 1) As Variant '起点坐标
For i = 1 To 300 '循环300次
fpnt(0) = 3000 * Rnd: fpnt(1) = 3000 * Rnd '起点随机坐标
Dim leng As Double
leng = Rnd * 10 + 1 '边长随机长度
ReDim lpnt(0 To num * 2 - 1) As Double '下面是画正多边形
pnt = fpnt
lpnt(0) = pnt(0)
lpnt(1) = pnt(1)
Dim st As Integer
For st = 1 To num - 1
pnt = ThisDrawing.Utility.PolarPoint(pnt, (3.14159265 * 2 / num) * (st - 1), leng)
lpnt(st * 2) = pnt(0)
lpnt(st * 2 + 1) = pnt(1)
Next st
Set myselect(i) = ThisDrawing.ModelSpace.AddLightWeightPolyline(lpnt)
Next i
ThisDrawing.Regen (True)
End Sub
系统提示pnt = ThisDrawing.Utility.PolarPoint(pnt, (3.14159265 * 2 / num) * (st - 1), leng)出错,求大神指点怎么修改 pnt应该是三维点坐标,你给的是二维的 zzyong00 发表于 2017-11-20 17:37
pnt应该是三维点坐标,你给的是二维的
好的,我现在修改下看行不行 zzyong00 发表于 2017-11-20 17:37
pnt应该是三维点坐标,你给的是二维的
我试过了,还是不行,应该不是pnt几维坐标的问题,我把这个命令流改成只画一个固定起点坐标和固定边长的多边形就可以运行了,改成随机起点和随机边长就行不通 q123135abc 发表于 2017-11-20 18:29
我试过了,还是不行,应该不是pnt几维坐标的问题,我把这个命令流改成只画一个固定起点坐标和固定边长的 ...
不相信?
Sub polygon()
Dim myselect(1 To 300) As AcadEntity '定义选择集数组
Dim num As Integer
Dim pnt As Variant
Dim lpnt As Variant
num = 5 '正多边形边数
Dim fpnt(0 To 2) As Double '起点坐标
Dim leng As Double
Dim i As Long
For i = 1 To 300 '循环300次
fpnt(0) = 3000 * Rnd: fpnt(1) = 3000 * Rnd: fpnt(2) = 0 '起点随机坐标
leng = Rnd * 10 + 1 '边长随机长度
ReDim lpnt(0 To num * 2 - 1) As Double '下面是画正多边形
pnt = fpnt
lpnt(0) = pnt(0)
lpnt(1) = pnt(1)
Dim st As Integer
For st = 1 To num - 1
pnt = ThisDrawing.Utility.PolarPoint(pnt, (3.14159265 * 2 / num) * (st - 1), leng)
lpnt(st * 2) = pnt(0)
lpnt(st * 2 + 1) = pnt(1)
Next st
Set myselect(i) = ThisDrawing.ModelSpace.AddLightWeightPolyline(lpnt)
Next i
ThisDrawing.Regen (True)
End Sub
zzyong00 发表于 2017-11-20 21:22
不相信?
大神你说的是对的,我应该是把数据类型弄错了,所以导致运行错误,非常感谢你的帮忙,以后还得多多向你请教! 还缺少一行闭合多段线的命令吧?不然会是有缺口的多边形。
myselect(i).Closed = True
页:
[1]