C# custom component SolveInstance() behavior

Hi to all,
I’m developing a custom component for Grasshopper. I use this component to create some geometry in RH/GH then created into another calculation software throught API. When this geometry is created throught API a GUID is assigned in the calculation software. This GUID is stored (in the GH component) and then when the component run again (because the inputs data in GH are changed) the component delete the old element in the calculation software and create the new one and new GUID is assigned. The GUID is then saved using Write() override and then Read() method run when the GH file is opened.

What I understand is that SolveInstance is (or can be) called many times within the same solution (refer to https://www.grasshopper3d.com/forum/topics/is-the-solveinstance-method-called-everytime-an-input-is-changed) but I don’t understand the behaviour when I open the GH file that has been saved. Debugging seems that the solution SolveInstance() run again and I’m worried that the element is created two times into the calculation software (that already containts the same element).
From my tests seems that, even if SolveInstance() run again after Grasshopper is opened seems that no new elements are created. Have Grasshopper some kind of check that the previous inputs have not changed when I open the file or I have to provide this control? This control can be to store data when the file is saved/closed, and set persistent data when the file is opened?
In few word I want to avoid or to control running a new solution when the .gh file is opened.

Then SolveInstance() can run many times within the same solution, but I don’t understand if the process is:
a.) Many loops of BeforeSolveInstance()-> SolveInstance() -> AfterSolveInstance()
or is:
b.) BeforeSolveInstance() -> many loop of SolveInstance() -> AfterSolveInstance()?

Since SolveInstance() has and iterator seems to be b.).

Additionally I have another question: Components run one at once or they run in the same time if they are not dependent on each other? I mean varius SolveInstance() can be called at the same time?

Thanks

Yes, but why not do your stuffs just in the first iteration when DA.Iteration == 0?

There is no different of computation when you open or save the file, a solution is a solution at any moment, just do it when you need it and avoid rerun when you don’t need it.

Nothing happens at the same time in software, except (virtually) when the process in running in parallel in differents threads. But GH doesn’t do that, it just do it inside some components, but not between them. Components run one by one, ensuring all expired objects are recomputed and running source components first.

You can’t do that, and you don’t need it. Just set a bool variable in your comp class, and execute your stuffs if that variable is false and when you do that, set it to true.

OK thanks, this is very usefull to know. Since I have some unexpected behaviour in my calculation software I can exclude the problem come from from Grasshopper.

Hi Dany,
Thanks very much for replying!
ok, good idea. Is DA.Iteration reset to 0 when the solution is recalculated?

Thanks. I’m using this approch right now and work. I was only worried that the behavior can be different when I reopen the file but as you said nothing change respect when the file is open.

It is setted to 0 when the comp starts to run. The reason is that the components can run several times based on their input access and data structures feeded. For example, if an input is list access and you are using a tree with 4 branches, the comp will run 4 times, the DA.Iteration will give you that info.

If you want to compute a single time in the instanced life of the comp, you can set an bool variable in your class and do a gate with it, like:

class Comp : GH_Component{
  private bool computed;

  void SolveInstance(...){
      if(!computed){
        computed = true;
        // Do whatever
     }
  }
}

Sorry for no be more clear before.

Don’t worry, this is very clear, and usually I used the same approach in other components I made.

Ok i got it. However what I don’t understand is why when I reopen the file SolveInstance is recalled again?

GH solve the definition when you open it, because there’s no point in having unexecuted components.

ok thanks!