Hi, I’m now trying to use my class that inherits GH_Param as Hops output.
I defined classes below,
- “Whisky” class
- “GHWhisky” class which inherits GH_Goo
- “Param_Whisky” class which inherits GH_Param
- “ConstructWhisky” component class which inherits GH_Component
And I cloned Compute.rhino3d Repository, added codes about my classes in all parts that switches behavior depending on passed type in compute.geometry and Hops projects.
As a result, Hops can recognize my Param_Whisky as output and add output port to Hops Component, but returns null.
As I debugged, compute.geometry server seems to return null as Value in VolatileData in Param_Whisky.
Do I have to override some Interfaces or methods or modify other parts in compute.geometry?
Thanks in advance,
public class Whisky
{
public Whisky()
{
}
public Whisky(string name, int distilledYear, int matureYear, string distillerName, string bottlerName)
{
Name = name;
DistilledYear = distilledYear;
MatureYear = matureYear;
DistillerName = distillerName;
BottlerName = bottlerName;
}
public string Name { get; set; }
public int DistilledYear { get; set; }
public int MatureYear { get; set; }
public string DistillerName { get; set; }
public string BottlerName { get; set; }
public bool IsBottlers => DistillerName == BottlerName;
}
public class GHWhisky : GH_Goo<Whisky>
{
public GHWhisky(GH_Goo<Whisky> other) : base(other)
{
Value = other.Value;
}
public GHWhisky()
{
}
public GHWhisky(Whisky internal_data) : base(internal_data)
{
Value = internal_data;
}
public override bool IsValid => !string.IsNullOrEmpty(Value.Name) && !string.IsNullOrEmpty(Value.DistillerName) && !string.IsNullOrEmpty(Value.BottlerName);
public override string TypeName => typeof(Whisky).Name;
public override string TypeDescription => "Whisky class for grasshopper.";
public override IGH_Goo Duplicate()
{
return new GHWhisky { Value = new Whisky { Name = Value.Name.Clone() as string, DistilledYear = Value.DistilledYear, MatureYear = Value.MatureYear, BottlerName = Value.BottlerName.Clone() as string, DistillerName = Value.DistillerName.Clone() as string } };
}
public override string ToString()
{
return $"{Value.Name} {Value.MatureYear}yo, Bottled by {Value.BottlerName}, Distilled in {Value.DistilledYear} by {Value.DistillerName}";
}
public override bool CastFrom(object source)
{
switch (source)
{
case Whisky w:
this.Value = w;
return true;
default:
return false;
}
}
public override bool CastTo<Q>(ref Q target)
{
switch (target)
{
case Whisky w:
target = (Q)(object)Value;
return true;
default:
return false;
}
}
}
public class Param_Whisky : GH_Param<GHWhisky>
{
public Param_Whisky(string name, string nickname, string description, GH_ParamAccess access) : base(name, nickname, description, "", "", access)
{
}
public Param_Whisky() : base("Whisky", "W", "Whisky class", "HopsTest", "Param", GH_ParamAccess.tree)
{
}
public Param_Whisky(IGH_InstanceDescription tag) : base(tag)
{
}
public Param_Whisky(IGH_InstanceDescription tag, GH_ParamAccess access) : base(tag, access)
{
}
public override Guid ComponentGuid => new Guid("0f143222-e477-4d81-bba2-c6f0dce5b7e6");
}
public class ConstructWhisky : GH_Component
{
public ConstructWhisky() : base("ConstructWhisky", "CW", "", "HopsTest", "Construction")
{
}
public override Guid ComponentGuid => new Guid("8c394fe0-bee5-4f94-8296-c2b08d71db45");
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddTextParameter("Name", "N", "", GH_ParamAccess.item);
pManager.AddIntegerParameter("MatureYear", "MY", "", GH_ParamAccess.item);
pManager.AddIntegerParameter("DiltilledYear", "DY", "", GH_ParamAccess.item);
pManager.AddTextParameter("DistillerName", "DN", "", GH_ParamAccess.item);
pManager.AddTextParameter("BottlerName", "BN", "", GH_ParamAccess.item);
pManager[4].Optional = true;
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddParameter(new Param_Whisky("Whisky", "W", "", GH_ParamAccess.item));
}
protected override void SolveInstance(IGH_DataAccess DA)
{
string name = default;
int mYear = default;
int dYear = default;
string dName = default;
string bName = default;
if (!DA.GetData(0, ref name))
return;
if (!DA.GetData(1, ref mYear))
return;
if (!DA.GetData(2, ref dYear))
return;
if (!DA.GetData(3, ref dName))
return;
if (!DA.GetData(4, ref bName))
bName = dName;
DA.SetData(0, new Whisky(name, dYear, mYear, dName, bName));
}
}