samifox Posted March 30, 2013 Posted March 30, 2013 Hi Ive come into a situation I need to define a Boolean function, but get hard time to code it. In the following function I test if the entity is a poly , if it is I want it to return T , else return nill. In this code it will wrongly evaluate the last statement anyway (defun isLWPOLYLINE ( ent ) (if (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE") (alert "wall is a poly") T ) (alert "wall is not a poly") ) Thanks Shay Quote
VVA Posted March 30, 2013 Posted March 30, 2013 (edited) (defun isLWPOLYLINE (ent)(eq(cdr(assoc 0(entget ent))) "LWPOLYLINE")) Edited March 30, 2013 by VVA Quote
Tharwat Posted March 30, 2013 Posted March 30, 2013 (edited) This would return T = true if the selection is a LWpolyline otherwise a message shows that it's not .... (defun isLWPOLYLINE (ent) (if (eq (type ent) 'ENAME) (if (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE") T ) ) ) e.g of usage . (islwpolyline (car (entsel))) Edited March 30, 2013 by Tharwat Quote
Stefan BMR Posted March 30, 2013 Posted March 30, 2013 Tharwat, Your function will always return something different than nil, either T or a sting, returned by (princ... . So, if you use it like: (if (islwpolyline ent) 1 0) it will always return 1. You can fix this using [color=red](not [/color](princ "\n .... )[color=red])[/color] Quote
Lee Mac Posted March 30, 2013 Posted March 30, 2013 As per VVA's example, since you are supplying the if function with the boolean value that is to be returned, the if expression is redundant, e.g.: (defun lwpolyline-p ( ent ) (and (= 'ename (type ent)) (= "LWPOLYLINE" (cdr (assoc 0 (entget ent)))) ) ) Quote
neophoible Posted April 1, 2013 Posted April 1, 2013 (edited) Also note that, if you intend to include alert, this function always returns nil; thus you would need to preserve your initial result if you need it after issuing alert. Usually, there is no need to issue alert to the user unless there is a problem. If you only send alert when there is a nil result, then the value could be preserved without a setq. In this case, using if is valid, though I personally prefer using cond. (defun isLWPOLYLINE ( ent ) (if (/= "LWPOLYLINE" (cdr (assoc 0 (entget ent))) ) (alert "wall is not a poly") T ) ) NOTE: I only show this as a method, not as a specific recommendation. There may be good reasons to use alert separately from your function. You will have to figure that part out yourself. Edited April 1, 2013 by neophoible added example, highlighted items 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.