Author Topic: Need some help on wiritng a plugin  (Read 1383 times)

Offline onekk

  • CNC Jedi
  • *****
  • Posts: 510
    • View Profile
Need some help on wiritng a plugin
« on: February 23, 2019, 10:22:50 am »
Hi to all the plugin writers.

I'm developing a "bigger plugin" that incorporate many "additional CAD operation", and I want to make it to appear in a menu.

But I'm having some problems, in the plugin initialization.

I have done the plugin and it is called in the "usual" way invoking ti from the plugin menu.

It then open the main form of the plugin and then i click the buttons to execute the operations.

What I'm trying to achieve is:

I create the menu items and place them in the proper place, maybe in the top menu bar and in the "view context menu" and them at every menu invocation the proper operation is executed.

But I need to invoke the main class at the plugin initizialization and I haven't found around some source code to "use as inspiration".

Probably I'm missing some "stupid" thing but I'm stuck with this problem.

Regards

Carlo D.
Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4376
    • View Profile
Re: Need some help on wiritng a plugin
« Reply #1 on: February 23, 2019, 11:05:27 am »
Carlo,

Is this the sort of thing you need ?
The menu part goes as normal but the main plugin uses,  "CamBam.ThisApplication.TopWindow.Load" event;

Code: [Select]
namespace AutoSave
{
    public class autosave
    {
        public static void InitPlugin(CamBamUI ui)
        {
            CamBam.ThisApplication.TopWindow.Load += TopWindow_Load;
            ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
            toolStripMenuItem.Text = "AutoSave";
            toolStripMenuItem.Click += new EventHandler(plugin_clicked);
            ui.Menus.mnuPlugins.DropDownItems.Add(toolStripMenuItem);
        }

        public static void plugin_clicked(object sender, EventArgs e)
        {
            Params.uiForm = new Form1();
            Params.uiForm.Owner = ThisApplication.TopWindow;
            Params.uiForm.Show();
        }

        public static void TopWindow_Load(object sender, EventArgs e)
        {
            MyPlugin mp1 = new MyPlugin();
            mp1.MypluginInit();
        }
    }

    public class MyPlugin
    {
      public void MypluginInit()
       {      
           // your code goes here
       }
    }
}
« Last Edit: February 23, 2019, 11:07:42 am by EddyCurrent »
Made in England

Offline onekk

  • CNC Jedi
  • *****
  • Posts: 510
    • View Profile
Re: Need some help on wiritng a plugin
« Reply #2 on: February 23, 2019, 11:14:16 am »
Thanks, I will test this solution and see if it solve my problem, but maybe tomorrow, today I have to bring my two sons at the scout reunion, and I'm gone all the afternoon.

Regards

Carlo D.
Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam

Offline onekk

  • CNC Jedi
  • *****
  • Posts: 510
    • View Profile
Re: Need some help on writing a plugin
« Reply #3 on: February 24, 2019, 09:33:50 am »
Hi Eddy, I have tried your solution, but this causes some problem in the actual code.

The plugin is based on a Form, and this form has to be initialised, but not showed, when a menu voice is clicked the form has to be set in the proper way and then shown.

But actually it is called in this manner:

Code: [Select]


public static void plugin_clicked(object sender, EventArgs e)
        {
MainForm iface = new MainForm ();
iface.Owner = CamBam.ThisApplication.TopWindow;
iface.Show();

        }

Probably a deep refactoring of the code is needed as now all the initialization are done at the form creation, but if I split the init part and left the MainForm code, all the "references" are broken and not accessible outside the MainForm.

I will try to put together a simple example to permit to experimenting with the namespaces and the invocations, and then ask for another advice on some less abstract basis and with a simpler code to discuss on.

Thanks for now and regards.

Carlo D.

 
Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4376
    • View Profile
Re: Need some help on wiritng a plugin
« Reply #4 on: February 24, 2019, 10:57:38 am »
Carlo,

You went on a wrong track.

It was nothing to do with the form.

I wanted to show this;

1. The menu part works like a normal plugin
2. But use,  public static void TopWindow_Load(object sender, EventArgs e)
 to automatically run the plugin code when CamBam opens without the user having to do anytihng.

I may be wrong but thought that is what you wanted to do.

like this;

 public static void plugin_clicked(object sender, EventArgs e)
        {
          // add code to do what you want when the menu item is clicked
        }

 public static void TopWindow_Load(object sender, EventArgs e)
        {
          // add code here to run automatically when CamBam starts
        }
« Last Edit: February 24, 2019, 11:01:57 am by EddyCurrent »
Made in England

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1322
    • View Profile
Re: Need some help on wiritng a plugin
« Reply #5 on: February 24, 2019, 11:25:04 am »
Hi Carlo
this is the complete code from class1.cs which is seperate to the form.cs .
Code: [Select]
using System.ComponentModel;
using System.Data;
using CamBam.CAD;
using CamBam.Geom;
using CamBam.UI;
using System.Timers;
using CamBam.Library;

namespace Collision_Detector
{

    public class Demo
    {
        public Demo()
        {
        }

        public static void InitPlugin(CamBamUI ui)
        {
            //MenuItem demomi = new MenuItem();
           
            ToolStripMenuItem mydemomi = new ToolStripMenuItem();
            mydemomi.Text = "Collision Detector VER 1.3.5";
            mydemomi.Click += new EventHandler(plugin_clicked);
            ui.Menus.mnuPlugins.DropDownItems.Add(mydemomi);
        }

        public static void plugin_clicked(object sender, EventArgs e)
        {
            Form1 iface = new Form1();
            iface.Owner = CamBam.ThisApplication.TopWindow;
            iface.Show();
           
        }
     
       
    }
}

and there's some initialisation  in the main form.
Code: [Select]
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            label1.Text = "Current Drawing Units are " + CamBamUI.MainUI.ActiveView.Grid.DrawingUnits.ToString();
            string emptyStr = String.Empty;
            string fn = CamBamUI.MainUI.ActiveView.CADFile.Filename + emptyStr;
            int fn1 = fn.Length;
            if (fn1 != 0)
            {
                string Drawingpath = CamBamUI.MainUI.ActiveView.CADFile.Filename.ToString();
                label2.Text = "Current File Path " + Drawingpath;
            }
            else { label2.Text = "NO File Loaded "; }
     
            Update_Layers();
           

        }
this is where you can initialise your plugin stuff.
Hope this helps
Dave