Crashing when calling Objects.Replace OwnerObject of CustomObjectGrips

I have a class A which is extends CustomMeshObject and another class B extends CustomObjectGrips, and I want recreate new A object when grips moved.

So I override method NewGeometry of class CustomObjectGrips to recreate a new instance of class B which is a custom mesh object, like this:

public class B: CustomObjectGrips
{
        protected override GeometryBase NewGeometry()
        {
            if (GripsMoved && OwnerObject is A old)
            {
                // making a new object
                var obj= new A();
                var doc = RhinoDoc.ActiveDoc;
                doc.Objects.Replace(new ObjRef(old), obj); // this line crash rhino without throw exception
            }
            return null;
        }
}

I guess old object failed to delete, so I try to Hide old object, and everything looks great, but I can’t do that.

public class B: CustomObjectGrips
{
        protected override GeometryBase NewGeometry()
        {
            if (GripsMoved && OwnerObject is A old)
            {
                var doc = RhinoDoc.ActiveDoc;
                if (doc.Objects.Hide(old, true))
                {
                    // making a new object
                    var obj= new A();
                    doc.Objects.AddRhinoObject(obj);
                }
            }
            return null;
        }
}

My questions is:

  1. Is this right way to do ?
  2. how can I make this right ?

I think figure out myself, override OnReset method instead of NewGeometry and put create new instance stuff before base.OnReset call, no crashing

1 Like