Site Tools


How to: Document Data Commands in a Rhino.NET Plug-in.

.NET

Demonstrates how to use RhinoScript's Document Data commands inside a Rhino.NET plug-in in Rhino 4

For information on using RhinoScript from a Rhino.NET plug-in see RhinoScriptFromDotNet.

There are some prerequisite steps there (specifically creating a COM Interop Wrapper for RhinoScript).

C#

class RhinoDocumentData
  {
      RhinoScript4.IRhinoScript rhScript;
 
      private string _appName;
      /// <summary>
      /// Name of the application
      /// A piece of data is stored under an AppName and a Key
      /// </summary>
      public string AppName
      {
          get { return _appName; }
          set { _appName = value; }
      }
 
      /// <summary>
      /// Constructor - starts [[developer:rhinoscript|RhinoScript]] and sets the AppName
      /// </summary>
      public RhinoDocumentData()
      {
          AppName = "MyApplication";
          this.StartRhinoScript();
      }
 
      /// <summary>
      /// Gets a connection to [[developer:rhinoscript|RhinoScript]]
      /// </summary>
      /// <returns></returns>
      private bool StartRhinoScript()
      {
          object obj = RhUtil.RhinoApp().GetRhinoScriptInterface();
          rhScript = (RhinoScript4.IRhinoScript)obj;
          if (rhScript == null)
          {
              return false;
          }
          return true;
 
      }
 
      /// <summary>
      /// Gets data associated with a Key.  The AppName is stored as this.AppName
      /// </summary>
      /// <param name="Key">The key</param>
      /// <returns>Teh string value associated with the key</returns>
      public string GetDocumentData(string Key)
      {
          object obj = rhScript.GetDocumentData(this.AppName, Key);
          //If obj is null then the AppName-Key combo was not found
          if (obj == null)
              return ;
          else
              return (string)obj;
      }
 
      /// <summary>
      /// Attempts to get data associated with the Key.  The AppName is stored as this.AppName
      /// </summary>
      /// <param name="Key">The Key</param>
      /// <param name="val">The value</param>
      /// <returns>Returns true is successful, false is the key is not found</returns>
      public bool TryGetDocumentData(string Key,ref string val)
      {
 
          object obj = rhScript.GetDocumentData(AppName, Key);
          //If obj is null then the AppName-Key combo was not found
          if (obj == null)
          {
              return false;
          }
          else
          {
              val = (string)obj;
              return true;
          }
      }
 
      /// <summary>
      /// Writes data to the Rhino document
      /// </summary>
      /// <param name="Key">The key</param>
      /// <param name="Val">The value</param>
      /// <returns>True if successful</returns>
      public bool SetDocumentData(string Key, string Val)
      {
          object obj = rhScript.SetDocumentData(AppName, Key, Val);
          if (obj == null)
              return false;
          else
              return true;
 
      }
  }

Using the Class

//write some data
RhinoDocumentData rdd = new RhinoDocumentData();
rdd.SetDocumentData("MyString", "Some Text");
double myNumber = 200.0;
rdd.SetDocumentData("MyNumber", myNumber.ToString());
 
//read some data
bool gotData = false
string myString = ;
gotData = rdd.TryGetDocumentData("MyString", ref myString);
string strMyNumber = ;
gotData = rdd.TryGetDocumentData("MyNumber", ref strMyNumber)
if (gotData) myNumber = double.Parse(strMyNumber);

Recommendations

To keep from having to maintain a lot of Document Data write and read statements, you could give your classes ToString() and Parse() methods that handle all of the data you want to store in the Rhino document. Then you would only need one write statement (using the ToString() method) and one read statement, after which you could use your class's Parse method to get the data from the string you read.

developer/sdksamples/documentdatafromdotnet.txt ยท Last modified: 2016/03/22 by sandy