I've got a form that has the ability to open a drawing, but have run into a complication in the event there is an active command so I've attempted to cancel any potentially active command before opening the document
if (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Count > 0)
{
try
{
//acedCommand(new object[3] {(int)5005, String.Format("{0}{0}", (char)27), 0});
acedCommand
(new object[1] { 0 }); //AcDb.ResultBuffer args = new AcDb.ResultBuffer();
//args.Add(new AcDb.TypedValue(5005, String.Format("{0}{0}", (char)27)));
//Command(args);
//acedPostCommand("CANCELCMD");
//acedPostCommand(String.Format("{0}{0}", (char)27));
//System.Threading.Thread.Sleep(0);
//Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute(String.Format("{0}{0}", (char)27), true, false, true);
//((AcadDocument)((Document)Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument).GetAcadDocument()).SendCommand(String.Format("{0}{0}", (char)27));
//System.Threading.Thread.Sleep(500);
}
catch (System.Exception ex)
{
MessageBox.Show(String.Format("{0}", ex));
}
}
try
{
((DocumentCollection)Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager).Open(((FileInfo)item.Tag).FullName, false);
}
catch (System.Exception ex)
{
MessageBox.Show(String.Format("{0}\n{1}", ((FileInfo)item.Tag).FullName, ex));
}
I get an error COMExeption Invalid execution context and the drawing does not open.
First I tried to find a method of testing for an active command without any luck, would have liked to just tell the user to finish what the hell they were doing before opening another doc.
Then I tried using the SendCommand method through the COM interface. I still get the same error, however after getting the error I can see at the command line that the escape command has been sent. I tried to remedy this with a Thread.Sleep call, but I still get the error. The drawing does however load, but then the focus switches back to the original document, which is pretty user unfriendly :realmad:
Then I tried using acedPostCommand as was suggested in
this post by Kean but I still get the same results.
Then I tried using acedCmd as was suggested
here by Tony, as below:
[System.Runtime.InteropServices.DllImport("accore.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint = "acedCmd")]
extern static int acedCmd(IntPtr pResbuf);
unsafe static int Command(Autodesk.AutoCAD.DatabaseServices.ResultBuffer args)
{
if (!Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.IsApplicationContext)
return acedCmd((IntPtr)args.UnmanagedObject.ToPointer());
else
return 0;
}
//Then in my routine:
AcDb
.ResultBuffer args
= new AcDb
.ResultBuffer(); args
.Add(new AcDb
.TypedValue(5005,
String.Format("{0}{0}",
(char)27))); Command(args);
But I'm getting the COM error still and the following error posted to the command line of the active document: Invalid type in acutBuildList() arg #1
Although, there were a couple times where for some unknown reason when I manually cancelled the command the document opened
Then I tried using acedCommand as follows with no sign of any command being sent to execute, same COM error:
[System.Runtime.InteropServices.DllImport("accore.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint = "acedCommand")]
extern static int acedCommand(object[] args);
//Then in my routine:
acedCommand
(new object[3] {(int)5005,
String.Format("{0}{0}",
(char)27),
0});
//Also tried passing just 0 as mentioned in the docs to just cancel a command
acedCommand
(new object[1] {0});
Can anyone comment on why it is that this is throwing a COM exception when I'm running purely through .NET by calling the .Open method?
Thanks