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

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Open drawing for a limited time
« on: September 12, 2006, 02:13:03 PM »
Can anyone give me some insight why this doesn't work?  I'm thinking that the timer is going out of scope, so that the timer event never fires.  If this is true, how can I code it so that it will work?

Thanks in advance.
Code: [Select]
using System;
using System.Diagnostics;
using System.Timers;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass (typeof (Test.TimeLimitOpen))]

namespace Test
{
/// <summary>
/// Description of TimeLimitOpen.
/// </summary>
public class TimeLimitOpen
{
public string[] DwgList;
System.Timers.Timer CountDownTimer = new System.Timers.Timer();
DocumentCollection DocMan = AcadApp.DocumentManager;
int cnt = 1;

public void OpenNext (object sender, ElapsedEventArgs e) {
DocMan.MdiActiveDocument.CloseAndDiscard();
Document NewDoc = DocMan.Open (DwgList[cnt], true);
DocMan.MdiActiveDocument = NewDoc;
++cnt;
if (cnt > DwgList.Length) {
CountDownTimer.Enabled = false;
CountDownTimer.Dispose ();
}
      }

[CommandMethod ("TestTimer", CommandFlags.Session)]
public void Main() {
CountDownTimer.Elapsed += new ElapsedEventHandler (OpenNext);
      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 != null) {
Document NewDoc = DocMan.Open (DwgList[0], true);
if (NewDoc != DocMan.MdiActiveDocument) {
DocMan.MdiActiveDocument = NewDoc;
}
//CountDownTimer.Interval = 300000;
CountDownTimer.Interval = 10000;
CountDownTimer.Enabled = true;
MessageBox.Show (CountDownTimer.Enabled.ToString());
      }
}
}
}
Tim

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

Please think about donating if this post helped you.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Open drawing for a limited time
« Reply #1 on: September 12, 2006, 04:02:54 PM »
Hey Tim,
Make your timer static.
Bobby C. Jones

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #2 on: September 12, 2006, 04:50:54 PM »
Hey Tim,
Make your timer static.
Thanks Bobby, but how would I do that?  I tried
Code: [Select]
static System.Timers.Timer CountDownTimer = new System.Timers.Timer();
But it didn't work.
Tim

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

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Open drawing for a limited time
« Reply #3 on: September 12, 2006, 05:37:07 PM »
you can try and set your timer in the constructor.
Just declare your timer in your class and create a 'new' timer in the constructor, here you can also add your event handler for your timer and intrval settings (or have them as properties to set/get). Then in your destructor you can remove the handler.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #4 on: September 12, 2006, 05:54:33 PM »
you can try and set your timer in the constructor.
Just declare your timer in your class and create a 'new' timer in the constructor, here you can also add your event handler for your timer and intrval settings (or have them as properties to set/get). Then in your destructor you can remove the handler.

I don't think I understand you.  I will do some research though, and get back when I think I know what you are talking about.  Sorry, still learning.
Tim

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

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Open drawing for a limited time
« Reply #5 on: September 12, 2006, 06:05:30 PM »
No prob's, I had five min's, not tested, just explaining what I meant:
Code: [Select]
using System;
using System.Diagnostics;
using System.Timers;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(Test.TimeLimitOpen))]

namespace Test
{
    /// <summary>
    /// Description of TimeLimitOpen.
    /// </summary>
    public class TimeLimitOpen
    {
        public string[] DwgList;
        System.Timers.Timer CountDownTimer;
        DocumentCollection DocMan = AcadApp.DocumentManager;
        int cnt = 1;
        //- Constructor: sets up var's etc for this class before use.
        public TimeLimitOpen()
        {
            CountDownTimer = new System.Timers.Timer();
            CountDownTimer.Elapsed += new ElapsedEventHandler(OpenNext);
            CountDownTimer.Interval = 10000;
            CountDownTimer.Enabled = true;
            MessageBox.Show(CountDownTimer.Enabled.ToString());
        }
 
        ~TimeLimitOpen() // <-- Destructor, do clean up here.
        {
            CountDownTimer.Elapsed -= new ElapsedEventHandler(OpenNext);
        }
        public void OpenNext(object sender, ElapsedEventArgs e)
        {
            DocMan.MdiActiveDocument.CloseAndDiscard();
            Document NewDoc = DocMan.Open(DwgList[cnt], true);
            DocMan.MdiActiveDocument = NewDoc;
            ++cnt;
            if (cnt > DwgList.Length)
            {
                CountDownTimer.Enabled = false;
                CountDownTimer.Dispose();
            }
        }

