Esprit Macro With .NET

Edit: I’m having some issues with a second tutorial, so at this point I don’t think this process below is correct.  2nd Edit:  This code should work fine, the issue was when I started using COM objects from Esprit.  There are a few gotchas that need to be taken care of.  I think the primary reason I had issues was due to writing the macro in C# instead of VB (the language the Esprit tutorials have).  I may be writing about it in the future.  If you are interested right now, send a message, or post in the forums.

This post will be a little more special interest them most of my posts. I recently started learning how to create macros for a program called Esprit (D.P. Technology). It’s a “CAD/CAM” program.

They still use COM/VBA as their plug-in interface, so to write a plug-in in .NET there are a few things that need to be taken care of. Their tutorial that comes with the program goes over setting everything up, but It misses a few steps. Well more likely, it was written for the one the earlier versions of Visual Studio, but anyways I will go over the steps necessary to get a “Hello World” running.

– Create a new Visual Studio project (Class Library), call it DotNETAddInTutorial1 as the tutorial from Esprit called it.

This tutorial goes over a simple hello world plug-in using Visual Studio 2005
– Go into the IDE menu Project->DotNETAddInTutorial1 Properties…
— Under the “application” tab, click the “Assembly Information…” button to the right. The pop-up will have a check-box at the bottom that says “Make Assembly COM-Visible” Check that. (important!) Click OK to close that pop-up window.
— Under the “Build” tab, check the “Register for COM interop” too

– Right click on References in the Solution Explorer window in the IDE. Select “Add Reference…”
– Select the COM tab and add these 4 references (Esprit Type Library, EspritCommands Type Library, EspritConstants Type Library, Microsoft Add-In Designer), take note that the Esprit ones will have version numbers in the name.

– Double click your Class1.cs file in the Solution Explorer so you can edit it.
Here is how the file should look:

using System;
using System.Collections.Generic;
using System.Text;
using Extensibility;
using System.Windows.Forms;

namespace DotNETAddInTutorial1
{
    public class Class1 : IDTExtensibility2
    {
        void IDTExtensibility2.OnAddInsUpdate(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTExtensibility2.OnBeginShutdown(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTExtensibility2.OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            MessageBox.Show("Add-In Tutorial 1 Connect", "", MessageBoxButtons.OK);
        }

        void IDTExtensibility2.OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
            MessageBox.Show("Add-In Tutorial 1 Disconnect", "", MessageBoxButtons.OK);
        }

        void IDTExtensibility2.OnStartupComplete(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}

Here is a nice tip:

public class <strong>Class1 </strong>: <em>IDTExtensibility2</em>
{
}

If that is all you type into the class, you will see a blue squiggly line under the word Class1. If you right click on IDTExtensibility2 and select “Implement Interface”->”Implement Interface Explicitly,” Visual Studio 2005 will automatically generate the function tabs needed. Otherwise if you read the Esprit Documentation about this, they have a list of functions that you can type in by hand (their tutorials are in VB.NET only though).

From there you can build your solution. Copy the files in from the project’s Bin/Release folder into probably the folder “C:\Program Files\D.P.Technology\ESPRIT\AddIns\DotNETAddInTutorial1”

The last thing you need to do is create a registry file so you can let Esprit know of your plugin.
Create a text file and name it install.reg.
Right click on it and select edit.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\D.P.Technology\ESPRIT\AddIns\DotNETAddInTutorial1.Class1]
"Description"="Test Add-in using .NET C#."
"FriendlyName"="Cool Testing Add-on"
"LoadBehavior"=dword:00000001
"CincomVersion"=dword:00000000
"NETInterOpDlls"="DotNETAddInTutorial1.dll"
"COMDlls"="DotNETAddInTutorial1.dll"

I’ve noticed that the last two lines are NOT needed, but I just leave them in there anyways.

Once you’ve save the file you can right click on it again to merge it into the registry. The other option would open up regedit and do it by hand. Notice that DotNETAddInTutorial1.Class1 specifically specifies the project name and class file to call.

That’s it!

This entry was posted in General and tagged , , , , , . Bookmark the permalink.