She's a goer! (for C# at least, I'm sure it would work for other .net languages though)
For some reason though it crashes the first time I load after a rebuild?? After that it loads and runs fine.
Here's a C# example of using the dll
// By MickD 27 Aug 2008. Use at your own risk!
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using PyAcadNET.PyAcadNET; // our ref'd in dll to load commands
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace PytestCmds
{
public delegate void callback(); // define a simple callback object, it can be used many times, see examples below.
public class Commands
{
public Commands()
{
//
// TODO: Add constructor logic here
//
}
// Define a Command
static public void test1()
{
// Put your command code here
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nMessage from delegate method 1");
}
// Define another Command
static public void test2()
{
// Put your command code here
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nMessage from delegate method 2...w00t!!");
}
}
public class App : IExtensionApplication
{
public void Initialize()
{
try
{
// Load our test callbacks:
// 1
callback py_clbk1 = new callback(Commands.test1);
PyAcadNET.PyAcadNET.PyCommands.load_command("PYCMDS", "PYTEST1", "PYTEST1",
(int)CommandFlags.Modal, py_clbk1);
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nLoaded command PYTEST1 successfully!");
// and another one:
callback py_clbk2 = new callback(Commands.test2);
PyAcadNET.PyAcadNET.PyCommands.load_command("PYCMDS", "PYTEST2", "PYTEST2",
(int)CommandFlags.Modal, py_clbk2);
ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nLoaded command PYTEST2 successfully!");
}
catch
{
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nFailed to load callback");
}
}
public void Terminate()
{
//empty
}
}
/// <summary>
/// Summary description for Commands.
/// </summary>
}
When Autocad loads a net dll it looks through the dll for attributes that define commands and probably does something similar to what we're doing here. As IronPython doesn't use attributes we need a work around, hopefully this is it.
How it works -
Another thing acad does when it loads the dll is it looks for an 'IExtensionApplication' interfaced class, you can only have one per dll but that's all you need to get acad and your dll talking together (as with any dll, there must be some type of 2 way interface code). The best part is that it can be used to load commands automatically at loading so all we have to do is define our commands, call the 'load_command' method in the Initialize() method of the IExtensionApplication class and you're ready to rock.
If someone wants to have a crack at writing an IronPython one for a test that would be great.
EDIT: I think the cause of the crash is coming from garbage collection, if I leave it for a while and come back 'boom' crash.
What's the best way to 'pin' the delegate/s from the garbage collector using 'safe' code?