Grasshopper

algorithmic modeling for Rhino

Hi,

I have input in the component of as guid.

IGH_Param geometryAsGuid = new Grasshopper.Kernel.Parameters.Param_Guid();
pManager.AddParameter(geometryAsGuid, "Guid", "Guid", "Guid", GH_ParamAccess.list);

I would like to flatten all inputs. For now I am doing it like this:

 pManager[0].DataMapping = GH_DataMapping.Flatten;

Is there other way to get all inputs?

Views: 1307

Replies to This Discussion

You could iterate over Params.Input, that way you can have a loop instead of specific statements for each input parameter.

What is the function after ... to get all data:

Params.Input[0].

Stop. Do you want to flatten the data inside the parameters, or do you just want to access all the data in one go inside SolveInstance()?

If you want the latter, then you should set the access of each input to GH_ParamAccess.tree, and inside SolveInstance you get all the data using either Params.Input[i].VolatileData.AllData(skipNulls), or first getting the data tree using DA.GetDataTree(index, tree)

Hi,

Yes I would like to have latter. But then I get into issue:

My input is guid.

Guid has to be declared as GH_Structure not datatree, somehow compiler says this.

But then GH_Structure accepts only Geometric Goo types which is not guid.

Please correct me if I am wrong.

What we call data trees are called IGH_Structure in code. IGH_Structure is an interface which provides a bunch of methods for examining data trees, but they are not typesafe. 

Actual data trees are always instances of GH_Structure<T>, where T must be a type which implements IGH_Goo. It has nothing to do necessarily with geometry, or IGH_GeometricGoo, but it cannot be types like string, Guid, Rhino.Geometry.Line or int. This is why Grasshopper provides wrapper classes for all standard data types. 

So if your input parameter is of type Param_Guid, then you know that the volatile data is of type GH_Structure<GH_Guid>.

In order to get all the guids of a specific input as a flat list, you need to do the following (untested code):

public class ComponentFlatInput : GH_Component
{
public override Guid ComponentGuid
{
  get { return new Guid("{E9BF4058-15C3-4A15-8833-066B327BF2D5}"); }
}
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
  pManager.AddParameter(new Param_Guid(), "Guids", "Id", "Collection of guids", GH_ParamAccess.tree);
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
  pManager.AddParameter(new Param_Guid(), "Flat guids", "Id", "Flat list of guids", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess da)
{
  GH_Structure<GH_Guid> tree;
  da.GetDataTree(0, out tree);

  List<Guid> guids = new List<Guid>();
  // Note that although System.Guid is a struct and can never be null,
  // GH_Guid is a class and _can_ be null whenever it feels like it.
  // So you need to decide whether to skip nulls, or replace them with Guid.Empty
  foreach (GH_Guid goo in tree.AllData(true))
  guids.Add(goo.Value);

  da.SetDataList(0, guids);
}
}

Thank you very much.

I wanted to do this long time ago, but just after getting this kind of explanation I do understand how it works.

I have one more question.

Is there any possibility once I add component to grasshopper canvas have some sliders attached already to component?

I saw some plugin with this kind of thing. I was wondering to redraw a component with slider like karamba components. But probably this would be very complicated. 

Yes, there's ways to hack this together. What you'd need to do is override the AddedToDocument method, figure out if a certain input contains no sources and no persistent data, then also add a slider to the document, already connected to the component.

If I can create an example in half an hour I'll post it, otherwise you'll have to wait until after the weekend.

public override void AddedToDocument(GH_Document document)
{
  base.AddedToDocument(document);

  Param_Number numberInput = Params.Input[0] as Param_Number;
  if (numberInput == null) return;
  if (numberInput.SourceCount > 0) return;
  if (numberInput.PersistentDataCount > 0) return;

  Attributes.PerformLayout();
  int x = (int)numberInput.Attributes.Pivot.X - 200;
  int y = (int)numberInput.Attributes.Pivot.Y;

  GH_NumberSlider slider = new GH_NumberSlider();
  slider.SetInitCode("0.00<0.50<1.00");
  slider.CreateAttributes();
  slider.Attributes.Pivot = new PointF(x, y);
  slider.Attributes.ExpireLayout();

  document.AddObject(slider, false);
  numberInput.AddSource(slider);
}

I copied this bit to visual studio and it does not produce any result.

Possibly I am missing something with this bit:

... figure out if a certain input contains no sources and no persistent data...

What do you mean by this ?

This code needs to be part of a GH_Component class. It assumes the first input parameter (which must be a Param_Number type) needs a slider if it has no sources and no persistent data.

If your first parameter is not a number parameter, then you'll need to modify the code. If you're looking to make this work on a different input, then you need to use the appropriate index in Params.Input[?]

Yay, thank you very much it works:)

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