New problem. It will cancel the command, and it reconizes the variable 'DocMan', but it won't do anything with it. So it will stop the command, but it won't close the current drawing. Here is the code. Maybe someone can see something that I can't. Problem portion is the function 'OpenNext'. I added a message box there, and it will fire it after the command cancels. I tried to get the current drawing the long way, and that still didn't let me get a reference to it to close it.
Long way:
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Thanks!
using System;
using System.Diagnostics;
using System.Timers;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(Test.TimeLimitOpenv02))]
namespace Test
{
/// <summary>
/// Description of TimeLimitOpen.
/// </summary>
public class TimeLimitOpenv02
{
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedPostCommand@@YAHPBD@Z")]
extern static public int acedPostCommand(string cmd);
public string[] DwgList;
DocumentCollection DocMan = AcadApp.DocumentManager;
System.Timers.Timer CountDownTimer;
int cnt = 1;
//- Constructor: sets up var's etc for this class before use.
public TimeLimitOpenv02()
{
CountDownTimer = new System.Timers.Timer();
CountDownTimer.Elapsed += new ElapsedEventHandler(OpenNext);
CountDownTimer.Interval = 10000;
}
~TimeLimitOpenv02() // <-- Destructor, do clean up here.
{
CountDownTimer.Elapsed -= new ElapsedEventHandler(OpenNext);
}
public void OpenNext(object sender, ElapsedEventArgs e)
{
acedPostCommand ("CancelCmd");
DocMan.MdiActiveDocument.CloseAndDiscard();
Document NewDoc = DocMan.Open(DwgList[cnt], true);
DocMan.MdiActiveDocument = NewDoc;
++cnt;
if (cnt > DwgList.Length)
{
CountDownTimer.Enabled = false;
CountDownTimer.Dispose();
}
NewDoc.SendStringToExecute ("_.3dorbit\n", false, false, true);
}
[CommandMethod("TestTimer", CommandFlags.Session)]
public void Main()
{
Autodesk.AutoCAD.Windows.OpenFileDialog Dia =
new Autodesk.AutoCAD.Windows.OpenFileDialog(
"Select drawings to update Cloud layer", "", "dwg", "",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
Dia.ShowDialog();
string[] DwgList = Dia.GetFilenames();
if (DwgList.Length > 1)
{
Document NewDoc = DocMan.Open(DwgList[0], true);
CountDownTimer.Enabled = true;
if (NewDoc != DocMan.MdiActiveDocument)
{
DocMan.MdiActiveDocument = NewDoc;
}
NewDoc.SendStringToExecute ("_.3dorbit\n", false, false, true);
}
}
}
}