Author Topic: Time and Date  (Read 3445 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Time and Date
« on: December 26, 2013, 12:00:17 PM »
Hello guys .  :-)

Is there a way to read the date and time from the computer with every minute or a second ?

That may need a reactor ? I have no idea .

Many thanks in advance .

Bhull1985

  • Guest
Re: Time and Date
« Reply #1 on: December 26, 2013, 12:38:27 PM »
Please look into the "Cdate" system variable.
This returns the current date and time and is read only
It returns in the following format:

YYYYMMDD.HHmmSSSS
Y=year
M=month
d=day
h=hour
m=minute
s=seconds

Coder

  • Swamp Rat
  • Posts: 827
Re: Time and Date
« Reply #2 on: December 26, 2013, 12:44:10 PM »
Thanks for your reply  :-)

I need the timing to be updated in a text with every minute , is that possible ?

Quote
(menucmd "m=$(edtime,$(getvar,date),HH:MM:SS)")

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Time and Date
« Reply #3 on: December 26, 2013, 07:13:14 PM »
I need the timing to be updated in a text with every minute , is that possible ?

You could use a Field Expression or RTEXT containing an appropriate DIESEL expression, however, this would still require some user interaction in AutoCAD in order to be updated (regen), and will not update automatically when AutoCAD is idle.

Coder

  • Swamp Rat
  • Posts: 827
Re: Time and Date
« Reply #4 on: December 28, 2013, 02:52:40 AM »
I need the timing to be updated in a text with every minute , is that possible ?

You could use a Field Expression or RTEXT containing an appropriate DIESEL expression, however, this would still require some user interaction in AutoCAD in order to be updated (regen), and will not update automatically when AutoCAD is idle.

Hi Lee .

I saw the Clock routine that you have on your lovely website and you have used a reactor to run the clock without any user interaction , and this is why I have said that it may need a reactor in my first post .  :-)

I don't know how to use reactors .  :-(

I hope you don't mind and have time to gather the reactor with the required codes .

Many thanks .

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Time and Date
« Reply #5 on: December 28, 2013, 08:53:00 AM »
Sounds like your looking for a continuously operating counting type mechanism, to be output as a line of text and updated by whatever time you want, similar to the AutoSave function in ACAD (except updates the text line), maybe it could be a block with a time attribute and the attribute gets updated.  (sounds like a performance robbing function)

Would it be possible to have the reactor fire just prior to saving, closing, changing tabs or printing the file, instead of continuously. (the need to have it working all the time as opposed to tied to an event or events seems like overkill)
« Last Edit: December 28, 2013, 08:57:50 AM by snownut2 »

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Time and Date
« Reply #6 on: December 29, 2013, 06:02:59 AM »
Field is a good way for that (attached)

or use this lisp http://www.lee-mac.com/clock.html

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Time and Date
« Reply #7 on: December 30, 2013, 05:46:03 PM »
Hi,

Some times ago I tried to build a timer for LISP with .NET.
I didn't test it deeply but it seems to work fine.
The attached ZIP files contains 2 DLLs: LispTimer_2011.dll for A2011-2012 and LispTimer_2013.dll for A2013+.
The DLL have to be loaded (using NETLOAD for example) in the current AutoCAD session.

Two LISP functions are defined:
- gc-TimerOn to start the timer. It requires 2 arguments: the name (string) of the LISP function to be run at time interval and the interval value in seconds (integer). Don't set an interval less than 5 seconds if you want to keep on working in AutoCAD.
The LISP function have to be prefixed with 'c:' or registered with vl-acad-defun to be invokable from .NET.
- gc-TimerOff to stop the timer.

