Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

LISP to assign the layer for selected object OR assign the layer as active one if no selected object

bigcarl5000kg
Advisor

LISP to assign the layer for selected object OR assign the layer as active one if no selected object

bigcarl5000kg
Advisor
Advisor

Dear community,

 

please, can someone create a LISP for assigning a predefined layer to the selected objects, if no objects were selected, then the predefined layer would only be set as active.

 

Example:
I have objects of different types selected, after calling LISP, the "CONTOUR" layer would be assigned to them, if I had no other object in the active selection, after calling LISP the "COUNTOUR" layer would be set as the currently active one, so that I could create new objects in it.

 

I don't know if this can be replaced using a command macro, that's why I chose the LISP option.

 

Thank you in advance for your help

 

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Reply
Accepted solutions (1)
363 Views
5 Replies
Replies (5)

paullimapa
Mentor
Mentor

give this a try:

; chssly function takes layer name as argument 
; if there are objects actively selected will change them to that layer
; else sets layer current
; Example:
; (chssly "CONTOUR") ; will change objects in active selection to layer "CONTOUR"
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-assign-the-layer-for-selected-object-or-assign-the-layer/m-p/11974514#M448598
(defun chssly (lyr / chk_ly cly ss)
 (defun chk_ly (nam)
  (if(not(tblsearch"Layer" nam))(command-s "'_.Layer" "_New" nam "" "")) ; create new layer if not exist
 )
 (if(setq ss(ssget "_I")) ; if there are objects actively selected
  (progn
   (chk_ly lyr)
   (foreach b (mapcar 'cadr (ssnamex ss))  ; create list of all entities from selection set & cycle through
    (if (= 'ename (type b)) ; confirm is entity
     (vla-put-layer (vlax-ename->vla-object b) lyr) ; change object to layer
    )
   ) ; loop
   (princ(strcat"\nSelected Objects Successfully Changed to Layer: [" lyr "]."))
  ) ; progn
  (progn
   (chk_ly lyr)
   (setvar"clayer"lyr) ; set as current layer
   (princ"\nNo Objects are actively selected.")
  ) ; progn
 )(princ)
) ; defun

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos

komondormrex
Advisor
Advisor

hi there,

code from adjacent topic, slightly refurbished.

 

 

;*****************************************************************************************************************************

(defun get_layer (/ temp_file_path dcl_filename_full sl_dcl_iddcl_file_id  dwg_layers_list index selected_layer)
  	(setq temp_file_path (vla-get-TempFilePath (vla-get-Files (vla-get-preferences (vlax-get-acad-object))))
		  dcl_filename_full (vl-filename-mktemp "Dialog" temp_file_path ".dcl"])
		  dcl_file_id (open dcl_filename_full "w")
	)
  	(vlax-for dwg_layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
			(setq dwg_layers_list (append dwg_layers_list
									  (list (vla-get-name dwg_layer))
							  	  )
			)
	)
	(setq dwg_layers_list (acad_strlsort dwg_layers_list))
	(mapcar '(lambda (dcl_str) (write-line
									dcl_str
									dcl_file_id
							   )
		 	 )
		 	 (list
		 		"Layers_List : dialog {"
    	 								"label = \"Pick layer\";"
		 									": list_box {"
		 													"key = \"Dwg_Layers_List\";"
		 													"width = 50;"
		 													"height = 20;"
		 												"}"
		 									"ok_cancel;"
		 		     	 			 "}"
			 )
	)
	(close dcl_file_id)
	(setq sl_dcl_id (load_dialog dcl_filename_full))
	(if sl_dcl_id
		(if (new_dialog "Layers_List" sl_dcl_id)
			(progn
			  	(start_list "Dwg_Layers_List" 3)
					(mapcar 'add_list
					  	(mapcar '(lambda (list_element)
					  			 	(strcat "\n" list_element)
					  			 )
					  			 dwg_layers_list
					  	)
					)
				(end_list)
				(action_tile "Dwg_Layers_List" "(cond
									(
										(= 4 $reason)
											(setq index $value)
											(done_dialog 1)
									)
									(
										t
											(setq index $value)
									)
								)
							   "
				)
				(setq dialog_result (start_dialog))
				(unload_dialog sl_dcl_id)
				(vl-file-delete dcl_filename_full)
				(cond
					(
						(= 1 dialog_result)
							(if index
								(setq selected_layer (nth (atoi index) dwg_layers_list))
							)
					)
					(
						t
							(setq selected_layer nil)
					)
				)
			)
		)
	)
	selected_layer
)

;*****************************************************************************************************************************

(defun c:move_to_layer (/ enames_sset move_to_layer_name)
	(if (setq move_to_layer_name (get_layer))
		(if (setq enames_sset (ssget))
			(progn
				
				(if move_to_layer_name 
					(foreach object (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex enames_sset))))
						(vla-put-layer object move_to_layer_name)
					)
					(alert "No layer was selected!")
				)
			)
			(vla-put-activelayer (vla-get-activedocument (vlax-get-acad-object))
					     (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) move_to_layer_name)
			)
		)
		(alert "\nNo layer was selected")
	)
	(princ)
)

 

 

Kent1Cooper
Consultant
Consultant
Accepted solution

@bigcarl5000kg wrote:

... a LISP for assigning a predefined layer to the selected objects, if no objects were selected, then the predefined layer would only be set as active. ....


I have had this simple command to do exactly that with Layer "0" for years.  Just substitute your desired Layer name.

 

 

(defun C:L0 ()
  ; put pre-selected object(s) [if any] on Layer 0, otherwise set current Layer to 0
  (if (ssget "_I")
    (command "_.chprop" "_layer" "0" ""); then
    (command "_.layer" "_thaw" "0" "_set" "0" ""); else
  ); if
  (princ)
)

 

 

That doesn't allow for the possibility that the Layer may not exist -- not an issue for Layer 0.  Do you need it to make it if it doesn't?  [You did say "predefined."]

Kent Cooper, AIA

bigcarl5000kg
Advisor
Advisor

Dear @Kent1Cooper,

 

your solution is amazing and easy, thank you very much and have a nice day.

 

 

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes

WeTanks
Mentor
Mentor

Wow! It is great, Thank you very much!

We.Tanks

EESignature

A couple of Fusion improvement ideas that could your vote/support:
図面一括印刷

0 Likes