.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Does Document.UserData persist over Acad sessions?

1 REPLY 1
Reply
Message 1 of 2
norman.yuan
641 Views, 1 Reply

Does Document.UserData persist over Acad sessions?

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
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: norman.yuan

Document.UserData isn't persistent.

You would need to persist it yourself in a dictionary or something.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6267790@discussion.autodesk.com...
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost