Windows Form PlugIn

Hi everyone.
I’m trying to write a plug-in for Rhino 5 in C# using Windows Form. I’m pretty new both in Rhino Plug-In and C#.
In the figure is shown first version of my plug-in.

The first column contains document layers in a ComboBoxColumn of a DataGridView while in the second column the user can write some values. When button run is clicked the script get objects with respect to the layers chosen in the Elements column.
First Question:
For example the code behind the Update button is

public void UpdateLayers()
        {
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).Items.Clear();
            var Layers = RhinoDoc.ActiveDoc.Layers;

            foreach (var layer in Layers)
            {
                ((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).Items.Add(layer.Name);
            }
        }

Can I write this code (and in general all command that I want to pass to the windows form command) in another C# file and after call in the namespace of the form and pass it to the button?

Second question
How can I raise exception with the windows form? If I use

 throw Exception
Rhino crash.

I hope I made myself clear

Thanks
Matteo

Hi Matteo,

I’m not sure we have enough information to answer this. But it is possible for one or more commands to access a docking panel, if that is the question.

Why do you want to raise an exception? Who is going to catch it? What are you trying to do and why?

Hi David,
thanks for the reply and sorry for bad explication.

I solved this. In fact it was very simple. I added a new file class (Class1.cs) to my project with a RhinoCommand like this one:


using Rhino

namespace Plug_Inv001
{
class Class1
{
public void Run()
{
RhinoApp.WriteLine(“Hello!!”);
}
}
}

and I passed this command to my button click event that is in the UserControl file


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Rhino;

namespace Plug_Inv001
{
[System.Runtime.InteropServices.Guid(“1EA2DD99-2DD8-4E68-B2CE-A50813DD845D”)]
public partial class FDM: UserControl
{
public FDM()
{
InitializeComponent();
}
}

private void btnRun_Click(object sender, EventArgs e)
{
    Class1 test = new Class1();
    test.Run();
}

}

I’d like to get objects that are in layers specified in my table and if there are no objects in a particular layer I’d like to stop the execution an write in the Rhino command line a message like “No objects in layer x”.


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Rhino;

namespace Plug_Inv001
{
[System.Runtime.InteropServices.Guid(“1EA2DD99-2DD8-4E68-B2CE-A50813DD845D”)]
public partial class FDM: UserControl
{
public FDM()
{
InitializeComponent();
}
}

private void btnRun_Click(object sender, EventArgs e)
{
    FdmData data = GetData();
    List<string> els = data.ElementLayrs;
    List<string> q = data.Q;
    
    RhinoObjcts[] rhElements = RhinoDoc.ActiveDoc.Objects.FindByLayer(els[0]);
    
    if (rhElements.Length == 0)
    {
        //Stop execution and write what is the problem
    }
    else
    {
        // Execution of the plug-in
    }
}

}

I don’t know how to implement the command to stop execution and write in the commnd line why the plug-in is stopped. Clear I can’t use Rhino.Commands.Result.Failure and if I use throw Exception rhino crashed. Any Idea?

Matteo

Well in this case, a command isn’t running. Rather, your button was clicked. So just return from the function…