【发布时间】:2016-05-24 21:43:32
【问题描述】:
我正在开发一个在 AutoCAD 中使用的 VBA 宏。目前它将 圆 转换为 3D 折线,并且它本身运行良好。这只是一个开始,我将能够在最后的例程中添加一些内容。
这是 VBA 宏:
Sub CircleToPolyline()
Dim objSel As AcadEntity
Dim myCircle As AcadCircle
Dim pickedPoint As Variant
' Get the user to select a circle
' Eventually we will use a Selection Set with Filtering to pick them all in the drawing
Call ThisDrawing.Utility.GetEntity(objSel, pickedPoint, "Select Circle:")
If objSel.ObjectName <> "AcDbCircle" Then GoTo SKIP
Set myCircle = objSel
Dim dAngle As Double, dAngleStep As Double, dMaxAngle As Double
dAngle = 0# ' We always start at 0 degrees / radians
dAngleStep = 0.17453293 ' This is 10 degrees in radians
dMaxAngle = 6.28318531 ' This is 360 degrees in radians
' So our polyline will always have 36 vertices
Dim ptCoord() As Double
Dim ptProject As Variant
Dim i As Integer
i = 0
While dAngle < dMaxAngle
ReDim Preserve ptCoord(0 To i + 2) ' Increase size of array to hold next vertex
' Calculate the next coordinate on the edge of the circle
ptProject = ThisDrawing.Utility.PolarPoint(myCircle.center, dAngle, myCircle.Radius)
' Add to the coordinate list
ptCoord(i) = ptProject(0)
ptCoord(i + 1) = ptProject(1)
ptCoord(i + 2) = ptProject(2)
' Increment for next coordinate/angle on the circle edge
dAngle = dAngle + dAngleStep
i = i + 3
Wend
' Create the 3D polyline
Dim oPolyline As Acad3DPolyline
Set oPolyline = ThisDrawing.ModelSpace.Add3DPoly(ptCoord)
oPolyline.Closed = True
oPolyline.Update
SKIP:
End Sub
我只是想知道是否有任何替代方法来管理我的动态数组 (ptCoord)?例如,有什么方法可以将ptProject 添加到动态列表中,然后在 Add3dPoly 例程中使用该列表?
问题是,PolarPoint 返回一个变体。 ptCoord 是一个 doubles 数组(这是 Add3dPoly 所期望的)。这就是我这样做的原因。我没有使用变体(除了处理返回值)。
我的代码非常简单且足够,但如果可以进一步简化,我会很想知道(考虑到 VBA 和 AutoCAD 环境的上下文)。
我希望我的问题很清楚。谢谢。
【问题讨论】:
标签: vba autocad autocad-plugin