If a selection has values

If a selection has values

My_Civil_3D
Advocate Advocate
433 Views
4 Replies
Message 1 of 5

If a selection has values

My_Civil_3D
Advocate
Advocate

Hi guys i need some help.

 

I am trying to make a lisp that if a selection set has items in it, it needs to do "this" otherwise do "that".

 

My Code:

 

(defun c:foo (/)
(setq xr(ssget "x" '((0 . "INSERT")(8 . "..TEST"))))
(setq xrnum (sslength xr))

 The Next part i need help with not sure how to do the if  argument if there are values or not:

 

(cond

         ((= xrnum nil)  <---- CANT GET THIS RIGHT not sure how to do the contains nothing or contains something

          (do "this")

          ))

 

 

Civil 3D Certified Professional
0 Likes
Accepted solutions (1)
434 Views
4 Replies
Replies (4)
Message 2 of 5

Automohan
Advocate
Advocate

You didn't mention that what you need to do !

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 3 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

You can check it right in the process of getting the selection:

 

(defun c:foo (/ xr xrnum)
  (if (setq xr (ssget "x" '((0 . "INSERT")(8 . "..TEST"))))
    (repeat (setq xrnum (sslength xr)) ;; then
      (... do ... "this" ...) ;; [assuming doing something to all selected]
    ); repeat
    (... do ... "that" ...) ;; else
  ); if
  ....

 

because (ssget) will return nil if there are no objects found that fit the filter list criteria.  If "this" is something collective that can be done to the entire selection [for example, changing them all to a certain Layer, assuming they're all in the current space], and doesn't involve doing something to each object found independently, it's simpler:

 

(defun c:foo (/ xr xrnum)
  (if (setq xr (ssget "x" '((0 . "INSERT") (8 . "..TEST"))))
    (... do ... "this" ...) ;; then
    (... do ... "that" ...) ;; else
  ); if
  ....

 

Kent Cooper, AIA
0 Likes
Message 4 of 5

john.kaulB9QW2
Advocate
Advocate

You are creating a boolean conditional construct and instead of using:

(if (setq xr (ssget "x" ...

 

you should create a "Boolean variable" to help construct a conditional.

(setq selectionsset (ssget "x" '((0 . "INSERT")) ...
(if selectionset ...

 

When creating a `Boolean variable` you should use positive variable names.
Examples:
full
ready
etc.

 

Using negative verbs results in confusing code like:
(if (not notfull) ...
(if (not notReady) ...

 

This keeps code more readable and straight forward (less confusing) and often times people assume the smaller number of lines of code the better it is but that isn't the case. The cleaner the code the better, and sometimes that means creating a variable to offer up more coherent code (for *OUR* readability included).

another swamper
0 Likes
Message 5 of 5

ronjonp
Mentor
Mentor

@My_Civil_3D It's also beneficial to read the online HELP

0 Likes