Lisp to run a command periodically

Lisp to run a command periodically

tanvirme2k9
Contributor Contributor
1,380 Views
10 Replies
Message 1 of 11

Lisp to run a command periodically

tanvirme2k9
Contributor
Contributor

Hi,

I have a lisp which reloads all existing xrefs in the drawing. What I want is to run that command automatically after a period of time (like the auto save command) so that I need not to reload the xref manually.

 

Spoiler
(defun c:XRE ()
(command "-Xref" "reload" "*")
)

Thanks in advance.

0 Likes
1,381 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Start routine can be associated with something you do often manually. For example, saving or regen.

0 Likes
Message 3 of 11

tanvirme2k9
Contributor
Contributor
How to make my custom 'XRE' command to work like saving or regen? i.e it will reload all xref after 10 or 15 minutes periodically.
0 Likes
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

I think it's a good practise save your drawing each 10-15 min. (and not rely on autosave). This means you need to run this manually.

 

(defun c:XRE () ;Save and Xref reload
  (command "_.QSAVE")
  (command "_.-XREF" "_Reload" "*")
  (princ)
)
0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

Here's one way to tie it to the QSAVE command, so that every time you Qsave the drawing, whether with Ctrl+S or a menu icon or the command name or an alias, it will check whether enough time has elapsed since the last Xref-Reload for it to do that again.

 

(command
  "_.time" "_on" "" ; just in case the user elapsed timer is off
  "_.undefine" "QSAVE"
); command

 

(defun C:QSAVE () ; new definition
  (command "_.qsave"); run native Qsave command
  (if (> (getvar 'tdusrtimer) 0.01); [0.01 of a day = 14.4 minutes elapsed yet?] -- EDIT 0.01 as desired
    (command
      "_.xref" "_reload" "*" ; reload all Xref's
      "_.time" "_reset" "" ; reset user timer to 0
    ); command
  ); if
); defun

 

It won't, however, just reload them all at regular intervals, even (for example) when you're out to lunch.  It depends on being tied to the use of Qsave [it could be similarly tied to any other command you prefer, or even to more than one].  But it has the advantage that you don't need to call for an Xref-Reloading command explicitly, so you don't need to have it in mind, and should be pretty reliable assuming you save regularly anyway.

 

Make sure that any macro or menu item you have that runs [or includes] Qsave uses the command name without the decimal-point prefix, so that it will invoke the new definition instead of the native definition.

Kent Cooper, AIA
0 Likes
Message 6 of 11

tanvirme2k9
Contributor
Contributor
Sometimes it says unknown command "QSAVE". Is there a way to run reload only after a period of time?
0 Likes
Message 7 of 11

tanvirme2k9
Contributor
Contributor

The lisp is not working correctlly. Having problem to save file.

0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

@tanvirme2k9 wrote:

Sometimes it says unknown command "QSAVE". Is there a way to run reload only after a period of time?

The lisp is not working correctlly. Having problem to save file.


It works for me.  I changed the value of TDUSRTIMER that it checks for to 0.001 so it's triggered after only 1.44 minutes, for testing purposes.  It works whether I pick the save icon, or type in the Qsave command name, or presst Ctrl+S.  If I do so less than 1.44 minutes after the last time, it just Qsaves without reloading Xrefs.  If I do it after more than 1.44 minutes has passed since the last time, it reloads all Xrefs and resets the timer.

 

Are there others out there who can try it out to see whether it works correctly for them?  I'm on an older version where I am right now, so it's possible there's some version-dependent difference that may cause it to not work on newer versions.

Kent Cooper, AIA
0 Likes
Message 9 of 11

hmsilva
Mentor
Mentor

@Kent1Cooper wrote:

@tanvirme2k9 wrote:

Sometimes it says unknown command "QSAVE". Is there a way to run reload only after a period of time?

The lisp is not working correctlly. Having problem to save file.


It works for me.  I changed the value of TDUSRTIMER that it checks for to 0.001 so it's triggered after only 1.44 minutes, for testing purposes.  It works whether I pick the save icon, or type in the Qsave command name, or presst Ctrl+S.  If I do so less than 1.44 minutes after the last time, it just Qsaves without reloading Xrefs.  If I do it after more than 1.44 minutes has passed since the last time, it reloads all Xrefs and resets the timer.

 

Are there others out there who can try it out to see whether it works correctly for them?  I'm on an older version where I am right now, so it's possible there's some version-dependent difference that may cause it to not work on newer versions.


Tested, and it works as expected.

 

'Sometimes it says unknown command "QSAVE". '

tanvirme2k9,

are your 'alias' defined in 'acad.lsp' or 'acaddoc.lsp' files?

 

If your 'qsave' alias, is defined in a function,

i.e.

 

(defun c:q nil
(command "qsave")
(princ) 
)

after loading the 'new' qsave function, if we enter 'q' to call the qsave command, we will get a

 

'Command: q
qsave Unknown command "QSAVE".  Press F1 for help.
Command:'

 

Henrique

EESignature

0 Likes
Message 10 of 11

tanvirme2k9
Contributor
Contributor
Right it's showing like this. How to fix. I don't know where the alias is defined.
0 Likes
Message 11 of 11

hmsilva
Mentor
Mentor

@tanvirme2k9 wrote:
Right it's showing like this. How to fix. I don't know where the alias is defined.

Undefining a command, we can't call the undefined command via 'command' function, we have to call the original command...

 

i.e.

(command "_.undefine" "QSAVE")

to call the original 'QSAVE' command, we'll have to precede thye command with a dot

 

(command ".qsave")

or to call Kent1Cooper's 'C:QSAVE' from another function

 

(C:QSAVE)

@tanvirme2k9 wrote:
...Is there a way to run reload only after a period of time?

Yes, there is.

Do you need the code to run in all dwg's?

 

Hope this helps,
Henrique

EESignature

0 Likes