Writing and Reading Document User Data, Rhinocommon

I have found the example for reading and writing object user data helpful: http://wiki.mcneel.com/developer/rhinocommonsamples/userdata
(I used the cs_userdataCommand command)

But what I need to read and write isn’t attached to any particular object. So, if I understand correctly, I need to work with the Document User Data. Is there an example for this somewhere? Something similar to the cs_userdataCommand but for document rather than object data?

Thanks,
Sam

You’re probably interested I this https://github.com/dalefugier/SampleCsDocumentUserData

I was able to get Dales c# sample working. The harder part for me is converting to VB.net and adding it to my project and then understanding it to the point that I can tailor it for my own needs.

For each of the two namespaces, I used a converter to get the VB.net equivalent, then added a class to my project and copied in the converted code. Is this the proper way to do it? Or should I be adding a Rhinocommon command and then cutting and pasting into the command? On a side question, I no longer seem to have the option of adding rhinocommon commands, how do I get that back?

I then get an error for the OnLoad function for the following lines:
Rhino.RhinoDoc.NewDocument += New EventHandler(Of DocumentEventArgs)(AddressOf RhinoDoc_NewDocument)
Rhino.RhinoDoc.BeginOpenDocument += New EventHandler(Of DocumentOpenEventArgs)(AddressOf RhinoDoc_BeginOpenDocument)
Did they not convert to VB.net properly or am I missing something?

In truth, I am a bit overwhelmed with this project as there is lots of stuff going on I don’t get. Perhaps there is a dumbed down version with just the essentials to understand reading and writing plugin data to 3DM files?

Just to let you know where I’m heading with this, I want to save a bunch of doubles to 3DM files, perhaps a couple hundred, and will likely want to use a dictionary to do it.

Thanks!
Sam

Too add handdlers you need to do it like this:

            AddHandler Rhino.RhinoDoc.NewDocument, AddressOf  RhinoDoc_NewDocument
            AddHandler Rhino.RhinoDoc.BeginOpenDocument, AddressOf RhinoDoc_BeginOpenDocument

And with DocumentuserData

I think you mean this:

RhinoDoc.ActiveDoc.Strings.SetString("Key", Value)
RhinoDoc.ActiveDoc.Strings.GetValue("Key")

Thanks, that simple method is something my feeble mind can understand :slight_smile:

I would like to call the setstring function when the document saves and the getvalue when it loads. What is the simplest and most straightforward way I can do this? I think Dale’s example was doing this, but once again, over my head…

Thanks,
Sam


'Value defined somehwere else

Protected Overrides Function OnLoad(ByRef errorMessage As String) As Rhino.PlugIns.LoadReturnCode
            AddHandler Rhino.RhinoDoc.BeginSaveDocument, AddressOf BeforeRhinoCloses
            AddHandler Rhino.RhinoDoc.EndOpenDocument, AddressOf EndOpenDocumentRhino
            Return Rhino.PlugIns.LoadReturnCode.Success
End Function

Private Sub BeforeRhinoCloses(ByVal sender As Object, ByVal e As Rhino.DocumentEventArgs)
     RhinoDoc.ActiveDoc.Strings.SetString("Key", Value)
End Sub

Private Sub EndOpenDocumentRhino(ByVal sender As Object, ByVal e As Rhino.DocumentEventArgs)
    Value = RhinoDoc.ActiveDoc.Strings.GetValue("Key")
End Sub

I think something like this

but your plugin should run before this works. So it will starting to get the info after the first command is triggered. Its also possible to force rhino to load the plugin when rhino opens.

2nd but. This is an “easy” way out. The one from Dale is a “deeper” code that will write a string table as document data.

I tried adding the code to my plugin class, but for some reason my plugin doesnt load anymore when I start rhino. I was having the same problem when I was trying to get a VB version of Dales sample working. Any idea what the problem is? Below is the code.

Thanks,
Sam

Namespace PodoCAD
    '''<summary>
    ''' <para>Every RhinoCommon .rhp assembly must have one and only one PlugIn-derived
    ''' class. DO NOT create instances of this class yourself. It is the
    ''' responsibility of Rhino to create an instance of this class.</para>
    ''' <para>To complete plug-in information, please also see all PlugInDescription
    ''' attributes in AssemblyInfo.vb (you might need to click "Project" ->
    ''' "Show All Files" to see it in the "Solution Explorer" window).</para>
    '''</summary>
    Public Class PodoCADPlugIn
        Inherits Rhino.PlugIns.PlugIn

        'Shared _instance As SampleVBPanelPlugIn

        Public Sub New()
            Instance = Me
        End Sub

        ''' <summary>
        ''' The only instance of this plug-in.
        ''' </summary>
        Public Shared Property Instance() As PodoCADPlugIn
            Get
                Return m_Instance
            End Get
            Private Set(ByVal value As PodoCADPlugIn)
                m_Instance = value
            End Set
        End Property
        Private Shared m_Instance As PodoCADPlugIn


        ' You can override methods here to change the plug-in behavior on
        ' loading and shut down, add options pages to the Rhino _Option command
        ' and mantain plug-in wide options in a document.

        
        Private Sub BeforeRhinoCloses(ByVal sender As Object, ByVal e As Rhino.DocumentEventArgs)
            Rhino.RhinoDoc.ActiveDoc.Strings.SetString("Key", 10)
        End Sub

        Private Sub EndOpenDocumentRhino(ByVal sender As Object, ByVal e As Rhino.DocumentEventArgs)
            Rhino.RhinoApp.WriteLine(Rhino.RhinoDoc.ActiveDoc.Strings.GetValue("Key"))
        End Sub


        ''' <summary>
        ''' Is called when the plug-in is being loaded.
        ''' </summary>
        Protected Overrides Function OnLoad(ByRef errorMessage As String) As Rhino.PlugIns.LoadReturnCode
            AddHandler Rhino.RhinoDoc.BeginSaveDocument, AddressOf BeforeRhinoCloses
            AddHandler Rhino.RhinoDoc.EndOpenDocument, AddressOf EndOpenDocumentRhino

            Dim panelType As System.Type = GetType(PodoCADPanel)
            Rhino.UI.Panels.RegisterPanel(Me, panelType, "Sample", System.Drawing.SystemIcons.Question)
            Return Rhino.PlugIns.LoadReturnCode.Success
        End Function

        ''' <summary>
        ''' The tabbed dockbar user control
        ''' </summary>
        Public Property UserControl() As PodoCADPanel
            Get
                Return m_UserControl
            End Get
            Set(ByVal value As PodoCADPanel)
                m_UserControl = value
            End Set
        End Property
        Private m_UserControl As PodoCADPanel



    End Class
End Namespace

Scratch that, my mistake, it works just fine!

I think that’s all for this topic. I have another question that is somewhat related to this, but different enough that I’ll ask it in a separate thread.

Thanks for all your help, Jordy, Minno.

Sam