Lisp - select all from layer based on current selection

Lisp - select all from layer based on current selection

cool.stuff
Collaborator Collaborator
1,393 Views
9 Replies
Message 1 of 10

Lisp - select all from layer based on current selection

cool.stuff
Collaborator
Collaborator

Good morning all! 🙂

 

I found this lisp that selects everything that is on selected layers.

It is possible to change it in order to select everything that is on selected layers, based on user selection (instead on drawing model space)?

 

;;; LayerObjectSelect
;;; Select all objects on selected layers, in current layout
;;; Required Subroutines: AT:ListSelect, AT:TabFilter
;;; Alan J. Thompson, 11.05.09
(defun c:LOS (/ _Layers #List #Filter #SS)
 (vl-load-com)
 (setq _Layers (lambda (/ d n l)
                 (while (setq d (tblnext "layer" (null d)))
                   (and (not (wcmatch (setq n (cdr (assoc 2 d))) "*|*"))
                        (setq l (cons n l))
                   ) ;_ and
                 ) ;_ while
                 (vl-sort l '<)
               ) ;_ lambda
 ) ;_ setq
 (cond
   ((if dos_multilist
      (setq #List (dos_multilist "Select all objects on Layers" "Select layers:" (_Layers)))
      (setq #List (AT:ListSelect
                    "Select all objects on Layers"
                    "Select layers:"
                    "30"
                    "15"
                    "true"
                    (_Layers)
                  ) ;_ AT:ListSelect
      ) ;_ setq
    ) ;_ if
    (setq #Filter "")
    (foreach x #List (setq #Filter (strcat #Filter x ",")))
    (and (setq #SS (ssget "_X" (list (AT:TabFilter) (cons 8 #Filter))))
         (sssetfirst nil #SS)
         (print #List)
    ) ;_ and
   )
 ) ;_ cond
 (princ)
) ;_ defun


;list select dialog
;create a temp DCL multi-select list dialog from provided list
;value is returned in list form, DCL file is deleted when finished
;example: (setq the_list (AT:listselect "This is my list title" "Select items to make a list" "25" "30" "true" (list "object 1" "object 2" "object 3"))
;if mytitle is longer than defined width, the width will be ignored and it will fit to title string
;if mylabel is longer than defined width, mylabel will be truncated
;myheight and mywidth must be strings, not numbers
;mymultiselect must either be "true" or "false" (true for multi, false for single)
;created by: alan thompson, 9.23.08
;some coding borrowed from http://www.jefferypsanders.com (thanks for the DCL examples)

(defun AT:ListSelect  ( mytitle       ;title for dialog box
           mylabel       ;label right above list box
           myheight      ;height of dialog box !!*MUST BE STRING*!!
           mywidth       ;width of dialog box !!*MUST BE STRING*!!
           mymultiselect ;"true" for multiselect, "false" for single select
           mylist        ;list to display in list box
           / retlist readlist count item savevars fn fo valuestr dcl_id )
(defun saveVars(/ readlist count item)
 (setq retList(list))
 (setq readlist(get_tile "mylist"))
 (setq count 1)
 (while (setq item (read readlist))
   (setq retlist(append retList (list (nth item myList))))
   (while
     (and
       (/= " " (substr readlist count 1))
       (/= ""   (substr readlist count 1))
     )
     (setq count (1+ count))
   )
   (setq readlist (substr readlist count))
 )
);defun
(setq fn (vl-filename-mktemp "" "" ".dcl"))
(setq fo (open fn "w"))
(setq valuestr (strcat "value = \"" mytitle "\";"))
(write-line (strcat "list_select : dialog {
           label = \"" mytitle "\";") fo)
(write-line 
(strcat "          : column {
           : row {
             : boxed_column {
              : list_box {
                 label =\"" mylabel "\";
                 key = \"mylist\";
                 allow_accept = true;
                 height = " myheight ";
                 width = " mywidth ";
                 multiple_select = " mymultiselect ";
                 fixed_width_font = false;
                 value = \"0\";
               }
             }
           }
           : row {
             : boxed_row {
               : button {
                 key = \"accept\";
                 label = \" Okay \";
                 is_default = true;
               }
               : button {
                 key = \"cancel\";
                 label = \" Cancel \";
                 is_default = false;
                 is_cancel = true;
               }
             }
           }
         }
}") fo)
(close fo)
(setq dcl_id (load_dialog fn))
(new_dialog "list_select" dcl_id)
 (start_list "mylist" 3)
 (mapcar 'add_list myList)
 (end_list)
 (action_tile "cancel" "(setq ddiag 1)(done_dialog)")
 (action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
 (start_dialog)
 (if (= ddiag 1)
    (setq retlist nil)
 )
(unload_dialog dcl_id)
(vl-file-delete fn)
retlist
);defun


(defun AT:TabFilter (/)
  ;; Tab filter for ssget selection filtering
  ;; Alan J. Thompson, 06.05.09
  (if (eq 1 (getvar 'cvport))
    (cons 410 (getvar 'ctab))
    (cons 410 "Model")
  )
)

 

Thanks 🙂

0 Likes
Accepted solutions (2)
1,394 Views
9 Replies
Replies (9)
Message 2 of 10

komondormrex
Mentor
Mentor
Accepted solution

hey there,

replace line 31 with this

(and (setq #SS (ssget "_X" (list (cons 8 #Filter))))

 

Message 3 of 10

cool.stuff
Collaborator
Collaborator

Many thanks 🙂

0 Likes
Message 4 of 10

cool.stuff
Collaborator
Collaborator

@komondormrexsorry, yesterday when I tested it, I should have made something wrong.

 

When I use this code (replacing line 31), the lisp still selects everything that is on that layer, and not from the current user selection..

 

Could you help please? Thanks

0 Likes
Message 5 of 10

komondormrex
Mentor
Mentor
Accepted solution

sorry, check the update below

;;; LayerObjectSelect
;;; Select all objects on selected layers, in current layout
;;; Required Subroutines: AT:ListSelect, AT:TabFilter
;;; Alan J. Thompson, 11.05.09
(defun c:LOS (/ _Layers #List #Filter #SS)						;	komondormrex
 (vl-load-com)
 (if (zerop (getvar 'pickfirst)) (zerop (getvar 'pickfirst)))
 (setq _Layers (lambda (/ d n l)
                 (while (setq d (tblnext "layer" (null d)))
                   (and (not (wcmatch (setq n (cdr (assoc 2 d))) "*|*"))
                        (setq l (cons n l))
                   ) ;_ and
                 ) ;_ while
                 (vl-sort l '<)
               ) ;_ lambda
 ) ;_ setq
 (cond
   ((if dos_multilist
      (setq #List (dos_multilist "Select all objects on Layers" "Select layers:" (_Layers)))
      (setq #List (AT:ListSelect
                    "Select all objects on Layers"
                    "Select layers:"
                    "30"
                    "15"
                    "true"
                    (_Layers)
                  ) ;_ AT:ListSelect
      ) ;_ setq
    ) ;_ if
    (setq #Filter "")
    (foreach x #List (setq #Filter (strcat #Filter x ",")))
    (if (cadr (ssgetfirst))
	    (and (setq #SS (ssget "_i" (list (AT:TabFilter) (cons 8 #Filter))))			;	komondormrex
	         (sssetfirst nil #SS)
	         (print #List)
	    ) ;_ and
      	    (and (setq #SS (ssget "_x" (list (AT:TabFilter) (cons 8 #Filter))))			;	komondormrex
	         (sssetfirst nil #SS)
	         (print #List)
	    )
    )
   )
 ) ;_ cond
 (princ)
) ;_ defun


;list select dialog
;create a temp DCL multi-select list dialog from provided list
;value is returned in list form, DCL file is deleted when finished
;example: (setq the_list (AT:listselect "This is my list title" "Select items to make a list" "25" "30" "true" (list "object 1" "object 2" "object 3"))
;if mytitle is longer than defined width, the width will be ignored and it will fit to title string
;if mylabel is longer than defined width, mylabel will be truncated
;myheight and mywidth must be strings, not numbers
;mymultiselect must either be "true" or "false" (true for multi, false for single)
;created by: alan thompson, 9.23.08
;some coding borrowed from http://www.jefferypsanders.com (thanks for the DCL examples)

(defun AT:ListSelect  ( mytitle       ;title for dialog box
           mylabel       ;label right above list box
           myheight      ;height of dialog box !!*MUST BE STRING*!!
           mywidth       ;width of dialog box !!*MUST BE STRING*!!
           mymultiselect ;"true" for multiselect, "false" for single select
           mylist        ;list to display in list box
           / retlist readlist count item savevars fn fo valuestr dcl_id )
(defun saveVars(/ readlist count item)
 (setq retList(list))
 (setq readlist(get_tile "mylist"))
 (setq count 1)
 (while (setq item (read readlist))
   (setq retlist(append retList (list (nth item myList))))
   (while
     (and
       (/= " " (substr readlist count 1))
       (/= ""   (substr readlist count 1))
     )
     (setq count (1+ count))
   )
   (setq readlist (substr readlist count))
 )
);defun
(setq fn (vl-filename-mktemp "" "" ".dcl"))
(setq fo (open fn "w"))
(setq valuestr (strcat "value = \"" mytitle "\";"))
(write-line (strcat "list_select : dialog {
           label = \"" mytitle "\";") fo)
(write-line 
(strcat "          : column {
           : row {
             : boxed_column {
              : list_box {
                 label =\"" mylabel "\";
                 key = \"mylist\";
                 allow_accept = true;
                 height = " myheight ";
                 width = " mywidth ";
                 multiple_select = " mymultiselect ";
                 fixed_width_font = false;
                 value = \"0\";
               }
             }
           }
           : row {
             : boxed_row {
               : button {
                 key = \"accept\";
                 label = \" Okay \";
                 is_default = true;
               }
               : button {
                 key = \"cancel\";
                 label = \" Cancel \";
                 is_default = false;
                 is_cancel = true;
               }
             }
           }
         }
}") fo)
(close fo)
(setq dcl_id (load_dialog fn))
(new_dialog "list_select" dcl_id)
 (start_list "mylist" 3)
 (mapcar 'add_list myList)
 (end_list)
 (action_tile "cancel" "(setq ddiag 1)(done_dialog)")
 (action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
 (start_dialog)
 (if (= ddiag 1)
    (setq retlist nil)
 )
(unload_dialog dcl_id)
(vl-file-delete fn)
retlist
);defun


(defun AT:TabFilter (/)
  ;; Tab filter for ssget selection filtering
  ;; Alan J. Thompson, 06.05.09
  (if (eq 1 (getvar 'cvport))
    (cons 410 (getvar 'ctab))
    (cons 410 "Model")
  )
)
Message 6 of 10

cool.stuff
Collaborator
Collaborator

Works like a charm!! Perfect!!! 🙂

 

Many thanks!! 🙂

0 Likes
Message 7 of 10

jaimuthu
Advocate
Advocate

user selection not work

0 Likes
Message 8 of 10

stev98312
Enthusiast
Enthusiast

I use this little thing, which works well for me, but my CAD drawings are not all that complicated and OP may need much more, as in the examples given above by @komondormrex .

;; Code extracted from ASMI Tools > RENLAY.
;; With slight modifications.
;; Selects all objects on same layer.
;
;  Run the program and pick a thing. All other things on the same 
;    layer as the thing you picked will be selected for you.

(defun c:GetAll ( / samObj filLst)
  (if (setq samObj (entsel "\nSelect object at wanted layer > "))
    (progn
      (setq filLst (assoc 8 (entget (car samObj))))
      (sssetfirst nil (ssget "_X" (list filLst)))
    )
  )
)
;(c:GetAll)

 

Steve

Message 9 of 10

jaimuthu
Advocate
Advocate
i want(select object:) user selection in this code
;;; LayerObjectSelect
;;; Select all objects on selected layers, in current layout
;;; Required Subroutines: AT:ListSelect, AT:TabFilter
;;; Alan J. Thompson, 11.05.09
(defun c:LOS (/ _Layers #List #Filter #SS) ; komondormrex
(vl-load-com)
(if (zerop (getvar 'pickfirst)) (zerop (getvar 'pickfirst)))
(setq _Layers (lambda (/ d n l)
(while (setq d (tblnext "layer" (null d)))
(and (not (wcmatch (setq n (cdr (assoc 2 d))) "*|*"))
(setq l (cons n l))
) ;_ and
) ;_ while
(vl-sort l '<)
) ;_ lambda
) ;_ setq
(cond
((if dos_multilist
(setq #List (dos_multilist "Select all objects on Layers" "Select layers:" (_Layers)))
(setq #List (AT:ListSelect
"Select all objects on Layers"
"Select layers:"
"30"
"15"
"true"
(_Layers)
) ;_ AT:ListSelect
) ;_ setq
) ;_ if
(setq #Filter "")
(foreach x #List (setq #Filter (strcat #Filter x ",")))
(if (cadr (ssgetfirst))
(and (setq #SS (ssget "_i" (list (AT:TabFilter) (cons 8 #Filter)))) ; komondormrex
(sssetfirst nil #SS)
(print #List)
) ;_ and
(and (setq #SS (ssget "_x" (list (AT:TabFilter) (cons 8 #Filter)))) ; komondormrex
(sssetfirst nil #SS)
(print #List)
)
)
)
) ;_ cond
(princ)
) ;_ defun


;list select dialog
;create a temp DCL multi-select list dialog from provided list
;value is returned in list form, DCL file is deleted when finished
;example: (setq the_list (AT:listselect "This is my list title" "Select items to make a list" "25" "30" "true" (list "object 1" "object 2" "object 3"))
;if mytitle is longer than defined width, the width will be ignored and it will fit to title string
;if mylabel is longer than defined width, mylabel will be truncated
;myheight and mywidth must be strings, not numbers
;mymultiselect must either be "true" or "false" (true for multi, false for single)
;created by: alan thompson, 9.23.08
;some coding borrowed from http://www.jefferypsanders.com (thanks for the DCL examples)

(defun AT:ListSelect ( mytitle ;title for dialog box
mylabel ;label right above list box
myheight ;height of dialog box !!*MUST BE STRING*!!
mywidth ;width of dialog box !!*MUST BE STRING*!!
mymultiselect ;"true" for multiselect, "false" for single select
mylist ;list to display in list box
/ retlist readlist count item savevars fn fo valuestr dcl_id )
(defun saveVars(/ readlist count item)
(setq retList(list))
(setq readlist(get_tile "mylist"))
(setq count 1)
(while (setq item (read readlist))
(setq retlist(append retList (list (nth item myList))))
(while
(and
(/= " " (substr readlist count 1))
(/= "" (substr readlist count 1))
)
(setq count (1+ count))
)
(setq readlist (substr readlist count))
)
);defun
(setq fn (vl-filename-mktemp "" "" ".dcl"))
(setq fo (open fn "w"))
(setq valuestr (strcat "value = \"" mytitle "\";"))
(write-line (strcat "list_select : dialog {
label = \"" mytitle "\";") fo)
(write-line
(strcat " : column {
: row {
: boxed_column {
: list_box {
label =\"" mylabel "\";
key = \"mylist\";
allow_accept = true;
height = " myheight ";
width = " mywidth ";
multiple_select = " mymultiselect ";
fixed_width_font = false;
value = \"0\";
}
}
}
: row {
: boxed_row {
: button {
key = \"accept\";
label = \" Okay \";
is_default = true;
}
: button {
key = \"cancel\";
label = \" Cancel \";
is_default = false;
is_cancel = true;
}
}
}
}
}") fo)
(close fo)
(setq dcl_id (load_dialog fn))
(new_dialog "list_select" dcl_id)
(start_list "mylist" 3)
(mapcar 'add_list myList)
(end_list)
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
(start_dialog)
(if (= ddiag 1)
(setq retlist nil)
)
(unload_dialog dcl_id)
(vl-file-delete fn)
retlist
);defun


(defun AT:TabFilter (/)
;; Tab filter for ssget selection filtering
;; Alan J. Thompson, 06.05.09
(if (eq 1 (getvar 'cvport))
(cons 410 (getvar 'ctab))
(cons 410 "Model")
)
)
0 Likes
Message 10 of 10

jaimuthu
Advocate
Advocate

How to solve this error

 

(defun c:LOS (/ #SS #List #Filter)
(vl-load-com)

;; Select objects first
(setq #SS (ssget '((0 . "*")))) ; Select all objects

;; Check if objects are selected
(if (null #SS)
(progn
(princ "\nNo objects selected.")
(return)
)
)

;; Extract layers from selected objects
(setq #List (extractLayers #SS))

;; Remove duplicate layers
(setq #List (vl-remove-duplicates #List :test 'equal))

;; Filter based on selected layers
(setq #Filter (AT:ListSelect "Select layers:" "Select layers:" "30" "15" "true" #List)) ; Filter based on selected layers

;; Process the selection
(cond
((/= #Filter nil)
(setq #SS (ssget "_X" (list (AT:TabFilter) (cons 8 (apply 'strcat #Filter)))))
(if (null #SS)
(princ "\nNo objects found on selected layers.")
(progn
(sssetfirst nil #SS)
(print #Filter)
)
)
)
(T (princ "\nNo layers selected."))
)
(princ)
)

(defun extractLayers (ss)
(mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 8)) (mapcar 'entget (ssnamex ss))))
)

(defun AT:TabFilter (/)
;; Tab filter for ssget selection filtering
;; Alan J. Thompson, 06.05.09
(if (eq 1 (getvar 'cvport))
(cons 410 (getvar 'ctab))
(cons 410 "Model")
)
)

(defun AT:ListSelect (mytitle mylabel myheight mywidth mymultiselect mylist / retlist readlist count item savevars fn fo valuestr dcl_id)
(defun saveVars (/ readlist count item)
(setq retlist (list))
(setq readlist (get_tile "mylist"))
(setq count 1)
(while (setq item (read readlist))
(setq retlist (append retlist (list (nth item mylist))))
(while (and (/= " " (substr readlist count 1))
(/= "" (substr readlist count 1))))
(setq count (1+ count))
)
(setq readlist (substr readlist count))
);defun

(setq fn (vl-filename-mktemp "" "" ".dcl"))
(setq fo (open fn "w"))
(setq valuestr (strcat "value = \"" mytitle "\";"))
(write-line (strcat "list_select : dialog {
label = \"" mytitle "\";") fo)
(write-line (strcat " : column {
: row {
: boxed_column {
: list_box {
label =\"" mylabel "\";
key = \"mylist\";
allow_accept = true;
height = " myheight ";
width = " mywidth ";
multiple_select = " mymultiselect ";
fixed_width_font = false;
value = \"0\";
}
}
}
: row {
: boxed_row {
: button {
key = \"accept\";
label = \" Okay \";
is_default = true;
}
: button {
key = \"cancel\";
label = \" Cancel \";
is_default = false;
is_cancel = true;
}
}
}
}
}") fo)
(close fo)
(setq dcl_id (load_dialog fn))
(new_dialog "list_select" dcl_id)
(start_list "mylist" 3)
(mapcar 'add_list mylist)
(end_list)
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
(start_dialog)
(if (= ddiag 1)
(setq retlist nil)
)
(unload_dialog dcl_id)
(vl-file-delete fn)
retlist
);defun