Get/Set user text on c#


(Ricardo) #1

How I can get/set user text on c#??

Python sample:
rs.SetDocumentUserText(“var1”,“test”)
a=rs.SetDocumentUserText(“var1”)

Thanks


(Travis Serio) #2

Document text is stored in the RhinoDoc.Strings table
https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_RhinoDoc_Strings.htm

Object Attribute Text is located in a RhinoObjects ObjectAttributes
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_ObjectAttributes_GetUserString.htm

Here’s a link to the original User Text / Doc Text panel project. It should give you plenty of ideas and examples


(Ricardo) #3

Hello @Trav ,

Can you write a simple comand to set an get the user text?

Sorry but , I using python and trying use some C# comands with python, so , the c# is just for Dock panel.

Thanks


(Travis Serio) #4

We have another sample here.


(Ricardo) #5

Sorry , @Trav I can not understand this C # syntax, it must be because I am, very attached to python syntax.

So , I have two C# buttons, and I whant set a user text on doc, and get user text from doc,
How can I do this easily? following this example?

  private void button1_Click(object sender, EventArgs e)
    {

      // equivalent to
     rs.SetDocumentUserText(“var1”,“test”)" 


    }

  private void button2_Click(object sender, EventArgs e)
    {

      // equivalent to

      String a = rs.GetDocumentUserText(“var1”) 
      MessageBox.Show(a.ToString());


    }

Thanks


(Travis Serio) #6

To use RhinoScript from inside of your button you’ll need to add a reference to the RhinoScript.tbl file located in your Rhino 6 \ Plug-ins directory to your project.

You can then add this code to your button and have access to RhinoScript functionality.

        var rhinoscript_object = RhinoApp.GetPlugInObject("RhinoScript");
        if (rhinoscript_object == null) return;
        var rs = (IRhinoScript)rhinoscript_object;

        rs.SetDocumentUserText("Key", "Value");

(Ricardo) #7

@Trav I teste you code:

    private void button1_Click(object sender, EventArgs e)
    {

        
        var rhinoscript_object = RhinoApp.GetPlugInObject("RhinoScript");
        
        if (rhinoscript_object == null) return;
        
        var rs = (IRhinoScript)rhinoscript_object;
        rs.SetDocumentUserText("Key", "Value");

}

“if (rhinoscript_object” return always null, the script stop on “if” line an return

thanks


(Travis Serio) #8

GetPluginObject wont return an object if the plug-in you specify isnt loaded. You need to open the Plug-in Manager and ensure RhinoScript is checked and restart Rhino.


(Ricardo) #9

Suite…, this will allow you to move faster.

I gone create a plugin with a installer (visual studio), there is a away to load this plugin (RhinoScript)
on frist installer?? with no user interference?

Thanks.


(Travis Serio) #10

This plug-in is generally loaded by default unless you’ve unloaded it or you have virus scanner or something blocking it.


(Steve Baer) #11

There’s no need to use RhinoScript for this. If you are trying to perform the C# equivalent of what one of the rhinoscriptsyntax functions does, I would recommend looking at the rhinoscriptsyntax implementation as that just directly calls RhinoCommon. For GetDocumentUserText you can look here

For C#, you would just call
string a = doc.Strings.GetValue("var1");


(Ricardo) #12

Hello @stevebaer, I test you example but there is a error on DOC.

Can be a using lib missing? I already check all this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Rhino;
using Rhino.PlugIns;
using Rhino.UI;
using Rhino.Commands;
using Rhino.Input.Custom;
using Rhino.DocObjects;
using Rhino.Input;
using RhinoScript;
using RhinoWindows;

Thanks


#13

‘doc’ refers to Rhino.RhinoDoc.ActiveDoc, the plugin’s ‘RunCommand’ method provides it as an argument. If your outside that method you’ll need to grab it explicitly like this “var doc = Rhino.RhinoDoc.ActiveDoc;”. Although I’m not sure if this is best practice.

Cheers,
Matt


(Ricardo) #14

Thank you