KitchenDraw SDK 2
Show / Hide Table of Contents

Create your first plugin: Hello World

This example walks you through creating your first plugin for KitchenDraw. The tutorial shows you how to add a new item to the KitchenDraw main menu and handle its invocation.

For this example, you'll create a plugin in Visual C# to add a custom menu item that looks like this:

Prerequisites

Before you start, make sure that you have the following installed on your computer:

  • KitchenDraw / InSitu;
  • Visual Studio (any edition), configured to support C# and .NET Framework development.

Create a plugin project

  1. From the File menu, click New > Project. At the bottom of the dialog window, enter a name for your project. We use HelloWorldPlugin name in this tutorial.

  2. In the upper left part of the dialog, choose Visual C# templates, and then choose Class Library (.NET Framework) in the upper right part.

    After pressing OK in the dialog, you should see Class1.cs file of your new project.

Define the Plugin class

  1. Rename Class1.cs to Plugin.cs.

    • On newer versions of Visual Studio, you'll get the You are renaming a file prompt, offering you to rename the class contained inside as well. Click Yes.
    • If no prompt has appeared, rename your Class1 to Plugin inside the file manually.
Note

For the KitchenDraw plugins, a public class named Plugin must exist, and its namespace must necessarily match the name of the assembly file (without a path and the .dll extension).

Add a reference to the KD.SDK2.dll

  1. Add a reference to the KD.SDK2.dll assembly.

    • In Solution Explorer, right click the References node and click Add Reference.
    • In the Add Reference dialog box, select the Browse tab.
    • In the bottom the the dialog box, press Browse button.
    • In the appeared dialog box locate the KD.SDK2.dll in the folder of your KitchenDraw / InSitu installation. Then press OK.
  2. In the upper part of your Plugin.cs file, insert using KD.SDK2; directive.

    using KD.SDK2;
    
  3. On the top of your Plugin class, add the Appli _appli; field.

  4. Define the public Plugin() class constructor, and initialize the _appli field in it like that:

        public class Plugin
        {
            Appli _appli;
    
            public Plugin()
            {
                _appli = new Appli();
            }
        }
    

    The _appli instance of the Appli class will internally establish a connection to a running KitchenDraw instance. This allows you to access all the features of the KitchenDraw SDK from your plugin.

Add OnPluginLoad, OnPluginUnload event handlers

  1. Define the OnPluginLoad method to the Plugin class like following:

            public bool OnPluginLoad(int unused)
            {
                return true;
            }
    

    This method is called by the KitchenDraw / InSitu software automatically when the plugin is loaded. The return value should indicate whether the load was OK or not. The int parameter isn't in use for now.

    You'll add some useful code to the method a bit later.

  2. Define the OnPluginUnload method to the Plugin class — in the same manner:

            public bool OnPluginUnload(int unused)
            {
                return true;
            }
    

    As the names suggest, these two methods allow to perform some actions on the plugin loading and unloading. One of the most common actions here — adding a custom menu item — is described next.

Add your custom menu item handler

  1. This sample uses MessageBox class from the System.Windows.Forms assembly.

    • In Solution Explorer, right click the References node and click Add Reference.
    • In the Add Reference dialog box, select the Assemblies tab.
    • Check System.Windows.Forms assembly in the list. Press OK.
  2. Add using directive in the top of the Plugin.cs for the System.Windows.Forms namespace:

    using System.Windows.Forms;
    
  3. Add OnMyMenuItem public method to the Plugin class:

            public bool OnMyMenuItem(int unused)
            {
                MessageBox.Show("Hello, world!");
    
                return true;
            }
    

    This will be your custom menu item handler - a method which is called when a user clicks your item in the KitchenDraw / InSitu menu.

    While OnPluginLoad and OnPluginUnload method names are required to be like they are by the KitchenDraw plugin subsystem, OnMyMenuItem name was chosen arbitrarily. You can use any other name that you like for the method.

