TheSwamp

Code Red => VB(A) => Topic started by: T.Willey on July 17, 2006, 06:41:39 PM

Title: Setting XRecord
Post by: T.Willey on July 17, 2006, 06:41:39 PM
Why won't this work?
Code: [Select]
Public Sub AddXRec(ByRef XrecName As String, DataType As Variant, Data As Variant)

Dim DictCol As Collection
Dim MyDict As Object
Dim Xrec As AcadXRecord

Set DictCol = This.Drawing.Dictionaries
Set MyDict = DictCol.Add("VBAtoLisp")
Set Xrec = MyDict.AddXRecord(XrecName)
xrec.SetXRecordData (datatype,data)

End Sub

When I hit enter on the last line, it gives and error stating
"Compile error:
Expected: ="
Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 06:47:49 PM
Lose the parenthesis
Code: [Select]
Public Sub AddXRec(ByRef XrecName As String, DataType As Variant, Data As Variant)

Dim DictCol As Collection
Dim MyDict As Object
Dim Xrec As AcadXRecord

Set DictCol = This.Drawing.Dictionaries
Set MyDict = DictCol.Add("VBAtoLisp")
Set Xrec = MyDict.AddXRecord(XrecName)
xrec.SetXRecordData datatype,data

End Sub
Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 06:49:20 PM
Rule of thumb for most things VBA, if you are on the right side of an equals such as
Code: [Select]
Set Xrec = MyDict.AddXRecord(XrecName) you use the parethesis, if not, you don't.
Title: Re: Setting XRecord
Post by: T.Willey on July 17, 2006, 06:56:47 PM
Thank you very much Bob.
Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 06:58:23 PM
Not a problem.
Title: Re: Setting XRecord
Post by: MP on July 17, 2006, 07:13:25 PM
If you wish to maintain consistant parenthesis use throughout your code you can use the call statement --


Public Sub AddXRec(ByRef XrecName As String, DataType As Variant, Data As Variant)

    Dim DictCol As Collection, _
        MyDict  As Object, _
        Xrec    As AcadXRecord

    Set DictCol = This.Drawing.Dictionaries
    Set MyDict  = DictCol.Add ("VBAtoLisp")
    Set Xrec    = MyDict.AddXRecord (XrecName)

    Call  xrec.SetXRecordData (datatype, data)

End Sub


(untested / drawing from memory)
Title: Re: Setting XRecord
Post by: T.Willey on July 17, 2006, 07:27:18 PM
Thanks Michael.  Just trying to get an idea of what this is all about.

Next question:
When you want to fill and XRecord, you need to use a variant, right?
Do you define the variant with and array of values?
Here is the code I'm trying.
Code: [Select]
Public Sub Main(ByRef Str1 As String, Str2 As String)

Dim DataType(1) As Variant
Dim Data(1) As Variant

Set Data(0) = Str1
Set Data(1) = Str2
Set DataType(0) = 1
Set DataType(1) = 2

AddXRec "VBAtoLisp", DataType, Data

End Sub

I pass Str1 & Str2 from a user form.  The error says that it wants an object for Str1, or any other variable that is first to get set (I have tried to set the DataType, with the same error).

Why?
Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 07:43:31 PM
Because you are Dimming the array as an array of 1 item with an index of 1.  You need to give it the entire range you are using so,

Dim DataType(0 to 1) as Variant
Dim Data(0 to 1) as Variant
Title: Re: Setting XRecord
Post by: T.Willey on July 17, 2006, 07:45:30 PM
Thanks Bob.  I will have to pick this up tomorrow, it's time for me to go home, and no Cad there.
Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 07:48:36 PM
Time for me to go as well.  We'll play some more tommorrow.
Title: Re: Setting XRecord
Post by: Jeff_M on July 17, 2006, 08:35:41 PM
Because you are Dimming the array as an array of 1 item with an index of 1.  You need to give it the entire range you are using so,

Dim DataType(0 to 1) as Variant
Dim Data(0 to 1) as Variant
Bob, This actually is not quite right......If your Index base is 0, using Dim Data(1) as Whatever  knows you mean Data(0 to 1).....here's a simple Example to demonstrate this:
Code: [Select]
Sub test()
Dim MyArray(2) As String
MyArray(0) = "Test1"
MyArray(1) = "Test2"
MyArray(2) = "Test3"

Dim i As Integer
For i = 0 To UBound(MyArray)
    Debug.Print MyArray(i)
Next
End Sub
Now, I prefer the way you show so you know the Lower & Upper bounds, but it's not mandatory.

