Grasshopper

algorithmic modeling for Rhino

Hello David,

just simple question but I have problem to find a solution for that. I try to make own component inspired by yours example of EventExample via external dll reference. Well, it works, but SolveInstance in component is called only when a new component is put on canvans or when i rebuilt solution for whole document.


How I can add double click event to component and call SolveInstance by click on it? Something like script component and form of QWhale editor.

 

Regards,

Jachym

Views: 1537

Attachments:

Replies to This Discussion

Before I answer the question, you're building and renaming an exe, not a dll. Please switch your project to 'Class Library'.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Mouse and Key events for objects on the Grasshopper canvas are handled by the Attributes of that object. Attributes also take care of display, layout, selection behaviour and tooltips. 

 

By default, all components create the same kind of attributes, namely Grasshopper.Kernel.Attributes.GH_ComponentAttributes.

 

If you want to respond to MouseDown, MouseMove, MouseUp and/or MouseDoubleClick events you'll need to write your own Attributes. Since you can derive from GH_ComponentAttributes this is not a big job:

 

Public Class ScanComponentAttributes

  Inherits Grasshopper.Kernel.Attributes.GH_ComponentAttributes

 

  Public Sub New(byval owner As ScanComponent)

    MyBase.New(owner)

  End Sub

 

  Public Overrides Function RespondToMouseDoubleClick( _

              ByVal sender As Grasshopper.GUI.Canvas.GH_Canvas, _

              ByVal e As Grasshopper.GUI.GH_CanvasMouseEvent) _

              As Grasshopper.GUI.Canvas.GH_ObjectResponse      

    If (ContentBox.Contains(e.CanvasLocation)) Then        

      Dim sc As ScanComponent = DirectCast(Owner, ScanComponent)

      sc.ExpireSolution(True)

       'Or show a dialog or do something else
      Return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled      

    End If


    Return Grasshopper.GUI.Canvas.GH_ObjectResponse.Ignore    

  End Function

End Class

 

All that's left at this point is to associate your Attributes with your Component. For that you have to override the CreateAttributes() method on your ScanComponent class:

 

Public Overrides Sub CreateAttributes()

  m_Attributes = New ScanComponentAttributes(Me)  

End Sub

 

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

1) Fine, I set project like Class Library.

2)

Thanks for fast replay, but I have some problem with conversion to C#. How to rewrite  //?????

 

 

 

public class ScanComponentAttributes : Grasshopper.Kernel.Attributes.GH_ComponentAttributes
    {
        public ScanComponentAttributes(ScanComponent owner)
        {
 // ????????????????????????????
        }

        public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDoubleClick(Grasshopper.GUI.Canvas.GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
        {
            if (ContentBox.Contains(e.CanvasLocation))
            {
                ScanComponent sc = // ????????????????????????????
                sc.ExpireSolution(true);
                return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled;
            }
            return Grasshopper.GUI.Canvas.GH_ObjectResponse.Ignore;
        }
    }

 

Thx David

public ScanComponentAttributes(ScanComponent owner) : base(owner) {}

 

ScanComponent sc = Owner as ScanComponent;

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

You could also have pasted my code into this webpage:

 

http://www.developerfusion.com/tools/convert/vb-to-csharp/

 

and got your answer :)

 


The difference btw. between:


ScanComponent sc = (ScanComponent)Owner;


and


ScanComponent sc = Owner as ScanComponent;


Is that the first will crash if Owner is not of type ScanComponent. The second approach will just leave sc null. But since this can never happen, it's academic.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David, thanks so much, now it works perfect.

 

Best,

Jachym

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