Automatic source (I/O) connection

Hi David, I would like to automatically connect component output with other component’s input after the output param is correctly created in the SolveInstance. I have tried to add something like RecievedComponent.Params.Input[0].AddSource(this.Params.Output[0]) into AfterSolveInstance method, but I have problem with “An object expired during a solution” exception. I didn’t expect that AfterSolveInstance is still related to component/parameter expiration, if I understand correctly that nested/double calling of expiration cast this problem.
Thank you for any help.
Lukas

Update:
I have overridden the ExpireSolution method and added ClearData, ExpireDownStreamObjects methods inside this method. Instead of ExpireSolution method (usually used inside overridden method) I am calling CollectData, ComputeData methods and OnSolutionExpired event sender. Automatic link (showed above) is added at the end of the overridden method.
It seems that this solution works, or at least I didn’t find any problems so far :slight_smile: .
I will be happy for any comments or suggestions how to improve this solution. Thanks!

Update:
No, there is a problem to open saved component from the gh file :frowning: . From ComputeData method I am receiving System.ArgumentException: ‘Component is not associated with a document’ - Component failed to deserialize itself.

Yes, you are not allowed to expire objects during a solution. Solutions are not per-component, they are for the entire document. Ideally what you do is make changes from schedule callback delegates.

You schedule a new solution from within the current solution, provide a callback method and then in that callback (which happens just before the next solution) you are allowed to add objects, delete objects, change data, modify wires, etc. etc.

GH_Document doc = ...;
doc.ScheduleSolution(10, ScheduleCallback);
...
private void ScheduleCallback(GH_Document doc)
{
  // Make changes here, but *do not* start a new solution.
  // Only expire objects, a new solution will start soon automatically. 
}

Please do not override ComputeData or CollectData or ExpireDownstreamObjects unless you absolutely have to. Messing with the core default behaviour will have all manner of unpredictable side-effects.

Thank you, I have add doc.ScheduleSolution(...) [delegate] into AfterSolveInstance method and add to CallBack method constrain if == GH_ProgressStep.PostProcess to run it only ones at the end, when the solution is finally solved. I added this constrain, becouse I found that CallBack methods is called more times; when disabled element is again enabled (even four times).

I found the problem. If more components are selected and I will disabled them through middle click menu, my CallBack function works only for one of them. If I repeat the procedure, the next component is solved. I am not clicking on any of them.
Do you know what can cast this behavior and how to solve it?

I have been thinking to move delegate from AfterSolveInstance to ExpireSolution after calling base method, but I am not sure, if it is a clear solution.

Thank you.