Tim,
The error you get regarding an Object is due to your using this format
Code: [Select]
Set Data(0) = Str1
Set Data(1) = Str2
Set DataType(0) = 1
Set DataType(1) = 2
You use Set ONLY when you are casting some type of object. In this case, strings and integers are not objects, so lose the Set..... another error will pop up though, as Autocad expects DXF codes to be Integers and the array must be dimensioned as such.....make sure to change the first Function/Sub to accept an Integer array, too.

So for anything that uses the DXF codes, such as Xrecords, SelectionSet filters, etc., you want to use this format:
Code: [Select]
Dim DataType(1) As Integer
Dim Data(1) As Variant

Data(0) = Str1
Data(1) = Str2
DataType(0) = 1
DataType(1) = 2
HTH




Title: Re: Setting XRecord
Post by: Bob Wahr on July 17, 2006, 10:19:39 PM
Thanks Jeff.  That's what I get for answering quickly instead of paying attention. 
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 11:06:16 AM
Thanks Jeff and Bob.  Now I'm erroring on a different part, and I don't understand why.  Here is the code.
Code: [Select]
Public Sub AddXRec(ByRef XrecName As String, DataType As Variant, Data As Variant)

Dim DictCol As Collection
Dim MyDict As Object
Dim Xrec As AcadXRecord

Set DictCol = This.Drawing.Dictionaries
MyDict = DictCol.Add("VBAtoLisp")
Xrec = MyDict.AddXRecord(XrecName)
Xrec.SetXRecordData DataType, Data


End Sub
It is erroring here.
Code: [Select]
MyDict = DictCol.Add("VBAtoLisp")
Saying that it is "Expected Function or variable".. What does it want.  I thought I could add a new dictionary with add, but it doesn't seem to like it.  It is highlighting ".Add" portion of the code, if that helps.


Edit:  It even errors if I use
Set MyDict = DictCol.Add("VBAtoLisp") [Tried per Jeff's comments about using Set for objects.]
Title: Re: Setting XRecord
Post by: Bob Wahr on July 18, 2006, 11:29:01 AM
Code: [Select]
Public Sub AddXRec(ByRef XrecName As String, DataType As Variant, Data As Variant)

Dim DictCol As Collection
Dim MyDict As AcadDictionary
'you want this to be a dictionary so no reason not to Dim it as such
Dim Xrec As AcadXRecord

Set DictCol = This.Drawing.Dictionaries
Set MyDict = DictCol.Add("VBAtoLisp")
'if you are setting an object, you need Set X = Y
Set Xrec = MyDict.AddXRecord(XrecName)
Xrec.SetXRecordData DataType, Data
'if you are setting the properties of an object, you don't

End Sub
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 11:33:08 AM
I tried that also Bob.  Maybe you didn't see my edit to my other post.  I will continue to look through the help to see if I can understand what is going on.  Thanks again.
Title: Re: Setting XRecord
Post by: MP on July 18, 2006, 11:47:07 AM
Alternative ...

Code: [Select]
Function AddXRec(ByRef XrecName As String, _
                 DataType       As Variant, _
                 Data           As Variant) as AcadXRecord

    Dim dict As AcadDictionary, _
        xrec As AcadXRecord
   
    Set dict = ThisDrawing.Dictionaries.Add("VBAtoLisp")
    Set xrec = dict.AddXRecord(xrecName)
   
    Call xrec.SetXRecordData(dataType, data)

    Set AddXRec = xrec

End Function

Code: [Select]
Sub Test ( )

    Dim xrecName As String, _
        dataType As Variant, _
        data     As Variant, _
        result   As AcadXRecord
   
    ReDim dataType(0 To 0) As Integer
    ReDim data(0 To 0) As Variant
   
    xrecName    = "Test"
    dataType(0) = 1
    data(0)     = "asd"
   
    Set result = AddXRec(xrecName, dataType, data)
   
    ''  do other stuff with result ...   
   
End Sub

Edit: Fixed the return value.
Title: Re: Setting XRecord
Post by: MP on July 18, 2006, 11:51:03 AM
You could even go as concise as this, tho I probably would use the previous variant --

Code: [Select]
Function AddXRec(ByRef xrecName As String, _
                 dataType       As Variant, _
                 data           As Variant) As AcadXRecord

    Dim xrec As AcadXRecord
   
    Set xrec = ThisDrawing.Dictionaries.Add("VBAtoLisp").AddXRecord(xrecName)
   
    Call xrec.SetXRecordData(dataType, data)

    Set AddXRec = xrec

End Function
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 12:06:54 PM
Thanks Michael.  Your code worked, now I'm lost why it does and mine doesn't.  Even if I change my to call out the DictCol variable to be and AcadDictionary like yours does.  Do you/anyone know why?
Title: Re: Setting XRecord
Post by: Jeff_M on July 18, 2006, 12:07:32 PM
Well, Michael beat me to it, but since I was going to post it I still will :-)
Code: [Select]
Option Explicit

