How to save/restore Instance w/ UserData to/from File3dm?

I want to define a block( a brep and use it as door type) and save to a .3dm file by AllInstanceDefinition,
later I will import the .3dm file, import its instance to doc.InstanceDefinition.
Then I can insert a block( draw a door), the brep is ok, but userdata lost. So how can I retain userdata.

Chen manhong

//////////// My Code /////////////////
public class DoorTypeUserData : UserData
    OnDupliate( )...
    Read( )...
    Write( )...
    ShouldWrite = true;

first save to File3dm:

      var geometry = new List<GeometryBase>();
      brep1 = Brep.CreateFromBox(box_corners);
      var ud1 = new DoorTypeUserData { is2D = false, Notes = "brep1-ud" };

      model = new File3dm();
      model.AllInstanceDefinitions.Add(idef_name, string.Empty, Point3d.Origin, geometry);
      model.Write("TestFile.3dm", 7);

second Import:

      var script = string.Format("_-Import \"{0}\" _Enter", "TestFile.3dm");
      RhinoApp.RunScript(script, true);

Insert block:

      var ins = doc.InstanceDefinitions[0];
      var obj = ins.Object(0); // it is ok
      var ud = obj.Geometry.UserData;  //ud always null ?????
// var ud = obj.Geometry.UserData.Find(typeof(DoorTypeUserData)) as DoorTypeUserData;
      e.Display.DrawObject(ins.Object(0), Transform.Identity); // it is ok

Hi @chen_manhong,

Rather than using File3dm, try this:

1.) Add your instance definition, including any user data, to the document InstanceDefinitionTable.

2.) Export your instance definition by scripting the _-BlockManager _Export command. Make sure the set SavePlugInData=Yes.

Does this help?

– Dale