Register your menu item

  1. Add Appli.MenuItem _myMenuItem field on the top of the Plugin class.

        public class Plugin
        {
            Appli _appli;
            Appli.MenuItem _myMenuItem;
    
  2. In the OnPluginLoad method, fill in the information about your menu item. Then register it with the Appli.InsertMenuItem call:

            public bool OnPluginLoad(int unused)
            {
                var menuInfo = new Appli.MenuItemInsertionInfo();
    
                menuInfo.Text = "Hello, world!";
                menuInfo.DllFilenameWithoutPath = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
                menuInfo.ClassName = nameof(Plugin);
                menuInfo.MethodName = nameof(OnMyMenuItem);
    
                _myMenuItem = _appli.InsertMenuItem(menuInfo, Appli.MenuItem.StandardId.Help_LoadHours);
    
                return true;
            }
    

    The Appli.InsertMenuItem method adds a custom menu item to the KitchenDraw menu and returns an Appli.MenuItem instance to manage it. The instance is saved in the _myMenuItem field of the Plugin class since you'll need it to remove your menu item as soon as the plugin unloads.

    The code above uses Assembly.GetExecutingAssembly from the System.Reflection namespace and Path.GetFileName from System.IO to get the name of assembly which contains the plugin. Add the usings for the namespaces in the top of the Plugin.cs file:

    using System.IO;
    using System.Reflection;
    
  3. Add code to remove your menu item to the OnPluginUnload method.

            public bool OnPluginUnload(int unused)
            {
                _appli.RemoveMenuItem(_myMenuItem);
    
                return true;
            }
    

Configure build and debug options

  1. In Solution Explorer, right click the project node and click Properties.

  2. Click on Build tab, choose All Configurtions for Configuration, and choose x86 for Platform Target.

  3. Click on Debug tab, and in Start Action section choose Start external program. To the corresponding textbox, set the path to the kd_app.exe / cp.exe file in your KitchenDraw / InSitu installation folder.

  4. Set Enable native code debugging flag.

Compile your plugin

  1. Select Build > Build Solution menu item to compile your plugin.

Run

  1. Select Debug > Start debugging menu item.

  2. Wait until KitchenDraw / InSitu application starts.

Register your plugin in KitchenDraw / InSitu

  • Using the Plugin Manager
  • By editing Space.Ini file
  1. Select ? > Service > Plugin Manager menu item.

  2. In the appeared window, click Open icon from the toolbar

  3. Locate your plugin assembly in the appeared dialog box. Press OK.

  4. Your plugin is loaded now. Click Save button in the toolbar if you want your plugin to be loaded automatically next time KitchenDraw / InSitu starts.

    You can load/unload your plugin by clicking the checkbox on the left of its name.

  1. Open Space.Ini file from your KitchenDraw / InSitu installation folder with your favourite text editor.

  2. Find the [Plugins] section.

    This section exists by default. But if you use some custom configuration of KitchenDraw / InSitu, it may absent. In this case, create the section by typing [Plugins] in the a row in the end of the file.

  3. Add a new entry for your plugin.

    M:\HelloWorldPlugin\bin\Debug\HelloWorldPlugin.dll=1
    

    (your path may differ)

    • The path can be either absolute either relative to the KitchenDraw / InSitu installation folder.

    • Set =1 after the your plugin path. 1 means that plugin should be loaded automatically when KitchenDraw / InSitu starts. If you set 0 instead, you can load your plugin manually after KitchenDraw / InSitu starts via Plugin Manager.

Debug your plugin

  1. Set keyboard cursor to the first row of the OnMyMenuItem method and press F9.

    This should set a breakpoint.

  2. Run your plugin project with Debug > Start debugging menu item (if it's not running yet).

  3. Choose ? > Hello, world! menu item.

  4. Now your Visual Studio debugger should stop on the breakpoint:

    This way, you can examine the execution flow of your plugin methods, inspect the values of variables and fields at runtime, and even influence program execution with certain limits. You can read more about debugging in Visual Studio at Microsoft Docs.

Next steps

This walkthrough showed you how to:

  • Create a plugin project.
  • Handle plugin loading and unloading.
  • Add a custom command to menu.
  • Compile, register a plugin in KitchenDraw / InSitu, and run it.
  • Debug a plugin.

The full source code of the Plugin.cs file can be found here.

Now that you know the basics of creating plugins, you can read material about implementing more specific howtos:

  • How to export scene content to a custom .xml file
  • How to create a catalog from custom .csv file
In this article
Back to top Generated by DocFX