Here's an example which displays a "clock" in the status line. The clock updates each minute (60 seconds).
Code - Auto/Visual Lisp: [Select]
  1. ;; The function to be run at each interval
  2. (defun clock ()
  3.   (grtext -1 (menucmd "m=$(edtime,$(getvar,date),HH:MM)"))
  4. )
  5. (vl-acad-defun 'clock)
  6.  
  7. ;; Starts the clock
  8. (defun c:clockon ()
  9.   (clock)
  10.   (gc-timerOn "clock" 60) ;_ starts the timer to run "clock" each 60 seconds
  11.   (princ)
  12. )
  13.  
  14. ;; Closes the clock
  15. (defun c:clockoff ()
  16.   (gc-timerOff)
  17.   (grtext)
  18.   (princ)
  19. )

<EDIT: new release>

For those who're interested, the C# code
Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.Runtime;
  4. using System;
  5. using System.Windows.Forms;
  6. using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
  7.  
  8. [assembly: CommandClass(typeof(Gile.LispTimer.LispFunctions))]
  9.  
  10. namespace Gile.LispTimer
  11. {
  12.     public class LispFunctions
  13.     {
  14.         private Timer _timer;
  15.         private string _fun;
  16.         private int _interval;
  17.         private Document _doc;
  18.  
  19.         public LispFunctions()
  20.         {
  21.             _interval = -1;
  22.             _doc = AcAp.DocumentManager.MdiActiveDocument;
  23.             AcAp.DocumentManager.DocumentCreateStarted += onActiveDocumentChanging;
  24.             AcAp.DocumentManager.DocumentToBeDeactivated += onActiveDocumentChanging;
  25.             AcAp.DocumentManager.DocumentToBeDestroyed += onActiveDocumentChanging;
  26.         }
  27.  
  28.         private void StartTimer()
  29.         {
  30.             if (_timer == null && _interval > 0)
  31.             {
  32.                 _timer = new Timer();
  33.                 _timer.Interval = _interval;
  34.                 _timer.Enabled = true;
  35.                 _timer.Tick += OnTick;
  36.                 _timer.Start();
  37.             }
  38.         }
  39.  
  40.         private void CloseTimer()
  41.         {
  42.             if (_timer != null)
  43.             {
  44.                 _timer.Stop();
  45.                 _timer.Dispose();
  46.                 _timer = null;
  47.             }
  48.         }
  49.  
  50.         void OnTick(object sender, EventArgs e)
  51.         {
  52.             try
  53.             {
  54.                 using (_doc.LockDocument())
  55.                 {
  56.                     AcAp.Invoke(new ResultBuffer(new TypedValue((int)LispDataType.Text, _fun)));
  57.                 }
  58.             }
  59.             catch (System.Exception ex)
  60.             {
  61.                 ShowErrorMessage(ex.Message == "eInvalidInput" ?
  62.                     string.Format("Incorrect LISP function: '{0}'", _fun) :
  63.                     ex.Message);
  64.                 CloseTimer();
  65.             }
  66.         }
  67.  
  68.         void onActiveDocumentChanging(object sender, DocumentCollectionEventArgs e)
  69.         {
  70.             CloseTimer();
  71.         }
  72.  
  73.         [LispFunction("gc-TimerOn")]
  74.         public TypedValue TimerOn(ResultBuffer resbuf)
  75.         {
  76.             try
  77.             {
  78.                 if (resbuf == null)
  79.                     throw new TooFewArgsException();
  80.  
  81.                 TypedValue[] args = resbuf.AsArray();
  82.                 if (args.Length < 2)
  83.                     throw new TooFewArgsException();
  84.                 if (args.Length > 2)
  85.                     throw new TooManyArgsException();
  86.  
  87.                 if (args[0].TypeCode != (int)LispDataType.Text)
  88.                     throw new ArgumentTypeException("stringp", args[0]);
  89.                 _fun = (string)args[0].Value;
  90.  
  91.                 if (!args[1].TryConvertToInt(ref _interval))
  92.                     throw new ArgumentTypeException("numberp", args[1]);
  93.                 _interval *= 1000;
  94.  
  95.                 CloseTimer();
  96.                 StartTimer();
  97.                 return new TypedValue((int)LispDataType.Nil);
  98.             }
  99.             catch (System.Exception ex)
  100.             {
  101.                 ShowErrorMessage("gc-TimerOn\n" + ex.Message);
  102.                 throw;
  103.             }
  104.         }
  105.  
  106.         [LispFunction("gc-TimerOff")]
  107.         public TypedValue TimerOff(ResultBuffer resbuf)
  108.         {
  109.             try
  110.             {
  111.                 if (resbuf != null)
  112.                     throw new TooManyArgsException();
  113.                 CloseTimer();
  114.                 return new TypedValue((int)LispDataType.Nil);
  115.             }
  116.             catch (System.Exception ex)
  117.             {
  118.                 ShowErrorMessage("gc-TimerOff\n" + ex.Message);
  119.                 throw;
  120.             }
  121.         }
  122.  
  123.         private void ShowErrorMessage(string msg)
  124.         {
  125.             MessageBox.Show(
  126.                 msg,
  127.                 "LispTimer",
  128.                 System.Windows.Forms.MessageBoxButtons.OK,
  129.                 System.Windows.Forms.MessageBoxIcon.Error);
  130.         }
  131.     }
  132.  
  133.     static class LispFunctionExtensions
  134.     {
  135.         public static bool TryConvertToInt(this TypedValue tv, ref int i)
  136.         {
  137.             if (tv.TypeCode == (int)LispDataType.Int16 ||
  138.                 tv.TypeCode == (int)LispDataType.Int32)
  139.             {
  140.                 i = Convert.ToInt32(tv.Value);
  141.                 return true;
  142.             }
  143.             else
  144.             {
  145.                 return false;
  146.             }
  147.         }
  148.     }
  149.  
  150.     class LispException : System.Exception
  151.     {
  152.         public LispException(string msg)
  153.             : base(msg) { }
  154.     }
  155.  
  156.     class TooFewArgsException : LispException
  157.     {
  158.         public TooFewArgsException()
  159.             : base("Too few arguments") { }
  160.     }
  161.  
  162.     class TooManyArgsException : LispException
  163.     {
  164.         public TooManyArgsException()
  165.             : base("Too many arguments") { }
  166.     }
  167.  
  168.     class ArgumentTypeException : LispException
  169.     {
  170.         public ArgumentTypeException(string msg, TypedValue tv)
  171.             : base(string.Format(
  172.             "Bad argument type: {0}: {1}",
  173.             msg,
  174.             tv.TypeCode == (int)LispDataType.Nil ? "nil" :
  175.             tv.TypeCode == (int)LispDataType.Text ? "\"" + tv.Value + "\"" :
  176.             tv.Value))
  177.         { }
  178.     }
  179. }
  180.  
« Last Edit: January 03, 2014, 05:50:54 AM by gile »
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Time and Date
« Reply #8 on: January 02, 2014, 12:52:49 AM »
Thank you gile , it works very good .  :-)

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Time and Date
« Reply #9 on: January 02, 2014, 12:04:44 PM »
Hi Gile

