Renaming Layers after PDF Import - Removing the "PDF_" Prefix and merging similar layers.

Renaming Layers after PDF Import - Removing the "PDF_" Prefix and merging similar layers.

mrjackgill
Participant Participant
928 Views
4 Replies
Message 1 of 5

Renaming Layers after PDF Import - Removing the "PDF_" Prefix and merging similar layers.

mrjackgill
Participant
Participant

Hello,

 

Thanks in advance for your help!

 

The Scenario: When I import a PDF file Autocad adds a prefix of "PDF#_" to all the layers. 

 

If you import several PDFs then you get incremented prefixes (PDF_LayerName, PDF2_LayerName, PDF3_LayerName, PDF4_LayerName....). The "#" symbol is never part of the prefix.

 

If you start with a document that has 50 layers and the PDF you are importing also has 50 layers and u import 5 of them you then have 300 layers (250 new layers + 50 from your original document). Many imported layers may have the same name as layers in your original document but they will have the PDF#_ prefix (ie Layer 0 (from original document), PDF_Layer 0 (imported), PDF2_Layer 0 (imported), PDF3_Layer 0 (imported), PDF4_Layer 0 (imported), PDF5_Layer 0 (imported).

 

I would like to be able to do the following: search through all the layers and find any with a "PDF#_" prefix (ie PDF#_Layer 0). If one is found then scan the list of layers to see if there is a layer with the same name excluding the prefix (Layer 0) and merger these into Layer 0. 

 

Assuming my original document had 50 layers and the pdf i was importing had 50 layers and the layer names were the same in both models then this script would take my 300 layer document (after 5 imports) back to a 50 layer document.

 

I got some help to get this far (from a few of you - I apologize if I have mangled your code) but it still doesn't work and I am lost as to how to fix it.

 

(defun C:PDFLM ; = PDF Layer Merge
  ;Define three variables
  (/ Elayer PDFlayer NEWname)
  ;Iterate through the list of layer names - store each layer in "Elayer" then test
  (while (setq Elayer (tblnext "layer" (not Elayer)))
  ;Look for any layer with a PDF in the name and store that name in "layname"
    (setq PDFlayer (cdr (assoc PDF lay)))
    ;Look for an existing layer with same name (wihout PDF prefix)
      (if (tblsearch "layer" (setq NEWname (substr PDFlayer (+ (vl-string-search "_" PDFlayer) 2))))
      ;If there is an existing layer with the same suffix - merge
        (command "_.laymrg" "_name" PDFlayer "" "_name" NEWname "_yes"); then
        ;If there no layer with the same suffix exists - remove prefix and save as suffix
        (command "_.rename" "_layer" PDFlayer NEWname); else
      ); if
    ); if
  ); while
  (princ)
); defun

 

Thank You!!

 

0 Likes
929 Views
4 Replies
Replies (4)
Message 2 of 5

hak_vz
Advisor
Advisor

Try this. Code is not fully tested since it is getting late here at my location.

 

 

(defun C:PDFLM(/ Table dwg_layers  pdf_layers)
	(defun Table (s / d r)
		(while (setq d (tblnext s (null d)))
			(setq r (cons (cdr (assoc 2 d)) r))
		)
	)
	(foreach lay (Table "layer")
		(if (= (substr lay 1 4) "PDF_")
			(setq pdf_layers (cons lay pdf_layers))
			(setq dwg_layers (cons lay dwg_layers))
		)
	)
	(foreach lay pdf_layers
		(if (member (substr lay 5) dwg_layers)
			(command "_.laymrg" "_name" lay "" "_name" (substr lay 5) "_yes")
			(command "_.rename" "_layer" lay (substr lay 5))
		)
	)
(princ)
)

 

 

 

If it works for you, accept this post as a solution.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 5

mrjackgill
Participant
Participant

Thank you VERY much for the reply...I think this could work but I don't understand how it handles the incremented PDF#_ layer names as it appears to test for just "PDF_".  The layer prefix could be any of the following: PDF_ (in which case this would work - although I haven't run it yet), PDF1_, PDF2_, PDF3_, PDF4_, PDF5_, etc...

 

Thanks!!

0 Likes
Message 4 of 5

hak_vz
Advisor
Advisor

--

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 5

hak_vz
Advisor
Advisor

Try this. It works on my test case.

 

(defun C:PDFLM(/  *error* Table dwg_layers  pdf_layers get_layers)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(princ)
	)
	(defun Table (s / d r)
		(while (setq d (tblnext s (null d)))
			(setq r (cons (cdr (assoc 2 d)) r))
		)
	)
	(defun get_layers ()
		(foreach lay (Table "layer")
			(if (= (substr lay 1 3) "PDF")
				(setq pdf_layers (cons lay pdf_layers))
				(setq dwg_layers (cons lay dwg_layers))
			)
		)
	)
	(setvar 'cmdecho 0)
	(get_layers)
	(foreach lay pdf_layers
		(get_layers)
		(if (member (substr lay (setq pos (+ (vl-string-search "_" lay) 2))) dwg_layers)
			(command "_.laymrg" "_name" lay "" "_name" (substr lay pos) "_yes")
			(command "_.rename" "_layer" lay (substr lay pos))
		)
	)
	(princ)
)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.