Grasshopper

algorithmic modeling for Rhino

Hi All,

 

I have finally taken the big step towards creating a GHA of components and fallen flat on my face, as my shoes seem to be tied together.

 

I created a VB.Script component for what I'm trying to do and that worked fine. Now I've dropped this code into VS and altered the various ins and outs and I get an error when I plug a List into my component.

 

Code from VS:

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

        pManager.Register_GenericParam("List", "L", "List to find Middle")

End Sub


Protected Overrides Sub RegisterOutputParams(ByVal pManager As Grasshopper.Kernel.GH_Component.GH_OutputParamManager)       

        pManager.Register_GenericParam("Mid", "M", "Value(s) in the Middle")      

        pManager.Register_IntegerParam("Index", "I", "Index of Value(s) in List")

End Sub


Protected Overrides Sub SolveInstance(ByVal DA As Grasshopper.Kernel.IGH_DataAccess)

        Dim L As New List(Of Object)

 

        If (Not DA.GetData(0, L)) Then Return

        If (L.Count = 0) Then Return

 

        Dim Out_M As New List(Of Object)

        Dim Out_I As New List(Of Object)

 

        Out_M.Add(L(Math.Floor(L.Count / 2)))

        Out_I.Add(CInt(Math.Floor(L.Count / 2)))

 

        If L(Math.Floor(L.Count / 2)) <> L(Math.Ceiling(L.Count / 2)) Then

            Out_M.Add(L(Math.Ceiling(L.Count / 2)))

            Out_I.Add(CInt(Math.Ceiling(L.Count / 2))) 

        End If


        DA.SetData(0, Out_M)

        DA.SetData(1, Out_I)

    End Sub

 

Where am I going wrong?

Views: 1729

Replies to This Discussion

pManager.Register_GenericParam("List", "L", "List to find Middle")

 

Maybe here you need to add another field:

 

GH_ParamAccess.list

 

Also, and probably the cause is here:

If (Not DA.GetData(0, L)) Then Return

 

L is a list, so you want:

 

DA.GetDataList

 

Will look at the rest now, but that seems to be something to try...

 

ok, and finally:

 

to make a list be the output, you should use:

 

DA.SetDataList(0, Out_M)

DA.SetDataList(1, Out_I)

p.s. you can also target trees by using:

 

GH_ParamAccess.tree

DA.GetDataTree()

DA.SetDataTree()

 

Of course, you will have to set up the logic for how you want to access the data inside the tree.

Thanks Luis, I'll give it a go.

Hi Luis,

 

Can you explain, with words of one syllable (or less) where I need to add GH_ParamAccess.list.

Thanks for your help

pManager.Register_GenericParam("List", "L", "List to find Middle", GH_ParamAccess.list)

 

The methods to register parameters are 'overloaded' meaning that there are various acceptable combinations of arguments which you can use depending on the data you have.

 

In the case of the pManager.Register_GenericParameter you have 2 options for supplying the arguments 1) with the Name, NickName, and Description and 2) with the same as before but with an additional argument which defines the type of input structure that you expect, i.e. an Item, List, or Tree.  Other Register_XXX methods might have an extra argument for a default value.  Some of these Register methods have 5 overloads.

 

In case you want to read about overloading functions, here is an MSDN article:

http://msdn.microsoft.com/en-us/library/ms973896.aspx

Thanks, I now get a different error. First it said that <> is not defined so I changed it to If Not (A = B) Then and still got this. Any ideas?

 

If A and B are both GH_Number, try:

 

If Not(A.value = B.value) Then...

 

GH Data types such as GH_Number are wrappers for other data types such as a System.Double in the .net framework.  To access their value, you need to get the property .value.

 

Here is David's explanation from the GrasshopperSDK.chm file in the article 'Basic Data Types'

 

Practically all native data types in Grasshopper are based either on a .NET Framework type or a RhinoCommon SDK type. For example System.Boolean, System.String, Rhino.Geometry.Point3d and Rhino.Geometry.Brep to name but a few. However the parameters in Grasshopper don't directly store Booleans, String, Points and Breps as these types can't handle themselves in the cauldron that is Grasshopper.

I'm just not getting it. It works and works well in the VB.Script component where it compares numbers and even points for equality.

 

I attach both the script, cluster and gha

 

Attachments:

So just let me get a clear objective...

you want to find the item in the middle of the list?  If the list has an odd number, then it will have one answer and if the list has an even number, the list will have two?

yup

Therefore Danny, should you not be getting the opposite behavior?  Currently in your file you have 3 lists going into the component, one with 10, one with 11, and one with 6.  Currently, your list with 11 returns two values, and the lists with 10 and 6 return one.  In any case, I changed the logic a bit.

Check it out in the other vb component.  The idea is just to check if the list is odd or even first with the Mod operator.  If the lsit is odd, return the middle item in the list as you have set it up.  If the list is even, return the mid items also in the same way as you have set it up.  I am attaching the edited .gh file, a Class1.cs file which shows in C# how I set up the component (translated vb code will follow), and the .gha I built from it.

 

The logic should be a bit easier to follow in this version.  Check out the edited vb component in the definition for a 'translated version.'

 

Apologies for the C# ness...I currently do not have Visual Studio Express installed, just Visual C# express.  This one is converting just fine to:

 

Dim list As New List(Of Object)()
Dim out_v As New List(Of Object)()
Dim out_i As New List(Of Integer)()
If Not DA.GetDataList(0, list) Then
    Return
End If

Dim val As Object = list(Convert.ToInt32(Math.Floor(Convert.ToDouble(list.Count / 2))))
Dim i As Integer = Convert.ToInt32(Math.Floor(Convert.ToDouble(list.Count / 2)))


If (list.Count Mod 2) Then
    out_v.Add(val)

    out_i.Add(i)
Else
    out_v.Add(val)
    out_i.Add(i)
    out_v.Add(list(i + 1))
    out_i.Add(i + 1)
End If

DA.SetDataList(0, out_v)
DA.SetDataList(1, out_i)
Attachments:
Thanks Luis. Its good to know I can air my stupidity on the forum here and find answers! Honestly you end up not being able to see the wood for the trees with some of these things. :) I'll have a look at it first thing Monday morning when I'm back in the office.

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