Public Sub AddXRec(ByRef XrecName As String, DataType() As Integer, Data() As Variant)

Dim DictCol As AcadDictionaries
Dim MyDict As AcadDictionary
Dim Xrec As AcadXRecord

Set DictCol = ThisDrawing.Dictionaries
Set MyDict = DictCol.Add("VBAtoLisp")
Set Xrec = MyDict.AddXRecord(XrecName)
Xrec.SetXRecordData DataType, Data


End Sub
Public Sub Main(ByRef Str1 As String, Str2 As String)

Dim DataType(1) As Integer
Dim Data(1) As Variant

Data(0) = Str1
Data(1) = Str2
DataType(0) = 1
DataType(1) = 2

AddXRec "VBAtoLisp", DataType, Data

End Sub

Sub test()
Main "MyString1", "MyString2"
End Sub
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 12:12:59 PM
My code was wrong from the very beginning.  I wrote this line wrong, it could have saved us all a lot of problems, but as it is I learened alot.  Thanks to all.

Code: [Select]
Set DictCol = This.Drawing.Dictionaries
should be (without the period between This Drawing)
Code: [Select]
Set DictCol = ThisDrawing.Dictionaries
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 12:16:07 PM
That wasn't the only problem now that I test it.  I guess you need to be as specific when declaring what the type of variables you are using, like you guys had.

Thanks again.
Title: Re: Setting XRecord
Post by: DaveW on July 18, 2006, 01:07:23 PM
Just as an FYI.

My understanding is that you can also use xrecord at the entity level, not just the drawing level. I am not sure how it is done, and I hear few use it, but it can be good way to get around the size limitations of xdata and assists in hiding data. I am not familar with the code that does this or I would have shared it.
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 01:10:26 PM
Just as an FYI.

My understanding is that you can also use xrecord at the entity level, not just the drawing level. I am not sure how it is done, and I hear few use it, but it can be good way to get around the size limitations of xdata and assists in hiding data. I am not familar with the code that does this or I would have shared it.
I have done one code (lisp) that uses the entity's dictionary.  I used the GetExtentionDictionary method on the object.
Title: Re: Setting XRecord
Post by: MP on July 18, 2006, 01:15:26 PM
I have done one code (lisp) that uses the entity's dictionary.  I used the GetExtentionDictionary method on the object.

Yep / Agreed, and there are other objects (i.e. non graphical) you can use as xdictionary hosts as well.

An aside, if you want the xdictionary to survive purging / wblocking et al. make it hard owned, as noted here (http://www.theswamp.org/index.php?topic=2646.msg33944#msg33944) (sorry, written in LISP).

:)
Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 01:28:42 PM
This might be the problem:
Quote
Set DictCol = This.Drawing.Dictionaries

should be

Code: [Select]
Set DictCol = ThisDrawing.Dictionaries



That was one, but others were found after reading how the others coded theirs.  Thanks, you should have chimed in sooner.   :wink: :roll:


(no offsense intended)
Title: Re: Setting XRecord
Post by: Chuck Gabriel on July 18, 2006, 01:37:05 PM
This might be the problem:
Quote
Set DictCol = This.Drawing.Dictionaries

should be

Code: [Select]
Set DictCol = ThisDrawing.Dictionaries



That was one, but others were found after reading how the others coded theirs.  Thanks, you should have chimed in sooner.   :wink: :roll:


(no offsense intended)

You were too fast for me. :-)  I posted that, not realizing the discussion continued beyond page one, and then deleted it about 15 seconds later (which appears to have been about 14 seconds too late).

Title: Re: Setting XRecord
Post by: T.Willey on July 18, 2006, 01:48:42 PM
This might be the problem:
Quote
Set DictCol = This.Drawing.Dictionaries

should be

Code: [Select]
Set DictCol = ThisDrawing.Dictionaries



That was one, but others were found after reading how the others coded theirs.  Thanks, you should have chimed in sooner.   :wink: :roll:


(no offsense intended)

You were too fast for me. :-)  I posted that, not realizing the discussion continued beyond page one, and then deleted it about 15 seconds later (which appears to have been about 14 seconds too late).


If it's a thread I start, I try to keep an eye on it, :wink: and as I'm just learning, all post are welcomed.