When I write a File3dm that has geometry in it with content in its UserDictionary, this content is not available when that file is subsequently opened in Rhino.
To reproduce follow the following two steps:
- draw a plane or other surface
- run the first command as below. This will save test.3dm on your desktop
- open the written test.3dm file
- run the second command on the geometry in the opened file. It will show the user dictionary to be empty.
Please let me know if this is by design (I suspect it is not?) and how I can write into the UserDictionary such that it is available in Rhino.
Rhino 5SR7 by the way.
Command to write a file with an object with user dictionary info.
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
ObjRef srfRef;
Result res = RhinoGet.GetOneObject("Select surface to write", false, ObjectType.Surface, out srfRef);
if (res != Result.Success) return res;
Surface s = srfRef.Surface();
s.UserDictionary.Set("MY_KEY", "MY STRING CONTENT");
File3dm f = new File3dm();
f.Polish();
Guid g = f.Objects.AddSurface(s.ToNurbsSurface());
File3dmObject obj = f.Objects.FirstOrDefault(o => o.Attributes.ObjectId == g);
if (null == obj)
{
RhinoApp.WriteLine("Object not found.");
return Result.Failure;
}
String found = obj.Geometry.UserDictionary.GetString("MY_KEY", "NOT FOUND");
RhinoApp.WriteLine(found+" was found in the user dictionary");
if (found == "NOT FOUND")
{
RhinoApp.WriteLine("User dictionary content not taken from geometry. Setting it directly on the object.");
obj.Geometry.UserDictionary.Set("MY_KEY", "MY STRING CONTENT");
String found2 = obj.Geometry.UserDictionary.GetString("MY_KEY", "NOT FOUND");
RhinoApp.WriteLine(found2 + " was found in the user dictionary");
}
// put the written file on the desktop for easy reference
String tmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.3dm");
bool written = f.Write(tmp, 5);
if (!written)
{
RhinoApp.WriteLine("Unable to write file");
return Result.Failure;
}
File3dm read = File3dm.Read(tmp);
File3dmObject obj2 = read.Objects.FirstOrDefault(o => o.Attributes.ObjectId == g);
if (null == obj2)
{
RhinoApp.WriteLine("Object not found in written file");
return Result.Failure;
}
String found3 = obj2.Geometry.UserDictionary.GetString("MY_KEY", "NOT FOUND");
RhinoApp.WriteLine(found3 + " was found in the read user dictionary");
// so far so good. The content is in the written object's user dictionary.
return Result.Success;
}
The user dictionary can be inspected with this command
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
ObjRef objRef;
RhinoGet.GetOneObject("Select object", true, ObjectType.AnyObject, out objRef);
if (null == objRef)
return Result.Failure;
RhinoObject obj = objRef.Object();
GeometryBase geo = obj.Geometry;
if (null == geo)
return Result.Failure;
RhinoApp.WriteLine("GUID: {0}",objRef.ObjectId);
foreach (string key in geo.UserDictionary.Keys)
{
RhinoApp.WriteLine(key+" => {0}",geo.UserDictionary[key]);
}
if (geo.UserDictionary.Count == 0)
RhinoApp.WriteLine("Empty");
return Result.Success;
}
When run on the content of the written file, you will find there is no content in the object’s user dictionary.