Invert Selection Set

Invert Selection Set

Kyudos
Advisor Advisor
6,780 Views
10 Replies
Message 1 of 11

Invert Selection Set

Kyudos
Advisor
Advisor

I'm looking for some LISP to invert the AutoCAD selection - deselect all selected items, and select all those not selected (forming a new selection set). Ideally, it would only work on the current selection (the PICKFIRST selection).

 

I found this on another board:

 

(defun c:invss (/ ss ss2 n)
  (setq	ss  (ssget)
    ss2 (ssget "_x")
  )
  (if ss
    (progn
      (repeat (setq n (sslength ss))
        (ssdel (ssname ss (setq n (1- n))) ss2)
      )
      (if ss2
        (sssetfirst nil ss2)
      )
    )
  )
)

But it doesn't seem to work properly. Knowing nothing about LISP I haven't a clue why.

 

Can anyone help?

 

Cheers:)

0 Likes
Accepted solutions (1)
6,781 Views
10 Replies
Replies (10)
Message 2 of 11

marko_ribar
Advisor
Advisor

For me it works, although I did some modifications...

 

(defun c:invss ( / ss ss2 n )
  (setq	ss (ssget)
    ss2 (ssget "_X")
  )
  (if ss
    (repeat (setq n (sslength ss))
      (ssdel (ssname ss (setq n (1- n))) ss2)
    )
  )
  (if ss2
    (sssetfirst nil ss2)
  )
  (princ)
)

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 3 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kyudos wrote:

I'm looking for some LISP to invert the AutoCAD selection - deselect all selected items, and select all those not selected (forming a new selection set). Ideally, it would only work on the current selection (the PICKFIRST selection).

....


Here's my take on it -- very lightly tested, but it works in noun-verb or verb-noun mode [i.e. if you have anything pre-selected when you enter the command, it selects/grips/highlights everything except that pre-selection, or if you don't, it asks you to select objects, then selects/grips/highlights everything except those].

 

(defun C:SAB (/ ss); = Select All But
  (setq ss
    (cond
      ((ssget "_I")); preselection, if any
      ((ssget)); User selection if no pre-selection
    ); cond
  ); setq
  (sssetfirst nil); clear preselection, if any, to not to in at Select command
  (command "_.select" "_all" "_remove" ss "")
  (sssetfirst nil (ssget "_P")); select/grip/highlight
); defun

 

Add an appropriate prompt if desired, and/or put the final selection into a variable if needed, etc.  If you really want it to work only with a pre-selection, isolate that out of the (cond) function and dump the User-selection part.

 

It has a slightly different effect than the versions using (ssget "_X"), since those will find everything in the drawing, even in other than the current space, whereas this will work only with things in the current space.  Depending on what you plan to do with the result, that may be preferable, anyway.

 

EDIT:  In fact, if you want it to work only with pre-selection, there isn't even any need for the 'ss' variable:

 

(defun C:SAB ()
  (if (ssget "_I")
    (progn ; then
      (sssetfirst nil)
      (command "_.select" "_all" "_remove" (ssget "_P") "")
      (sssetfirst nil (ssget "_P"))
    )
    (prompt "\nRequires pre-selection."); else
  ); if
); defun
Kent Cooper, AIA
Message 4 of 11

Kyudos
Advisor
Advisor
Perfect thank you
0 Likes
Message 5 of 11

franc_as
Enthusiast
Enthusiast

Hi
Is it possible to modify this routine, so it will consider only selected layer (To invert selection only within layer on which selected object(s) is placed)?

Regards

0 Likes
Message 6 of 11

Kent1Cooper
Consultant
Consultant

@franc_as wrote:

.... Is it possible to modify this routine...?


[Which one?]

Kent Cooper, AIA
0 Likes
Message 7 of 11

franc_as
Enthusiast
Enthusiast

(defun C:SAB ()
(if (ssget "_I")
(progn ; then
(sssetfirst nil)
(command "_.select" "_all" "_remove" (ssget "_P") "")
(sssetfirst nil (ssget "_P"))
)
(prompt "\nRequires pre-selection."); else
); if
); defun

0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

This seems to do that [minimally tested]:

(defun C:SABL (/ ss n lay laylist laystr)
; = Select All But, on pre-selected objects' Layer(s) only
  (if (setq ss (ssget "_I")); pre-selection
    (progn ; then
      (sssetfirst nil); clear selection
      (repeat (setq n (sslength ss)); Layer(s) of selected object(s)
        (setq lay (cdr (assoc 8 (entget (ssname ss (setq n (1- n)))))))
        (if (not (member lay laylist))
          (setq
            laylist (cons lay laylist); for checking if already present
            laystr (strcat (cond (laystr) ("")) lay ","); for (ssget)
          ); setq
        ); if
      ); repeat
      (command "_.select" (ssget "_X" (list (cons 8 laystr))) "_remove" ss "")
      (sssetfirst nil (ssget "_P")); select/grip/highlight
    ); progn
    (prompt "\nRequires pre-selection."); else
  ); if
  (prin1)
); defun
Kent Cooper, AIA
Message 9 of 11

franc_as
Enthusiast
Enthusiast
Thank you Kent...it works fine for me
0 Likes
Message 10 of 11

rmc9WR3X
Advocate
Advocate

To invert the selection, a command that every design software has had for decades now, I must code the command myself. No thank you, Autodesk. I am a designer attempting a simple design operation.

Message 11 of 11

john.uhden
Mentor
Mentor

@rmc9WR3X ,

AutoCAD has a litany of shortcomings.

That's why they pay nothing to have brilliant volunteers help provide solutions. 😞

John F. Uhden