Grasshopper

algorithmic modeling for Rhino

Hi

I use the following code to change change preview color of some geometry i create with a custom component.

It works if I only have one input for the first input parameter in the component.

However if I have multiple inputs only the first output geometry will change color and the rest will only be displayed as wireframe.

How can i solve this?

 

List<Brep> supportGeometry = new List<Brep>();

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Position", "Pos/SN", "Input PointCoordinates", GH_ParamAccess.item);
pManager.AddGenericParameter("NodeRestraint", "NR", "SupportRestraint", GH_ParamAccess.item);
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Support", "SN", "Support", GH_ParamAccess.item);
pManager.AddGenericParameter("Sphere", "S", "Resulting sphere", GH_ParamAccess.list);
}

protected override void SolveInstance(IGH_DataAccess DA)
{

...

// create sphere and cone
Sphere sphere = new Sphere(supportPlane, scale);

Cone cone = new Cone(conePlane, num2, scale);

Sphere sphere = new Sphere(supportPlane, scale);

supportGeometry.Clear();

supportGeometry.Add(sphere.ToBrep());

supportGeometry.Add(cone.ToRevSurface().ToBrep());

DA.SetDataList(1, supportGeometry);

}

public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
Rhino.Display.DisplayMaterial material = new Rhino.Display.DisplayMaterial(System.Drawing.Color.FromArgb(0, 0, 200), 0.5);
material.Transparency = 0.5;
for (int i = 0; i < supportGeometry.Count;i++ )
{
args.Display.DrawBrepShaded(supportGeometry[i], material);
}
}

 

Views: 1859

Replies to This Discussion

You clear your support geometry list in every SolveInstance. This means you always only get the last (not the first) brep in there.

You should override BeforeSolveInstance(), clear your support geometry there, and then only add to the list in SolveInstance.

--

David Rutten

david@mcneel.com

Hi David

Thank you for the help

However I think I have found three methods for this to work

protected override void BeforeSolveInstance()
{
supportGeometry.Clear();

}

or

public override void ClearData()
{
//supportGeometry.Clear();
}

or inside the solverinstance

if (DA.Iteration == 0)
{
supportGeometry.Clear();
}

They all seem to do the same but I guess there is a difference?

They all do somewhat the same.

  1. This happens after ClearData() but before SolveInstance(). If your component is running normally then those two calls will always happen in succession and it doesn't really matter which approach you use. However if your component is disabled, or if solutions in general are disabled then BeforeSolveInstance will not be called. In fact I'd say that overriding ClearData (always call the base class when you do!) is the best way.
  2. BeforeSolveInstance was basically added so that you could avoid writing code like (3). It's easy to override but may behave oddly in the corner case of disabled component or disabled solutions.
  3. This is pretty much how it had to be done a long time ago, I no longer recommend using the DA.Iteration field.

--

David Rutten

david@mcneel.com

Thank you so much for your explanation.

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