Set user text using C# component

Hi, I’m getting a wierd error using the Elefront component, so I’m trying to use the C# component instead just to update user text on some referenced curve objects. Here’s the code inside the C# component, and it doesn’t seem to be working. I get neither of the error messages I’ve coded, but the attributes don’t update.

private void RunScript(bool act, Curve crv, List<string> keys, List<string> values)
  {
    if(act){
      if (keys.Count != values.Count) {
        Print("Number of keys must equal number values.");
        return;
      }
      for(int i = 0; i < keys.Count; i++) {
        if(!crv.SetUserString(keys[i], values[i])){
          Print("User text update failed.");
          return;
        }
      }
    }
  }

This metadata can be added to various places, the geometry itself, its document attributes associated with a geometry, or its RhinoObject… You’re probably assigning it, but the method you use to check it looks in another one of these places. Maybe that is your problem.

Dani, thanks for the reply. The method I’m using to check it is simply Rhino’s built-in “Attributes User Text” pane of the Properties panel.

I’m not quite sure because your question is quite ambiguous, but I guess you have to take the Rhino object reference and assign the metadata to its attributes, rather than directly to the geometry as that script does.
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_ObjectAttributes_SetUserString.htm

1 Like

Good point, thanks! The following code worked. I don’t know if I’m using best practices to get to the ObjectAttributes from a piece of referenced geometry, but it does work. Now if I could just get the operation to register in the Undo stack, like the Elefront component does …
Thanks!
Carl

  private void RunScript(bool act, Guid guid, List<string> keys, List<string> values)
  {
    if(act){
      if (keys.Count != values.Count) {
        Print("Number of keys must equal number values.");
        return;
      }
      var obj = doc.Objects.Find(guid);
      if(obj != null){
        for(int i = 0; i < keys.Count; i++) {
          if(!obj.Attributes.SetUserString(keys[i], values[i])){
            Print("User text update failed.");
            return;
          }
        }
      }
    }
  }