TheSwamp
Code Red => VB(A) => Topic started by: T.Willey on July 17, 2006, 06:41:39 PM
-
Why won't this work?
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: ="
-
Lose the parenthesis
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
-
Rule of thumb for most things VBA, if you are on the right side of an equals such as
Set Xrec = MyDict.AddXRecord(XrecName)
you use the parethesis, if not, you don't.
-
Thank you very much Bob.
-
Not a problem.
-
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)
-
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.
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?
-
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
-
Thanks Bob. I will have to pick this up tomorrow, it's time for me to go home, and no Cad there.
-
Time for me to go as well. We'll play some more tommorrow.
-
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:
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
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:
Dim DataType(1) As Integer
Dim Data(1) As Variant
Data(0) = Str1
Data(1) = Str2
DataType(0) = 1
DataType(1) = 2
HTH
-
Thanks Jeff. That's what I get for answering quickly instead of paying attention.
-
Thanks Jeff and Bob. Now I'm erroring on a different part, and I don't understand why. Here is the code.
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.
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.]
-
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
-
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.
-
Alternative ...
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
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.
-
You could even go as concise as this, tho I probably would use the previous variant --
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
-
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?
-
Well, Michael beat me to it, but since I was going to post it I still will :-)
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
-
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.
Set DictCol = This.Drawing.Dictionaries
should be (without the period between This Drawing)
Set DictCol = ThisDrawing.Dictionaries
-
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.
-
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.
-
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.
-
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).
:)
-
This might be the problem:
Set DictCol = This.Drawing.Dictionaries
should be
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)
-
This might be the problem:
Set DictCol = This.Drawing.Dictionaries
should be
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).
-
This might be the problem:
Set DictCol = This.Drawing.Dictionaries
should be
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.