michael_h 发表于 2008-7-9 14:13:11

将点作为数组传递给函数......这应该不难......

大家好,
我正在开发一个VBA程序,它将为用户提供图层列表,允许用户选择一个图层,自动选择该图层上的第一个(也是唯一一个)对象,然后使用acselectionsetWindowPolygon方法选择该对象中的所有项目。
现在,我可以让Autocad选择我想要的对象,但我很难尝试让它将点传递给选择函数。
我使用以下代码获取对象的点:
Dim varPoints As Variant
varPoints = myRegionObj.Coordinates

然后,据我所知,我需要将这组2维点转换为3D点。我写了一个函数来做到这一点:
Function ConvertPoints(ByRef PointsArray() As Double) As Double
'function creates a new array to hold three dimensional points and converts 2D points into 3D with z=0
Dim PreviousBound As Integer, NewBound As Integer
PreviousBound = (UBound(PointsArray) + 1) / 2
NewBound = PreviousBound * 3
ReDim ConvertPoints(NewBound) As Double
Dim i As Integer
For i = 0 To UBound(PointsArray)
    If i Mod 3 = 0 Then ConvertPoints(i) = 0
    Else: ConvertPoints(i) = PointsArray(i)
Next i
End Function

然后,当然,我想做实际的选择,使用这样的东西:
dim mode as integer
mode = acselectionsetWindowPolygon
mySelectionSet.SelectByPolygon mode, ConvertPoints(varPoints())

但是,我似乎不知道如何将数组传递给函数。有人能告诉我,我是否正在以正确的方式在任何地方做这件事吗?
非常感谢您的帮助。
**** Hidden Message *****

Keith™ 发表于 2008-7-9 14:30:38

您可能需要做的是将ConvertPoints的返回值更改为变量
另一个选项是在函数中创建全局变量,而不是从函数返回值,而是在函数中设置值。这有意义吗?

michael_h 发表于 2008-7-9 14:40:19

基思,
感谢您的快速回复
Function ConvertPoints(ByRef PointsArray() As Double) As Variant

然而,当我尝试使用此命令进行选择时:
mySelectionSet.SelectByPolygon mode, ConvertPoints(varPoints())

我收到消息:“预期的类型不匹配、数组或用户定义类型”
如果我尝试以下操作(例如,使用不带括号的数组名称),我会收到相同的消息
ConvertPoints(varPoints)

我觉得我的语法有问题。无论如何,我将尝试创建一个全局函数,看看它是否有效
要做到这一点,我在代码的常规部分将Public NewArray()设为double,然后在函数中对其进行重新定义,对吗

michael_h 发表于 2008-7-9 14:45:56

唉,我刚刚试着做了一个公共变量:
编译器告诉我“...数组...不允许作为对象模块的公共成员”

Keith™ 发表于 2008-7-9 14:53:39

在模块(非表单)中
Dim MyArray() As Double
Public Function MyFunction(ByVal PointsArray As Variant)
'do stuff here to fill in MyArray
End Function

Atook 发表于 2008-7-9 15:57:39

Keith是对的,当传递数组时,你需要将它们作为变体调暗。

Jeff_M 发表于 2008-7-9 20:04:41

迈克尔,你的函数有缺陷,因为它没有创建适当大小的新数组,也没有正确填充它。
试试这个:
Function ConvertPoints(ByRef PointsArray() As Double) As Variant
'function creates a new array to hold three dimensional points and converts 2D points into 3D with z=0
Dim PreviousBound As Integer, NewBound As Integer, NewPoints() As Variant
PreviousBound = (UBound(PointsArray) + 1) / 2
NewBound = (PreviousBound * 3) - 1
ReDim NewPoints(NewBound)
Dim i As Integer
Dim j As Integer
For i = 0 To UBound(PointsArray) Step 2
    NewPoints(j) = PointsArray(i)
    NewPoints(j + 1) = PointsArray(i + 1)
    NewPoints(j + 2) = 0#
    j = j + 3
Next i
ConvertPoints = NewPoints
End Function

Rogue 发表于 2008-7-10 06:07:31

对我来说,太像工作了。我看不出有什么转换的必要;我也不知道为什么你的程序不起作用——你是在选择一条线作为对象吗?因为一条直线或只有一个子实体的多段线只有2个点(坐标),这不足以描述你想要选择的多边形。
此外,您意识到在许多情况下,您希望用来选择的多边形也不会选择,比如说,带有您用来描述多边形的坐标的折线?因为多边形会在选定的多边形窗口上,而不是在其中。
尝试下面的代码。它是从帮助文件中复制/粘贴而来的;它创建一个三角形多段线,并在其中创建一个圆,然后从多段线坐标创建选定边界。请注意,选择的是圆,而不是多段线....'-snip-snip-snip-snip-
Sub Main()
'创建折线。然后使用Coordinates
'属性返回折线中的所有坐标。

Dim plineObj As acad Polyline
'创建类似三角形的多段线
Dim points(作为Double
points(0)=-10:points(1)= 0:points(2)= 0
points(3)= 10:points(4)= 0:points(5)= 0
points(6)= 0:points(7)= 7:points(= 0
Set plineObj = this drawing。' model space . add polyline(points)
'返回折线的所有坐标。坐标

'在三角形的中间画一个圆
将MyCircle标注为AcadCircle
将中心点(2)标注为Double
中心点(0) = 0:中心点(1) = 3:中心点(2) = 0
设置MyCircle = ThisDrawing。model space . add circle(center point,1)
此绘图。' Regen acActiveViewport

'创建选择集
Dim mode As Integer
mode = acSelectionSetWindowPolygon
Dim my selectionset As AcadSelectionSet
Set my selectionset = this drawing。selection sets . Add(" TEST _ SSET ")

'使用折线中的点来选择实体
mySelectionSet。SelectByPolygon模式,retCoord
If mySelectionSet。Count > -1,然后
Dim acEnt As acad object
For I = 0到mySelectionSet。count-1
Set acEnt = my selection Set。项目(i)
MsgBox "Item " & i & " " & acEnt。object name
Next I
Else
MsgBox "选择集中无"
End If
mySelectionSet。delete Set my selection Set = Nothing结束Sub

Jeff_M 发表于 2008-7-10 10:09:54

Rogue,
如果为多边形获得的对象是LightWeghtPolyLine,则坐标属性返回一个必须转换为3D点列表的2D点列表。根据Michael的第一篇文章,他知道图层上的第一个实体是什么以及坐标返回的是什么,这是一个2D点列表。
页: [1]
查看完整版本: 将点作为数组传递给函数......这应该不难......