LISP Routine for Merging Layers after importing a PDF File...

LISP Routine for Merging Layers after importing a PDF File...

mrjackgill
Participant Participant
2,087 Views
19 Replies
Message 1 of 20

LISP Routine for Merging Layers after importing a PDF File...

mrjackgill
Participant
Participant

Hello,

 

I was hoping to learn how to write a LISP routine that would merge any layers that have a PDF#_ Prefix with a layer that has the same suffix after the PDF#_....

 

I have to do this anytime I import a PDF and each PDF can have 20-30 layers so its a time sink and repetitive...

 

As an example....

 

Drawing has these 3 Layers.

Layer 1

Layer 2

Layer 3

 

After I import a PDF it has these additional layers...

PDF1_Layer 1

PDF2_Layer 2

 

The routine would do the following:

1.)  Find any Layer with a PDF#_ Prefix (in this case PDF1_Layer 1 & PDF2_Layer 2)

2.) See if there is a layer with the Same Suffix (in this case Layer 1 & Layer 2)

3.) Meger PDF1_Layer 1 into Layer 1, Merge PDF2_Layer 2 into Layer 2

 

The only known information about the layer names is the Prefix PDF_ or PDF#_

 

Thanks for your help...Hopefully that makes sense?

 

 

Layer Name PDF1_Line1

0 Likes
2,088 Views
19 Replies
Replies (19)
Message 2 of 20

mrjackgill
Participant
Participant

Forgot to mention....If it doesnt find a layer with the same suffix then it should rename the layer to the suffix - PDF1_ANNO_DIMS becomes ANNO_DIMS.

0 Likes
Message 3 of 20

Kent1Cooper
Consultant
Consultant

This seems to do that, in minimal testing:

(defun C:PDFLM ; = PDF Layer Merge
  (/ lay layname newname)
  (while (setq lay (tblnext "layer" (not lay)))
    (setq layname (cdr (assoc 2 lay)))
    (if (wcmatch layname "PDF*")
      (if (tblsearch "layer" (setq newname (substr layname (+ (vl-string-search "_" layname) 2))))
        (command "_.laymrg" "_name" layname "" "_name" newname "_yes"); then
        (command "_.rename" "_layer" layname newname); else
      ); if
    ); if
  ); while
  (princ)
); defun
Kent Cooper, AIA
Message 4 of 20

mrjackgill
Participant
Participant

Thanks KEN!!

 

Looks super helpful and I NEVER would have come up with this...

 

I load it with AppLoad and it loads but then immediately throws a syntax error so i cannot get it to run.

 

I'm on Autocad Mac...not sure that should make a difference for this?

 

Any ideas?  Thanks!!

0 Likes
Message 5 of 20

Kent1Cooper
Consultant
Consultant

I don't know enough about what Mac AutoCAD can do with AutoLisp.  It worked for me on my PC, but I can't test it Mac-wise.  It may be that it can't handle the (vl-string-search) function [apparently Visual Lisp isn't supported, yet Help for that function claims it can be used under Mac OS].  If that's the issue, there may be some other way to find the portion of the Layer name after the underscore.

 

I also found an exchange that asks whether a Mac user used RTF format in saving it to a .lsp file -- if you've done that, at least try a truly plain-text format.

Kent Cooper, AIA
0 Likes
Message 6 of 20

mrjackgill
Participant
Participant

Syntax error was from wrong format text file - Solved that!  Thanks!

 

Now when i run it i get this...

mrjackgill_0-1612305459143.png

 

Thanks so much for your help!!!

0 Likes
Message 7 of 20

Sea-Haven
Mentor
Mentor

Pretty sure Mac does not support VL, but it does support getpropertyvalue & set. But some properties are not exposed compared to VL. 

; this should confirm if it works should display 3

(vl-string-search "_" "Abc_def")

 

May have to resort to a substr check each each character in string remembering postion. Sure Kent can help you with that.

0 Likes
Message 8 of 20

mrjackgill
Participant
Participant

Here is the issues as best as I can tell...

 

mrjackgill_0-1612313401189.png

 

and I can confirm that 

(vl-string-search "_" "Abc_def")

returns 3....

 

Thanks again for all your help!! 

0 Likes
Message 9 of 20

Kent1Cooper
Consultant
Consultant

@mrjackgill wrote:

....

Now when i run it i get this...


The top line of the command history in the image suggests that the file didn't load, because if it loaded successfully, the command name should be recognized.  Were there any messages on APPLOADing it?  But then, the unknown command being "name" suggests it got as far as that option in the LAYMRG command.  That suggests that maybe LAYMRG operates a little differently in the Mac version, perhaps?  If, for example, it allows only one Layer to be merged into another at a time, then there's no telling it you're done specifying Layers to merge, so maybe the Enter "" ends the command, and then it doesn't know NAME as a command, and then "TEXT" is your newname variable, and there's the "_yes" that the Text command doesn't like.  If that's the problem, try removing that "".  If that doesn't do it, do the LAYMRG thing manually and note exactly what the prompts are and the sequence of inputs to Merge by specifying names rather than picking objects.

Kent Cooper, AIA
0 Likes
Message 10 of 20

pbejse
Mentor
Mentor

@mrjackgill wrote:

 

(vl-string-search "_" "Abc_def")

