Hello! I’m new with C# and I was trying to develop a C# component sender to a clustered receiver, can anyone help me please?
Sender:
using System;
using Grasshopper.Kernel;
public class SenderComponent : GH_Component
{
public SenderComponent() : base("Sender", "S", "Send numeric data to receiver", "Test", "Data") { }
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddNumberParameter("Data", "D", "Numeric data to send", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager) { }
protected override void SolveInstance(IGH_DataAccess DA)
{
double data = 0;
if (!DA.GetData(0, ref data)) return;
// Get the nickname of the target receiver
string targetNickname = "ReceiverNickname";
// Find the remote component with the given nickname within the cluster
GH_Component targetComponent = null;
foreach (IGH_DocumentObject obj in OnPingDocument().Objects)
{
if (obj is GH_Cluster)
{
GH_Cluster cluster = obj as GH_Cluster;
foreach (IGH_Component clusterComponent in cluster.ClusterObjects(true))
{
if (clusterComponent.NickName == targetNickname && clusterComponent is GH_Component)
{
targetComponent = clusterComponent as GH_Component;
break;
}
}
}
}
// Check if the target component was found
if (targetComponent != null)
{
// Send data to the target component
targetComponent.Params.Input[0].AddVolatileData(new GH_Path(0), 0, data);
}
// Expire solution to update the changes
ExpireSolution(true);
}
public override Guid ComponentGuid
{
get { return new Guid("GUID_HERE"); }
}
}
Receiver:
using System;
using Grasshopper.Kernel;
public class ReceiverComponent : GH_Component
{
public ReceiverComponent() : base("Receiver", "R", "Receive numeric data from sender", "Test", "Data") { }
protected override void RegisterInputParams(GH_InputParamManager pManager) { }
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Data", "D", "Received numeric data", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
double data = 0;
// Receive data from the sender component
DA.GetData(0, ref data);
// Do something with the received data...
// Output the received data
DA.SetData(0, data);
}
public override bool Read(GH_IReader reader)
{
// Perform any additional reading here if needed
return base.Read(reader);
}
public override bool Write(GH_IWriter writer)
{
// Perform any additional writing here if needed
return base.Write(writer);
}
protected override void ExpireDownStreamObjects()
{
// Expire downstream objects to trigger a re-computation when data is received
OnPingDocument().NewSolution(true);
base.ExpireDownStreamObjects();
}
public override Guid ComponentGuid
{
get { return new Guid("GUID_HERE"); }
}
}
Thanks!