Grasshopper

algorithmic modeling for Rhino

Hello all,

I am trying to retrieve the GUID of an input component with C#.

By looking on the forum I found out something for python developed by others and have it working for my purpose with this piece of code (slightly modified from the one I found):

GUIDs = []
counter = 0

for input in ghenv.Component.Params.Input:
for source in input.Sources:
GUIDs.append(source.InstanceGuid)
GUID = str(GUIDs)
++counter

Now, I would like to achieve the same goal in C#. I am working in Visual Studio Express 2013.

PS: It would be useful to understand if there is an equivalent of ghenv in C#.

Thank you!

Views: 1895

Replies to This Discussion

Since you're using Visual Studio, you're making your own GH_Component classes right? You're not using the C# component within Grasshopper?

List<Guid> guids = new List<Guid>();

foreach (IGH_Param input in Params.Input)

  foreach (IGH_Param source in input.Sources)

    guids.Add(source.InstanceGuid);

Yes David, I am making my own GH_Component classes.

This is exactly what I needed. I'll add this to my script and play around with it.

Thanks for the very fast reply!

David,

I gave it a shot but instead of getting the GUID I get this:

System.Collections.Generic.List'1[System.Guid]

I tried casting it to string but that didn't do it (see attached screenshot).

any hint?

Thank you!

Attachments:

Also, with the python component I was getting a similar thing but at least I had the GUID at the end. My aim is to get the GUID only in output without all the additional s#it stuff. :)

Well, you're assigning a List<string> to the output, so that's what you get.

If you want to output all the GUIDs of all parameters that provide data for the input, then you need to first of all declare the N output as a list access parameter. So inside RegisterOutputParams:

IGH_Param param = new Grasshopper.Kernel.Parameters.Param_Guid();
pManager.AddParameter(param, "Ids", "N", "Input ids", GH_ParamAccess.list);

There's no specific overload the Param_Guid type, so you have to construct the parameter ahead of time and use the general purpose AddParameter method.

Then, inside SolveInstance, you have to use the SetDataList() method:

DA.SetDataList(0, guids);

And you fill up your list of Guids as you do now, except without the ToString():

List<Guid> guids = new List<Guid>();

foreach (IGH_Param source in Params.Input[0].Sources)

  guids.Add(source.InstanceGuid);

Sidenote... you cannot output only a single Guid, because a parameter can have any number of sources.

David,

Thanks for the very fats and compelling answer; -I'll give it a shot.

Thank you!

Unfortunately, not there yet.

I get an error in GH about the type (see screenshot attached).

My attempt to fix this:

I tried to change my List to <GH_Guid> but, as expected, I got an error when I try to add the InstanceGuid to the list. I guess I need some sort of casting if I want to have a List<GH_Guid>. Or maybe this is not what the green insect is asking for...?!

Thanks a lot.

Attachments:

SetDataList, not SetData. You're calling both at the moment, only call SetDataList and call it once the list is fully populated.

That's true!

Thanks a lot, that did it. I can now finally add this to my project!

PS: I remember you being taller than 6"4' (see attached). :)

Attachments:

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