Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need lisp that lets me auto-rename multiple layers

6 REPLIES 6
Reply
Message 1 of 7
evannB2AWD
656 Views, 6 Replies

Need lisp that lets me auto-rename multiple layers

I already have a routine I'm working on, that will automatically rename specific layer names, to other specific layer names. So one by one, it'll will rename "EXAMPLE-LAYER-1" to "FANCY-NEW-LAYER". But in order for it to work correctly, I need to know exactly which layers will be in a file. What I need is for every layer that includes the word "EXAMPLE-" to be renamed to "FANCY-NEW-LAYER". But I just don't know how to set that up. I'm pasting my current lisp for reference. Can someone tell me how to tweak it?

 

{code}
( defun c:EVAN ( / ss_newlayer change_en )
( setq layerlist ( list ( cons "ENB_ROW-1" "ENB-ROW-1" )
( cons "EXISTING-LAYER-SUCKS" "NEW-LAYER-ROCKS" )
( cons "EXISTING-LAYER-UGGH" "NEW-LAYER-ROCKS" )
( cons "EXISTING-LAYER-SHEESH" "NEW-LAYER-ROCKS" ) );list
);sq
( foreach item layerlist
( if ( and ( tblsearch "Layer" ( car item ) )
( not ( tblsearch "Layer" ( cdr item ) )) );a
( command "-Rename" "LAYER" ( car item ) ( cdr item ) )
);i
);fe

( setq ss_newlayer ( ssget "X" ))
( while ( setq change_en ( ssname ss_newlayer 0 ))
( setq ss_newlayer ( ssdel change_en ss_newlayer ))
( if ( assoc ( cdr ( assoc 8 ( entget change_en ))) layerlist )
( entmod ( subst ( cons 8 ( cdr ( assoc ( cdr ( assoc 8 ( entget change_en ))) layerlist ))) ( assoc 8 ( entget change_en )) ( entget change_en ) ))
);i
);w


( princ "\nEVAN IS INSANELY HANDSOME:" )(princ ))
{code}

Labels (5)
6 REPLIES 6
Message 2 of 7
Kent1Cooper
in reply to: evannB2AWD

[Edited -- at first I missed the test of whether the target Layer name exists.]  You appear to be trying to put objects from multiple source Layers into the same target Layer.  Would LAYMRG be the better approach?

Kent Cooper, AIA
Message 3 of 7
paullimapa
in reply to: evannB2AWD

One way to do this is to use tblnext function to cycle through each layer name and use wcmatch function to see if the layer name pattern is currently used. If so use either vl-string-translate or vl-string-subst to generate the new layer name but first use tblsearch function to check if that new layer name exists before using the rename command to rename the layer. 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 7
evannB2AWD
in reply to: Kent1Cooper

I think laymrg would work better, now that you say it.

 

The same question applies though, I guess. How would I write it so that multiple layers with the same prefix or suffix, could be grabbed. So that they don't have to be exact in order to be selected and merged?


The problem is, we have a survey crew that doesn't follow much of a naming standard, but the layer names are 'generally' close. Sometimes we'll see X-BNDRY (for existing property lines), sometimes we'll see EX-BNDRY, BNDRY, etc. And so when I get their surveys, I would like to hit a button, and ALL 'BNDRY' layers be merged into our standard 'X-PROP'. Our surveys will sometimes have 50 and 60 layers, most of which are variations of the same entity. So I'd like to merge them all to maybe 15 standard layers for our engineering base plans.

Message 5 of 7
Kent1Cooper
in reply to: evannB2AWD


@evannB2AWD wrote:

.... Sometimes we'll see X-BNDRY (for existing property lines), sometimes we'll see EX-BNDRY, BNDRY, etc. And so when I get their surveys, I would like to hit a button, and ALL 'BNDRY' layers be merged into our standard 'X-PROP'. ....


Something like this for that example [minimally tested]? 

 

(defun C:WHATEVER (/ lay layname)
  (while (setq lay (tblnext "layer" (not lay)))
    (setq layname (cdr (assoc 2 lay)))
    (if (wcmatch (strcase layname) "*BNDRY*")
      (command "_.laymrg" "_name" layname "" "_name" "X-PROP" "_yes")
    )
  )
  (prin1)
)​

 

You can use a (cond) function instead of (if) to cover multiple categories.

Kent Cooper, AIA
Message 6 of 7
evannB2AWD
in reply to: Kent1Cooper

Sorry for the late reply. This is awesome, and it does work. But how do I repeat it, so that I can do more than one layer group. For example, I was able to use this to convert any layer with the word 'TIN' in it, to a layer called DELETE. That ultimately, later in the routine- gets deleted. But I'd like to be able to combo it, and make it so that it also converts all 'TOPO' layers to 'GRADING', etc.

 

Secondly, how do I add this to another lisp? I've tried to include it so that it runs in sequence, but I keep getting the 'malformed list' error. Thanks so much for your help.

Message 7 of 7
Kent1Cooper
in reply to: evannB2AWD

Something like this [untested]?

(defun C:WHATEVER (/ lay layname)
  (while (setq lay (tblnext "layer" (not lay)))
    (setq layname (cdr (assoc 2 lay)))
    (foreach pair '(("*BNDRY* "X-PROP") ("*TIN*" "DELETE") ("*TOPO*" "GRADING"))
      (if (wcmatch (strcase layname) (car pair))
        (command "_.laymrg" "_name" layname "" "_name" (cadr pair) "_yes")
      )
    )
  )
  (prin1)
)​
Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report