I want to put a parameter C from code to be the recipient of a parameter A and the source of a parameter B, where before A and B were connected (bridging a wire). I only want to do this when it makes sense according to the data types (IGH_Goo) of A, B and C (IGH_Param), checking whether A-C makes sense and C-B makes sense. This sense is the data conversion logic of GH. I need a function that takes two IGH_Param and returns a boolean which determines if GH can connect them without conversion errors.
I have this, it works well sometimes, as integer to number, but it does not work for example from curve to number, throwing a null reference exception (in toGoo.CastFrom(fromGoo)). I am using a scripting component for debugging purposes.
My logic has been to create an instance of the data type of the two candidate parameters to connect in order to use the IGH_Goo.CastFrom() method.
Any clue how to make this right? Thank you!
try{
IGH_Param From = this.Component.Params.Input[0].Sources[0];
IGH_Param To = this.Component.Params.Input[1].Sources[0];
IGH_Goo fromGoo;
if (From.Type == typeof(IGH_Goo) || To.Type == typeof(IGH_GeometricGoo)) fromGoo = new Grasshopper.Kernel.Types.GH_ObjectWrapper();
else fromGoo = Activator.CreateInstance(From.Type) as IGH_Goo;
if (fromGoo == null) { Print("fromGoo: null"); return; }
else {Print("fromGoo: {0}", fromGoo.TypeName);}
IGH_Goo toGoo;
if (To.Type == typeof(IGH_Goo) || To.Type == typeof(IGH_GeometricGoo)) toGoo = new Grasshopper.Kernel.Types.GH_ObjectWrapper();
else toGoo = Activator.CreateInstance(To.Type) as IGH_Goo;
if (toGoo == null) { Print("toGoo null"); return; }
else {Print("toGoo: {0}", toGoo.TypeName);}
Print("CanCast: {0}", toGoo.CastFrom(fromGoo).ToString());
}catch(Exception e){
Print(e.ToString());
}
Check if a type of IGH_Goo can be casted to other type of IGH_Goo.gh (4.8 KB)