Author Topic: Trying to get a Lisp to Erase a Layer Prefix  (Read 3578 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Trying to get a Lisp to Erase a Layer Prefix
« on: May 13, 2015, 08:28:15 AM »
I having been messing around with this lisp, trying to get it to remove all layers with a prefix (DEMO_*) to be replaced with (EX_*).

DEMO_PAVEMENT
to
EX_PAVEMENT

Thanks guys for any help!


Code: [Select]
(defun c:demo (/ _ss->list e el l pre ss)
  (defun _ss->list (ss / n out)
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (if (and (setq pre (getstring t "\nEnter prefix: ")) (setq ss (ssget)))
    (progn (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (wcmatch (strcase l) (strcat (strcase pre) "*"))
        (progn (princ "\nObject already has that layer prefix..."))
        (progn (if (not (tblobjname "layer" (strcat pre l)))
(entmakex (subst (cons 2 (strcat pre l)) (assoc 2 el) el))
       )
       (entmod (subst (cons 8 (strcat pre l)) (assoc 8 (entget e)) (entget e)))
        )
      )
    )
    )
  )
  (princ)
)
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #1 on: May 13, 2015, 08:37:49 AM »
You could use the rename command for this.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #2 on: May 13, 2015, 08:41:52 AM »
I am playing around with the -rename (I was thinking about doing a macro for this) but how can I get it to do multiple selections at a time? I will keep looking at it.
Civil3D 2020

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #3 on: May 13, 2015, 08:54:50 AM »
If I may .

Code - Auto/Visual Lisp: [Select]
  1. (while (setq b (tblnext "LAYER" (not b)))
  2.   (if (and (wcmatch (strcase (setq n (cdr (assoc 2 b)))) "DEMO_*")
  3.            (not(tblsearch "LAYER" (setq r (strcat "EX_" (substr n 5)))))
  4.       )
  5.     (entmod (subst (cons 2 r) (assoc 2 (setq e (entget (tblobjname "LAYER" n))))
  6.              e
  7.       )
  8.     )
  9.   )
  10. )

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #4 on: May 13, 2015, 09:02:58 AM »
Here's a quick snippet to change a layer prefix:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:lprefix (/ ln new old)
  2.   (setq old "DEMO_")
  3.   (setq new "EX_")
  4.     (if   (wcmatch (strcase (setq ln (vla-get-name lay))) (strcase (strcat old "*")))
  5.       (vla-put-name lay (strcat new (substr ln (1+ (strlen old)))))
  6.     )
  7.   )
  8.   (princ)
  9. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #5 on: May 13, 2015, 09:06:09 AM »
lol why is this so easy for you guys! lol. I some times have the hardest time trying to figure this code out. Thanks again for the help though.
Civil3D 2020

BlackBox

  • King Gator
  • Posts: 3770
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #6 on: May 13, 2015, 09:06:34 AM »
[Edit] - @%#$&!; Ninja-ed by ronjonp, but mine supports UNDO.  :-D

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:LayerRename (/ *error* old new acDoc oLayers i oldName newName)
  3.  
  4.   (defun *error* (msg)
  5.     (if acDoc (vla-endundomark acDoc))
  6.     (cond ((not msg))                                                   ; Normal exit
  7.           ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
  8.           ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
  9.     )
  10.     (princ)
  11.   )
  12.  
  13.   (if (and (setq old (getstring T "\nEnter string to replace: "))
  14.            (setq new (getstring T "\nEnter replacement string: "))
  15.       )
  16.     (progn
  17.       )
  18.       (setq oLayers (vla-get-layers acDoc))
  19.       (setq i 0)
  20.       (vlax-for x oLayers
  21.         (if (vl-string-search old (setq oldName (vla-get-name x)))
  22.           (if
  23.             (tblsearch "layer"
  24.                        (setq newName (vl-string-subst new old oldName)) ; single replacement only
  25.             )
  26.              (prompt
  27.                (strcat
  28.                  "\nLayer \"" newName "\" already exists. "
  29.                  "Move all objects, then delete layer \"" oldName "\" "
  30.                 )
  31.              )
  32.              (progn
  33.                (vla-put-name x newName)
  34.                (setq i (1+ i))
  35.              )
  36.           )
  37.         )
  38.       )
  39.       (prompt
  40.         (strcat "\n" (itoa i) " layer" (if (= 1 i) "s " " ") "renamed. ")
  41.       )    
  42.     )
  43.   )
  44.  
  45.   (*error* nil)
  46. )
  47.  
"How we think determines what we do, and what we do determines what we get."

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #7 on: May 13, 2015, 09:23:49 AM »
Ok here is where I am at. The lisp I posted takes a layer name that I selected in the drawing and adds the (DEMO_) in front of the layer name. I was hoping I could reverse that process. Where if I select an object on the (DEMO_Water line) it will remove the (DEMO_) and replace it with (EX_) and back onto the original layer if its there, and if not it can create the layer. Hopefully that makes alittle sense.
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #8 on: May 13, 2015, 09:27:32 AM »
Ok here is where I am at. The lisp I posted takes a layer name that I selected in the drawing and adds the (DEMO_) in front of the layer name. I was hoping I could reverse that process. Where if I select an object on the (DEMO_Water line) it will remove the (DEMO_) and replace it with (EX_) and back onto the original layer if its there, and if not it can create the layer. Hopefully that makes alittle sense.
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:lprefix2 (/ _ss->list el l new nl old ss)
  2.   (defun _ss->list (ss / n out)
  3.     (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  4.   )
  5.   (setq old "DEMO_")
  6.   (setq new "EX_")
  7.   (if (setq ss (ssget ":L" (list (cons 8 (strcat old "*")))))
  8.     (foreach e (_ss->list ss)
  9.       (setq el (entget (tblobjname "layer" (setq l (cdr (assoc 8 (entget e)))))))
  10.       (setq nl (strcat new (substr l (1+ (strlen old)))))
  11.       ;; Create new layer matching old layer properties
  12.       (entmake (subst (cons 2 nl) (assoc 2 el) el))
  13.       ;; Move item to new layer
  14.       (entmod (subst (cons 8 nl) (assoc 8 (entget e)) (entget e)))
  15.     )
  16.   )
  17.   (princ)
  18. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

lamarn

  • Swamp Rat
  • Posts: 636
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #9 on: May 13, 2015, 09:31:53 AM »
For these occasion's I use rrename.vlx. Could try it. Works good.
http://www.cadforum.cz/cadforum_en/rename-on-steroids-complex-renaming-of-autocad-objects-tip9265
Design is something you should do with both hands. My 2d hand , my 3d hand ..

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #10 on: May 13, 2015, 09:36:30 AM »
BOOM! Thanks for sharing all the different ways to do this. This is awesome!
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Trying to get a Lisp to Erase a Layer Prefix
« Reply #11 on: May 13, 2015, 09:40:34 AM »
BOOM! Thanks for sharing all the different ways to do this. This is awesome!
:)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC