Author Topic: Change layer routine  (Read 3432 times)

0 Members and 1 Guest are viewing this topic.

stimmo520

  • Guest
Change layer routine
« on: March 04, 2011, 09:57:24 AM »
Hello,

This is my first post here and I am definetly a learning novice to the world of Lisp writing/editing. I am editing a lisp that my company has been using to change the construction phase of elements..ie from h-duct_sup_exist to h-duct_sup_demo. I want this routine now to take my annotations from the 1/8 scale layers to the 1/4 scale layers which read like h-text8_cfm_new to h-text4_cfm_new. Ive gotten this far, then ran into a snag. It would work great if we were a single discipline using this lisp, but we also have plumbing and fp in house. Now the delimiters are the same, but the discipline changes, ie. h-text8_pipe_new or p-text8_pipe_new. The program trims to  the _ delimeter as is and chops the layers in half, and can replace the whole h-text8, but that only works for h discipline. Im stuck, and its driving me batty trying to figure out a way to trim and replace the original discipline variable to the layer.

Code: [Select]
;;;; 24.LSP - Changes picked objects to the corresponding "h-text4" Layer.                 ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;         ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(vl-load-com)
(defun C:24 ( / SS CNT LYR SLYR vEN EN LyrNew LyrTab oLayers tChr nSuf nPre )
  (prompt "\nChange selected objects layer by changing the suffix.")
  ;;
  (setq tChr "_") ;;<<-- variable delimiter
  (setq nSuf "") ;;<<--- your new suffix
  (setq nPre "h-text4") ;;<<--- your new prefix
    ;;
  (setq SS (ssget))
  (if (and SS (> (sslength SS) 0))
    (progn
      (setq oLayers ;;get layer table link
     (vla-get-layers
       (vla-get-activedocument
(vlax-get-acad-object))))
      ;;
      ;;Got the objects, now stream through them
      (repeat (setq CNT (sslength SS))
;;
(setq En (ssname SS (setq CNT (1- CNT))) ;;Entity name from set
      vEN (vlax-ename->vla-object En) ;;entity object reference
      LYR (vla-get-layer vEN);;get the layer name
      sLYR LYR
      )
;;
;;check to see if already changed
;;
(if (not (wcmatch LYR (strcat  nPre "*" nSuf)))
  (progn
    ;;
    ;; LYR does not match the pattern test
    ;;
    ;; Change the prefix only if nPRE is not empty and the
    ;; delimeter can be found in the source layer name.

    ;;
    (if (/= nPRE "") ;;change the prefix?
      (progn
(if (wcmatch LYR (strcat "*" tChr "*")) ;;find the delim?
    (setq LYR (vl-string->list LYR) ;;change to list
  LYR (member (ascii tChr) LYR) ;;trim to delim
  LYR (vl-list->string LYR)) ;;back to string
  )
(setq LYR (strcat nPRE LYR)) ;;add new prefix
)) ;;end IF nPRE progn
    ;;
    (if (/= nSUF "") ;;change the suffix?
      (progn
(if (wcmatch LYR (strcat "*" tChr "*")) ;;find the delim okay?
    (setq LYR (vl-string->list LYR) ;;convert LYR name to list of ASCIIs
  LYR (reverse LYR)         ;;flip it around
  LYR (member (ascii tChr) LYR) ;;trim to delim
          LYR (reverse LYR) ;;flip it back to forward
  LYR (vl-list->string LYR)) ;;back to string
    )
(setq LYR (strcat LYR nSUF))
)) ;;end IF nSUF progn
    ;;
    ;; Check to see if the new layer exists
    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;     (if (not (tblsearch "LAYER" LYR))
;       (progn ;;add the new layer
;         (setq LyrNew (vla-add oLayers LYR)
;       LyrTab (vla-item oLayers sLYR)
;       )
; ;; Clone the properties
; (vla-put-linetype LyrNew (vla-get-linetype LyrTab))
; (vla-put-truecolor LyrNew (vla-get-truecolor LyrTab))
; (vla-put-freeze LyrNew (vla-get-freeze LyrTab))
; (vla-put-layeron LyrNew (vla-get-layeron LyrTab))
; (vla-put-lineweight LyrNew (vla-get-lineweight LyrTab))
; (vla-put-lock LyrNew (vla-get-lock LyrTab))
; (vla-put-material LyrNew (vla-get-material LyrTab))
; ;(vla-put-plotstylename LyrNew (vla-get-plotstylename LyrTab))
; )) ;;end layer add
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;
    ;; Update the layer name of the object
    (vla-put-layer vEN LYR)
    )) ;;end if _DEMO already there
) ;;end REPEAT
      )) ;;end SS test
  (princ)
  )
