grasshopper3d / GrasshopperDocsSite

Documentation for open source components of Rhino and Grasshopper
33 stars 3 forks source link

Abstract component classes shouldn't be parsed #6

Closed karakasa closed 4 years ago

karakasa commented 4 years ago

Brief Description

Abstract component classes are listed in the docs, which they shouldn't be.

How to replicate

You can find this in the Pancake Docs.

In the code, it is defined as such:

public abstract class pcNativeExport : GH_Component
{
...
    public pcNativeExport(string formatName)
        : base("Export " + formatName, "pcExport" + formatName, "This component exports geometries from GH to " + formatName + " file directly\r\nThis component doesn't rely on Export command.", "Pancake", "I/O")
    {
    }
...

This is a base class for some other components. It shouldn't be included because it is marked abstract and it will be ignored by Grasshopper. Since the reflection method Activator.CreateInsntace on abstract types will throw an exception, it may be the reason why the indexer doesn't work with Pancake.

Here's the code used by GH to load components. You can see here abstract classes are not loaded:

Grasshopper.Kernel.GH_ComponentServer.ParseGHA(Assembly assembly, string path, GH_LoadingMechanism mechanism)

...
if (IsPotentialDocObject(t3))
{
    LoadDocObject(t3, gH_AssemblyInfo, ref conflictSolution);
    list.RemoveAt(l);
}
...

Grasshopper.Kernel.GH_ComponentServer.IsPotentialDocObject(Type T)

private static bool IsPotentialDocObject(Type T)
{
    if (!T.IsClass)
    {
        return false;
    }
    if (T.IsAbstract) // Here
    {
        return false;
    }
    if (T.IsGenericTypeDefinition)
    {
        return false;
    }
    if (!HasEmptyConstructor(T))
    {
        return false;
    }
    if (!typeof(IGH_DocumentObject).IsAssignableFrom(T))
    {
        return false;
    }
    return true;
}

Expected behavior

They shouldn't be listed or parsed.

robinrodricks commented 4 years ago

Done! Thanks for the detailed bug report info. It helped :)

http://grasshopperdocs.com/addons/pancake.html

If you have any more good addons to suggest, feel free, I'll add them in.