Does autolisp have a 'wait for x seconds'?

43 Views Asked by At

is there a way to make AutoLisp wait for X seconds (or milliseconds) before proceeding to the next line of code? I haven't found a function in the cad software I'm using (DraftSight 2023).

3

There are 3 best solutions below

0
Lee Mac On BEST ANSWER

I might suggest the following to avoid unnecessarily calling the DELAY command repeatedly unless absolutely required -

(defun sleep ( sec / cmd )
    (setq cmd (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (repeat (/ (fix sec) 32767) (command "_.delay" "32767"))
    (command "_.delay" (rem sec 32767))
    (setvar 'cmdecho cmd)
    nil
)
0
G Beck On

I came up with this solution for my needs. Perhaps you will find it useful as well.


;; Delay until x seconds have past. 
(defun sleep ( rSeconds / EndTime)
    (setq EndTime (+ rSeconds (Date_To_Seconds (getvar "CDATE"))))
    (while (> EndTime (Date_To_Seconds (getvar "CDATE"))) nil)
);defun

;; Converts CDATE into seconds. Input REAL value to receive seconds.
(defun Date_To_Seconds (rDate /)
    
    ;; Date to Seconds
    (if (and (numberp rDate)(>= (abs rDate) 100))(+ ; Sum days
        (* 86400.0 (fix (* (fix (/ rDate 10000)) 365.25)));-----------; Years to Seconds
        (* 86400 (MonthDay ;------------------------------------------; Months to Seconds
            (fix (/ rDate 10000));------------------------------------; -Input: Year
            (- (fix (/ rDate 100)) (* (fix (/ rDate 10000)) 100));----; -Input: Month
        ));Multiply<-MonthDay
        (* 86400 (- (fix rDate)(* (fix (/ rDate 100)) 100)));---------; Days to Seconds
        (* 3600 (- (fix (* rDate 100)) (* 100.0 (fix rDate))));-------; Hours to Seconds
        (* 60 (- (fix (* rDate 10000)) (* 100.0 (fix (* rDate 100))))); Minutes to Seconds
        (- (* rDate 1000000) (* 100 (fix (* rDate 10000))));----------; Seconds to Seconds
    ));if<-plus
);defun

;; Converts a month to days
(defun MonthDay (iYear iMonth /)
    (+ ; Sum days
        (cond ; Completed months
            ((= iMonth 01) 0);; January: No previous months to add days
            ((= iMonth 02) 31); Febuary: Adding January's days
            ((= iMonth 03) (if (= (/ iYear 4.0)(fix (/ iYear 4))) 29 28))
            ((= iMonth 04) 31)
            ((= iMonth 05) 30)
            ((= iMonth 06) 31)
            ((= iMonth 07) 30)
            ((= iMonth 08) 31)
            ((= iMonth 09) 31)
            ((= iMonth 10) 30)
            ((= iMonth 11) 31)
            ((= iMonth 12) 30); December: Adding November's days
        );cond
        (if (> iMonth 2)(MonthDay iYear (1- iMonth)) 0)
    );plus
);defun

Side Notes: As stated here, AutoLisp runs in series by using a single processor thread. This can be overcome by executing and running a second, remote session of the cad software. A start towards this direction can be found here. Alternatively, research the pause command to wait for user input.

0
G Beck On

This solution works for long periods of time. If the value is less than 32.768 seconds, then the code can be further simplified to (command "DELAY" [XXXXX]). The command, DELAY, accepts integers between 0 and 32767 as milliseconds.

;; Delay until x seconds have past. 
(defun sleep ( rSeconds / iEcho)
    (setq iEcho (getvar "CMDECHO"))
    (setvar "CMDECHO" 0)
    (repeat (fix rSeconds)
        (command "DELAY" "1000"); Requires an integer between 0 and 32767 (milliseconds).
    );repeat
    (command "DELAY" (fix (* (- rSeconds (fix rSeconds)) 1000)))
    (setvar "CMDECHO" iEcho)
    nil
);defun

Thank you @MacLee for bringing the DELAY command to my attention.