Adapting VBA into an exe

Discussion in 'AutoCAD' started by GCronin82, Mar 22, 2006.

  1. GCronin82

    GCronin82 Guest

    I am adapting code I wrote in VBA for Architectural Desktop into code
    that will run as an application seperate from Autocad in order to allow

    running on other versions. My problem is that when I access objects in

    VBA, I can use ThisDrawing.HandleToObject(a(0).Handle), where a is a
    selection set. Doing this in Visual Studio, though, the object does
    not get assigned. My test code looks like this:

    Private Sub Command1_Click()
    Dim a, App as object
    Dim MyBlock As AcadBlockReference
    Dim Temp As String
    Dim atts As Variant
    Dim i As Integer


    Set App = GetObject(, "AutoCAD.Application.16")
    On Error Resume Next
    App.ActiveDocument.SelectionSets.Item("MySS").Delete


    Set a = App.ActievDocument.SelectionSets.Add("MySS")
    a.SelectOnScreen
    Set MyBlock = App.ActiveDocument.HandleToObject(a(0).Handle)
    atts = MyBlock.GetAttributes
    Temp = "Attributes:" & vbCrLf
    For i = LBound(atts) To UBound(atts)
    Temp = Temp & atts(i).TextString & vbCrLf
    Next
    MsgBox "Handle: " & MyBlock.Handle & vbCrLf & Temp, vbOkOnly, "Test"

    a.Delete
    End Sub


    When Executed, no message box comes up. If I replace MyBlock.Handle
    with a(0).Handle, the message box comes up, but lists no attributes.
    This seems to say that the Application is unable to create the block,
    but no error is being thrown or anything. Any feedback would be
    wonderful. Thanks in advance!
     
    GCronin82, Mar 22, 2006
    #1
  2. GCronin82

    mark Guest

    Take out your 'On Error Resume Next' statement your code should break at
    the error.
    Ahh, you misspelt ActievDocument 3 lines up.

    Anyway, the object 'a' is the selection set which has properties like
    'item' and 'count'.

    So you need to get to the objects in the selection set 'a'. But first
    lets Dim SSetObj as AcadSelectionSet (substituting for a).

    You could write:

    For each MyBlock in SSetObj
    atts = MyBlock.GetAttributes
    Temp = "Attributes:" & vbCrLf
    For i = LBound(atts) To UBound(atts)
    Temp = Temp & atts(i).TextString & vbCrLf
    Next i
    Next MyBlock

    Or address the objects in SSetObj by iterating through array, where the
    index starts at 0, and it upper boundary is SSetObj.count -1.

    Dim i as integer
    For i = 0 to SSetObj.count -1
    Set MyBlock = SSetObj.item(i)
    Next i

    Note that these examples assume that the contents of SSetObj will be
    block references.

    Hope this helps.


    Regards,
     
    mark, Mar 22, 2006
    #2
  3. GCronin82

    GCronin82 Guest

    Thanks, that helps. I discovered between posting and now that I was
    using On error resume next wrong. Thought it only caught an error in
    the next line, not every line after that. Thanks for the suggested
    application as well... I was simply writing something to check whether
    the code worked, so I was only taking the first element of the
    selection set.
     
    GCronin82, Mar 22, 2006
    #3
  4. GCronin82

    GCronin82 Guest

    Okay, so I adapted the code for the error. There's no error thrown if
    this code is run from within Autocad, but from Visual Studio, it tells
    me Type Mismatch. when I try to assign MyBlock. Any thoughts?

    Sub TestProject()
    Dim a As Object
    Dim App As Object
    Dim MyBlock As AcadBlockReference
    Dim Temp As String
    Dim atts As Variant
    Dim i As Integer

    Begin:
    Set App = GetObject(, "Autocad.Application.16")
    On Error Resume Next
    App.ActiveDocument.SelectionSets.Item("myss").Delete
    On Error GoTo 0

    Set a = App.ActiveDocument.SelectionSets.Add("myss")
    a.SelectOnScreen
    Set MyBlock = a(0)
    atts = MyBlock.GetAttributes
    Temp = "Attributes:" & vbCrLf
    For i = LBound(atts) To UBound(atts)
    Temp = Temp & atts(i).TextString & vbCrLf
    Next
    MsgBox "Handle: " & MyBlock.Handle & vbCrLf & Temp, , "Test"
    a.Delete
    End Sub
     
    GCronin82, Mar 22, 2006
    #4
  5. GCronin82

    mark Guest

    In VBA within AutoCAD the object properties can be forgiving. In VB the
    AutoCAD type library is just one of many, so you really have to use the
    full syntax while you get used to it.

    a(0)is not an object. a.item(0) is an object in the selection set of 'a'

    Also, in VB, be sure AutoCAD's type library is selected in
    Project/References.

    Look to Tools/Options and check the auto-complete members box. This
    option helps list the properties and methods of declared objects you're
    working with weather they be a block ref, a selection set or a grid on a
    form.

    What is it you'd like to do with your blocks?
     
    mark, Mar 23, 2006
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.