TheSwamp
Code Red => VB(A) => Topic started by: michael_h on July 09, 2008, 02:13:11 PM
-
Hi all,
I'm working on a VBA program that will provide the user with a list of layers, allow the user to choose a layer, automatically select the first (and only) object on that layer, and then select all items within that object using the acselectionsetWindowPolygon method.
Right now, I can get Autocad to select the object that I want, but I'm having a hard time trying to get it to pass the points to the selection function.
I'm getting the points of the object using the following code:
Dim varPoints As Variant
varPoints = myRegionObj.Coordinates
Then, as I understand it, I need to convert this 2 dimensional set of points into 3d points. I wrote a function to do that:
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
and then of course, I would want to do the actual selection, using something like this:
dim mode as integer
mode = acselectionsetWindowPolygon
mySelectionSet.SelectByPolygon mode, ConvertPoints(varPoints())
However, I can't seem to figure out how to pass an array to the function. Can someone tell me if I'm going about this anywhere even near the right way?
Many thanks for the help.
-
What you might need to do is change the return value of ConvertPoints to a variant.
Another option would be to create a global variable in the function and not return a value from the function, but set the value within the function. Does that make sense?
-
Keith,
Thanks for the quick reply.
I've made the change you suggested:
Function ConvertPoints(ByRef PointsArray() As Double) As Variant
However, when I try to make the selection using this command:
mySelectionSet.SelectByPolygon mode, ConvertPoints(varPoints())
I get the message: "Type mismatch, array or user-defined type expected"
I get the same message if I try the following (e.g. using the name of the array without the parentheses).
ConvertPoints(varPoints)
I feel like I'm doing something wrong with the syntax. Anyway, I'll try to create a global function and see if that will work.
To do that, I'd ass Public NewArray() as double in the general portion of the code and then redim it in the function, correct?
-
Alas, I just tried to make a public variable:
Public NewArray() As Double
and the compiler told me that "... arrays ... are not allowed as Public members of object modules"
-
In a module (not a form)
Dim MyArray() As Double
Public Function MyFunction(ByVal PointsArray As Variant)
'do stuff here to fill in MyArray
End Function
-
Keith is right, when passing arrays, you need to dim them as variants.
-
Michael,
Your function is flawed, in that it isn't creating the proper size new array and it isn't filling it in correctly either.
Try this:
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
-
Seems too much like work, to me. I dont see the need to convert anything; and Im not sure why your routine isnt working - are you selecting a single line as an object? Because a single line, or a polyline with only one subentity, will only have 2 points (coordinates), which isnt enough points to describe the polygon you wish to select by.
Also, you realize that in a lot of cases, the polygon you wish to use to select by WONT also select, say, the polyline with the coordinates you used to describe the polygon? because the polygon would be ON, not IN, the selectionset polygonal window.
Try the code below. ts beasically whipped up from copy/paste from the help files; it creates a triangular polyline, and a circle inside it, then creates the selectionset boundary from the polyline coordinates. Note that the circle is selected, and not the polyline....
' -------- snip -------- snip -------- snip -------- snip --------
Sub Main()
' Create a polyline. Then use the Coordinates
' property to return all the coordinates in the polyline.
Dim plineObj As AcadPolyline
' Create Polyline resembling a triangle
Dim points(8) As 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(8) = 0
Set plineObj = ThisDrawing.ModelSpace.AddPolyline(points)
' Return all the coordinates of the polyline
Dim retCoord As Variant
retCoord = plineObj.Coordinates
' Draw a circle in the middle of the triangle
Dim MyCircle As AcadCircle
Dim CenterPoint(2) As Double
CenterPoint(0) = 0: CenterPoint(1) = 3: CenterPoint(2) = 0
Set MyCircle = ThisDrawing.ModelSpace.AddCircle(CenterPoint, 1)
ThisDrawing.Regen acActiveViewport
' create a selection set
Dim mode As Integer
mode = acSelectionSetWindowPolygon
Dim mySelectionSet As AcadSelectionSet
Set mySelectionSet = ThisDrawing.SelectionSets.Add("TEST_SSET")
' use our points from the polyline to select entities
mySelectionSet.SelectByPolygon mode, retCoord
If mySelectionSet.Count > -1 Then
Dim acEnt As AcadObject
For i = 0 To mySelectionSet.Count - 1
Set acEnt = mySelectionSet.Item(i)
MsgBox "Item " & i & " " & acEnt.ObjectName
Next i
Else
MsgBox "Nothing in selection set"
End If
mySelectionSet.Delete
Set mySelectionSet = Nothing
End Sub
-
Rogue,
If the object obtained for the Polygon is a LightWeightPolyLine, the Coordinates property returns a 2D point list which must be converted to a 3D point list. Based on Michael's first post, he knows what the first entity on a layer is and what the Coordinates returns, which is a 2D point list.