C#, Rhino 7, How to Purge Object from Document?

I would like to Purge some objects from my Rhino file. Some Breps are created recursively and when I manually delete them in the RhinoDoc and rerun the code they don’t get baked again.
Is it correct in understanding that this happens because they have the same GUID as the previous ones?
Anyhow, the code I am running is:

    uint doc_serial_numnber = RhinoDoc.ActiveDoc.RuntimeSerialNumber;
    var purge = ObjectTable.Purge(doc_serial_numnber); //1

    Rhino.DocObjects.RhinoObject[] rhinoObjects = doc.Objects.FindByObjectType(Rhino.DocObjects.ObjectType.Brep);

    foreach (Rhino.DocObjects.RhinoObject bb in rhinoObjects)
      Guid rhino_object_id = bb.Id;
      RhinoObject cc = (RhinoObject) bb;
      ObjectTable.Purge(cc); //2

Those are the errors I get.

  1. Error (CS0120): An object reference is required for the non-static field, method, or property ‘Rhino.DocObjects.Tables.ObjectTable.Purge(uint)’
  2. Error (CS0120): An object reference is required for the non-static field, method, or property ‘Rhino.DocObjects.Tables.ObjectTable.Purge(Rhino.DocObjects.RhinoObject)’

Is it not possible to purge after deleting? (even though rhino is finding all of their ids)

Any help would be appreciated, either as a correction of code or references where I can look up how this works (e.g. C++, Rhino 6, How to Purge Object from Document?) :slight_smile:

Thank you!

If you look at ObjectTable.Purge Method you will see that it is an instance method, so you need to call it on a document object table instance. In your example code you have a document instance in doc, so call Purge on its Objects property: doc.Objects.Purge(rhinoOb).

The first one that takes an uint32 takes a serial number of the object you want to purge from the document (ObjectTable.Purge Method (UInt32)). You pass it the document serial number, which is incorrect.

The second overload (ObjectTable.Purge Method (RhinoObject)) takes indeed the RhinoObject like you try in the second part of your code.

To delete all breps you would do:

using Rhino.DocObjects;

RhinoObject[] rhinoObjects = doc.Objects.FindByObjectType(ObjectType.Brep);

foreach(RhinoObject bb in rhinoObjects)

Naturally, if you want to delete only certain objects you need to ensure that your rhinoObjects array contains only those RhinoObjects you want to permanently delete without them being in the undo list.

1 Like

Thank you so much!
Follow up question: How do I find an object’s uint32?

The overkill~another not particularly rhino related: I am not quite getting the “instance of an object” meaning. Do you happen to know of any documentation/ tutorial that clearly explains it?


Word of warning: note that this is a number you should not save between sessions. As the name tries to tell: it is valid only during that session.

When I say instance in this thread I mean an instance of a class. So we had in this thread already: RhinoDoc, RhinoObject and ObjectTable as classes, and to use the non-static methods these classes provide you need instances of such classes to do so.

If you implement a command in a custom Rhino plug-in in C# you’ll be overriding the method RunCommand, which as one of its parameters will have RhinoDoc doc. You get a variable with the name doc that points to an instance of a RhinoDoc. Likewise the Objects property on RhinoDoc will give you access to an instance of an ObjectTable.

Does this make sense?

1 Like

It does! Thanks again! :slight_smile:

1 Like