PDA

View Full Version : Exploding a polyline in VBA



Nimbokwezer
15th Dec 2006, 10:53 pm
I'm not sure why the following code isn't working:



Dim PolyLine As AcadLWPolyline
Dim explodeArray As Variant
Dim i As Integer

explodeArray = PolyLine.Explode

For i = 0 To UBound(explodeArray)
ListBox1.AddItem (explodeArray(i).ObjectName)
ListBox1.AddItem (explodeArray(i).StartPoint(0) & ", " & explodeArray(i).StartPoint(1) & ", " & explodeArray(i).StartPoint(2))
Next i

When I run this code, I get the error:
"Property let procedure not defined and property get procedure did not return an object" that points to the line with the StartPoint methods.

If I remove the 2nd Listbox1.AddItem line, it correctly lists all of the lines in order from start to end of the polyline, so I know the explode method is correctly writing the object to the explodeArray. I tried making a minor change with the following code, and it worked:



Dim PolyLine As AcadLWPolyline
Dim explodeArray As Variant
Dim i As Integer
Dim Line As AcadLine

explodeArray = PolyLine.Explode

For i = 0 To UBound(explodeArray)
Line = explodeArray(i)
ListBox1.AddItem (explodeArray(i).ObjectName)
ListBox1.AddItem (Line.StartPoint(0) & ", " & Line.StartPoint(1) & ", " & Line.StartPoint(2))
Next i


The code now correctly prints the startpoints of all of the lines. I'm just wondering why "explodeArray(i).StartPoint(0)" isn't valid , but "explodeArray(i).ObjectName" is. It seems pretty arbitrary, and I don't like throwing in the extra step of dimming the line object for a cheap patch-up that I don't understand.

SEANT
16th Dec 2006, 01:01 am
That's quite interesting, and a little annoying. The information is there, as witnessed by the "Watch Window", but crashes enroute to a calling procedure.

Properties that are more derivitive, i.e., .length, .angle, etc., perform properly.

I wonder if you've happened upon an area where Autodesk allowed the COM interface to fall between the cracks.

fixo
16th Dec 2006, 12:01 pm
Line is object, for object you must to use Set statement
change this line:

Line = explodeArray(i)
on


Set Line = explodeArray(i)

Hth

~'J'~

SEANT
16th Dec 2006, 01:16 pm
I think that is what the OP eventually did, the 'Set' statement wasn't included in the post however.

What I was referring to was that much of the array item properties were available without 'Set'ting it to a line object. See the modified example below.

It crashes at MsgBox "Startpoint: " & explodedObjects(I).StartPoint though the earlier Msgbox calls execute correctly.

It has to be assumed that the Startpoint and Endpoint of a line are prerequisites for Length and Angle Properties. In fact, if explodedObjects is added to the Watch Window, the start and end points for explodedObjects(I) are shown, but there is some break in the pipeline to a calling procedure.


Sub Example_Explode()
' This example creates a lightweight polyline in model space.
' It then explodes the polyline.

Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double

' Define the 2D polyline points
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 1

' Create a lightweight Polyline object in model space
Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(poin ts)

' Set the bulge on one segment to vary the
' type of objects in the polyline
plineObj.SetBulge 3, -0.5
ZoomAll

' Explode the polyline
MsgBox "Explode the polyline?", , "Explode Example"
Dim explodedObjects As Variant
explodedObjects = plineObj.Explode

' Loop through the exploded objects
Dim I As Integer
For I = 0 To UBound(explodedObjects)
explodedObjects(I).Update
MsgBox "Exploded Object " & I & ": " & explodedObjects(I).ObjectName, , "Explode Example"
MsgBox "Length: " & explodedObjects(I).Length
MsgBox "Angle: " & explodedObjects(I).Angle
MsgBox "Startpoint: " & explodedObjects(I).StartPoint
explodedObjects(I).Color = acByLayer
explodedObjects(I).Update
Next

End Sub

fixo
16th Dec 2006, 07:29 pm
Hope this will be works for you correct


Sub Example_Explode()
' This example creates a lightweight polyline in model space.
' It then explodes the polyline.
Dim lineObj As AcadLine
Dim arcObj As AcadArc
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double

' Define the 2D polyline points
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 1

' Create a lightweight Polyline object in model space
Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(poin ts)

' Set the bulge on one segment to vary the
' type of objects in the polyline
plineObj.SetBulge 3, -0.5
ZoomAll

' Explode the polyline
MsgBox "Explode the polyline?", , "Explode Example"
Dim explodedObjects As Variant
explodedObjects = plineObj.Explode

' Loop through the exploded objects
Dim I As Integer
For I = 0 To UBound(explodedObjects)
explodedObjects(I).Update
MsgBox "Exploded Object " & I + 1 & ": " & explodedObjects(I).ObjectName, , "Explode Example"

If TypeOf explodedObjects(I) Is AcadLine Then
Set lineObj = explodedObjects(I)
MsgBox "Length: " & lineObj.Length
MsgBox "Angle: " & lineObj.Angle
MsgBox "Startpoint: " & lineObj.StartPoint(0) & ";" & lineObj.StartPoint(1) & ";" & lineObj.StartPoint(2)
lineObj.color = acByLayer
lineObj.Update
ElseIf TypeOf explodedObjects(I) Is AcadArc Then
Set arcObj = explodedObjects(I)
MsgBox "Length: " & arcObj.ArcLength
MsgBox "Angle: " & arcObj.TotalAngle
MsgBox "Startpoint: " & arcObj.StartPoint(0) & ";" & arcObj.StartPoint(1) & ";" & arcObj.StartPoint(2)
arcObj.color = acByLayer
arcObj.Update
End If
Next

End Sub


~'J'~

SEANT
16th Dec 2006, 09:25 pm
I think you are correct that Set lineObj = explodedObjects(I) or similar is the way to go. As a matter of fact, until this post I thought it was impossible to get anywhere unless that object set was accomplished.

The previous code snippets, along with the one below, demonstrate that it is not the hard requirement I assumed.

I'm not suggesting anyone use this shortcut. As we have seen, it is unreliable. I just find it interesting.



Read/Write Properties are also available.

Sub Example_Explode()
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double

points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 1

Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(poin ts)

plineObj.SetBulge 3, -0.5
MsgBox "Explode the polyline?", , "Explode Example"
Dim explodedObjects As Variant
explodedObjects = plineObj.Explode
plineObj.Delete

Dim I As Integer
For I = 0 To UBound(explodedObjects)
explodedObjects(I).Update
If TypeOf explodedObjects(I) Is AcadLine Then
''''''''''Without Set Statements''''''''''
explodedObjects(I).Thickness = 2
ElseIf TypeOf explodedObjects(I) Is AcadArc Then
explodedObjects(I).radius = 2
End If
explodedObjects(I).Update
Next

End Sub

Bryco
18th Dec 2006, 05:04 am
It's hard to tell when this error is going to happen so I've become used to dimming a variant for the startpoint/center/endpoint. This way you never get the error message. It usually works out less expensive in computing and typing in the long run so it becomes an easy habit.
Dim S,E
S=explodedObjects(I).StartPoint
debug.Print S(0),S(1),S(2)

SEANT
19th Dec 2006, 11:30 am
Yes.

Autocad's underlying line class is somewhat particular with regard to that property. Just as well; it promotes a more structured coding I suppose.

Much like the OP, I'm working on a procedure that involves exploding and the restructuring of LWPolylines in 3D space. It is proving to be quite a handful.