        [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);
                if (NewDoc != DocMan.MdiActiveDocument)
                {
                    DocMan.MdiActiveDocument = NewDoc;
                }             
            }
        }
    }
}
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #6 on: September 12, 2006, 06:31:47 PM »
Thanks Mick.  I doesn't work either.  I think I know what you did, but will do some reading to make sure I know.

I'm starting to think this can't be done.  I'm thinking that it thinks the command is done once it opens the first drawing, so the timer event never gets firered.
Tim

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

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Open drawing for a limited time
« Reply #7 on: September 12, 2006, 06:40:48 PM »
Ok, what exactly are you trying to do, the code above is a bit 'spagetti' like.
Do you want to set a timer to open drg's or to close them after a certain time?
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #8 on: September 12, 2006, 06:46:44 PM »
Ok, what exactly are you trying to do, the code above is a bit 'spagetti' like.
Do you want to set a timer to open drg's or to close them after a certain time?
I want to select a certain amout of drawings.  Open one up for a predetermined amout of time, and the close that one, and open the next.  This is related to a post in the lisp area.  I don't think it can be done with lisp and reactors, and since I just did a count down program, I thought this wouldn't be to hard.  Guess I was wrong.  :-D

The post wanted to be able to use the 3dorbit command while the drawings were open.  I thought I would work on that after I got this part done.

Thanks Mick.

Edit: Here is the link to original question.
« Last Edit: September 12, 2006, 06:48:19 PM by T.Willey »
Tim

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

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Open drawing for a limited time
« Reply #9 on: September 12, 2006, 07:01:37 PM »
Ok, this is how I would approach it, you need a function to get a list of drawing and a function to manage each drawing (open/close).
Given that we can create a list of drg's we can then create a method to open/view/close each drawing.
Inside your open/close method just create a timer object that counts 'up' to a certain time then closes the doc (passed in as an argument to the open/close method). There's no need for an event handler.

eg.
public OpenClose(string filename)
{
     //create and set your timer:
     doc = OpenFile(filename);
     while(time != totaltime)
    {
        //do something with doc, another method perhaps:
    }
    close doc;
}

try to break your class into simple functions/methods. A Function/method should only perform *1* task, if it strays off doing other tasks it's time to split it up again. If you keep it simple like this it's much easier to handle and debug.

<edit> added note to create timer in method </edit>
« Last Edit: September 12, 2006, 07:14:42 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #10 on: September 12, 2006, 07:08:40 PM »
Thanks again Mick.  I might not be able to post again until tomorrow.  I go home in 30 minutes, and no cad there, but I will try this now.
Tim

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

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Open drawing for a limited time
« Reply #11 on: September 12, 2006, 07:17:26 PM »
no prob's, note though that this is a simple explaination, there may be many other things to consider such as the user hitting esc, changing doc's etc. but again, having simple methods makes this easier to handle ;)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #12 on: September 13, 2006, 12:05:09 PM »
I don't think this method will work with the 3dorbit command, which is what is wanted right now.  I think it requires a timer event, but I can't figure out how to cancel the command.  I have asked on the Adesk .Net forums, since this place (.Net forum @ theswamp) seems to be popping when I'm about to leave.

Thanks again.

Edit:  But if anyone shows up here, and knows how to cancel the 3dorbit command, please post.  Thanks.
« Last Edit: September 13, 2006, 12:09:38 PM by T.Willey »
Tim

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

Please think about donating if this post helped you.

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Open drawing for a limited time
« Reply #13 on: September 13, 2006, 12:17:48 PM »
I, for the life of me, can not figure out why you'd be using a timer if you are going to go in and edit the drawing anyway.  Using a timer just limits the amount of time you can do something, plus unless you have the program set up to resume the timer after your done editing I don't see how the program can be running the timer and you editing the drawing at the same time.

Now if you are having the program do everything for you, once again you don't need a timer as you can have it fire off the rest of the code once you've done your edits.

Can you explain what you are trying to accomplish w/ the program?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #14 on: September 13, 2006, 12:28:50 PM »
I, for the life of me, can not figure out why you'd be using a timer if you are going to go in and edit the drawing anyway.  Using a timer just limits the amount of time you can do something, plus unless you have the program set up to resume the timer after your done editing I don't see how the program can be running the timer and you editing the drawing at the same time.

Now if you are having the program do everything for you, once again you don't need a timer as you can have it fire off the rest of the code once you've done your edits.

Can you explain what you are trying to accomplish w/ the program?
Sure.
The program is going to used in a presentation.  The user will select the drawings, the program will open them (read-only), issue the 3dorbit command.  Then it will change to the next drawing, either when the 3dorbit command has ended, or the timer even fires.

Hope that is clear.
Tim

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

Please think about donating if this post helped you.