Unselect objects outside of dragbox

Unselect objects outside of dragbox

Anonymous
Not applicable
1,276 Views
11 Replies
Message 1 of 12

Unselect objects outside of dragbox

Anonymous
Not applicable

Does Autocad has a function to unselect selected objects outside of dragbox?

With shift key i can unselect objects which are inside of dragbox, but didn't find an inverted operation.

Or maybe some easy lisp code exists for such thing?

 

0 Likes
Accepted solutions (2)
1,277 Views
11 Replies
Replies (11)
Message 2 of 12

ВeekeeCZ
Consultant
Consultant

Would you post a screenshot of what you call 'dragbox' ?

0 Likes
Message 3 of 12

CodeDing
Advisor
Advisor

@Anonymous ,

 

I think the KEY question here is are you trying to also keep ALL of the objects inside of your dragbox?

...If YES, then you are essentially just using a normal (ssget) and can completely wipe out any previous selection set by using the following code, then using just a normal (ssget) to select objects...

(sssetfirst nil nil)

...If NO, and you want to ONLY remove items outside of your dragbox, then I would compare your new "dragbox selection set" against your original selection set, and only keep mutual items.. Here's an example of how that could be implemented:

(defun c:TEST ( / ss1 ss2 ssFinal)
(if (and (setq ss1 (progn (prompt "\nSelect Initial sel set: ") (ssget)))
	 (setq ss2 (progn (prompt "\nSelect Items to Keep: ") (ssget))))
  (if (setq ssFinal (KeepMutual ss1 ss2))
    (prompt "\nDo stuff w/ final sel set...")
    (prompt "\n...No mutual items were found.")
  );if
;else
  (prompt "\n...There was an empty selection set.")
);if
(princ)
);defun

(defun KeepMutual (ss1 ss2 / ssFinal e e2 cnt cnt2 eFound)
(setq ssFinal nil ssFinal (ssadd))
(repeat (setq cnt (sslength ss2))
  (setq e (ssname ss2 (setq cnt (1- cnt)))
	eFound nil)
  (repeat (setq cnt2 (sslength ss1))
    (setq e2 (ssname ss1 (setq cnt2 (1- cnt2))))
    (if (eq e e2) (setq eFound t))
  );repeat ss1
  (if eFound (ssadd e ssFinal))
);repeat ss2
(if (> (sslength ssFinal) 0) ssFinal nil)
);defun

Best,

~DD

Message 4 of 12

CodeDing
Advisor
Advisor

Here's an example of ONLY removing items outside of my new selection set..

First, my initial selection set consists of 4 lines..

image.png image.png

 

...Next, I want to REMOVE everything outside of my "dragbox" (second selection set) (This means that even if I select the cyan line, it will NOT be part of my final selection set)...

image.png image.png

...Thus my FINAL selection set would only consist of the 2 green lines.

 

Best,

~DD

Message 5 of 12

dlanorh
Advisor
Advisor

I suspect you are dragging from right to left which defaults to a "crossing" selection and will select object that are partially inside the dragbox.

If you drag from left to right this defaults to a "window" selection and will only select objects that are completely within the box.

I am not one of the robots you're looking for

0 Likes
Message 6 of 12

Anonymous
Not applicable

CodeDing, this is exactly what i need, problem is Your lisp is not working on my end as You pointed on screenshots.

I select particular objects from wider area, run TEST, there is question "Select Items to Keep" so i select much narrow area, and after RMB or enter key script finishes without error, and without anything selected.

I also made attempt to run TEST with empty selection, but the result is the same.

There is "Do stuff w/ final sel set" message but the selection is empty.

I will make the task manually for now, and will analyze script later. Weird is there is no error though.

 

EDIT: after first step, on second step "Select Items to Keep" the selection is cleared. I made a reverse operation also, so area first, and then items i need to keep, but the same result.

0 Likes
Message 7 of 12

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

Well, I'm not sure if you have any existing code or anything so my code was mostly example of how it could accomplished.. Try replacing this line in my code..

(prompt "\nDo stuff w/ final sel set...")

...with this...

(progn (sssetfirst nil nil) (sssetfirst nil ssFinal))

Best,

~DD

Message 8 of 12

Anonymous
Not applicable

Stupid me, i didn't notice that there is only prompt to do whatever i want next. Feel ashamed of myself even if i have only little experience with lisp.

