Jump to content

Lisp for Rename the layer


gmmdinesh

Recommended Posts

Hi,

I need a lisp code for automatically rename or change a particular layer to another layer name or new layer through just click the line object.

Anything will be greatly appreciated.:)

 

Thanks in advance

Edited by gmmdinesh
Link to comment
Share on other sites

There are plenty of lisp routines that will change a layer name. A little effort is all it takes to find them. Seek and ye shall find.

Link to comment
Share on other sites

Heres an example:

(defun C:test ( / v ns se c le lx n s i enx)
(while (setq v (tblnext "LAYER" (not v))) (setq ns (cons (cdr (assoc 2 v)) ns)))
(if 
	(and 
		(setq se (car (entsel "\nSelect object, to redefine its layer: ")))
		(setq lx (entget (setq le (tblobjname "LAYER" (cdr (setq c (assoc 8 (entget se))))))))
		(/= 4 (logand 4 (cdr (assoc 70 lx))))
		(or 
			(setq n (car (LM:listbox "Select Existing layer to change to" (acad_strlsort ns) 0)))
			(setq n (PromptBox "Create New Or Type Existing Layer" " Type your layer's name here " ))
		)
	)
	(progn 
		(if (not (member n ns)) (Layer n))
		(if (setq s (ssget "_X" (list c)))
			(repeat (setq i (sslength s))
				(setq 
					enx (entget (ssname s (setq i (1- i))))
					enx (subst (cons 8 n) (assoc 8 enx) enx)
				)
				(entupd (cdr (assoc -1 (entmod enx))))
			)
		)
	)
)
(princ)
);| defun |; (vl-load-com) (princ)


(defun Layer (Nme) ; Lee Mac
(entmake
	(list 
		(cons 0 "LAYER")
		(cons 100 "AcDbSymbolTableRecord")
		(cons 100 "AcDbLayerTableRecord")
		(cons 2 Nme)
		(cons 70 0)
	)
)
)

; PromptBox example by Lee Mac
; Arguments:
; title - Dialog Box Title
; msg - [Optional] Text to Display
; Returns: Entered String if user presses OK, else nil
(defun PromptBox ( title msg / dcl dch file val )
(cond
	(
		(not
			(and
				(setq dcl (vl-filename-mktemp nil nil ".dcl"))
				(setq file (open dcl "w"))
				(progn
					(write-line
						(strcat ; this was original set:     edit_width = 60; edit_limit = 2048
							"promptbox : dialog 
							{ label = \"" title "\"; initial_focus = \"txt\";
							spacer; "
							": edit_box { key = \"txt\"; edit_width = 40; edit_limit = 2048; allow_accept = true; }
							spacer; ok_cancel; 
							}"
						)
						file
					)
					(setq file (close file))
					(findfile dcl)
				)
			)
		)
	)
	(
		(<= (setq dch (load_dialog dcl)) 0)
		(vl-file-delete dcl)
	)
	(
		(not (new_dialog "promptbox" dch))
		(unload_dialog dch)
		(vl-file-delete dcl)
	)
	(T
		(if msg (setq val (set_tile "txt" msg)))
		(action_tile "txt" "(setq val $value)")
		(if (zerop (start_dialog)) (setq val nil))
		(unload_dialog dch)
		(vl-file-delete dcl)
	)
); cond
val
); defun PromptBox



;; List Box  -  Lee Mac
;; Displays a DCL list box allowing the user to make a selection from the supplied data.
;; msg - [str] Dialog label
;; lst - [lst] List of strings to display
;; bit - [int] 1=allow multiple; 2=return indexes
;; Returns: [lst] List of selected items/indexes, else nil
(defun LM:listbox ( msg lst bit / dch des tmp rtn )
(cond
	(   (not
		(and
			(setq tmp (vl-filename-mktemp nil nil ".dcl"))
			(setq des (open tmp "w"))
			(write-line
				(strcat "listbox:dialog{label=\"" msg "\";spacer;:list_box{key=\"list\";multiple_select="
					(if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
				)
				des
			)
			(not (close des))
			(< 0 (setq dch (load_dialog tmp)))
			(new_dialog "listbox" dch)
		)
	)
	(prompt "\nError Loading List Box Dialog.")
	)
	(   t     
		(start_list "list")
		(foreach itm lst (add_list itm))
		(end_list)
		(setq rtn (set_tile "list" "0"))
		(action_tile "list" "(setq rtn $value)")
		(setq rtn
			(if (= 1 (start_dialog))
				(if (= 2 (logand 2 bit))
					(read (strcat "(" rtn ")"))
					(mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
				)
			)
		)
	)
)
(if (< 0 dch)
	(unload_dialog dch)
)
(if (and tmp (setq tmp (findfile tmp)))
	(vl-file-delete tmp)
)
rtn
)																	

Though It won't purge the original layer.

Link to comment
Share on other sites

  • 2 years later...

This topic is old but it seems that there is not a clear and easy answer in internet, so I put this here in case it's helpful:

 

(defun C:aux ( / acadDocument theLayers layName)

(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(princ)
);defun

(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (member layName '("Layer_to_be_rename"))
(vla-put-Name thelayer (strcat "New_Layer_Name"))
) ;if
);defun
(princ)

A little modification of a lisp that some day I found in some place

Link to comment
Share on other sites

Rather than iterating over the entire Layers Collection to rename a single layer, you can obtain it directly using tblobjname, e.g.:

(defun c:layren ( / enx obj old new )
    (setq old "Layer_to_be_Renamed"
          new "New_Layer_Name"
    )
    (if (and (setq obj (tblobjname "layer" old))
             (setq enx (entget obj))
        )
        (entmod (subst (cons 2 new) (assoc 2 enx) enx))
    )
    (princ)
)

 

  • Thanks 1
Link to comment
Share on other sites

20 hours ago, Lee Mac said:

Rather than iterating over the entire Layers Collection to rename a single layer, you can obtain it directly using tblobjname, e.g.:

Thank you Lee Mac! An easier way to get it. I Didn't know tblobjname function

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...