Objects User Text (User String)

Hi everyone, I’m tring to read and write user text in CSharp , I searched a lot but there’s no easy explanation about…
I would like to know in which namespace and which exactly method shoud I call to use it… The final goal should be have two list or a dictionary with keys and values…
With user text I mean this:
Attributes

Hello - in RhinoCommon, this may help - is that what you mean?
https://developer.rhino3d.com/api/RhinoCommon/search.html?SearchText=Usertext

-Pascal

Thank you Pascal for the answer, but no, these are text field I’ve already checked, I’m looking for user text

Hi @sartoriedo,

This from the the SDK samples:

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      var go = new GetObject();
      go.SetCommandPrompt("Select objects");
      go.GroupSelect = true;
      go.GetMultiple(1, 0);
      if (go.CommandResult() != Result.Success)
        return go.CommandResult();

      for (var i = 0; i < go.ObjectCount; i++)
      {
        var obj_ref = go.Object(i);
        var obj = obj_ref.Object();
        if (null != obj)
        {
          var value = obj.Attributes.GetUserString(SampleCsUserStringData.Key);
          if (!string.IsNullOrEmpty(value))
            RhinoApp.WriteLine(string.Format("<{0}> {1}", SampleCsUserStringData.Key, value));
        }
      }

      return Result.Success;
    }

GitHub - mcneel/rhino-developer-samples: Rhino and Grasshopper developer sample code

– Dale

Hi Dale,thanks for your answer
I saw it, but I was looking for something more direct, this example is not very understandable for who’s approaching to Rhino’s libraries…
There’s any direct method to get ALL key and values usertext?
For example why have I to declare a new class like SampleCsUserStringData? No methods returns list of keys and values?
Kindly, Edoardo

I’d take a moment to look over the ObjectAttributes class for its available methods. You can retrieve all of an objects user text strings as a NameValueCollection.

@Trav Thanks, I saw it but cannot get any values, I’m doing this:

public static bool GetUserID ()
        {
            var getobj = new GetObject();
            getobj.SetCommandPrompt("Select the object");
            getobj.EnablePreSelect(false, false);
            getobj.Get();
            if (getobj.CommandResult() != Result.Success)
                return false;

            var objt = getobj.Object(0).Brep();
            if (objt == null)
                return false;
            
            NameValueCollection usertexts = new NameValueCollection();
            usertexts = objt.GetUserStrings();
            
            return true;
        }

What am I doing wrong?

I find out a way and I will write it here for new users approaching:

public static bool GetUserID ()
        {
            
            ObjRef objref;
            var rc = RhinoGet.GetOneObject("Select the object", true,ObjectType.AnyObject,out objref);
            if (rc!= Result.Success || null == objref)
                return false;
            var obj = objref.Object();
            if (obj == null)
                return false;
            //With this I get all usertext
            var userdata = obj.Attributes.GetUserStrings();
            //Here how to access them
            var volume = userdata.GetValues(0)[0];
                       
            return true;
        }

Anyway I’m still curious to know how to properly use NameValueCollection so please if you can anyway tell me what’s wrong in my previous code or if you have an example that explain and shows

You can learn more about NameValueCollection from Microsoft’s website.

I also have an example project here that’s all about user text that has everything involved in reading/writing user text from objects.

Hi @sartoriedo,

Try this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject();
  go.SetCommandPrompt("Select objects");
  go.GroupSelect = true;
  go.GetMultiple(1, 0);
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  foreach (var objRef in go.Objects())
  {
    var rhObj = objRef.Object();
    if (null != rhObj)
    {
      var userStrings = rhObj.Attributes.GetUserStrings();
      if (null != userStrings)
      {
        foreach (var key in userStrings.AllKeys)
          RhinoApp.WriteLine("{0}: {1}", key, userStrings[key]);
      }
    }
  }

  return Result.Success;
}

– Dale

Thanks you all, at the end I learned how properly use NameValueCollection and I created a function to obtain a dictionary and its override to get a named collection… I share it here for someone else who need:

///This first function give back a dictionary
public static bool GetUserID(out Dictionary<string, string> Usertextdic)
        {
          //Initialize the dict
            Usertextdic = new Dictionary<string, string>();

            ObjRef objref;
            var rc = RhinoGet.GetOneObject("Select the object", true, ObjectType.AnyObject, out objref);
            if (rc != Result.Success || null == objref)
                return false;
            var obj = objref.Object();
            if (obj == null)
                return false;
            var userdata = obj.Attributes.GetUserStrings();
            if (userdata == null)
                return false;

            foreach (var userd in userdata.AllKeys)
            {
                Usertextdic.Add(userd, userdata[userd]);
            }
            return true;
        }

        public static bool GetUserID(out NameValueCollection Usertextncol)
        {
            Usertextncol = new NameValueCollection();

            ObjRef objref;
            var rc = RhinoGet.GetOneObject("Select the object", true, ObjectType.AnyObject, out objref);
            if (rc != Result.Success || null == objref)
                return false;
            var obj = objref.Object();
            if (obj == null)
                return false;

            Usertextncol = obj.Attributes.GetUserStrings();
            return true;
        }
1 Like