Grasshopper

algorithmic modeling for Rhino

I have a component with a set of inputs that i have to ensure are flattened before the component is run. Does anyone know how to go about this. I have tried the following with no success

/* Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Brep> BrepsGH = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Brep>();
            if (!DA.GetDataTree(1, out BrepsGH)) { return; }

            List<Rhino.Geometry.Brep> Breps = new List<Rhino.Geometry.Brep>();
            for (int i = 0; i < BrepsGH.Branches.Count; i++)
            {
                for (int i2 = 0; i2 < BrepsGH.Branches[i].Count; i2++)
                {
                    Grasshopper.Kernel.Types.GH_Brep c = BrepsGH.Branches[i][i2];
                    Rhino.Geometry.Brep brep = null;
                    c.CastTo<Rhino.Geometry.Brep>(out brep);
                    Breps.Add(brep);
                }
            }*/

The problem that i have is i dont know if the user will be passing a list or a tree. It has to become a list in either event, simmilar to using the flatten option. If i can somehow trigger "flatten" on instanciation and lock it that would be the best.

Thanks Everyone,
Erik






Views: 2571

Replies to This Discussion

Hi Erik,

I don't know if this resolves the issue in your case (as the user can override the flatten option) but you can set the flatten flag by default for an input parameter when the component is created. (The same as if the user had opted for a flatten data stream)

In the override for RegisterInputParams try along the lines of :
pManager[0].DataMapping = GH_DataMapping.Flatten;

Cheers,

Jon
Jon,
Thanks allot this is exactly what i was looking for.

Erik
If you can *only* operate on flattened inputs, you should set the input parameter to type tree, and then flatten the data inside SolveInstance().

Everything else can be changed by the user.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
David,
That was the original plan but i was unable to figure it out, i was using flatten, flattenData and then cast as in the example i posted above, but nothing worked for me. Can you give me an example of how to do this.

Erik
I attached source code for a component that eats data trees and flattens them internally. Add this to your project (baldly assuming you're using C#) and compile. Then you'll be able to open the attached ghx file that shows the component in action.

Very important when using DA.GetDataTree is that you do not modify the tree structure or the data items in the tree. The tree structure and the data items may well be shared among multiple objects and you might cause unpredictable behaviour if you modify it.

You can use FlattenData() on GH_Structure to get a flat list of data without changing the structure. If you want to modify the GH_Structure, you should first make a copy using GH_Structure.ShallowDuplicate(). You can also duplicate both the tree topology and the data items using GH_Structure.Duplicate().

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Attachments:

Hello, I am having similiar trouble. I am trying to flatten an input of points or vectors (which could be an item, list, or tree), into a list. I tried to rewrite the sample in VB.

The error:

"You cannot call IGH_DataAccess.GetDataTree() on a List Parameter"

 

Protected Overrides Sub RegisterInputParams(ByVal pManager As Grasshopper.Kernel.GH_Component.GH_InputParamManager)

pManager.Register_BooleanParam("Overwrite Points", "Overwrite", "Overwrite the analysis node file.", 0, GH_ParamAccess.item)
pManager.Register_PointParam("Analysis Nodes", "Points", "The new analysis nodes/points.", GH_ParamAccess.tree)
pManager.Register_VectorParam("Analysis Direction", "Vectors", "The vector direction of each analysis point, default is up.", GH_ParamAccess.tree)

pManager(1).Optional = True
pManager(2).Optional = True

End Sub
Protected Overrides Sub RegisterOutputParams(ByVal pManager As Grasshopper.Kernel.GH_Component.GH_OutputParamManager)
pManager.Register_StringParam("Output Message", "Out", "Output Message")
End Sub
Protected Overrides Sub SolveInstance(ByVal DA As Grasshopper.Kernel.IGH_DataAccess)
Dim Overwrite_points As Boolean = 0
Dim errorlist As New List(Of String)

Dim pts_tree As GH_Structure(Of GH_Point) = Nothing
Dim vec_tree As GH_Structure(Of GH_Vector) = Nothing

If (Not DA.GetDataTree(1, pts_tree)) Then Return
If (Not DA.GetDataTree(2, vec_tree)) Then Return

pts_tree = pts_tree.ShallowDuplicate()
pts_tree.Flatten()
Dim points As New List(Of Point3d)
For i As Integer = 0 To pts_tree(0).Count() - 1
points.Add(pts_tree.Branch(0)(i))
Next

vec_tree = vec_tree.ShallowDuplicate()
vec_tree.Flatten()
Dim vectors As New List(Of Vector3d)
For i As Integer = 0 To vec_tree(0).Count() - 1
vectors.Add(vec_tree.Branch(0)(i))
Next

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service