returns 3....


Really now? And me all this time thinking VL does not work with Mac version of AutoCAD, perhaps i does on 2021 version of Mac/VSCode?

 

 

0 Likes
Message 11 of 20

Kent1Cooper
Consultant
Consultant

@pbejse wrote:

@mrjackgill wrote:
(vl-string-search "_" "Abc_def")

returns 3....


... And me all this time thinking VL does not work with Mac version ....


I ran across something that made me think that maybe  (vl-...  functions can be used, but not  (vla-...  functions.  [I don't recall its mentioning  (vlax-...  functions.]

 

Another thought....  @mrjackgill , can you get it to fail again, but show more command history than in the image in Message 6?  I'm thinking it looks like it's the second "_name" option in my LAYMRG command that's not accepted as a command name, and that would mean something killed it before that point.  I think LAYMRG was once an Express Tool, not a native command, and if that's still the case in Mac Acad, it could explain it.  What happens before it gets to the first thing shown in your first image?

Kent Cooper, AIA
0 Likes
Message 12 of 20

ronjonp
Advisor
Advisor

Here's another approach:

 

(defun c:foo (/ el l ln s)
  ;; RJP » 2021-02-05
  (if (setq s (ssget "_X" '((8 . "PDF#_*,PDF##_*"))))
    (foreach e (mapcar 'cadr (ssnamex s))
      (setq l (cdr (assoc 8 (setq el (entget e)))))
      (setq nl (substr l (if (wcmatch (strcase l) "PDF#_*") 6 7)))
      (if (tblobjname "layer" nl)
	(entmod (append el (list (cons 8 nl))))
	(entmod (append el '((8 . "ANNO_DIMS"))))
      )
    )
  )
  (princ)
)

 

0 Likes
Message 13 of 20

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

....

....
  (if (setq s (ssget "_X" '((8 . "PDF#_*,PDF##_*"))))
....

That needs to also account for the first PDF brought in, which will not have a number:

(if (setq s (ssget "_X" '((8 . "PDF_,PDF#_*,PDF##_*"))))

with corresponding adjustment for the position to start the extraction of the Layer name without the PDF-related prefix part.

Kent Cooper, AIA
0 Likes
Message 14 of 20

ronjonp
Advisor
Advisor

@Kent1Cooper wrote:

@ronjonp wrote:

....

 

....
  (if (setq s (ssget "_X" '((8 . "PDF#_*,PDF##_*"))))
....

 


That needs to also account for the first PDF brought in, which will not have a number:

(if (setq s (ssget "_X" '((8 . "PDF_,PDF#_*,PDF##_*"))))

with corresponding adjustment for the position to start the extraction of the Layer name without the PDF-related prefix part.


Oops you're right .. missed that detail. Now if only I could go back and edit my post. 😂

0 Likes
Message 15 of 20

ronjonp
Advisor
Advisor

Code updated to account for PDF_ prefix. Thanks for catching that @Kent1Cooper  🍻

(defun c:foo (/ el l ln s)
  ;; RJP » 2021-02-05
  (if (setq s (ssget "_X" '((8 . "PDF_*,PDF#_*,PDF##_*"))))
    (foreach e (mapcar 'cadr (ssnamex s))
      (setq l (cdr (assoc 8 (setq el (entget e)))))
      (setq nl (substr l
		       (+ 2 (vl-string-search "_" l))
		       ;; Use snippet below if vanilla is needed
;;;		       (cond ((wcmatch (strcase l) "PDF_*") 5)
;;;			     ((wcmatch (strcase l) "PDF#_*") 6)
;;;			     (7)
;;;		       )
	       )
      )
      (if (tblobjname "layer" nl)
	(entmod (append el (list (cons 8 nl))))
	(entmod (append el '((8 . "ANNO_DIMS"))))
      )
    )
  )
  (princ)
)
Message 16 of 20

mrjackgill
Participant
Participant

I'm sorry.....I feel like a total DOPE....

 

I have loaded the code and run it but nothing happens....very confused.  Thanks for all of you collective help!!

 

I don't get any errors when i run it...I renamed it PDFLM2.....

 

THANK YOU!!!

 

mrjackgill_1-1612562435699.png

 

 

 

mrjackgill_0-1612562382017.png

 

0 Likes
Message 17 of 20

mrjackgill
Participant
Participant

I think part of the issue was a poor explanation on my part....The "#" in the prefix of "PDF#" was meant to represent a number corresponding to the number of pdf sheets that have been imported...the first import is just PDF, the second is PDF2, the third is PDF3, PDF4, ...

0 Likes
Message 18 of 20

ronjonp
Advisor
Advisor

If you ran my code, do a purge after and see what your layer list looks like.

0 Likes
Message 19 of 20

pbejse
Mentor
Mentor

@ronjonp wrote:

Oops you're right .. missed that detail. Now if only I could go back and edit my post. 


Still on the wishlist, but I won't hold my breath if I were you. 😁

0 Likes
Message 20 of 20

ronjonp
Advisor
Advisor

@pbejse wrote:

@ronjonp wrote:

Oops you're right .. missed that detail. Now if only I could go back and edit my post. 


Still on the wishlist, but I won't hold my breath if I were you. 😁


No breath holding here ... 🤓

0 Likes