Grasshopper

algorithmic modeling for Rhino

so can anyone tell me the method or property to expose the ability to access/launch a user from form right clicking a context option in a component.

 

Basically, consider the color swatch or the path component. Right clicking the color swatch launches teh interactive color 3d box for selecting color.

 

Is there a method or property in the grasshopper SDK for this?

 

Basically I want to be able to launch a form when the user right clicks such a context]]thanks

Views: 4327

Replies to This Discussion

Hi Duck,

 

you can override the Menu_AppendDerivedItems() method on GH_Component. This allows you to insert additional items into the component context menu. Alternatively you can override AppendToInstanceMenu to create the entire menu from scratch. GH_DocumentObject, GH_ActiveObject and GH_Component classes all have protected functions that may help to create new menu items. Have a look at all the functions that start with "Menu_"

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks David, Im still trying to get my head round it. Finding it a bit difficult to be honest !

A lot of the method names are a bit awkward. They sort of grow over time and don't have a consistent naming system. I'll probably mark a lot of them Obsolete soon and replace them with more obvious names. In the meantime:


Public Overrides Sub Menu_AppendDerivedItems( _

        ByVal iMenu As System.Windows.Forms.ToolStripDropDown)

  'First append a separator line

  GH_DocumentObject.Menu_AppendSeparator(iMenu)


  'Then append a new item

  GH_DocumentObject.Menu_AppendGenericMenuItem(iMenu, "Name", _                                AddressOf MyMenuItemClicked)

End Sub


Private Sub MyMenuItemClicked(ByVal sender As Object, ByVal e As EventArgs)

  'Put code here that will be executed when the menu item is clicked.

  'For example:

  Me.RecordUndoEvent("Behaviour Toggle")

  Me.SpecialBehaviour = Not Me.SpecialBehaviour

  Me.ExpireSolution(True)

End Sub

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks David. Just to clarify tho, the above code refers to an item in the grasshopper menu for a document ? If so I'm more interested in how to launch a windows form by right clicking on the input parameter of the component. Exactly like the swatch color form selector for the swtch input parameter for the color selector component. Thanks for trying to pint in right direction

The above adds an extra item to the popup menu of a parameter or component. Very few components change the menu (it would be difficult to discover such a feature), but parameters and especially 'weird' parameters like Sliders, Text Panels and Colour Swatches do this a lot. Sometimes they even override the entire menu instead of just adding a few special items into the default menu.

 

What you do when an item is clicked is entirely up to you. You could change a local variable (my example) or you could display a form, or you could randomly delete three out of every four files on the hard-disk...

 

To launch a window from a menu item, replace the code in MyMenuItemClicked(). Something like this:

 

Dim dlg As New MySpecialForm()

dlg.SettingA = Me.m_settingA

dlg.SettingB = Me.m_settingB

 

If (dlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then

  Me.RecordUndoEvent("MyComponent Settings")

  Me.m_settingA = dlg.SettingA

  Me.m_settingB = dlg.SettingB

  Me.ExpireSolution(True)

End If

 

 

This code assumes you have a form defined in your project called MySpecialForm and it has two properties called SettingA and SettingB. These properties mirror local variables on your component. So the steps are:

 

  1. Create a new instance of MySpecialForm
  2. Set it up so that it matches the state of the current component
  3. Display the form on screen as a modal dialog
  4. If the form was closed with OK, then
  5. Add an undo record to record the change in settings (only necessary if you change the state of the serialization of your component)
  6. copy the settings (A & B) from the form back into local variables
  7. Expire your component (and, by association, everyone who depends on this component) and start a new solution.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David

 

thank you so much for this information, this helps a lot. I will try this later tonight and let you know how i get on,

 

thanks again!

 

Hi Guys,

I'm in the process of doing a similar exercise. I have added a new item to the drop down menu for the component. I can call the user form and enter the required information, but i'm not able to get that information to show up on the output side of the component. 

For example, if the user form asks the user for two inputs (a string and a number), how do I get those values to populate the the output params of the component?

Thanks!

You can only populate outputs from within SolveInstance(). So you either have to read the state of your component in SolveInstance or place some class level variables that are written from within the menu click handlers and then copied inside SolveInstance.

--

David Rutten

david@mcneel.com

Tirol, Austria

Thanks for the info. That makes sense. How would I go about calling the SolveInstance() sub from a right click situation?

Thanks

[C]

DON'T call SolveInstance yourself. Instead, expire the solution and let Grasshopper call that method. I.e.

ExpireSolution(true)

--

David Rutten

david@mcneel.com

Tirol, Austria

Hi David,

Recently I ran into a situation that didn't occur before. The settings in the menu are no longer preserved. When a user selects certain settings from the right-click pop-up menu, saves the file, closes the file, and re-opens the file, the selection settings are reverted back to default. Each of the menu items is a public variable in the component class, with it's get and set.

Would you be able to shed some light on what I may be doing incorrectly? Thanks,

I am having the same problem here. The selection settings are gone once I close the program for a custom component I created myself. Is there a solution to this?

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