Grasshopper

algorithmic modeling for Rhino

GHA coding of behavior like the toggle between Union/Per Object in the Bounding Box

Hi all-

I'm building a new component and I'd like it to have the ability to toggle between behaviors much as the bounding box component does between Union and Per Object.  I'm not even sure what the correct term is to describe this, so I'm having trouble finding anything in the SDK...any pointers on how I can set this up and utilize the variable in my component?

thanks!

Views: 596

Replies to This Discussion

There is no standard way of doing this, you'll have to write the following bits yourself:

AppendAdditionalComponentMenuItems()

Override the AppendAdditionalComponentMenuItems() method and add a menu item that allows the user to toggle the state. The implementation in the BoundingBox component is as follows:

Protected Overrides Sub AppendAdditionalComponentMenuItems(ByVal menu As ToolStripDropDown)
  Dim item As ToolStripMenuItem = Menu_AppendItem(menu, "Union Box", _
                                                  AddressOf Menu_UnionBoxClicked, True, GetValue("UnionBox", False))

  item.ToolTipText = "When checked, a single box for all items in each list is returned"
End Sub

You don't have to use GetValue/SetValue, you can also use your own private field for this. However GetValue/SetValue means you won't have to handle (de)serialization yourself. The handler for the item, is defined as follows:

Private Sub Menu_UnionBoxClicked(ByVal sender As Object, ByVal e As EventArgs)
  SetValue("UnionBox", Not GetValue("UnionBox", False))
  ExpireSolution(True)
End Sub

I just realized that SetValue is not undo-able, I'll fix that in the next release.

When the "UnionBox" value changes, two things need to happen:

  1. The GUI message needs to be updated.
  2. The output parameter access needs to be changed to item or list, whichever is applicable.

Thus, by overriding the ValuesChanged() method, you can make sure your component is set up correctly:

Protected Overrides Sub ValuesChanged()
  If (GetValue("UnionBox", False)) Then
    Message = "Union Box"
    Params.Output(0).Access = GH_ParamAccess.item
    Params.Output(1).Access = GH_ParamAccess.item
  Else
    Message = "Per Object"
    Params.Output(0).Access = GH_ParamAccess.list
    Params.Output(1).Access = GH_ParamAccess.list
  End If
End Sub

Ultimately, in SolveInstance you read the state of the "UnionBox" value and act accordingly.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Fantastic.  Thanks!

On second thoughts, I'm not going to add auto-undo to the GetValue/SetValue mechanism. You'll have to record your own undo record just before changing the value:

RecordUndoEvent("Name Of Undo")

SetValue("UnionBox", Not GetValue("UnionBox", False))

ExpireSolution(True)

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Please, can you add this discussion to the FAQ section ? (It'll be much easier to find)

Would it make more sense to add a topic to the SDK helpfile?

--

David Rutten

david@mcneel.com

Poprad, Slovakia

That's where I looked first...so I'd say yes.  And thanks again...works great

@David Stasiuk - I did same.

@David Rutten - Sure. If you're going to just copy-paste the whole thread, please erase my previous post (and this one as well) ;)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service