GetGeometry serialization error for struct-based geometry

@fraguada or @AndyPayne

I ran into a serialization error from rhino.compute when using GetGeometry with lines and curves. After digging into the static IGH_GeometricGoo DeserializeGeometry(ResthopperObject restobj) function, I found the root cause: Rhino structs like Line, Arc, and Circle are value types that serialize to flat property-JSON (e.g. {"From":...,"To":...}), rather than the archive-dictionary format expected by CommonObject.FromJSON.

Part in question:

As a workaround I added a separate deserialization path for structs:

static IGH_GeometricGoo DeserializeGeometry(ResthopperObject restobj)
{
    try
    {
        var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(restobj.Data);
        if (dict != null && Rhino.Runtime.CommonObject.FromJSON(dict) is GeometryBase gb)
            return GH_Convert.ToGeometricGoo(gb);
    }
    catch { }

    if (TryDeserializeStruct<Circle>(restobj.Data, out var circle) && circle.IsValid)
        return GH_Convert.ToGeometricGoo(new ArcCurve(circle));
    if (TryDeserializeStruct<Arc>(restobj.Data, out var arc) && arc.IsValid)
        return GH_Convert.ToGeometricGoo(new ArcCurve(arc));
    if (TryDeserializeStruct<Line>(restobj.Data, out var line) && line.IsValid)
        return GH_Convert.ToGeometricGoo(new LineCurve(line));

    return null;
}

static bool TryDeserializeStruct<T>(string data, out T value) where T : struct
{
    try { value = JsonConvert.DeserializeObject<T>(data); return true; }
    catch { value = default; return false; }
}

My question: is struct geometry intentionally unsupported in rhino.compute, or is there a proper way to handle this that I’m missing?

Thanks in advance!

Which build of rhino.compute are you using? I’ve been making ALOT of changes to rhino.compute recently and I’m just wondering if the code you’re referencing is code from a previous build, or something from my latest changes. I haven’t released a new build of Hops/Rhino.Compute but I’m hoping I can release something soon. But I’ll want to try to address your concerns in this next release too.

Thanks Andy.

I pulled a fresh 8x branch today and added some of my modifications on top of it, but I don’t think I’m touching any of the serialization logic.

What I have not checked yet, and will do tomorrow, is whether I have the newest version of Hops installed.

And thanks for all the changes eg. stopping active children might have some use for that already

Actually, I’m pushing a PR right now which basically covers your whole fix (thanks for reporting it). I typically use Hops which was actually already converting some of this stuff on the fly so I didn’t really notice this as an issue. But, here’s the PR in case you’re interested (compute.geometry: support struct-based geometry (Line/Arc/Circle) in … by andyopayne · Pull Request #946 · mcneel/compute.rhino3d · GitHub). This should be included in the next Hops/Rhino.Compute release

Yes, the change log is quite big for this next build. It will likely be released as a new major version 0.17.0 which will be available for 8.x builds of Rhino.

Perfect looks like this will solve it!

Thank you!

@AndyPayne not sure if this is relevant to you, but I’ve been working for a while on a type-safe TypeScript package for Rhino.Compute. It handles the communication with the server, scheduling solves, retries, cancellation, and Three.js visualization. Basically trying to take care of all the boring plumbing stuff so you can focus on the Grasshopper definitions.

It has some selva specific stuff on there but works also with pure rhino.compute

Yes I remember looking at that when you first released it. Very cool stuff and seems like it could have a lot of applications. I don’t personally use TypeScript stuff myself and I don’t have a specific use case at the moment, but your work does seem promising. Definitely let me know if there are rhino.compute features that you need in order to make your life easier.