Custom vector field Grasshopper component

plugin
grasshopper
csharp

(Miroljub A) #1

I am trying to create a grasshopper plugin that outputs the vector field type that could be feed in other vector field components, such as Evaluate Field … The strategies that I had was to derive from the GH_FieldElement Class https://developer.rhino3d.com/wip/api/grasshopper/html/T_Grasshopper_Kernel_Types_GH_FieldElement.htm
but the problem was, I assume, that the Field element should somehow be “converted” to GH_Field Type for what I couldn’t find the way.

Is there anything that I am missing or it is just not possible.


(David Rutten) #2

You have to construct a new GH_Field instance and append your element(s) to that instance.


(Miroljub A) #3

Thank you David for your prompt reply. It works. Although, do you have any recommendation how to implement the following abstract fields of the GH_FieldElement Class:

ElementGuid
BoundingBox
Duplicate()

What I did is the following:

namespace CustomField
{
    class MyField : GH_FieldElement
    {
        public override Guid ElementGuid
        {
            get
            {
                return new Guid();
            }
        }

        public override BoundingBox BoundingBox
        {
            get
            {
                return new BoundingBox();
            }
        }

        public override IGH_FieldElement Duplicate()
        {
            return this.Duplicate();
        }

        public override Vector3d Force(Point3d location)
        {
            return new Vector3d(1,1,1);
        }
    }
}

(David Rutten) #4

Nope, always return the same Guid. I’d recommend having a static readonly field on your class which you return:

private static readonly Guid _id = new Guid("{0E85A82C-3778-4DD8-A82C-58EB2DCB3072}");
public override Guid ElementGuid
{
  get { return _id; }
}

The bounding box contains the visual elements of your field. If there are no visual elements then you can return BoundingBox.Empty.

Isn’t that just recursively calling the same method? If so, that will definitely crash with a stackoverflow exception whenever it’s called. Just return a new instance of your class with the exact same state.


(Miroljub A) #5

Thanks David once again for your prompt reply. Cheers