Dear all,
I am just a begineer for auto lisp. I wrote my first program thanks to afra lisp, but got struck, I am unable to get out from while loop if i Press "enter". Please find the program below.
(defun c:arsh ()
(while
(setq p1 (getpoint "\n pick point :"))
(setq l 1500)
(setvar "osmode" 0)
(initget 7)
(setq p2 (polar p1 (dtr 0) l))
(initget 7)
(setq p3 (polar p2 (dtr 270) (/ l 2)))
(initget 7)
(setq p4 (polar p3 (dtr 180) l))
(command "line" p1 p2 p3 p4 "c")
(setvar "osmode" 111)
); end of while
(princ)
)
if any one could help me
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by pbejse. Go to Solution.
Solved by pbejse. Go to Solution.
sorry I did not included the dtr function.
(defun c:arsh ()
(while
(setq p1 (getpoint "\n pick point :"))
(setq l 1500)
(setvar "osmode" 0)
(initget 7)
(setq p2 (polar p1 (dtr 0) l))
(initget 7)
(setq p3 (polar p2 (dtr 270) (/ l 2)))
(initget 7)
(setq p4 (polar p3 (dtr 180) l))
(command "line" p1 p2 p3 p4 "c")
(setvar "osmode" 111)
); end of while
(princ)
)
(defun dtr (x)
(* pi (/ x 180.0))
)
(while (and (not (initget 128)) (setq p1 (getpoint "\n pick point :"))) ....
dear
Thanks for your reply but still not working the following error occured, plz see the command window msg.
Command: arsh
pick point :nil
Really?
(defun c:arsh () (while (And (not (initget 128)) (setq p1 (getpoint "\n pick point :"))) (setq l 1500) (setvar "osmode" 0) (initget 7) (setq p2 (polar p1 (dtr 0) l)) (initget 7) (setq p3 (polar p2 (dtr 270) (/ l 2))) (initget 7) (setq p4 (polar p3 (dtr 180) l)) (command "line" p1 p2 p3 p4 "c") (setvar "osmode" 111) ) ; end of while (princ) )
yes
I copied your lisp now I got this error.
Command: arsh
pick point :error :bad argument type: numberp:
Command:
thanks very much!
I got it.
I dont know how i got it but i just reopened cad and ran the program worked fine.
thanks very much
hoping for help in future also:smiley happy:
@syedarshad wrote:yes
I copied your lisp now I got this error.
Command: arsh
pick point :error :bad argument type: numberp:
Command:
Odd.....
EDIT:
<< I dont know how i got it but i just reopened cad and ran the program worked fine.>>
okay then.. you had me worried there for a while syedarshad
Cheers
Could the problem have something to do with those (initget 7) lines in there? I couldn't figure out what they could be accomplishing, so I commented them out, it works fine -- either Enter or space ends it.
Some other suggestions: Those orthogonal directions are easy enough to define in relation to pi radians that it hardly seems worth bothering with a (dtr) conversion. And I usually prefer not to bother with setting a variable that is used only once, instead just figuring that value at the place where it's needed. The following also sets the l variable only once instead of setting it to the same value over again every time through the (while) loop.
(defun c:arsh (/ l p1)
(setq l 1500)
(while (setq p1 (getpoint "\n pick point :"))
(setvar "osmode" 0)
(command "_.line"
p1
(polar p1 0 l)
(polar (getvar 'lastpoint) (* pi 1.5) (/ l 2))
(polar p1 (* pi 1.5) (/ l 2))
"c"
); command
(setvar "osmode" 111)
); end of while
(princ)
)
The following also works, if you don't mind the result being a closed Polyline rather than four independent Lines [which is preferable for several reasons, unless you have a reason to need them as Line entities]. It doesn't require turning Osnap off [it does it for each point designation], and sets the Osnap mode ahead of time since it doesn't need to turn it off and on inside the (while) loop.
(defun C:ARSH2 (/ l p1)
(setvar 'osmode 111)
(setq l 1500)
(while
(setq p1 (getpoint "\nPick point: "))
(command "_.rectangle" "_none" p1 "_none" (mapcar '+ p1 (list l (/ l -2.0) 0)))
); while
(princ)
); defun
The one that draws Lines could similarly not bother turning Osnap off and back on, if you preface each point with "_none" -- you just need to do that more times than with a Rectangle.
[Of course, if l is always going to be 1500, you could omit it as a variable entirely, and build the values in:
(defun C:ARSH3 (/ p1)
(setvar 'osmode 111)
(while
(setq p1 (getpoint "\nPick point: "))
(command "_.rectangle" "_none" p1 "_none" "@1500,-750")
); while
(princ)
); defun
but I assume you have it as a variable either to more easily build different versions for different sizes, or to ask the User for a value in the final form of the routine, or something.]
@Kent1Cooper wrote:Could the problem have something to do with those (initget 7) lines in there? I couldn't figure out what they could be accomplishing, so I commented them out, it works fine -- either Enter or space ends it.
Whoa. i did'nt notice that. True enough it does matter.
I was so too pre-occupied with why the while function fails from the start and did not bother to check the rest.
Good catch Kent.
@pbejse wrote:
@Kent1Cooper wrote:Could the problem have something to do with those (initget 7) lines in there? I couldn't figure out what they could be accomplishing....
Whoa. i did'nt notice that. ... True enough it does matter.
....
Good catch Kent.
Thank you. At first I didn't think about why it would matter, but simply noticed (initget) being used without a (get[something]...) function that typically follows it, for which it would be imposing limitations on the User's response. So since I merely didn't know why they were there, I just tried eliminating them, and it worked.
Thinking about it some more, I realized why it matters. Any (initget) "waits around" until some kind of (get...) function comes along that it can apply itself to, apparently however long that takes. In this case, after the first point is picked, the (getpoint) function in every pass through that (while) loop is being affected by the last (initget) in the previous pass through the loop, because that hasn't had a chance to do its thing yet. [I assume, though I'm not positive, that the last one in each pass overrides those before it that also haven't had a chance to operate on anything yet.] Since (initget 7) means "No Enter, no zero, no negative for the next (get...) function," therefore the (getpoint) functions won't allow Enter. It's the 1 bit in the binary makeup of the 7 that forbids Enter, so my guess [without trying it] is that if it were (initget 6) instead, or using any even number, the problem would go away. The (initget) functions would still not be doing anything, as far as I can tell, but at least they wouldn't be preventing Enter in response to that (getpoint) function.
Welcome to the wonderful life of LISP programming.
Another method of escaping the while loop is to study this:
(setq p1 (getpoint "Specify first point: "))
(while p1
[do your thing here]
(setq p1 (getpoint "Specify next point: "))
);while
scot-65 🙂
Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.
Thank you all.
Thats really a good idea for asking for a point and not passing.
best regards,
Syed Arshad
@scot-65 wrote:Welcome to the wonderful life of LISP programming.
Another method of escaping the while loop is to study this:
(setq p1 (getpoint "Specify first point: "))
(while p1
[do your thing here]
(setq p1 (getpoint "Specify next point: "))
);while
scot-65 🙂
Another similar method (example for collecting points and returning a list):
((lambda (/ lst pnt) (if (car (setq lst (list (getpoint "\nSpecify first point: ")))) (while (setq pnt (getpoint (car lst) "\nSpecify next point: ")) (setq lst (cons pnt lst)) ) ) (reverse lst) ) )