It took me a while to pinpoint, but this command reproduces the problem. In short, calling CommitChanges()
on a CustomMeshObject
reverts it to a normal MeshObject
. I would very much like it to keep on being CustomMeshObject
protected Result RunCommand(RhinoDoc doc, RunMode mode)
{
RhinoApp.WriteLine("Run this command in Shaded mode!");
Mesh m = Mesh.CreateFromBox(new BoundingBox(Point3d.Origin, new Point3d(10, 10, 10)), 2, 2, 2);
MyMeshObject mesObj = new MyMeshObject(m);
doc.Objects.AddRhinoObject(mesObj);
Guid id = mesObj.Id;
RhinoObject obj = doc.Objects.Find(id);
RhinoApp.WriteLine("Type of OBJ: {0}", obj.GetType().Name); // MyMeshObject
doc.Views.Redraw();
string str = String.Empty;
RhinoGet.GetString("ENTER to continue - the mesh should be gray-ish and show red bounding box", true, ref str);
m = obj.Geometry as Mesh;
if (null != m)
{
Color[] newColors = new Color[m.Vertices.Count];
for (int i = 0; i < newColors.Length; ++i)
newColors[i] = Color.Violet;
m.VertexColors.SetColors(newColors);
}
obj.CommitChanges(); // this is vital to propagate the color change, but reverts the custom object to a normal mesh object
doc.Views.Redraw();
str = String.Empty;
// the mesh is violet, but the bounding box is not drawn. In fact, it is no longer a MyMeshObject.
RhinoGet.GetString("ENTER to continue - the mesh should be violet and show red bounding box", true, ref str);
obj = doc.Objects.Find(id);
RhinoApp.WriteLine("Type of OBJ: {0}", obj.GetType().Name); // MeshObject <== why is this not MyMeshObject?
return Result.Success;
}
public class MyMeshObject : CustomMeshObject
{
private BoundingBox _bb;
public MyMeshObject(Mesh m)
: base(m)
{
if (null != m)
_bb = m.GetBoundingBox(true);
}
public MyMeshObject()
{
// required
}
protected override void OnDuplicate(RhinoObject source)
{
MyMeshObject other = source as MyMeshObject;
if (null != other)
{
_bb = other._bb;
}
base.OnDuplicate(source);
}
protected override void OnDraw(DrawEventArgs e)
{
if (_bb.IsValid)
e.Display.DrawBox(_bb, Color.Red, 2);
base.OnDraw(e);
}
}