Hello Everyone!
I need help on the code below to ask the user if yes or no. If yes continue with the script, if no cancel routine.
(initget "Yes No") (if (eq (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes") (setvar '?????) ) (load "C:/folder1/someroutine.lsp") (command "someroutine")
Thanks in advance!
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
Solved by alanjt_. Go to Solution.
(initget "Yes No") (if (eq (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes") (setvar '?????) (progn (load "C:/folder1/someroutine.lsp") (command "someroutine") ) )
HTH
Henrique
(initget "Yes No") (if (and (eq (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes") (load "C:/folder1/someroutine.lsp" nil) ; check if lisp actually loads ) (command "someroutine") ; not sure why using command. command won't work on lisp routines )
Unfortunately, Script files will not pause for user input. Script files just contain a sequence of commands that execute from beginning to end.
You'll need to use Lisp to request for a pause and user input.
Also as others have stated, don't use (command "someroutine") to execute Lisp function.
Assuming lisp function is coded with (defun c:someroutine ()..
then the code to call the lisp function is: (c:someroutine)
If lisp function is coded with just (defun someroutine ()..
then the code to call the lisp function is (someroutine)
Sure..no problem...glad to have helped.
@JCprog wrote:
....
I need help on the code below to ask the user if yes or no. If yes continue with the script, if no cancel routine.
(initget "Yes No") (if (eq (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes") (setvar '?????) ) (load "C:/folder1/someroutine.lsp") (command "someroutine")....
[EDIT: Someone beat me to the punch on some of this, but I'll leave it anyway....]
That's not a script [AutoCAD means something specific and different by that word, though Scripts can include AutoLISP code within limits as noted in another Reply]. But assuming you're only using the term in the way that a lot of people often do mistakenly --
I don't think you need to set any variable. I would try this:
(initget "Yes No")
(if
(and
(= (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes")
(load "C:/folder1/someroutine.lsp"); it loaded successfully
); and
(someroutine); then expression
); if
That assumes that the someroutine.lsp file defines a function of the same name, i.e. it contains a line starting like this:
(defun someroutine (.....
But a file name doesn't necessarily need to be the same as the name of the function it defines, and a single file can define more than one function and/or command. If the function that you want to run from that file has some other name, use that instead in the 'then' expression parentheses above. If it defines a command with a C: in front:
(defun C:someroutine (/ .....
then include that C: in the 'then' expression parentheses, i.e. use:
(C:someroutine); then
since you can't use the (command) function to call a Lisp-defined command, only a native AutoCAD command.
Kent's load call is flawed. While the second variable in load is optional, it will error if the routine cannot be found.
Command: (load "someroutine.lsp" nil) nil Command: (load "someroutine.lsp" "did not load") "did not load" Command: (load "someroutine.lsp") Error: LOAD failed: "someroutine.lsp"
See the differences of how load functions when the second variable is/isn't filled in. If you use the code snippet I posted before, it will yield the results you want, just replace (command) with an acutal call to the lisp routine (which is exactly what I questioned you about). eg. (c:CommandName)
(initget "Yes No") (if (and (eq (getkword "\nThis wil load & run someroutine, Do you wish to continue? [Yes/No] <No>: ") "Yes") (load "C:/folder1/someroutine.lsp" nil) ; check if lisp actually loads ) (c:CommandName) )
Sure. 🙂
For completeness, perhaps also a check to ensure the program defines the function in question:
(initget "Yes No") (if (= "Yes" (getkword "\nLoad & run someroutine? [Yes/No] <No>: ")) (if (or c:commandname (and (load "C:/folder1/someroutine.lsp" nil) c:commandname)) (c:commandname) (princ "\nc:commandname could not be defined.") ) )
While I do agree with you, that's quite a bit of overkill error checking for a bit of code to load a lisp routine someone compiled. lol