Rhinocommon History on Brep

Hi all - Im attempting to create History for a brep solely for the purpose of getting a warning when editing the brep, because in a later Command the original object is being referenced by other objects as a child via GUID in User Text.
I have attempted to create a HistoryRecord and add the HistoryRecord instance to a duplicate of the original Brep with the aim of the deleting the original object and ending up with an identical Brep, but with History enabled. Here’s the method:

    public static List<Guid> CollectBrepsWithHistory(GetObject go, RhinoDoc doc, Command commandInstance)
    {
        var newBrepGuids = new List<Guid>();

        for (int i = 0; i < go.ObjectCount; i++)
        {
            Rhino.DocObjects.ObjRef objRef = go.Object(i);
            Brep brep = objRef.Brep();
            if (brep == null)
            {
                RhinoApp.WriteLine("Object is not a valid Brep. Skipping.");
                continue;
            }

            Guid originalId = objRef.ObjectId;

            Brep dupBrep = brep.DuplicateBrep();
            if (dupBrep == null)
            {
                RhinoApp.WriteLine("Failed to duplicate Brep. Skipping.");
                continue;
            }

            HistoryRecord history = new HistoryRecord(commandInstance, 0);

            history.SetBrep(0, brep);
            history.SetString(0, originalId.ToString());

            ObjectAttributes attr = new ObjectAttributes();

            Guid newId = doc.Objects.AddBrep(dupBrep, attr, history, false, false);
            if (newId != Guid.Empty)
            {
                newBrepGuids.Add(newId);
            }
            else
            {
                RhinoApp.WriteLine("Failed to add duplicate Brep with history to the document.");
            }
        }

        doc.Views.Redraw();
        return newBrepGuids;
    }

Unfortunately, I cannot get the History to actually be enabled, I’ve suspected it might be because Im performing actions later on that break history in the command itself, but even when looking at the C# History Developer Sample, I could not get History to be activated, not even when writing a snippet creating a Loft and adding history to a new object.

Can anybody help me out here?

You might try monitoring the RhinoDoc.ReplaceObject event. If your object is being changed, this event should fire. You can, IIRC, even script the _Undo command if you want to prevent changes.

Tip: if you’re scripting from your command, make the command a scriptrunner, see https://developer.rhino3d.com/api/rhinocommon/rhino.commands.style)

[CommandStyle(Style.ScriptRunner)] // <-- this
public class MyCommand : Command
{
// contents
}
1 Like

Thank you for your quick response menno, I took your advice and subscribed to the Delete, Replace, and Add event Handlers, Im getting to a point where this is could be a very good solution for us. Thanks for that.
FYI: When trying to call RunScript inside of an event watcher, I get a pretty funny ApplicationException, telling me to “contact steve[at]mcneel.com to discuss why I need to do this”.

You need to do any document modifications outside of the event watcher. Document modifications shoudl be done in RhinoApp.Idle

The way to do it is:

  • use the Add/Delete/ReplaceObject event watchers to follow object replace/delete/add events
  • if such an event is fired and you want to modify something, subscribe to the RhinoApp.Idle event and save any relevant data
  • when the RhinoApp.Idle event fires and you’re in its event handler:
    • unscubscribe from RhinoApp.Idle
    • modify the document
1 Like