; DDCHL change selected objects layer using list box ; comparable to : ; (progn (initcommandversion) (command-s "_.laymch" (ssget) "" "_n")) ; this presents a stretchable dialog but no layer name filter ; OP: ; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/ribbon-or-dcl-for-all-layers/m-p/13810135#M166574 (defun c:DDCHL (/ b fto getlay hit idx lay lst pl:listbox wid ss) (setq wid 80 ; dialog width hit 50 ; dialog height fto "Defpoints,*|*" ; layer names to filter out ) (vl-load-com) ; getlay function to get list of layers in current dwg ; fil-arg = layer names to filter out (defun getlay (fto-arg / la) (vlax-for x (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))) (if (not (wcmatch (vla-get-name x) fto-arg)) ; filter layers out (setq la (cons (vla-get-name x) la)) ) ) (vl-sort la '<) ) ;;;--- pl:listbox function ; displays dialog list box to do selection returning list of item(s) selected ; title-arg = A string containing the dialog box title ; msg-arg = A string containing the dialog box msg ; list-arg = A list of string values ; sel-arg = An integer of item to be selected starting with 0 as first item ; wid-arg = An integer of dialog width ; hit-arg = An integer of dialog height ; mul-arg = nil is single item selection & T is multiple item selection ; Usage: ; (pl:listbox nil nil nil 0 20 30 nil) ; lists default items with first item selected on a list box that's 20 wide x 30 high with single selection enabled ; (pl:listbox "Selection Window" "Select Item:" nil 1 20 30 T) ; lists default items with second item selected on a list box that's 20 wide x 30 high with multiple selection enabled (defun pl:listbox (title-arg msg-arg lst-arg sel-arg wid-arg hit-arg mul-arg / count dch dcl dcl-list des item listbox_err listbox_olderr ret retlist tmp ) (defun listbox_err ( msg ) (term_dialog) (if (< 0 dch) (unload_dialog dch)) (if (= 'file (type des)) (close des)) (if (and (= 'str (type dcl)) (findfile dcl)) (vl-file-delete dcl)) (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))) (princ (strcat "\nError: " msg)) ) (if listbox_olderr (setq *error* listbox_olderr)) ; Restore old *error* handler (princ) ) (if(eq lst-arg nil) ; if argument not provided (progn (setq count -1) (repeat 9 (setq lst-arg (append lst-arg (list (strcat "Item " (itoa (setq count (1+ count)))))))) ; list 9 items ) ) ; if list argument is nil (if (eq title-arg nil) (setq title-arg "Selection Window:")) ; if argument not provided (if (or (not (type sel-arg)) (/= (type sel-arg) 'INT)) (setq sel-arg 0)) ; if argument not provided (if (or (not (type wid-arg)) (/= (type wid-arg) 'INT)) (setq wid-arg 20)); if argument not provided (if (or (not (type hit-arg)) (/= (type hit-arg) 'INT)) (setq hit-arg 30)); if argument not provided (if (>= sel-arg (length lst-arg)) (setq sel-arg (1- (length lst-arg)))) ; if selection item is greater than list items set to last item (if (eq msg-arg nil)(setq msg-arg "Choose Item in List:")) ; if argument not provided (setq count 1) (if (and (setq dcl (vl-filename-mktemp nil nil ".dcl")) (setq des (open dcl "w")) (setq tmp (list "listbox : dialog" "{" (strcat " label = \" " title-arg "\";") " spacer;" " : row" " {" " : list_box" " {" (strcat " label = \" " msg-arg "\";") " key = \"lst\";" " width = " (itoa wid-arg) ";" " height = " (itoa hit-arg) ";" " fixed_width = true;" " fixed_height = true;" ) ) (if (not mul-arg) ; chk selection type (setq tmp ; then single selection (append tmp (list " allow_accept = true;" ) ) ) (setq tmp ; else multiple selection (append tmp (list " multiple_select = true;" ) ) ) ) (setq tmp (append tmp (list " }" " }" " spacer;" " ok_cancel;" "}" ) ) ) (foreach str tmp (write-line str des) ) (not (setq des (close des))) (< 0 (setq dch (load_dialog dcl))) (new_dialog "listbox" dch "" pl:dlg_pos) ; location to position dialog ) (progn (setq listbox_olderr *error* ; Save error routine *error* listbox_err ; Substitute ours ) (start_list "lst") ; populate list box with list items (foreach itm lst-arg (add_list itm)) (end_list) (set_tile "lst" (setq item (itoa sel-arg))) ; select item on list box (action_tile "lst" "(setq item $value)") (action_tile "accept" "(setq item (get_tile \"lst\"))(setq pl:dlg_pos (done_dialog 1))") (action_tile "cancel" "(setq item nil)(done_dialog 0)") (start_dialog) (if (findfile dcl) (vl-file-delete dcl)) ; delete dcl (if item ;;;---cycle through the list getting all of the selected items (while (setq ret (read item)) (setq retlist (append retlist (list (nth ret lst-arg)))) (while (and (/= " " (substr item count 1)) ; add space (/= "" (substr item count 1)) ) (setq count (1+ count)) ); while (setq item (substr item count)) ); while ) ; if (if listbox_olderr (setq *error* listbox_olderr)) ; Restore old *error* handler (princ) ) (princ "\nUnable to write/load/display dialog.") ) ; if retlist ) ; defun (if (and (setq ss (ssget ":L")) ; select objects on unlocked layers (setq lst (getlay fto)) ; get layer list with filter (setq idx (vl-position (getvar "clayer") lst)) ; find list index of current layer (setq lay (pl:listbox "Selection Window" "Select Item:" lst idx wid hit nil)) ; list box given width & height values ) (progn (foreach b (mapcar 'cadr (ssnamex ss)) ; create list of all entities from selection set & cycle through (if (= 'ename (type b)) ; confirm is entity (setpropertyvalue b "LayerId" (tblobjname "LAYER" (car lay))) ; set to selected layer ) ) ) (alert "Function Cancelled") )(princ) ) ; defun