Run lisp with variables or let it ask for input

Run lisp with variables or let it ask for input

Anonymous
Not applicable
2,576 Views
5 Replies
Message 1 of 6

Run lisp with variables or let it ask for input

Anonymous
Not applicable

 

I have been trying to find the solution for this for a while and I am going in circles. I want to have two possible workflows:

1- Call a lisp via macro in the tool palette with pre-define variables within the command.

2- Type the command to call the "generic" lisp (without variables) and been prompted for input to define those same variables. 

 

After searching for a solution my understanding so far is:

I can either have something like:

(defun C:NL ()
    ;main script
)

that works fine for my 2nd requirement but not the 1st,

 

or something like this:

(defun NL (MS PS);MS=measurement system, PS=paper size, 
    ;main script
)

that I can run from the tool palette providing the two arguments, but doing it via command line without arguments will return "few arguments"

 

I am quite sure there must be a solution to this, any ideas?

 

 

 

0 Likes
Accepted solutions (2)
2,577 Views
5 Replies
Replies (5)
Message 2 of 6

Shneuph
Collaborator
Collaborator
Accepted solution

Does something like below work?

(defun C:NL ()
(setq MS (getwhatever "\nEnter Whatever M: ")
PS (getwhatever "\nEnter Whatever P: ") (NL MS PS) )

 

(defun NL (MS PS);MS=measurement system, PS=paper size, 
    ;main script
)
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 3 of 6

john.uhden
Mentor
Mentor
Accepted solution

You should have both types of functions.  Since the internal code for them will be the same, put all the effort into the non-command function and then call that function within the command function.

For example:

(defun NL (space / local1 local2 etc)
  (cond
    ((= space "Model")(do_this))
    ((= space "Paper")(do_that))
    (1 (exit))
  )
)
(defun c:NL ( / space)
  (initget 7 "Model Paper")
  (setq space (getkword "\nWhich space?, [Model/Paper]: "))
  (NL space)
  (princ)
)

John F. Uhden

0 Likes
Message 4 of 6

Anonymous
Not applicable

Thank you both! You were hinting the same thing. It was as simple as to move the "ask for input" part to an independent routine that it will just be used when I run it from command line. 

Thanks guys!

0 Likes
Message 5 of 6

john.uhden
Mentor
Mentor
There is another way to do it... using vlax-add-cmd which can make a real
AutoCAD command from an AutoLisp function.
That way, if your C: function asks for responses, you can provide them in
the command string just like (command "_.line" p1 p2 "").
But I think there are several here who really don't approve of it. I have
tried it and find that it works. Though I guess I prefer to be more
conservative and would do things the way I had earlier suggested.
Whatever. We are all glad to see your skills improving.

John F. Uhden

0 Likes
Message 6 of 6

Anonymous
Not applicable

It sounds interesting. So much to learn. Thanks for contributing in the community, it is of a much of help.

0 Likes