File3dmObject Adding User Text and Write to File




I have trouble adding user text consistently to File3dmbObject and write them to a file. For example, the code below tries to add “k” as key and “v” as value to curves and write them to a file. However, some objects in the resulting 3dm file contains user text and some objects do not. I wonder if I missed anything using the File3dm.Write method.

List<LineCurve> lines = new List<LineCurve>();
for(int i = 0;i < 100;i++)
        lines.Add(new LineCurve(new Point3d(i, 0, 0), new Point3d(i, 1, 0)));
string filepath = "C:\\temp\\test-bake-file.3dm";

Rhino.FileIO.File3dm f = new Rhino.FileIO.File3dm();
foreach(Curve l in lines)
   ObjectAttributes att = new ObjectAttributes();
   att.SetUserString("k", "v");
   f.Objects.AddCurve(l, att);
f.Write(filepath, 5);


(Dale Fugier) #2

Hi Alan,

I am able to reproduce this with your code. Looking into it…

– Dale



Thanks for looking into this.


(Dale Fugier) #4

Hi Alan,

It looks like if you store the user text on the geometry, then all is good. For example:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  const string path = @"C:\Users\Dale\Desktop\test-bake-file.3dm";

  var file = new Rhino.FileIO.File3dm();

  var layer = new Layer { Name = "Default", Color = Color.Blue };
  var layer_index = file.Layers.Count - 1;

  var attributes = new ObjectAttributes { LayerIndex = layer_index };

  for (var x = 0; x < 100; x++)
    var line_curve = new LineCurve(new Point3d(x, 0, 0), new Point3d(x, 1, 0));
    if (line_curve.IsValid)
      line_curve.SetUserString("Line", x.ToString());
      file.Objects.AddCurve(line_curve, attributes);

  var rc = file.Write(path, 5);

  return rc ? Result.Success : Result.Failure;

I’m still looking into the issue…

– Dale

(Dale Fugier) #5

Hi Alan,

Your code appears to work in the current Rhino WIP. I’d report this as a bug, but there isn’t going to be another Rhino 5 service release. So for Rhino 5, I suggest attaching your user text to the geometry.

– Dale






Hi Dale-

I’m guessing my issue is similar for Rhino 5 and won’t be addressed, but I’d like to confirm. I am also finding that certain attributes don’t get written to the File3dm, specifically ObjectColor and ColorSource. For example:

Rhino.FileIO.File3dm newFile = new Rhino.FileIO.File3dm();

var att = new ObjectAttributes();

att.Name = "Test";
att.ObjectColor = Color.Red;
att.ColorSource = ObjectColorSource.ColorFromObject;

newFile.Objects.AddCircle(new Circle(5), att);

When I Polish and Write my File3dm, it saves it fine with my object. The object Name attribute is applied, but ObjectColor and ColorSource are not. Adding the same object to the ActiveDoc of the file from which I authoring the new File3dm results in the correct application of object attributes.

Am I missing something, or is this known and being addressed in V6?


(Dale Fugier) #8

Hi @dave_stasiuk,

This seems to work in Rhino 6:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  var file = new File3dm();

  var layer = new Layer
    Name = "Default",
    Color = Color.Black

  var layer_index = file.AllLayers.Count - 1;

  var attributes = new ObjectAttributes
    LayerIndex = layer_index,
    Name = "Test",
    ObjectColor = Color.Red,
    ColorSource = ObjectColorSource.ColorFromObject

  file.Objects.AddCircle(new Circle(5), attributes);

  var sb = new StringBuilder(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

  file.Write(sb.ToString(), 60);

  return Result.Success;

Does this help?

– Dale


Yes, good to know!