broncos15 Posted February 10, 2016 Posted February 10, 2016 So I had a quick question on why I am getting the error "lentityp nil" when I hit enter in this code. The code is working fine, but I get this error every time I exit the code through hitting the space bar/enter. I have looked through it numerous times, but I can't find my error. My code is (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*) (defun *error* (msg) (if (not (member msg '("Function cancelled" "quit / exit abort")) ) (princ (strcat "\nError: " msg)) )) (setq whilestop t) (while whilestop (setq hsel (car (entsel "\nSelect hatch: "))) (cond ((= "HATCH" (cdr (assoc 0 (entget hsel)))) (if (and (setq pt1 (getpoint "\nSelect 1st point for angle: ")) (setq pt2 (getpoint "\nSelect 2nd point for angle: "))) (progn (setq angr (angle pt1 pt2)) (setq angd (/ (* angr 180) pi)) (command "._-hatchedit" hsel "" "" "" angd) (princ) ) (setq whilestop nil) ) ) ((= hsel nil) (setq whilestop nil) ) (princ "\n Not a hatch, try again.") ) ) (princ) ) Quote
Jef! Posted February 10, 2016 Posted February 10, 2016 Hi bronkos Your error comes from the fact that when you dont select anything, hsel is nil. When your hsel is nil, the first thing you try to do is that conditionnal (= "HATCH" (cdr (assoc 0 (entget hsel)))) You can't entget it, and it cause your error. just swap your 2 conditionnals together, making your code stop condition be evaluated first, and exit the routine right away (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*) (defun *error* (msg) (if (not (member msg '("Function cancelled" "quit / exit abort")) ) (princ (strcat "\nError: " msg)) ) ) (setq whilestop t) (while whilestop (setq hsel (car (entsel "\nSelect hatch: "))) (cond ((= hsel nil) (setq whilestop nil) ) ((= "HATCH" (cdr (assoc 0 (entget hsel)))) (if (and (setq pt1 (getpoint "\nSelect 1st point for angle: ")) (setq pt2 (getpoint "\nSelect 2nd point for angle: ")) ) (progn (setq angr (angle pt1 pt2)) (setq angd (/ (* angr 180) pi)) (command "._-hatchedit" hsel "" "" "" angd) (princ) ) (setq whilestop nil) ) ) (princ "\n Not a hatch, try again.") ) ) (princ) ) Cheers Jef! Quote
broncos15 Posted February 10, 2016 Author Posted February 10, 2016 (edited) Hi bronkos Your error comes from the fact that when you dont select anything, hsel is nil. When your hsel is nil, the first thing you try to do is that conditionnal (= "HATCH" (cdr (assoc 0 (entget hsel)))) You can't entget it, and it cause your error. just swap your 2 conditionnals together, making your code stop condition be evaluated first, and exit the routine right away Thanks Jef! Your reply was super useful. I ended up messing with the code a little bit to try and get the while to exit loop to exit only when the user hits enter and not if they accidently don't select anything. Besides that it is working perfectly thanks to you! I really appreciate it and I definitely learned to check the order of my conditionals more closely. (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*) (defun *error* (msg) (if (not (member msg '("Function cancelled" "quit / exit abort")) ) (princ (strcat "\nError: " msg)) ) ) (while (setq hsel (car (entsel "\nSelect hatch: "))) (cond ((= hsel nil) (princ "\nNothing selected.") ) ((= "HATCH" (cdr (assoc 0 (entget hsel)))) (if (and (setq pt1 (getpoint "\nSelect 1st point for reference angle: ")) (setq pt2 (getpoint "\nSelect 2nd point for reference angle: ")) ) (progn (setq angr (angle pt1 pt2)) (setq angd (/ (* angr 180) pi)) (command "._-hatchedit" hsel "" "" "" angd) (princ) ) ) ) (t (princ "\nNot a hatch, try again.") ) ) ) (princ) ) Edited February 11, 2016 by broncos15 Quote
Jef! Posted February 11, 2016 Posted February 11, 2016 I'm glad I could help. One little comment about the last posted code... a while statement will only keep going as long as the next expression enclosed is not nil. Si if you have (while (setq hsel (car (entsel "\nSelect hatch: "))) ...it means that if you misclick (and/or select nothing), hsel will be nil, and the while statement stop being evaluated at that moment. It also meant that as it is, your first conditionnal (= hsel nil) is not usefull and could never be met/executed. Sometimes I use some flags variable as the while criteria (sometimes combined with an "and or an "or" to any other criteria to suit my needs) and for each possible outcome i use/change the flag to control the while. Very basic example (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop keepgoingflag *error*) (defun *error* (msg) (if (not (member msg '("Function cancelled" "quit / exit abort")) ) (princ (strcat "\nError: " msg)) ) ) [color="darkorange"](setq keepgoingflag T)[/color] (while [color="darkorange"](or[/color] (setq hsel (car (entsel "\nSelect hatch: "))); [b]with a "or" if you misclick and select nothing...[/b] [color="darkorange"]keepgoingflag)[/color]; [b]...the flag will prevent the while statement to stop being evaluated[/b] (cond ((= hsel nil);[b]now this cond can be evaluated[/b] (princ "\nNothing selected. Try again or hit escape to end command"); [b]and now escape is the only way out[/b] ) ((= "HATCH" (cdr (assoc 0 (entget hsel)))) (if (and (setq pt1 (getpoint "\nSelect 1st point for reference angle: ")) (setq pt2 (getpoint "\nSelect 2nd point for reference angle: ")) ) (progn (setq angr (angle pt1 pt2)) (setq angd (/ (* angr 180) pi)) (command "._-hatchedit" hsel "" "" "" angd) (princ) ) ) ) (t (princ "\nNot a hatch, try again.") ) ) ) (princ) ) Sometimes a plain ol' while is enough, and sometimes you need to twirl a little bit around it. Happy coding! Jef! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.