Question for .NET masters. I write the next wrappers:
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(XrefLib.XrefClass))]
namespace XrefLib
{
class Wrapper
{
static int version = Autodesk.AutoCAD.ApplicationServices.Application.Version.Major;
// For AutoCAD 2006:
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
EntryPoint = "?acedXrefUnload@@YA?AW4ErrorStatus@Acad@@PBD_NPAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefUnload16(string XrefBlockname, bool bQuiet, IntPtr db);
// For AutoCAD 2007:
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode,
EntryPoint = "?acedXrefUnload@@YA?AW4ErrorStatus@Acad@@PB_W_NPAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefUnload17(string XrefBlockname, bool bQuiet, IntPtr db);
// For AutoCAD 2006:
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
EntryPoint = "?acedXrefReload@@YA?AW4ErrorStatus@Acad@@PBD_NPAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefReload16(string XrefBlockname, bool bQuiet, IntPtr db);
// For AutoCAD 2007:
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode,
EntryPoint = "?acedXrefReload@@YA?AW4ErrorStatus@Acad@@PB_W_NPAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefReload17(string XrefBlockname, bool bQuiet, IntPtr db);
// For AutoCAD 2006:
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
EntryPoint = "?acedXrefBind@@YA?AW4ErrorStatus@Acad@@PBD_N1PAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefBind16(string XrefBlockname, bool bInsertBind, bool bQuiet, IntPtr db);
// For AutoCAD 2007:
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode,
EntryPoint = "?acedXrefBind@@YA?AW4ErrorStatus@Acad@@PB_W_N1PAVAcDbDatabase@@@Z")]
extern public static ErrorStatus acedXrefBind17(string XrefBlockname, bool bInsertBind, bool bQuiet, IntPtr db);
static public ErrorStatus acedXrefUnload(string XrefBlockname, bool bQuite, Database db)
{
switch (version)
{
case 16: return acedXrefUnload16(XrefBlockname, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
case 17: return acedXrefUnload17(XrefBlockname, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
}
return ErrorStatus.NotImplementedYet;
}
static public ErrorStatus acedXrefReload(string XrefBlockname, bool bQuite, Database db)
{
switch (version)
{
case 16: return acedXrefReload16(XrefBlockname, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
case 17: return acedXrefReload17(XrefBlockname, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
}
return ErrorStatus.NotImplementedYet;
}
static public ErrorStatus acedXrefBind(string XrefBlockname, bool bInsertBind, bool bQuite, Database db)
{
switch (version)
{
case 16: return acedXrefBind16(XrefBlockname, bInsertBind, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
case 17: return acedXrefBind17(XrefBlockname, bInsertBind, bQuite, (db == null) ? IntPtr.Zero : db.UnmanagedObject);
}
return ErrorStatus.NotImplementedYet;
}
}
public class XrefClass
{
[CommandMethod("X_UNLOAD")]
static public void XUnload()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptStringOptions pr = new PromptStringOptions("\nType XrefBlockName: "); pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status == PromptStatus.OK)
{
ErrorStatus es = Wrapper.acedXrefUnload(res.StringResult, true, HostApplicationServices.WorkingDatabase);
if (es != ErrorStatus.OK)
{
ed.WriteMessage("\nError in unloading xref <{0}>: {1}", res.StringResult, es);
}
}
}
[CommandMethod("X_RELOAD")]
static public void XReload()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptStringOptions pr = new PromptStringOptions("\nType XrefBlockName: "); pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status == PromptStatus.OK)
{
ErrorStatus es = Wrapper.acedXrefReload(res.StringResult, true, HostApplicationServices.WorkingDatabase);
if (es != ErrorStatus.OK)
{
ed.WriteMessage("\nError in reloading xref <{0}>: {1}", res.StringResult, es);
}
}
}
[CommandMethod("X_BIND")]
static public void XBind()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptStringOptions pr = new PromptStringOptions("\nType XrefBlockName: "); pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status == PromptStatus.OK)
{
ErrorStatus es = Wrapper.acedXrefBind(res.StringResult, false, true, HostApplicationServices.WorkingDatabase);
if (es != ErrorStatus.OK)
{
ed.WriteMessage("\nError in binding xref <{0}>: {1}", res.StringResult, es);
}
}
}
}
}
It is interesting that sometime ErrorStatus es has wrong value. For example, while unloading nonexisting in this drawing xref name. Who can explain me what is wrong? In native ObjectARX equivalent I get eNotImplementedYet for this situation. In .NET I get different values, such as number 156168256, which can not be member of enum Acad::ErrorStatus.