Grasshopper

algorithmic modeling for Rhino

Adding input/output parameters without using IGH_VariableParameterComponent

This is a continuation to the previous thread.

My original question was: 

Are there other ways to determine/add/remove of inputs/outputs after the canvas is dragged onto the canvas? I was hoping in having a right-click checkbox menu to allow users to select what shows as output and what doesn't. I want to be able to achieve this without IGH_VariableParameterComponent().

Thanks!

Views: 1468

Replies to This Discussion

It is possible, but even then IGH_VariableParameterComponent (IGHVPC) is advised. The IGHVPC interface handles (de)serialization of variable parameters, which will be problematic without it. For example, imagine the following:

  • There is a component which has 2 outputs by default.
  • There exists an option (accessible through the component menu) which adds a third output.
  • Someone uses the option and saves a file with the component in its 3-output state.
  • The file is then later read back in, however before the actual file data is parsed, the RegisterInputParams method is already called and the component (which has 2 outputs by default) is now trying to parse file data for 3 outputs. Most likely, all the stored information will get lost.

The best way to approach this is to implement IGHVPC, but implement it with empty methods. Always return False, don't do anything in the required functions. This will effectively hide the zooming UI, but it will still retain the added (de)serialization logic needed for saving and opening files.

To add or remove a parameter yourself, all you have to do is register or unregister it with the Params objects that is part of GH_Component. Note that you should not do this during solutions. It's ok to do it from event handlers such as mouse clicks.

An example of a method that adds an output parameter might look like this:

private void AddOutput()
{
  Grasshopper.Kernel.Parameters.Param_Boolean param = new Grasshopper.Kernel.Parameters.Param_Boolean();

  param.NickName = "B";
  param.Name = "Bool";
  param.Description = "Boolean output";
  param.Access = GH_ParamAccess.list;

  Params.RegisterOutputParam(param);
  ExpireSolution(true);
}

--

David Rutten

david@mcneel.com

Several ways to solve it, really depends on what you'd rather do.

1. Create a different bool for each option:

private bool _showTime = true;

private bool _showTemp = true;

private bool _showDewP = true;

....

This is easy because you know the variables are always there. With an array you'd have to keep testing for null to be sure not to cause a NullReferenceException.

2. Create an array of bools. Least amount of typing.

3. Create an enumeration with the Flags option set to store the state:

[Flags]

private enum OutputState

{

  ShowNone = 0,

  ShowTime = 1,

  ShowTemp = 2,

  ShowDewP = 4,

  ShowWind = 8,

  ...

  ShowAll = 127

}

private OutputState _state = OutputState.ShowAll;

4. There's other ways as well, but I'd pick one of these three.

--

David Rutten

david@mcneel.com

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