I gave my question to the ADN, and this is the code they said to use. It works, to a certain degree. It will not cancel the 3dOrbit command unless I add a MessageBox in the command cancel function though. I have tried other ways to get the code to look at Acad, but none seem to work.
Can anyone tell me how to get this to work without the MessageBox?
Thanks in advance.
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.TimeLimitOpenv04))]
namespace Test
{
///
/// Description of TimeLimitOpen.
///
public class TimeLimitOpenv04
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedPostCommand@@YAHPBD@Z")]
extern static public int acedPostCommand(string cmd);
private static string[] DwgList;//<---Modification
private static DocumentCollection DocMan = AcadApp.DocumentManager;
private static Document NewDoc;
private static int cnt = 0; //<---Modification
System.Timers.Timer CountDownTimer;
//- Constructor: sets up var's etc for this class before use.
public TimeLimitOpenv04()
{
CountDownTimer = new System.Timers.Timer();
CountDownTimer.Elapsed += new ElapsedEventHandler(CancelCommand);
CountDownTimer.Interval = 15000;
}
~TimeLimitOpenv04() // <-- Destructor, do clean up here.
{
CountDownTimer.Elapsed -= new ElapsedEventHandler(CancelCommand);
}
[CommandMethod("reopen", CommandFlags.Session)]
public void reopen()
{
try
{
TimeLimitOpenv04.NewDoc.CloseAndDiscard();
TimeLimitOpenv04.NewDoc = TimeLimitOpenv04.DocMan.Open (DwgList[cnt], true);
if (TimeLimitOpenv04.NewDoc != TimeLimitOpenv04.DocMan.MdiActiveDocument)
{
TimeLimitOpenv04.DocMan.MdiActiveDocument = TimeLimitOpenv04.NewDoc;
}
TimeLimitOpenv04.NewDoc.SendStringToExecute ("_.3dorbit\n", true,false,true);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void CancelCommand(object sender, ElapsedEventArgs e)
{
//IntPtr hWnd = FindWindow (null,TimeLimitOpenv04.NewDoc.Name);
//SetForegroundWindow (hWnd);
MessageBox.Show ("CancelCommand start");
//acedPostCommand ("CancelCmd");
if (TimeLimitOpenv04.NewDoc != TimeLimitOpenv04.DocMan.MdiActiveDocument)
{
TimeLimitOpenv04.DocMan.MdiActiveDocument = TimeLimitOpenv04.NewDoc;
}
// Close document
CountDownTimer.Enabled = false;
++TimeLimitOpenv04.cnt; // used for next document's index
// Stop timer if all documents were opened.
if (TimeLimitOpenv04.cnt >= (TimeLimitOpenv04.DwgList.Length))
{
TimeLimitOpenv04.cnt = 0;
}
//SetForegroundWindow (hWnd);
//MessageBox.Show ("About to open next drawing.");
CountDownTimer.Enabled = true;
TimeLimitOpenv04.NewDoc.SendStringToExecute ("\x03reopen\n", true,false,true);
}
[CommandMethod("TestTimer4", 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();
TimeLimitOpenv04.DwgList = Dia.GetFilenames();
if (TimeLimitOpenv04.DwgList.Length > 1)
{
CountDownTimer.Enabled = true;
TimeLimitOpenv04.NewDoc = TimeLimitOpenv04.DocMan.Open (DwgList[0], true);
if (TimeLimitOpenv04.NewDoc != TimeLimitOpenv04.DocMan.MdiActiveDocument) {
TimeLimitOpenv04.DocMan.MdiActiveDocument = TimeLimitOpenv04.NewDoc;
}
TimeLimitOpenv04.NewDoc.SendStringToExecute ("_.3dorbit\n", false, false, true);
}
}
}
}