I am using AUTOCAD 2013 and I am getting following error :-

Quote
Command: clockon
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.StubHelpers.StubHelpers.BeginStandalone(IntPtr pFrame, IntPtr pNMD, Int32 dwStubFlags)
   at acDocManagerPtr()
   at Autodesk.AutoCAD.ApplicationServices.DocumentCollection.get_MdiActiveDocument()
   at Gile.LispTimer.LispFunctions..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.InvokeLisp(); error: ADS request error

Can you help me why is it so ?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Time and Date
« Reply #10 on: January 03, 2014, 03:26:00 AM »
Hi mailmaverick,

Two Three thoughts:
- save the DLL locally on your computer rather than on a server
- unblock the DLL (right click > Properties > Unblock)
- works only with A2011 or later
« Last Edit: January 03, 2014, 05:54:29 AM by gile »
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Time and Date
« Reply #11 on: January 03, 2014, 05:55:33 AM »
I noticed some issues while switching between documents.
I updated the download, the timer stops when switching to another document.

Another example, to automatically save the drawing each 10 minutes

Code - Auto/Visual Lisp: [Select]
  1. (defun autosave (/ dir base files index filename isavebak)
  2.   (setq isavebak (getvar 'isavebak))
  3.   (setvar 'isavebak 1)
  4.   (or (vl-file-directory-p "C:\\Z_ISAVE\\") (vl-mkdir "C:\\Z_ISAVE\\"))
  5.   (setq base     (vl-filename-base (getvar 'dwgname))
  6.         bak      (strcat (getvar 'dwgprefix) base ".bak")
  7.         filename (strcat (getvar 'dwgprefix) base (menucmd "M=$(edtime,$(getvar,date),_YYYY-MO-DD-HH-MM)") ".dwg")
  8.   )
  9.   (vl-file-rename bak filename)
  10.   (princ (strcat "File saved as:\n" filename))
  11.   (princ)
  12. )
  13.  
  14. (vl-acad-defun 'autosave)
  15.  
  16. (gc-TimerOn "autosave")
« Last Edit: January 03, 2014, 06:01:16 AM by gile »
Speaking English as a French Frog

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Time and Date
« Reply #12 on: January 03, 2014, 09:07:45 AM »
Thanks Gile,

Problem resolved.

The problem was of Blocked DLL.


ROBBO

  • Bull Frog
  • Posts: 217
Re: Time and Date
« Reply #13 on: March 11, 2014, 08:30:21 AM »
Hello Gile,

This looks like a very useful tool. I am having trouble using this. I get this error when using the autosave lisp. I do not want to display the clock, but would like to autosave to a temp directory and act like a true autosave not the AutoCAD autosave that is only useful when the AutoCAD crashes.

Please can you help?

Please see error message attached when dll loaded and autosave lisp envoked.

Many thanks in advance, Robbo.
The only thing to do with good advice is to pass it on.
It is never of any use to oneself.

Oscar Wilde
(1854-1900, Irish playwright, poet and writer)