DocumentCollectedException on CustomMeshObject after change in Viewport Display

I’m getting a “DocumentCollectedException” when I try to modify (in this case, apply a transform) my CustomMeshObject after a change in the Display mode.

That is, the class works great when I create the object in Wireframe mode with a single viewport. After a transforming it a couple times, when I switch to Shaded mode and try to transform the object, I get the error (“this object is controlled by the document, etc”).

Strangely, if I create the object with multiple viewports, it all works (so far). Also, starting in Shaded mode and then switching to wireframe mode gives the same error.

Could this be something to do with not creating the render meshes properly?

Here’s a barebones version of the code. Uncommenting the Transform line in Main Loop should produce the error.

[System.Runtime.InteropServices.Guid("2EE69310-EECB-41AF-AE70-59465DFF3F0F")]
    public class CustomMeshProblem : Command
    {
        static CustomMeshProblem _instance;
        public DispatcherTimer Timer;
        public RhinoDoc doc;
        public MyCustomMesh cMesh;

        public CustomMeshProblem()
        {
            _instance = this;
        }

        ///<summary>The only instance of the CustomMeshProblem command.</summary>
        public static CustomMeshProblem Instance
        {
            get { return _instance; }
        }

        public override string EnglishName
        {
            get { return "CustomMeshProblem"; }
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {

            this.doc = doc;

            //create a custom mesh obj
            cMesh = new MyCustomMesh();
            doc.Objects.AddRhinoObject(cMesh);

            // Hook up the Elapsed event for the timer.
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(50);
            Timer.Tick += new EventHandler(MainLoop);
            Timer.Start();
            Timer.IsEnabled = true;

            RhinoApp.WriteLine("Initialized the counters");

            //Hook up escape key handler
            RhinoApp.EscapeKeyPressed += new EventHandler(OnEscEvent);
            Global.readyToSimulate = true;

            return Result.Success;
        }



        private void OnEscEvent(object source, EventArgs e)
        {
            if (Timer.IsEnabled)
                Timer.IsEnabled = false;
            else
                Timer.IsEnabled = true;

        }

        private void MainLoop(object source, EventArgs arg)
        {

            Transform xform = Transform.Translation(new Vector3d(0, 0, -0.1));
            //doc.Objects.Transform(cMesh.Id, xform, true);

            doc.Views.Redraw();
        }
    }

    public class MyCustomMesh : Rhino.DocObjects.Custom.CustomMeshObject
    {

        public MyCustomMesh()
        {
            var p = new Point3d(5, 5, 5);
            var box = new Box(Plane.WorldXY, new List<Point3d>{p, -p});
            var mesh = Mesh.CreateFromBox(box, 1, 1, 1);
            this.SetMesh(mesh);
        }

        protected override void OnDraw(Rhino.Display.DrawEventArgs e)
        {
            base.OnDraw(e);
        }

        protected override void OnAddToDocument(RhinoDoc doc)
        {
            base.OnAddToDocument(doc);
        }

        protected override void OnDuplicate(Rhino.DocObjects.RhinoObject source)
        {
            var obj = (MyCustomMesh)source;
            base.OnDuplicate(source);
        }

        protected override void OnTransform(Transform transform)
        {
            base.OnTransform(transform);
        }

        protected override void OnPicked(Rhino.Input.Custom.PickContext context, System.Collections.Generic.IEnumerable<Rhino.DocObjects.ObjRef> pickedItems)
        {
            base.OnPicked(context, pickedItems);
        }

        protected override System.Collections.Generic.IEnumerable<Rhino.DocObjects.ObjRef> OnPick(Rhino.Input.Custom.PickContext context)
        {
            return base.OnPick(context);
        }

        protected override void OnDeleteFromDocument(RhinoDoc doc)
        {
            base.OnDeleteFromDocument(doc);
        }
    }

You might want to try the SR10 release candidate. I fixed several related issues for custom objects in there.

I tried that - same error.