Grasshopper

algorithmic modeling for Rhino

Hi

I tried this link to create a mouse responsive GH_Component that would open another program.

http://www.architexted.com/how-to-create-a-mouse-responsive-gh-comp...

 

However I'm using C# and managed to translate it.

 

But to my questions:

Is it possible to make a simple definition where you trigger a function on doubleclick, without having 3 separate classes

 

Thank you

Views: 1533

Replies to This Discussion

And your reason for not wanting 3 classes is?

You will always need at least two types if you want to have a special object that responds in some non-standard way to UI events. You'll need the object (which could be almost empty if it doesn't participate in the solution) and you'll need attributes which take care of display and mouse events.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Ok, but how do I add in- and output parameters to the following code

public class DoubleMouseClick : GH_Param/font>GH_Integer>

    {

       

public DoubleMouseClick()

            :

base(new GH_InstanceDescription( "Test DoubleMouseClick", "Test DoubleMouseClick", "Test DoubleMouseClick", "Extra", "Test"))

        {

        }

       

public override void CreateAttributes()

        {

            m_attributes = new HelperAttributes(this);

        }

 

       

}

You can't. Only components have inputs and outputs so you'll have to derive from GH_Component instead of GH_Param.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Basically do this.

  1. Derive from GH_Component and add all the parameters you need.
  2. Create another class which derives from Grasshopper.Kernel.Attributes.GH_ComponentAttributes
  3. Override the RespondToMouseDoubleClick method on these attributes and do whatever you want to do on a double click.
  4. Override the CreateAttributes on your component class and assign these special attributes to m_attributes (as you already did).

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David

Thank you

 

In the class derived from GH_Component i made some public variables. In this way i can access the input parameters from the HelperAttribute class.

I just have one problem

 

In the HelperAttribute class i have the following code:

 

       

public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e)

        {

            Rhino.RhinoApp.WriteLine("double click called\n");

           

if (robotBeam.Calc == true)

            {

               

// new object Robot application

               

RobotApplication robApp = null;

               

for (int try_count = 0; try_count < 15; try_count++)

                {

                   

try

                    {

                        robApp =new RobotApplication();

                       

if (robApp != null) break;

                    }

                   

catch

                    {

                        robApp =null;

                        System.Threading.Thread.Sleep(100);  //  Sleep for 1/10 second to allow Robot to wake up

                    }

                }

               

if (robApp == null)

                {

                    System.Windows.Forms.MessageBox.Show("ERROR : Unable to open an instance of Robot\nRobot needs to be installed on your machine for this function to work");

                   

return;

                }

 

               

//if Robot is not visible

               

if (robotBeam.Visible == true)

                {

                   

//set robot visible and allow user interaction

                    robApp.Visible = 1;

                    robApp.Interactive = 1;

                }

However in the scope if (robApp == null) I get an error:

An object of type convertible to 'Grasshopper.GUI.Canvas.GH_ObjectResponse' is required

on the line with the return statement.

 

How can I fix this

And how to send data from the HelperAttribute class back to the Main component class, to be used as an output parameter for the component?

I suppose you could just write data to a public or internal field on the component class and then use that field from within SolveInstance.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I tried that, but cannot make it work

 

I tried this in the HelperAttributes Class

// assign length to the RobotBeam class??
robotBeam.length = length;
// thought this might work but it did not
//base.Owner.Params.Output[0] = length;

 

and simply added this to the Main component class in the SolverInstance

DA.SetData(0,"" + length);

Any suggestions?

You cannot assign data to parameters willy-nilly. The data inside parameters will get nuked whenever a new solution starts. You need to store data in a field you own and then read this field during SolveInstance and assign it to the appropriate output.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

It works partially when I use the script above

However I have to recompute the solution in grasshopper for the solverinstance to update.

As far as I can see it will not update automatically when running the RespondToMouseDoubleClick in the HelperAttributes Class

 

How can I trigger this to update automatically when double clicking the component?

I managed to trigger an update of the component with this line:

robotBeam.ExpireSolution(true);

You don't have to add a \n to RhinoApp.WriteLine() incidentally, it already appends a newline char.

RespondToMouseDoubleClick has to return a value. You cannot just use return;

that's the error you get. Please return an actual GH_ObjectResponse. I think in your case you always want to return Handled, even if the double click didn't work.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

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