It works now. The only side effect of script is that Autocad displays final selection as ... nothing selected at all. I need to select any other element on drawing, then Autocad starts display the selection as selection ...

0 Likes
Message 9 of 12

Anonymous
Not applicable

Well, I can't find the reason why selection is not displaying. I can even point element which i know is selected (not click, only point with cursor) and Autocad 2010 behaves like it is not selected at all. I have found some people had similar issue with lisp, and sometime helpful were additional (princ), another user used (command "regenerate") but nothing worked here. I need to manually select other, not selected object and the Autocad refreshes selection properly. Or start command like copy or move.

I tried different commands, sssetfirst or ssget repeat, but it doesn't work.

 

0 Likes
Message 10 of 12

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

Sorry for the delay. This should fix it. I should have cleared the existing selection set first:

(defun c:TEST ( / ss1 ss2 ssFinal)
(sssetfirst nil nil)
(if (and (setq ss1 (progn (prompt "\nSelect Initial sel set: ") (ssget)))
	 (setq ss2 (progn (prompt "\nSelect Items to Keep: ") (ssget))))
  (if (setq ssFinal (KeepMutual ss1 ss2))
    (sssetfirst nil ssFinal)
    (prompt "\n...No mutual items were found.")
  );if
;else
  (prompt "\n...There was an empty selection set.")
);if
(princ)

Best,

~DD

Message 11 of 12

Anonymous
Not applicable

It fixes the issue, but now i need to mark everything in TEST, previously i could have interesting objects already selected. I will look at this later, probably need to save current selection.

I didn't understand why it didn't work before because clearing selection was here:

(if (setq ssFinal (KeepMutual ss1 ss2))
    (progn (sssetfirst nil ssFinal) (sssetfirst nil ssFinal))
    (prompt "\n...No mutual items were found.")

 

Last small thing which bothers me now - how to check with "if" if variable contains selection?

(setq sss (ssget))

(if (sss) <- doesn't work

(if (not sss) <- work only if sss is nil, otherwise throws error

The only i have found working is:

(if (setq something sss)

or

(if (ssget "_I") <- needs active selection

 

Thank You for help.

0 Likes
Message 12 of 12

CodeDing
Advisor
Advisor

@Anonymous ,

 


Last small thing which bothers me now - how to check with "if" if variable contains selection?

(setq sss (ssget))

(if (sss) <- doesn't work


This doesn't work because you have your selection set wrapped in parentheses "()" which means that you are calling a function instead of passing an argument.. Remove the parentheses and this approach will work as you expect..

(if sss

Here is the updated code AGAIN. Lol, sometimes it's hard to interpret what help exactly somebody is asking for if it is not explicitly expressed.. I think this code accomplishes what you wish.. If not, let me know.

(defun c:TEST ( / ss1 ss2 ssFinal ssInitial cnt)
(setq ssInitial (ssget "_I")) ;(princ "\n..") (princ (sslength ssInitial))
(sssetfirst nil nil)
(prompt "\nSelect Initial sel set: ")
(if (not (setq ss1 (ssget))) (setq ss1 (ssadd)))
(prompt "\nSelect Items to Keep: ")
(if (setq ss2 (ssget))
  (progn
    (cond
      ((and ssInitial ss1) (repeat (setq cnt (sslength ssInitial)) (ssadd (ssname ssInitial (setq cnt (1- cnt))) ss1)))
      ((not ss1) (setq ss1 ssInitial))
    );cond
    (if (setq ssFinal (KeepMutual ss1 ss2))
      (sssetfirst nil ssFinal)
      (prompt "\n...No mutual items were found.")
    );if
  );progn
;else
  (prompt "\n...No items selected to keep.")
);if
(princ)
);defun

(defun KeepMutual (ss1 ss2 / ssFinal e e2 cnt cnt2 eFound)
(setq ssFinal nil ssFinal (ssadd))
(repeat (setq cnt (sslength ss2))
  (setq e (ssname ss2 (setq cnt (1- cnt)))
	eFound nil)
  (repeat (setq cnt2 (sslength ss1))
    (setq e2 (ssname ss1 (setq cnt2 (1- cnt2))))
    (if (eq e e2) (setq eFound t))
  );repeat ss1
  (if eFound (ssadd e ssFinal))
);repeat ss2
(if (> (sslength ssFinal) 0) ssFinal nil)
);defun

Best,

~DD

 

0 Likes