Jump to content

Recommended Posts

Posted

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

Posted (edited)
(defun isLWPOLYLINE (ent)(eq(cdr(assoc 0(entget ent))) "LWPOLYLINE"))

Edited by VVA
Posted (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 by Tharwat
Posted

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]

Posted

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))))
   )
)

Posted (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 by neophoible
added example, highlighted items

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...