Author Topic: Open drawing for a limited time  (Read 13341 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #30 on: September 14, 2006, 02:15:56 PM »
Couldn't figure out how to do it in pure C#, so I made a cheapy little lisp routine, and the had the C# code invoke a timer event that would fire the cancel command.  Here is the link to that post.

I will continue to look for the answer.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #31 on: October 02, 2006, 03:33:29 PM »
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.
Code: [Select]
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);
            }
        }
    }
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.