Changing object properties in external files from plugin


(Luis Fraguada) #1

Hello. If I am trying to edit the text of a TextObject in a document, I can do this:

Rhino.DocObjects.ObjRef tobj = new Rhino.DocObjects.ObjRef(_id);
Rhino.DocObjects.TextObject text = tobj.Object() as Rhino.DocObjects.TextObject;
text.TextGeometry.Text = "hello world";
text.CommitChanges();

I’d like to do the same for a text object in an external file, but I am not finding the proper route to do so. I can create a new object in an external file with the new text, but it would be much nicer to be able to use the procedure above (with CommitChanges() I can keep the same Guid of the object). Is there any way to achieve the above procedure for objects in other files without deleting them and adding them again with different attributes? This obviously does not work, as .CommitChanges() return false:

string path = "string to external file url";
Rhino.FileIO.File3dm extFile = Rhino.FileIO.File3dm.Read(path);

foreach (Rhino.FileIO.File3dmObject f3obj in extFile.Objects)
    {
      switch (f3obj.Geometry.ObjectType)
      {
        case  Rhino.DocObjects.ObjectType.Annotation:
          Print(f3obj.Geometry.ObjectType.ToString());
          Rhino.DocObjects.ObjRef aObj = new Rhino.DocObjects.ObjRef(f3obj.Attributes.ObjectId);
          Rhino.DocObjects.TextObject textObj = aObj.Object() as Rhino.DocObjects.TextObject;
          textObj.TextGeometry.Text = "some new text";
          textObj.CommitChanges();
          break;
        default:
          Print(f3obj.Geometry.ObjectType.ToString());
          break;
      }
    }

Change text dot's text programmatically