Jump to content

Searching Layername by Keyword then Renaming it


john_0104

Recommended Posts

Hi ,
 

I am new in lisp world and have searched a lot of topics about renaming layer but haven't found a specific one that would cater to my need. The point is I received survey files from different surveyors and I want to rename them into a new one.
Ex.
Received layers "219_POWER DOME", "268_POWER METER BOX", "power_mh" to be merged into new one "v-SERV-powr-"

On some instances, some of the aforementioned received layers were not present on a specific file. I don't want to searched for a specific layer name and changed it to mine because various entities named their layers differently so a single letter, number or character may ruin the program, and it'll be tedious to encode all the layers. I just want to find a common keyword in the 100+ layers and renamed/ categorized it into my standards. It would be also helpful if I can modify the linetype and color too on the LISP, but if I can't it's okay.

Thanks a lot,
John

Link to comment
Share on other sites

Merge all layers to "v-SERV-powr-" and purge layers only.

Type LMA or LayMrgAll

 

What exactly do you want to do with the linetypes and color if bylayer match layer prop before merge?

 

;;----------------------------------------------------------------------------;;
;; Merge All Existing Layers to "v-SERV-powr-" and Purge
(defun C:LMA () (C:LayMrgAll))
(defun C:LayMrgAll (/ Drawing x SS)
  (vla-startundomark (setq Drawing (vla-get-activedocument (vlax-get-acad-object))))
  (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "v-SERV-powr-") (70 . 0) (62 . 7)))
  (vlax-for lay (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (/= (setq x (vla-get-name lay)) "v-SERV-powr-")
      (progn
        (setq SS (ssget "X" (list (cons 8 x))))
        (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
          ;future linetype code
          ;future color code
          (vla-put-Layer (vlax-ename->vla-object ent) "v-SERV-powr-")
        )
        (vl-cmdf "_Purge" "LA" x "N")      
      )
    )
  )
  (vla-endundomark Drawing)
  (princ)
)

 

 

Edited by mhupp
Link to comment
Share on other sites

17 hours ago, mhupp said:

Merge all layers to "v-SERV-powr-" and purge layers only.

Type LMA or LayMrgAll

 

What exactly do you want to do with the linetypes and color if bylayer match layer prop before merge?

 

;;----------------------------------------------------------------------------;;
;; Merge All Existing Layers to "v-SERV-powr-" and Purge
(defun C:LMA () (C:LayMrgAll))
(defun C:LayMrgAll (/ Drawing x SS)
  (vla-startundomark (setq Drawing (vla-get-activedocument (vlax-get-acad-object))))
  (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "v-SERV-powr-") (70 . 0) (62 . 7)))
  (vlax-for lay (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (/= (setq x (vla-get-name lay)) "v-SERV-powr-")
      (progn
        (setq SS (ssget "X" (list (cons 8 x))))
        (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
          ;future linetype code
          ;future color code
          (vla-put-Layer (vlax-ename->vla-object ent) "v-SERV-powr-")
        )
        (vl-cmdf "_Purge" "LA" x "N")      
      )
    )
  )
  (vla-endundomark Drawing)
  (princ)
)

 

 

I am not only merging all layers into one. I am receiving file with 100+ layers that' would need to be categorized each into my designated 30-40 layers. I am looking for an automation to combine some layers into new one using keywords from layer name of the received file. Hope this makes sense.

Link to comment
Share on other sites

It is looking like you need a old,new file and each file is relevant to who supplied you the survey, I would start by contacting them and trying to get a full list of their layer names, not just the ones in a dwg, if they are using CIV3D this is buried in the description keys, but the good news is you can export the keys to excel. 

 

I have done this where we get the survey from another package so rename layers and change color, existing survey is set to light grey color so sits in background.

 

 

Link to comment
Share on other sites

@john_0104 Give this a try .. refer to the comments in the code.