;; END OF LISP

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #1 on: March 04, 2011, 10:24:16 AM »
If I'm following your post correctly, look into vl-string-translate  ..

(vl-string-translate "Text8" "Text4" "h-text8_cfm") = "h-text4_cfm"

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

stimmo520

  • Guest
Re: Change layer routine
« Reply #2 on: March 04, 2011, 10:35:16 AM »
Would this need to be applied to all the layers, or could I use a wildcard to show x-text8_xxx_xxx?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #3 on: March 04, 2011, 10:39:16 AM »
Do you have a drawing you could post with the layers? You're just changing one layer name to another correct?

*Edit ... just looked at your code .. .so you're cycling through objects changing their layers if they match a certain criteria. Will this new layer always exist?
« Last Edit: March 04, 2011, 10:47:22 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

stimmo520

  • Guest
Re: Change layer routine
« Reply #4 on: March 04, 2011, 10:48:22 AM »
here is a listing of the layers, but the ones Im concerned with are the -text8_ to -text4_

stimmo520

  • Guest
Re: Change layer routine
« Reply #5 on: March 04, 2011, 10:50:01 AM »
yes..our dwgs are discipline use discipline specific templates. ie HVAC= h-layers, Plumbing=p-layers and so on.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #6 on: March 04, 2011, 11:17:14 AM »
See if this helps you out:

Code: [Select]
(defun c:4to8 (/ e el ln n ss)
  (setq n -1)
  (if (setq ss (ssget '((8 . "*text4*"))))
    (while (setq e (ssname ss (setq n (1+ n))))
      (setq el (entget e))
      (setq ln (cdr (assoc 8 el)))
      (entmod (subst (cons 8 (vl-string-translate "text4" "text8" ln)) (assoc 8 el) el))
    )
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

stimmo520

  • Guest
Re: Change layer routine
« Reply #7 on: March 04, 2011, 11:27:05 AM »
Your amazing. Ive been going about this the wrong way. I did try the subst route, but not with a vl-string-translate. Very lean code..thanks a bunch.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #8 on: March 04, 2011, 12:01:39 PM »
Your amazing. Ive been going about this the wrong way. I did try the subst route, but not with a vl-string-translate. Very lean code..thanks a bunch.

Glad to help :) One nice thing about entmod is the layer you're putting the object on doesn't have to exist  8-)

All you need to check for is locked layers or change the selection set to (setq ss (ssget ":L" '((8 . "*text4*"))))
« Last Edit: March 04, 2011, 12:04:47 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

stimmo520

  • Guest
Re: Change layer routine
« Reply #9 on: March 04, 2011, 01:39:23 PM »
we are really on our standards here, and the only layers that will ever be locked will be our bx- layers (xrefs). But thanks for the suggestion..Ive been playing with different variables in your code. It really only likes the text layers..tried demo and exist, but it creates crazy new layers.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #10 on: March 04, 2011, 02:50:02 PM »
we are really on our standards here, and the only layers that will ever be locked will be our bx- layers (xrefs). But thanks for the suggestion..Ive been playing with different variables in your code. It really only likes the text layers..tried demo and exist, but it creates crazy new layers.

Did not even test the demo and exist ... it does give some odd results  :ugly:. Give this a try ... made a bit more versatile  :-)

Code: [Select]
(defun moveto (source dest / _replace e el ln n ss)
  (defun _replace (source dest string)
    (while (setq p (vl-string-search (strcase source) (strcase string)))
      (setq string (strcat (substr string 1 p)
   dest
   (substr string (1+ (+ p (strlen source))))
   )
      )
    )
    string
  )
  (setq n -1)
  (if (setq ss (ssget (list (cons 8 (strcat "*" source "*")))))
    (while (setq e (ssname ss (setq n (1+ n))))
      (setq el (entget e))
      (setq ln (cdr (assoc 8 el)))
      (entmod (subst (cons 8 (_replace source dest ln)) (assoc 8 el) el))
    )
  )
  (princ)
)

;;Examples of usage
(moveto "exist" "demo")
(moveto "text4" "text8")
(moveto "scoobydoo" "where are you")
« Last Edit: March 05, 2011, 11:45:06 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

stimmo520

  • Guest
Re: Change layer routine
« Reply #11 on: March 09, 2011, 12:43:48 PM »
Ill have to test it out soon...Been busy ;(

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Change layer routine
« Reply #12 on: March 09, 2011, 03:00:56 PM »
Ill have to test it out soon...Been busy ;(

Good deal ... let me know how it fairs  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC