How to Code in C# to detect the last operation in grasshopper

Is there a way to code in C# like,

if (I just changed the name of a function) {
Now disconnect all the output lines from this function;

or something like,

if (the last operation in grasshopper is CURVE) {
I’ll update how many curve functions are present in current grasshopper page;

Hope I described it clear, I hope to find a attribute in C# to detect the last operation in grasshopper, so that can help me to create some kind of work flow.

If not, but there is a way to run code at runtime, will work too

I’m confused. When you say ‘function’ do you mean a C# method or a Grasshopper component or …?

I was referring to a gh component, but that was just an example. I just want to put a past operation (if I create a new component, delete a component, connect components, rename it, or explode a cluster etc.) in that if statement

or an Alternative solution I cam looking for is to run part of the script (for example that if statement) every second/minute/hour etc in grasshopper to keep it update

There’s no small set of predefined operations that are allowed on files. Sure, almost every single thing that changes is either (1) add or remove component (2) create or remove wire (3) move or resize object, but there are lots of operations that are specific to individual objects and Grasshopper doesn’t know about any of the details. Slider changes, Dial changes, Text Panel changes, script code changes, component menu option changes, etc. etc.

It’s also possible for any amount of the aforementioned operations to occur together into a single big operation. This happens during paste or cluster explode for example.

If you want to make changes to a file in response to other changes in that same file, then I suggest you handle the GH_Document.SolutionStart event. Inside that handler you check to see if something you care about has changed (please make this quick, you’ll be delaying every single solution by doing stuff in this event) and then take appropriate action. From within the SolutionStart event you are allowed to modify wires, add objects, change persistent values, expire objects, but once the solution really begins this is no longer possible.

Hi @DavidRutten,

I’m trying to write my custom component that will record/alert whenever any changes is done to the document.

From here I understood that, for example, a new object is added to the document, an ObjectAddedEvent will be raised? My question is, how to catch that event so that I can do something upon event happening?

public delegate void ObjectsAddedEventHandler(
Object sender,
GH_DocObjectEventArgs e

Also I found this handler as well. What is the difference between GH_Document and IGH_Document? Does below eventhandler ‘catch’ changes when any components in the document moved, or parameter changed, etc?

public delegate void ObjectChangedEventHandler(
IGH_DocumentObject sender,
GH_ObjectChangedEventArgs e

Alternatively, I saw the approach of using Undo Stack Records -

I’m currently able to get the whole records whenever the custom component is ‘refreshed’ - because the whole code is within SolveInstance. However, what I want is to be alerted whenever any change is done (whenever a new record is added) - and details of that change (e.g components moved from position 1 to position 2).

I don’t understand how the event firing mechanism work, or do I have to keep a record of the whole components in the document and manually check by having ScheduleSolution?

Kindly advice and many many thanks!!