Grasshopper

algorithmic modeling for Rhino

Hi All,

 

I'd like to set an input parameter (for a custom component) from a list of options in a pop-up menu.  The SDK provides enough information that I ought to be able to figure it out, but I'm still having a bit of trouble.  I see how to append items to the menu, and how to point to a handler when the item is clicked, but once inside the handler I'm not sure how to set the input parameter (i.e., to the value of the sender), nor how to access the check-box so that the user can see what has been selected.  Any helpful hints?

 

Thanks,

 

Jon

Views: 2131

Replies to This Discussion

Hi Jon,

 

not quite sure what you mean. Are you looking to assign values to the Persistent Data of an input parameter? If so, what type is the parameter? what values are you looking to assign? and what does this "i.e., to the value of the sender" mean?

 

I'll assume for the following example that you are looking to assign integers to a Param_Integer. There will be 4 menu items, each of which assigns the constant 1, 2, 3 or 4 to the second input parameter.

 

First you need to override the AppendAdditionalComponentMenuItems function. It seems like you already have this figured out.



Protected Overrides Sub AppendAdditionalComponentMenuItems( _

                             ByVal menu As ToolStripDropDown)
  'First see if there's a single value already stored in the parameter.
  'If there isn't, cv will have a value of Int32.MinValue.
  Dim cv As Int32 = Int32.MinValue
  Dim param As Parameters.Param_Integer = _

               DirectCast(Params.Input(1), Parameters.Param_Integer)

  If (param.PersistentDataCount = 1) Then
    cv = param.PersistentData(0).Value
  End If

  Menu_AppendGenericMenuItem(menu, "First", _

               AddressOf Menu_SetFirstDefault, , , , cv = 1)
  Menu_AppendGenericMenuItem(menu, "Second", _

               AddressOf Menu_SetSecondDefault, , , , cv = 2)
  Menu_AppendGenericMenuItem(menu, "Third", _

               AddressOf Menu_SetThirdDefault, , , , cv = 3)
  Menu_AppendGenericMenuItem(menu, "Fourth", _

               AddressOf Menu_SetFourthDefault, , , , cv = 4)
End Sub



Then you need to write the handler, which sets the single value:

 

Private Sub Menu_SetFirstDefault(ByVal sender As Object, _

                                 ByVal e As EventArgs)
  RecordUndoEvent("Constant changed")
  Dim param As Parameters.Param_Integer = _

               DirectCast(Params.Input(1), Parameters.Param_Integer)
  param.ClearPersistentData()
  param.AddPersistentData(1)
  param.ExpireSolution(True)
End Sub

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David,

 

Sorry for being vague.  I was trying to assign strings, but the integer example is just as good.  By "to the value of the sender", I meant that I wanted to set persistent data based on the sender value (ByVal sender As Object -- or the strings "First", "Second", "Third", or "Fourth" in your example), so that I wouldn't have to write a unique handler for each menu item.  (In my case, the number of menu items is large and variable, since I'm drawing them from the names of files in a folder.)  At any rate, it's working perfectly now.  Thanks for the help!

 

Jon

Ah... that sender. Yes, you could cast the sender to a ToolstripMenuItem and then harvest the name easily enough.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David,

I was looking through the example above; it works perfectly for me (despite that some Menu Subs have been replaced). However I didn't get completely how to "cast the sender to a ToolstripMenuItem" and get the name of the menu item and assign it to the input parameter.

Same as Jon, I am looking for only one event handler.
Another small item: I would like that after clicking on to the specific menu item - it stays selected and checked, therefore easely recognising what item is selected.

Can you release a bit more light on that?

Below is the example I need to adjust:

Private purplist As List(Of String)

Public Overrides Sub AppendAdditionalMenuItems(menu As Windows.Forms.ToolStripDropDown)

'Adding purposes into the menu
purplist=New List(Of String)
purplist.Add("prop1")
purplist.Add("prop2")
purplist.Add("prop3")
purplist.Add("prop4")

Dim i As Int32=0
For i=0 To purplist.Count-1
Menu_AppendItem(menu,purplist(i),AddressOf SetDef)
Next
Menu_AppendSeparator(menu)
End Sub

Private Sub SetDef(ByVal sender As Object, ByVal e As EventArgs)
RecordUndoEvent("Constant changed")
Dim param As Parameters.Param_String=DirectCast(Params.Input(1), Parameters.Param_String)
param.PersistentData.Clear()
param.SetPersistentData(???)
param.ExpireSolution(True)
End Sub

Thanks in advance!

Kind regards,
Dmitriy

The menu item cannot 'stay' checked, as it is destroyed as soon as the menu is hidden. It is your responsibility to assign the checked state inside AppendAdditionalComponentMenuItems(), basically here:

Menu_AppendItem(menu,purplist(i),AddressOf SetDef)

Event handlers should always have a sender (as System.Object) and some arguments (derived from System.EventArgs). The sender in this case will be the menu item, so you'll need to cast it to access its name:

Dim item As ToolstripMenuItem = TryCast(sender, ToolstripMenuItem)

If (item Is Nothing) Then Return 'This should always work, but better to catch any errors.

...

param.SetPersistentData(item.Text)

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thank you David,

Works perfectly.

Kind regards,

Dmitriy.

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