algorithmic modeling for Rhino
I want to load a variety of geometry on a c# input pin, sometimes a mesh, sometimes a point. I've created an input parameter of type GeometryBase which works fine for the mesh... but get a cast error when hooking a point up to it. The error message states cannot cast Point >> GeometryBase.
On the other hand, if I us one of the pre-made C# modules it seems to work great. So I'm clearly missing something...?
Here are the actual code lines involved...
// registering the input parameter...
pManager.Register_GeometryParam("TargetObj", "T", "Target geometry to perform analysis on", GH_ParamAccess.item);
// loading the parameter inside the solver routine...
GeometryBase TargetGeo = null;
if (!DA.GetData("TargetObj", ref TargetGeo)) { return; }
Thanks!
- Brian -
Tags:
Sounds like the issue raised by Erik last week, which I didn't understand until now as I've been overloading geometric inputs and accepting points since I started coding addons for Grasshopper. Now I see the difference.
Do you have to handle different geometry on a case by case basis? (ie do you accept annotations, lights, hatches etc?)
If you do, then try this code:
public class TestComponent : GH_Component
{
public TestComponent() : base("InputGeom", "testGeom", "InputGeom", "Extra", "Test") { }
public override GH_Exposure Exposure { get { return GH_Exposure.primary; } }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.Register_GeometryParam("Geometry", "G", "Geometry", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.Register_GeometryParam("Base Geometry", "G", "Base Geometry within Block");
}
protected override void SolveInstance(IGH_DataAccess DA)
{
IGH_GeometricGoo g = null;
if (DA.GetData<IGH_GeometricGoo>(0, ref g))
{
GH_Point p = g as GH_Point;
if(p != null)
DA.SetData(0,g);
//else other geometric types and errors for invalid ones.
}
}
public override Guid ComponentGuid { get { return new Guid("{FBB5813E-295A-4691-86BF-369D7A367B71}"); } }
}
GetData() will perform whatever casting is needed to gie you what you ask for. If you ask for a GH_Point you'll get the actual instance stored inside the parameter (this instance might be shared amongst multiple parameters though, so if you change it it may have far reaching consequences). If you ask for a Point3d you'll get the coordinate stored inside the GH_Point instance. As you mentioned, Point3d is a struct, so you're getting a 'safe' copy of the data.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by