Error using XRecord in VB.NET

Discussion in 'AutoCAD' started by Chris Cox, Nov 3, 2004.

  1. Chris Cox

    Chris Cox Guest

    Hello...

    I am trying to do a very simple test using Xrecords. However, I cannot get it to work. I am doing this in VB.NET.


    By the way, the "Doc" below has a valid connection to the ACAD document. I just left that part out for easier viewing.

    Here is the code:
    Dim XRec As AcadXRecord
    Dim Dict As AcadDictionary
    Dim XRecType(0) As Object
    Dim XRecData(0) As Object

    Try
    'This part works fine
    Dict = Doc.Dictionaries.Add("TestDictionary")
    XRec = Dict.AddXRecord("XRecordTest")
    XRecType(0) = 1
    XRecData(0) = "TestValue"

    'After this next line, an error will always occur.
    XRec.SetXRecordData(XRecType, XRecData)

    Catch ex As Exception
    'An error always occurs. Error message reads as follows...
    '"Invalid argument type in SetXRecordData method"
    End Try
     
    Chris Cox, Nov 3, 2004
    #1
  2. Chris Cox

    Chris Cox Guest

    Hi Jeff,

    Yeah, I tried that as well. It still does not work. Any other suggestions?

    Thanks,
    Chris
     
    Chris Cox, Nov 3, 2004
    #2
  3. Chris Cox

    Jeff Mishler Guest

    Hi Chris,
    Sorry I don't have VB.Net to try this in, just throwing things out.......in
    VBA this does work:

    Sub test()

    Dim XRec As AcadXRecord
    Dim Dict As AcadDictionary
    Dim XRecType(0) As Integer 'note Integer type
    Dim XRecData(0) As Variant 'note Variant type

    Set Dict = ThisDrawing.Dictionaries.Add("TestDictionary")
    Set XRec = Dict.AddXRecord("XRecordTest")
    XRecType(0) = 1
    XRecData(0) = "TestValue"

    XRec.SetXRecordData XRecType, XRecData

    End Sub
     
    Jeff Mishler, Nov 3, 2004
    #3
  4. Chris Cox

    MP Guest

    In addition to Jeff's correction, you must coordinate your group code with the type of data to be stored
    Look up the dxf references on group codes for xrecords/xdata

    ----- Original Message -----
    From: Chris Cox
    Newsgroups: autodesk.autocad.customization
    Sent: Wednesday, November 03, 2004 4:45 PM
    Subject: Error using XRecord in VB.NET


    Hello...

    I am trying to do a very simple test using Xrecords. However, I cannot get it to work. I am doing this in VB.NET.
     
    MP, Nov 3, 2004
    #4
  5. Chris Cox

    Jeff Mishler Guest

    Chris, the Group Codes can be found in the DXF Reference. But, 1 is for
    strings so that portion of your code is OK

    --
    Jeff
    check out www.cadvault.com
    Hi MP,

    Thanks for your help as well. I don't mean to sound dumb, but I cannot find
    any further information on how to set the group codes for XRecords. The
    help file for XRecords does talk about the group codes, but there is no
    information on what to do with it or how to implement it.

    The following is quoted from the help file:

    The following group codes are common to all XRecord objects:
    100 - Subclass marker (AcDbXrecord)
    1-369 (except 5 and 105) These values can be used by an application in any
    way.

    Besides this information above, I cannot find any way to implement the group
    codes. I have tried in my sample test to change the integer value to
    different values instead of 1 as in my sample, but nothing works. Any
    further suggestions?

    Thanks again!
    Chris


    In addition to Jeff's correction, you must coordinate your group code with
    the type of data to be stored
    Look up the dxf references on group codes for xrecords/xdata

    ----- Original Message -----
    From: Chris Cox
    Newsgroups: autodesk.autocad.customization
    Sent: Wednesday, November 03, 2004 4:45 PM
    Subject: Error using XRecord in VB.NET


    Hello...

    I am trying to do a very simple test using Xrecords. However, I cannot
    get it to work. I am doing this in VB.NET.
     
    Jeff Mishler, Nov 3, 2004
    #5
  6. Chris Cox

    Chris Cox Guest

    Hi MP,

    Thanks for your help as well. I don't mean to sound dumb, but I cannot find any further information on how to set the group codes for XRecords. The help file for XRecords does talk about the group codes, but there is no information on what to do with it or how to implement it.

    The following is quoted from the help file:

    The following group codes are common to all XRecord objects:
    100 - Subclass marker (AcDbXrecord)
    1-369 (except 5 and 105) These values can be used by an application in any way.

    Besides this information above, I cannot find any way to implement the group codes. I have tried in my sample test to change the integer value to different values instead of 1 as in my sample, but nothing works. Any further suggestions?

    Thanks again!
    Chris


    In addition to Jeff's correction, you must coordinate your group code with the type of data to be stored
    Look up the dxf references on group codes for xrecords/xdata

    ----- Original Message -----
    From: Chris Cox
    Newsgroups: autodesk.autocad.customization
    Sent: Wednesday, November 03, 2004 4:45 PM
    Subject: Error using XRecord in VB.NET


    Hello...

    I am trying to do a very simple test using Xrecords. However, I cannot get it to work. I am doing this in VB.NET.
     
    Chris Cox, Nov 3, 2004
    #6
  7. Chris Cox

    Chris Cox Guest

    Hey Jeff,

    Yeah that works great in VBA. But for some reason it causes an error in
    VB.NET. I think the problem lies within the fact the VB.NET does not allow
    for the Variant type, and therefore I am using the Object type for my
    variable "XRecData". Very weird problem. I've also tried changing the
    variable to String and Integer, but nothing seems to work.

    --Chris
     
    Chris Cox, Nov 3, 2004
    #7
  8. Chris Cox

    MP Guest

    I don't know .net but is there an "As Any " modifier available?
     
    MP, Nov 3, 2004
    #8
  9. Chris Cox

    Chris Cox Guest

    Actually the Object is the most generic one available. There is no "any"
    available. Good suggestion though... I see where you are going with that.
    I'm gonna look further into it, and try a bunch of different types to see if
    any make it work.

    --Chris
     
    Chris Cox, Nov 4, 2004
    #9
  10. Chris Cox

    Joe Burke Guest

    Chris,

    This is a long shot, but may be worth testing.

    I'm new to xrecords, so I'm not an expert. Also know nothing about NET or VBA.

    While learning about xrecords I ran into the following problem. Here's a function
    (GetXRData) which adds data to an empty xrecord. Notice the line near the end, (not
    (vlax-invoke... and the comment above. That line of code works fine when the data
    list contains more than one dotted pair. However it causes the same error message you
    mentioned when passed just '((7 . "Romans")). So I had to use the vlax-make-safearray
    stuff which solved the problem.

    I think you might get the same error if you try to use SetXRecordData with an xrecord
    which already contains some data. But I'm not sure about that.

    Joe Burke

    ;; function: put data into an *empty* xrecord
    ;; argument: strings - dictionary name and xrecord name
    ;; and a quoted list - example: '((7 . "Romans") (8 . "LayName") (40 . 3.0))
    ;; returns T if successful
    (defun PutXRData (dictname xrname lst / dicts typlst datalst dictobj xrecobj)
    (setq dicts
    (vla-get-dictionaries
    (vla-get-activedocument
    (vlax-get-acad-object))))
    (and
    (not (GetXRData dictname xrname)) ;check for existing data
    (foreach x lst
    (setq typlst (cons (car x) typlst))
    (setq datalst (cons (cdr x) datalst))
    )
    (setq typlst (reverse typlst))
    (setq datalst (reverse datalst))
    (setq dictobj (ValidItem dicts dictname))
    (setq xrecobj (ValidItem dictobj xrname))
    ;doesn't work when passed single dotted pair '((7 . "Romans")) - why?
    ;(not (vlax-invoke xrecobj 'SetXRecordData typlst datalst)) <<<<<<<<<<<<<<
    (not (vla-setxrecorddata xrecobj
    (vlax-safearray-fill (vlax-make-safearray vlax-vbinteger
    (cons 0 (1- (length typlst)))) typlst)
    (vlax-safearray-fill (vlax-make-safearray vlax-vbvariant
    (cons 0 (1- (length datalst)))) datalst)
    )
    )
    ) ;and
    ) ;end

    ------------------------
    ;; function: return the data contained in an xrecord
    ;; argument: strings - dictionary name and xrecord name
    ;; returns: a dotted pair data list - example: ((7 . "Romans") (8 . "LayName") (40 .
    3.0))
    (defun GetXRData ( dictname xrname / dicts dictobj xrecobj types values )
    (setq dicts
    (vla-get-dictionaries
    (vla-get-activedocument
    (vlax-get-acad-object))))
    (if
    (and
    (setq dictobj (ValidItem dicts dictname))
    (setq xrecobj (ValidItem dictobj xrname))
    (not (vla-getxrecorddata xrecobj 'types 'values))
    types values
    )
    (mapcar 'cons (vlax-safearray->list types)
    (mapcar 'vlax-variant-value (vlax-safearray->list values)))
    )
    ) ;end
     
    Joe Burke, Nov 4, 2004
    #10
  11. Chris Cox

    Chris Cox Guest

    Thank you everybody for helping me try to solve this problem!!!

    I figured out the solution... it is such a simple solution that I'm ready to shoot myself after spending so much time on it.

    Instead of declaring the first item "XRecType" as an object or integer, it must be declared as an "Int16", which is a 16-bit integer. This makes it compatible with VBA in autocad. So to fix the problem, I just did the following:

    Dim XRec As AcadXRecord
    Dim Dict As AcadDictionary
    Dim XRecType(0) As Int16 <--- This has been changed
    Dim XRecData(0) As Object

    Whew!!! Glad that is over.

    Thanks again!
    Chris Cox



    Hello...

    I am trying to do a very simple test using Xrecords. However, I cannot get it to work. I am doing this in VB.NET.


    By the way, the "Doc" below has a valid connection to the ACAD document. I just left that part out for easier viewing.

    Here is the code:
    Dim XRec As AcadXRecord
    Dim Dict As AcadDictionary
    Dim XRecType(0) As Object
    Dim XRecData(0) As Object

    Try
    'This part works fine
    Dict = Doc.Dictionaries.Add("TestDictionary")
    XRec = Dict.AddXRecord("XRecordTest")
    XRecType(0) = 1
    XRecData(0) = "TestValue"

    'After this next line, an error will always occur.
    XRec.SetXRecordData(XRecType, XRecData)

    Catch ex As Exception
    'An error always occurs. Error message reads as follows...
    '"Invalid argument type in SetXRecordData method"
    End Try
     
    Chris Cox, Nov 4, 2004
    #11
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.