Access array value of late bound object properties

Access array value of late bound object properties

gifari.zulk
Participant Participant
598 Views
6 Replies
Message 1 of 7

Access array value of late bound object properties

gifari.zulk
Participant
Participant

Hi, all. This is a continue of my previous question about late binding AutoCAD VBA.

I have been able to late bind AutoCAD objects in VBA. But I am unable to get the value of the array-type object properties.

 

Here is my code:

 

Sub LateBindingCAD()

'Declaration and late binding the application objects
Dim AcadApp As Object
Dim AcadDoc As Object
Dim AcadObj As Object
Dim LineObj As Object
Dim LineLayers(), LineCoords()
Dim i
Set AcadApp = GetObject(, "AutoCAD.Application")
Set AcadDoc = AcadApp.ActiveDocument

'Select objects in targeted layers, works fine, not important to the question
    SSName = Array("COLUMN","BEAM","WALL","FLOOR")
    For i = 0 To 3
        AcadDoc.SelectionSets.Item(SSName(i)).Delete
        AcadDoc.SelectionSets.Add SSName(i)
        FilterType(0) = 8
        FilterData(0) = SSName(i)
        AcadDoc.SelectionSets.Item(SSName(i)).Select 4, , , FilterType, FilterData
    Next i

'Extract data from selected object, by late binding the objects inside application objects
    ReDim LineLayers(100), LineCoords(100)
    i = 0
    For Each SSObj In AcadDoc.SelectionSets
        For Each AcadObj In SSObj
            If AcadObj.Layer = "COLUMN" And AcadObj.ObjectName = "AcDbLine" Then
                Set LineObj = AcadObj
                LineLayers(i) = LineObj.Layer 'success getting non-array value
                LineCoords(i) = LineObj.StartPoint(0) 'error 451 here <----------
                i = i + 1
            End If
        Next
    Next

End Sub

 

The error occurs in almost last line: "LineCoords(i) = LineObj.StartPoint(0)", the error is "Run-time error '451': Property let procedure not defined and property get procedure did not return an object".
Assigning values for non-array type of this object works fine, for example the "LineLayers(i) = LineObj.Layer" works fine.
The "LineObj.StartPoint" is Variant/Double array type with size of (0 To 2).

 

Any suggestion? Thank you in advance.

0 Likes
Accepted solutions (2)
599 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

When you use LineObj.StartPoint(0), the 0 acts as an index specification. The call to the index of 0 fails because the StartPoint is not a collection. There is just the one value, a variant array of doubles. Just use LineObj.StartPoint. If you want to get the x value of the start point, pull that from the variable you just assigned it to.

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Well, since you are using late binding and the LineObj is declared as "Object", then its properties are defaulted also to be "object", which required "Let/Set" and "Get" accessor to be reachable.

 

In your case, since we know that the Start/EndPoint of an AcadLine (or, more in general, a point's coordinate) is a 3-element array of Double, which is held with a Variant type in VBA/COM API, you can "cast" it into variable of Variant type and then access it as if it is an array. Following late-binding code works, assume every entities when the code loops through in the ModelSpace is AcadLine:

 

Public Sub Test2()

    Dim line As Object
    Dim point As Variant
    Dim x As Double
    Dim y As Double
    
    For Each line In ThisDrawing.ModelSpace
        point = line.StartPoint
        x = point(0)
        y = point(1)
        MsgBox "X=" & x & vbCrLf & "Y=" & y
    Next

End Sub

The corresponding early-binding code would be:

Public Sub Test1()

    Dim line As AcadLine
    Dim x As Double
    Dim y As Double
    
    For Each line In ThisDrawing.ModelSpace
        x = line.StartPoint(0)
        y = line.StartPoint(1)
        MsgBox "X=" & x & vbCrLf & "Y=" & y
    Next

End Sub

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

gifari.zulk
Participant
Participant
Thank you all for the answers. It works smoothly now. Your explanations also really helps me to understand better, thanks.
0 Likes
Message 5 of 7

Ed__Jobe
Mentor
Mentor

@gifari.zulk Norman's Test2 shows what I described as "If you want to get the x value of the start point, pull that from the variable you just assigned it to." He declared point as a Variant, which at runtime, gets assigned an array of doubles. Then you can access the the array using an index of 0 or 1.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 7

gifari.zulk
Participant
Participant

Yes I understood. I revised my code following your comment before his reply and worked. I accepted his comment just because it has sample code that may help others too.

 

Edit: oh I just realized we can accept multiple solutions.

0 Likes
Message 7 of 7

Ed__Jobe
Mentor
Mentor

Good to know. I get pressed for time with my 'day job' and often only have time to describe the solution, but not provide code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature