Replace special characters in CAD layer names.

Replace special characters in CAD layer names.

vishalghuge2500
Enthusiast Enthusiast
1,153 Views
8 Replies
Message 1 of 9

Replace special characters in CAD layer names.

vishalghuge2500
Enthusiast
Enthusiast

Hello everyone,

I'm searching for a LISP routine that can replace all special characters in CAD layer names with "-".

I'm currently using AutoCAD 2020. I've come across some solutions, but I'm struggling to understand how to use them.

 

And I need this for my revit Dyanmo script solution

https://forum.dynamobim.com/t/import-cad-text-layer-names-with-special-characters/100631

 

vishalghuge2500_0-1714726932295.png

 

0 Likes
1,154 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

@vishalghuge2500 wrote:

.... I've come across some solutions, but I'm struggling to understand how to use them. ....


Example(s), please, to avoid re-inventing the wheel if there's something out there that works.  Describe what you don't understand about how to use them.

 

And it occurs to me:  What should happen if you have multiple Layers whose names consist of only special characters, and the same number of them?  You'll get an error if you try to change a Layer's name to "-----" if there's already one called that, coming from some other original name.

Kent Cooper, AIA
Message 3 of 9

vishalghuge2500
Enthusiast
Enthusiast

@Kent1Cooper 

After loading below LISP file into AutoCAD, I'm entering the command "RENLAY", but no command is found by that name.

is this because of Autocad 2020

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/rename-layers-in-some-cases-for-spec...

vishalghuge2500_0-1714736378766.png

vishalghuge2500_1-1714736396245.png

 

 

 

 

0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant

Which of the many routines in that topic?  I don't see any file attachments there, so you must have saved one of them to the file you loaded.  And the file name is not the command name -- a .lsp file can contain multiple command and routine definitions.  A command name that you would type in must always be the part immediately following

(defun C:

in the code [the C can be in either case].

 

It's confusing that a lot of them have

; here start c:renlay

at the top, because they don't start that, but a non-command function.

 

If you took the accepted-solution routine, or the modification of it at Message 22, that does not define a command [it is missing the C: in the (defun line], but a routine.  Try this [with the parentheses] at the command line:

(renlay)

Or, change its (defun line to make a command whose name you can enter:

(defun C:renlay (/ ....

and then RENLAY will be a command name.

 

If you used the routine I see there that actually defines a command with the C: in the (defun line, at Message 17, its command name is STRIPLAY.

Kent Cooper, AIA
0 Likes
Message 5 of 9

Sea-Haven
Mentor
Mentor

Like others it was solved and does exist, I am pretty sure was asked here. Did You Google search, phrase is very relevant, trying to find I am sure did something, maybe 12 months old. ® @ © or something like that.

0 Likes
Message 6 of 9

TomBeauford
Advisor
Advisor

You should also look at the AutoCAD Layer Translator aka the LAYTRANS (Command).

https://www.autodesk.com/blogs/autocad/autocad-layer-translator-tuesday-tips-with-frank/

64bit AutoCAD Map & Civil 3D 2023
Architecture Engineering & Construction Collection
2023
Windows 10 Dell i7-12850HX 2.1 Ghz 12GB NVIDIA RTX A3000 12GB Graphics Adapter
0 Likes
Message 7 of 9

vishalghuge2500
Enthusiast
Enthusiast

 

thanks to @paullimapa & chatGPT

(defun c:RENLAY ()
  (defun get_uni_lyrnam (nam / i j nnam tnam)
   (if (tblsearch "LAYER" nam)
     (progn
      (setq j (- 255 2)) ; leave 2 characters to add <##>
      (if (> (strlen nam) j) 
        (setq tnam (substr nam 1 j))
        (setq tnam nam)
	  )
      (setq i 1)
      (while i
       (if (tblsearch "LAYER" (setq nnam (strcat tnam (itoa i)))) ; if name exists then try again
        (setq i (1+ i))
        (setq i nil)
       )
      ) ; while
     ) ; progn
     (setq nnam nam)
   ) ; if
   nnam
  ) ; defun get_uni_lyrnam  

 (defun get_canonical_layers (/ tbl lst lname) 
  (vl-remove-if
    'not
    (mapcar
      (function
        (lambda (s)
          (vl-some
   (function
     (lambda (ch)
              (if (vl-string-search ch s) s)
             ); lambda
   ); function
           CHAR2DEL
          ); vl-some
        ); lambda
      ); function
      (progn
       (while (setq tbl (tblnext "layer" (not tbl)))
        (if (not (vl-string-search "|" (setq lname (cdr (assoc '2 tbl))))) ; xdep layer is skipped
         (setq lst (cons lname lst))
        ); if
       ); while
       lst
      ); progn
    ); mapcar
  ); vl-remove-if
 );  build_data 
  
 (defun get_lyrlst (/ chrchk tbl lst n lstnew)
       (while (setq tbl (tblnext "layer" (not tbl)))
        (if (not (vl-string-search "|" (setq lname (cdr (assoc '2 tbl))))) ; xdep layer is skipped
         (setq lst (cons lname lst))
        ); if
       ); while
       (foreach itm lst ; loop through initial layername list
         (setq n 0)
         (while (< n (length CHAR2DEL))
           (setq chrchk (nth n CHAR2DEL))
           (if(> (strlen chrchk) 1)(setq chrchk (substr chrchk 2 1))) ; start from 2nd charcter with these special strings
           (if (vl-string-search chrchk itm) 
             (progn ; there's a match
               (setq n (length CHAR2DEL)) ; stop loop
               (if(not(member itm lstnew))(setq lstnew (cons itm lstnew))) ; add to lstnew
             )
             (setq n (1+ n)) ; do next
           ) ; if
         ) ; while
       ) ; foreach
      lstnew
 ) ; defun
  
 ; del redundant characters from layer name 
 (defun del_redun_char (wName nCh / eCh xCh)
  (foreach eCh CHAR2DEL
   (setq xCh eCh)
   (if(> (strlen xCh) 1)(setq xCh (substr xCh 2 1))) ; start from 2nd charcter with these special strings
   (while (vl-string-search xCh wName)
    (setq wName (vl-string-subst nCh xCh wName))
   )  
  ); foreach
  wName
 ); del_redun_char

 (defun is_layer_renamed (wName oName / ename elist)
  (setq wName (get_uni_lyrnam wName)) ; get layer name that does not exist so can rename
  (if (not (tblobjname "layer" wName))
   (progn
    (setq elist (entget (tblobjname "layer" oName)))
    (setq wName (vl-string-right-trim " " (vl-string-left-trim " " wName)))
    (if (entmod (subst (cons '2 wName) (assoc '2 elist) elist)) wName)
   ); progn
  ); if
;   (vl-cmdf"_.Rename""_Layer" oName wName) ; use command to rename layer
 ); is_layer_renamed

 (defun summarize ()
  (cond
   ((= ctr 0)
    (princ "\n0 layer(s) found to rename.")
   ); case
   ( t
    (princ (strcat "\n" (itoa ctr) " layer(s) successfully renamed."))
   ); case
  ); cond
 ); summarize
  ; here start c:renlay
 (command "_.layer" "_unlock" "*" "")
 ;; (command "LAYTHW") ; removed to keep layers frozen
 (setq adoc (vla-get-ActiveDocument (vlax-get-acad-object)))
 (vla-startUndoMark adoc)
 (setq CHAR2DEL '("." "~" "!" "@" "#" "%" "^" "&" "©" "®" "(" ")" "[" "]" "{" "}" "+" "'" "؛" "÷" "×" "\""
                   "@" "•")) ; add the special characters here
 (setq ctr 0) 
 (foreach orgName (reverse (get_lyrlst)) ; (reverse (get_canonical_layers))
  (cond
   ((is_layer_renamed (del_redun_char orgName "") orgName)
    (setq ctr (1+ ctr))
   ); case
   ((setq s (is_layer_renamed (del_redun_char orgName " ") orgName))
    (setq ctr (1+ ctr))
    (princ (strcat "\nLayer \"" orgName "\" is renamed to \"" s "\"."))
   ); case 
   ((setq d (is_layer_renamed (del_redun_char orgName "-") orgName)) ; accommodates for single character layer names
    (setq ctr (1+ ctr))
    (princ (strcat "\nLayer \"" orgName "\" is renamed to \"" d "\"."))
   ); case 
   (t
    (princ (strcat "\nfail to rename layer \"" orgName "\"."))
   ); case
  ); cond
 ); foreach
 (summarize)
 (vla-endUndoMark adoc)
 (vlax-release-object adoc)
 (princ) 
); c:RENLAY

 

 

0 Likes
Message 8 of 9

Sea-Haven
Mentor
Mentor

Glad you found it, but it is a good idea is to always quote the contributors name that wrote the code or use a link to original post.

Message 9 of 9

ec-cad
Collaborator
Collaborator

I copyed the lisp and saved it in C:/disk8/  folder, with name renlay.lsp.

This is what I get when I run it..

 

Command: (load "c:/disk8/renlay")
C:RENLAY   - echoed at the Command Prompt, showing the Function name. "RENLAY"

Command: renlay       -- type this at the Command Prompt, after loading the lisp.

0 layer(s) found to rename.   --- return value after it ran.

 

I don't know if this will work on your drawings, but this is how to call it up.

 

ECCAD

0 Likes