TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pedroantonio on July 15, 2018, 04:12:35 AM

Title: fIND AND RENAME LAYERS
Post by: pedroantonio on July 15, 2018, 04:12:35 AM
Ηι. I have a problem and i need help.I youse a lot of layers in my drawings. I need a lisp to search my layers in the drawing and rename some of them.I need to add in the code all the layers i use and  another list with the new names . I find acode but is not working. Can any one help ?

Code - Auto/Visual Lisp: [Select]
  1. ;;function to rename a layer.
  2. ;;if old layer exists, and new layer doesn't exist, the old layer is simply renamed.
  3. ;;if old layer exists, and new layer is already there, it takes everything on old layer and puts them on new layer.
  4. ;;if old layer doesn't exist, it does nothing.
  5. (defun renlay (ol nl / ss i ent )
  6.   (cond ((and (tblsearch "layer" ol) (not (tblsearch "layer" nl)))
  7.          (command "._rename" "la" ol nl)
  8.         )
  9.         ((and (tblsearch "layer" ol)(tblsearch "layer" nl))
  10.           (setq ss (ssget "x" (list (cons 8 ol))))
  11.           (setq i -1)
  12.            (repeat (sslength ss)
  13.               (setq ent (entget (ssname ss (setq i (1+ i))))
  14.                     ent (subst (cons 8 nl) (cons 8 (cdr (assoc 8 ent))) ent)
  15.               )    
  16.               (entmod ent)
  17.            )
  18.         )
  19.         ((not (tblsearch "layer" ol))
  20.           (prompt (strcat "\nLayer " ol " not found. "))
  21.         )
  22.   )
  23.   (princ)
  24. )
  25.  
  26. ;;example
  27. (defun c:test ()
  28.   (renlay "0" "A-Flor-Strs")
  29.   (renlay "_fence" "A-Flor-Tptn")
  30.   (renlay "ARWOOD" "A-Flor-Wdwk")
  31.   (renlay "ARFURNITURE" "A-Furn")
  32.   (renlay "ARWINDOW" "A-Glaz")
  33. )
  34.  
Title: Re: fIND AND RENAME LAYERS
Post by: Crank on July 15, 2018, 06:50:39 AM
Can't you use the LAYTRANS command to create a file and then use (acet-laytrans) (https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-AutoLISP/files/GUID-4BD312D1-1850-4DE4-A63D-35367C5F7F5D-htm.html) in your lisp?
Title: Re: fIND AND RENAME LAYERS
Post by: kdub_nz on July 15, 2018, 10:11:21 AM
Since you haven't explained the "but is not working" part ...
Wild assed guess :
Layer 0 can not be deleted ... what happens if you comment out the first statement in the test function.

asides :
The routine would be more efficient if you assigned the values from the tblsearch methods to variables prior to the conditional, and replace the in-line calls with the variable.

Adding an error function would be a little more professional.
Title: Re: fIND AND RENAME LAYERS
Post by: pedroantonio on July 15, 2018, 11:51:49 AM
Thanks for the reply. For the 0 layer i already find it .I am not good in lisp. if anyone can do some changes to improve the code ?

Thanks
Title: Re: fIND AND RENAME LAYERS
Post by: Crank on July 16, 2018, 01:09:12 PM
Quote
if old layer exists, and new layer is already there, it takes everything on old layer and puts them on new layer.
Basicly this means that you want to merge layers. If layers are used in blocks or xdata then this is a lot more complicated than renaming a layer.
Also: You can't merge layer '0' or layer 'Defpoints'.

The attached lisp is a modified version of a lisp from the expresstools. I don't use it any more, because it's very slow and new objects are not supported.

The suggestion from my previous post is fast and only needs one line of code. Just make sure that the .dws (or rename the .dws to .dwg) that you've created with the LAYTRANS command is in your search path.
Code: [Select]
(or (member "laytrans.arx" (arx)) (arxload "laytrans"))
(acet-laytrans "YourFile.dws" 7)