I am in a project development that may want to have a few pieces of data embeded in a drawing for later process. I looked at Document.UserData and though it meets my need well with its ease of use.
I kew there was artical from Kean Walmsley on this topic. However, his sample code was written to run within the same Acad session.
There is no document on whether the UserData persists over Acad sessions. So, I wrote a few line of testing code to see if the UserData survives Acad session. The result is bad: the UserData does not live through Acad session.
Can anyone confirm that Document.UserData is only useful within the same Acad session?
Here is my code:
{code}
''Simplified data class to be embeded in Document.UserData
Public Class MyData
Public IntData As Integer = 3
Public StrData As String = "ABC"
Public BolData As Boolean = True
Public DatData As DateTime = DateTime.Today
End Class
Public Class MyCommand
Private Const DATA_KEY As String = "MY_DATA"
_
Public Shared Sub SetDocData()
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim store As Hashtable = dwg.UserData
If store.ContainsKey(DATA_KEY) Then
store.Remove(DATA_KEY)
End If
store.Add(DATA_KEY, New MyData())
dwg.Editor.WriteMessage(vbCrLf & "Data has been saved with key """ & DATA_KEY & """.")
End Sub
_
Public Shared Sub GetData()
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = dwg.Editor
Dim store As Hashtable = dwg.UserData
If store.ContainsKey(DATA_KEY) Then
Dim data As MyData = TryCast(store(DATA_KEY), MyData)
If data IsNot Nothing Then
ed.WriteMessage(vbCrLf & "Data stored in this drawing has been found!")
Else
ed.WriteMessage(vbCrLf & "Data stored in this drawing is not type of MyData!")
End If
Else
ed.WriteMessage(vbCrLf & "No data was saved in this document!")
End If
End Sub
End Class
{code}
That is, if I run the command "SetData" and then run "GetData" right after, the embeded UserData was found.
However, if I run command "SetData", save and close the drawing, and then open it again, run command "GetData", the UserData was not there.
Obviously, the Docuement.UserData does not stay over Acad sessions, I'd use other means, such as Dictionary/XData...
Edited by: norman.yuan on Oct 7, 2009 6:50 PM