Author Topic: Open drawing for a limited time  (Read 13398 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: 3636
  • (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: 3636
  • (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: 3636
  • (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: 3636
  • (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: 3636
  • (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.

nivuahc

  • Guest
Re: Open drawing for a limited time
« Reply #15 on: September 13, 2006, 12:29:24 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?

Actually, he's trying to accomplish something for me. I'll attempt to explain it here, hopefully in better detail than I did in my previous post in the Lisp forum.

A friend of mine is an artist. He does a lot of 3D work. He's your typical starving artist and he has a gallery show coming up soon with some of his sculptures and various other art work.

He wanted to setup a touch screen display with some of his 3D models on view for gallery patrons to interact with. He wants to have a list of drawings created, the first one opened, the 3DOrbit command invoked, then stopped after a period of 5 minutes or so, then the next drawing opened, etc.

The only interface available to the patrons will be the touch screen. They cannot hit the [ESC] key or right-click to cancel the 3DOrbit command.

I've got this (mostly) figured out with Lisp already, it's just the cancelling of the 3DOrbit command that's causing the hangup.

3DOrbit isn't an editing command (per se) as much as it's a viewing command. AutoCAD will see it as an edit because the view has changed... but it won't be saved and the patrons won't actually be editing anything.


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #16 on: September 13, 2006, 12:34:57 PM »
I still believe that you won't be able to do it with just lisp, as it won't be able to check anything while the 3dorbit command is in use.  That is why I think you need a timer event, but I can't figure out how to cancel the command when the time ends.
Tim

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

Please think about donating if this post helped you.

nivuahc

  • Guest
Re: Open drawing for a limited time
« Reply #17 on: September 13, 2006, 12:42:38 PM »
I may just bang something out with AutoIt for the time being. I'm still curious to figure out a (native to AutoCAD) solution for this though. :)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Open drawing for a limited time
« Reply #18 on: September 13, 2006, 12:48:06 PM »
I want to apologize to both Tim and Chuck for my post.  I should have read Chucks other post cause I know it was mentioned.  Thanks to both of you for clearing it up for me.

Question....Can't you set up the timer to halt and resume on mouse movement?  Then set up a timer for when the mouse is moving and when it is not?  Have those to work together to get your 5 minutes or whatever you need for metric time? ^-^

nivuahc

  • Guest
Re: Open drawing for a limited time
« Reply #19 on: September 13, 2006, 12:56:00 PM »
It's not a timer issue, from what I can see. It's a "cancel the 3DOrbit command" issue (again, from what I can see).

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #20 on: September 13, 2006, 01:13:30 PM »
I want to apologize to both Tim and Chuck for my post.  I should have read Chucks other post cause I know it was mentioned.  Thanks to both of you for clearing it up for me.

Question....Can't you set up the timer to halt and resume on mouse movement?  Then set up a timer for when the mouse is moving and when it is not?  Have those to work together to get your 5 minutes or whatever you need for metric time? ^-^
No problem Greg, and I have to agree with Chuck.
It's not a timer issue, from what I can see. It's a "cancel the 3DOrbit command" issue (again, from what I can see).
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 #21 on: September 13, 2006, 01:15:47 PM »
Why should this be a problem if the timer is running down.  Once it hits it's mark it should cancel the orbit command and close the drawing and open the new one.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #22 on: September 13, 2006, 01:33:14 PM »
Why should this be a problem if the timer is running down.  Once it hits it's mark it should cancel the orbit command and close the drawing and open the new one.
That is the problem.  I can't seem to figure out how to cancel the command.  It looks like I need to use the ObjectARX code for 'PostCommand' but I don't know how to impliment it in C#.
Tim

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

Please think about donating if this post helped you.

LE

  • Guest
Re: Open drawing for a limited time
« Reply #23 on: September 13, 2006, 02:37:58 PM »
Have you tried with: sendStringToExecute()  ?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #24 on: September 13, 2006, 02:48:03 PM »
Have you tried with: sendStringToExecute()  ?
That is how I call the 3dorbit command.  I found a way to get it to work.  I have to import something, and then I can call 'acedPostCommand' and it will cancel the command.  Found code by Tony T to do it.
Code: [Select]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedPostCommand@@YAHPBD@Z")]
extern static public int acedPostCommand(string cmd);

Then I call it like

acedPostCommand ("CancelCMD");
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 #25 on: September 13, 2006, 03:10:00 PM »
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:
Code: [Select]
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

Thanks!
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.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);
            }
        }
    }
}
Tim

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

Please think about donating if this post helped you.

LE

  • Guest
Re: Open drawing for a limited time
« Reply #26 on: September 13, 2006, 03:13:26 PM »
Have you tried with: sendStringToExecute()  ?
That is how I call the 3dorbit command.  I found a way to get it to work.  I have to import something, and then I can call 'acedPostCommand' and it will cancel the command.  Found code by Tony T to do it.
Code: [Select]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedPostCommand@@YAHPBD@Z")]
extern static public int acedPostCommand(string cmd);

Then I call it like

acedPostCommand ("CancelCMD");

OK...

Here is the arx code, just in case:

Code: [Select]
acDocManager->sendStringToExecute(acDocManager->mdiActiveDocument(), _T("\3\3"), false, false, false);

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #27 on: September 13, 2006, 03:16:13 PM »
Have you tried with: sendStringToExecute()  ?
That is how I call the 3dorbit command.  I found a way to get it to work.  I have to import something, and then I can call 'acedPostCommand' and it will cancel the command.  Found code by Tony T to do it.
Code: [Select]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedPostCommand@@YAHPBD@Z")]
extern static public int acedPostCommand(string cmd);

Then I call it like

acedPostCommand ("CancelCMD");

OK...

Here is the arx code, just in case:

Code: [Select]
acDocManager->sendStringToExecute(acDocManager->mdiActiveDocument(), _T("\3\3"), false, false, false);
Thanks Luis.
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 #28 on: September 13, 2006, 03:28:55 PM »
When it tries to close it does it hang up because it's confiming a change to the drawing and you don't have anything saying no to that?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Open drawing for a limited time
« Reply #29 on: September 13, 2006, 03:36:28 PM »
When it tries to close it does it hang up because it's confiming a change to the drawing and you don't have anything saying no to that?
It shouldn't because I use the 'CloseAndDiscard' method.  It doesn't really hang, as you can continue to do other things, but it won't proceed with the code.
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 #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.