Grasshopper

algorithmic modeling for Rhino

Passing a proprietary class from one GH_Component to antother

Hi,

using Visual Studio C# 2015 and the GH_Component Wizard, I have a class "Node", which I have packed in a DLL. I reference this library in the GH_Component class.

I would like to output the "Node" class and to register it as an input in another GH_Component. As long as they both reference my class library, they should be able to understand the datatype.

As far as I can see, in RegisterInputParams and RegisterOutputParams, there are no Add_Object_Parameter, only Curve, Line, Vector, etc. And then there is AddParameter. It's first input is an IGH_Param type. How can I add my own class "Node" here? With this function?

Must my class "Node" implement the IGH_Param Interface (or inherit the GH_Param class)?

Is there an easier way to output/input own classes?

If not, which methods in GH_Param must be overridden?

Also, what should come instead of the <T> in the abstract class:

public class NodeParam : GH_Param<T>
    {
        public override Guid ComponentGuid
        {
            get
            {
            return new Guid("{d5250eb8-620c-4604-a506-3b7cddd66ce8}");
        }
        }

    ....

Cheers,

Eirik

Views: 1065

Replies to This Discussion

Hi,

I got a bit further, using this Method (David Rutten Custom input output):

My class Node is wrapped in the class  NodeGHData : GH_Goo<Node>

This class again is made available in NodeGHParam : GH_Param<NodeGHData>

In the GH_component the NodeGHParam-class is registered as an output param:

pManager.AddParameter(ngp, "NODES", "nodes", "node intersections", GH_ParamAccess.list);

It all builds and I can use the component, but I must miss some code, because I get this error (one of the supplied items could not be converted):

Maybe I need to do something to convert the Node to NodeGHParam?

Or is there a problem with list/item access?

I've added the source code.

Cheers,

Eirik

Attachments:

Hi. Eirik

In order to pass a custom class from one component to another you need to wrap it in a class  GH_Goo(Node) that will let GH make sense of the data that is being ferried around.

In order to pass custom data from one component to another you can use pManager.AddGenericParameter to create inputs/outputs that work with all data. You need to pass your GH_Goo class through those outputs.

You can also create a dedicated input/output for your custom Node_Goo type. In order to do that you need to create a Param Class that inherits from GH_Param(T) where T is your Node_Goo class. Let's call it Node_Goo_Param.

After that you can just use pManager.AddParameter( new Node_Goo_Param(), "Name", "NickName", "Description", GH_ParamAccess.item);  

The "Goo" and custom param wrapping method is absolutely the "correct" way to do this. However, it is not strictly necessary - if you use "AddGenericParam" for your inputs and outputs expecting custom types, you can pass your custom class to and fro without any trouble.

Dear Andrei and Andrew,

thanks a lot for your input.

AddGenericParam solved my issue!

I would like to understand why my initial code didn't work. I first created a GH_Goo class; NodeGHData : GH_Goo<Node>. I wrapped this in a GH_Param<T> class; NodeGHParam : GH_Param<NodeGHData>.

In my component I registered the parameter like this: pManager.AddParameter(new NodeGHParam(), "NODES", "nodes", "node intersections", GH_ParamAccess.item);

It all built like a charm, but GH threw the "one of the supplied items could not be converted" error.

Do you mind having a look at the code structure? Where did I go wrong?

Cheers, have a great weekend,

Eirik

Attachments:

Sorry, I was a bit too quick.

Registering the output parameter worked fine, registering these as input in a new component however, did not:

I used the following code:

 protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Nodes", "n", "node intersections", GH_ParamAccess.list);
        }

and in SolveInstance:

List<NodeGHData> wrappers = new List<NodeGHData>();

            DA.GetDataList(0, wrappers);

Did I miss something?

The data is stored as IGH_Goo in a generic parameter, you'll have to get a list of that type.

Cheers David,

I got one step forward, adding the input parameter list to list of GH_ObjectWrapper objects.

How do I get from GH_ObjectWrapper to my custom class? Casts don't seem to work using for instance NodeGHData nd = objectWrapperList[0].Value;

...

sorry, more questions in one thread, but...

when I try to print (ToString) the objects to see if I have access, I can't seem to output a list of strings.

Although I register an output param Text, the output param in GH is shown as NodeGHData:

here's the code:

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Nodes", "n", "node intersections", GH_ParamAccess.list);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Node_Text", "nText", "Text description of nodes", GH_ParamAccess.list);
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<Node> nodes = new List<Node>();
            //List<NodeGHData> wrappers = new List<NodeGHData>();
            List<GH_ObjectWrapper> wrappers = new List<GH_ObjectWrapper>();
            DA.GetDataList(0, wrappers);

            List<String> noDesc = new List<String>();

            foreach (GH_ObjectWrapper ow in wrappers)
            {
                // how to get to NodeGHData

                noDesc.Add(ow.Value.ToString());
            }

            DA.SetDataList("Node_Text", noDesc);

        }

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service