(defun c:foo (/ a d l la nl)
  ;; Create your list of conditions, the first entry is the check and the second is the new layername
  ;; Refer to WCMATCH filters atthe link below
  ;; https://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-EC257AF7-72D4-4B38-99B6-9B09952A53AD
  (setq	l '(;; ("219_POWER DOME,268_POWER METER BOX,power_mh" "v-SERV-powr-")
	    ;; This changes all layers that have 'POWER' and 'PWR' to the new layer .. a bit easier to maintain than the check above
	    ("*POWER*,*PWR*" "v-SERV-powr-")
	    ("*LIGHT*" "V-SERV-LIGHT-")
	    ("*TOILET*" "V-SERV-SEWER-")
	   )
  )
  (vlax-for l (vla-get-layers (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (cond ((= -1 (vlax-get l 'lock)) (vlax-put l 'lock 0) (setq a (cons l a))))
  )
  ;; Create the new layers
  (foreach x l (vla-add (vla-get-layers d) (cadr x)))
  (vlax-for b (vla-get-blocks d)
    (if	(= -1 (vlax-get b 'islayout))
      (vlax-for	o b
	(cond
	  ((vlax-write-enabled-p o)
	   (setq la (strcase (vla-get-layer o)))
	   (if (vl-some '(lambda (x) (and (wcmatch la (strcase (car x))) (setq nl (cadr x)))) l)
	     (vla-put-layer o nl)
	   )
	  )
	)
      )
    )
  )
  (foreach l a (vlax-put l 'lock -1))
  ;; Purge Layers
  (repeat 2 (vla-purgeall d))
  (princ)
)

 

Edited by ronjonp
Link to comment
Share on other sites

  • 4 weeks later...

Hi Ron,

Sorry it took a while before I responded, I was so excited that I already trust the encoding you did and enlisted all the layers I needed to merge/purge. At the end, the code did not work with all the layers I enlisted. So I tried your given code and created only the layers in your sample. What happened was it all deletes the layers except for "0". I'm gonna upload the screenshot of the sample and the lisp with all the layers I encoded. Please check and advise.


Thanks

On 12/18/2022 at 4:33 AM, ronjonp said:

@john_0104 Give this a try .. refer to the comments in the code.

(defun c:foo (/ a d l la nl)
  ;; Create your list of conditions, the first entry is the check and the second is the new layername
  ;; Refer to WCMATCH filters atthe link below
  ;; https://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-EC257AF7-72D4-4B38-99B6-9B09952A53AD
  (setq	l '(;; ("219_POWER DOME,268_POWER METER BOX,power_mh" "v-SERV-powr-")
	    ;; This changes all layers that have 'POWER' and 'PWR' to the new layer .. a bit easier to maintain than the check above
	    ("*POWER*,*PWR*" "v-SERV-powr-")
	    ("*LIGHT*" "V-SERV-LIGHT-")
	    ("*TOILET*" "V-SERV-SEWER-")
	   )
  )
  (vlax-for l (vla-get-layers (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (cond ((= -1 (vlax-get l 'lock)) (vlax-put l 'lock 0) (setq a (cons l a))))
  )
  ;; Create the new layers
  (foreach x l (vla-add (vla-get-layers d) (cadr x)))
  (vlax-for b (vla-get-blocks d)
    (if	(= -1 (vlax-get b 'islayout))
      (vlax-for	o b
	(cond
	  ((vlax-write-enabled-p o)
	   (setq la (strcase (vla-get-layer o)))
	   (if (vl-some '(lambda (x) (and (wcmatch la (strcase (car x))) (setq nl (cadr x)))) l)
	     (vla-put-layer o nl)
	   )
	  )
	)
      )
    )
  )
  (foreach l a (vlax-put l 'lock -1))
  ;; Purge Layers
  (repeat 2 (vla-purgeall d))
  (princ)
)

 

 

2023-01-09 21_20_12-Greenshot image editor.png

Survey Layer Rename 7.lsp

Link to comment
Share on other sites

13 hours ago, john_0104 said:

Here are the 2 samples of the CAD files I used. You may already use the accumulated list of keywords and layer names on the code. Thanks.

16733.dwg 290.98 kB · 0 downloads 20009-F01-1.0.dwg 3.87 MB · 0 downloads

The code works here .. but you need to close the first line: (defun c:slr (/ a d l la nl)

image.png.486632b96b470c4fdbce09677b1f46ff.png

Edited by ronjonp
Link to comment
Share on other sites

21 hours ago, ronjonp said:

The code works here .. but you need to close the first line: (defun c:slr (/ a d l la nl)

image.png.486632b96b470c4fdbce09677b1f46ff.png

Hi Ron,
This works really great. I just need to update some of the keywords that are not included in the list I've encoded. Nonetheless, it's a applaudable lisp. Thanks a lot.

Link to comment
Share on other sites

2 hours ago, john_0104 said:

Hi Ron,
This works really great. I just need to update some of the keywords that are not included in the list I've encoded. Nonetheless, it's a applaudable lisp. Thanks a lot.

Glad to help :beer:

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...