LISP to create a line and text from a layer name

Anonymous

LISP to create a line and text from a layer name

Anonymous
Not applicable

I am new to LISP and was wondering if it is possible to draw a line and then another below that (such as an array) one each for all the layers in a drawing? I would also like to create text next to the line that has the name of the layer. This line and text should be on the layer in question.

 

I can do it manually but wondered if was easier to automate!

 

Many thanks for your time

 

Adam

0 Likes
Reply
Accepted solutions (1)
8,723 Views
28 Replies
Replies (28)

Ranjit_Singh2
Advisor
Advisor
Accepted solution

@Anonymous wrote:

I am new to LISP and was wondering if it is possible to draw a line and then another below that (such as an array) one each for all the layers ...........

Adam


If I understand correctly then something like this may work.

(defun c:somefunc  (/ pt dat ln)
 (setq pt (getpoint "\nSelect insertion point: "))
 (while (setq dat (tblnext "layer" (null dat)))
  (entmake (list '(0 . "line") (cons 10 pt) (cons 11 (mapcar '+ pt '(2.5 0 0))) (cons 8 (setq ln (cdr (assoc 2 dat))))))
  (entmake (list '(0 . "text") (cons 10 (mapcar '+ pt '(3.0 0 0))) '(40 . 0.1) (cons 1 ln) (cons 8 ln)))
  (setq pt (mapcar '- pt '(0 0.15 0))))
 (princ))

layer_list.gif

 

Anonymous
Not applicable

That is exactly what I was looking for! Pure genius. Love the little video too!

0 Likes

braudpat
Mentor
Mentor

 

Hello Ranjit (The MAPCAR Jedi)

 

1) Beautiful (and SO SHORT) routine, so Kudos for you !

 

2) Please may I ask for a few improvments:

- Display Layer Name = OK

- Please display after the Description Layer Name (if there is one)

- Is it possible to have a choice :

-- Either ALL Layers (as your 1st routine)

-- OR select Entities (Classic AutoCAD selection)

so we will get only the Layers used (by the selected entities) ...

 

Do you see what I mean ?

 

Thanks in advance, Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes

Ranjit_Singh2
Advisor
Advisor

Hi @braudpat. I assume you meant adding the description column from the layer properties manager. I would recommend adding error trap. Very minimal testing.

(defun c:somefunc  (/ pt rsp dat ln lst desln)
 (initget "All Select")
 (setq pt (getpoint "\nSelect insertion point: "))
 (initget "Select All")
 (setq rsp (getkword "\nSpecify option [Select / All] <All>:"))
 (cond ((= rsp "Select")
        (mapcar '(lambda (x) (setq pt (somefunc2 pt x)))
                (progn (acad_strlsort (mapcar '(lambda (x) (and (null (member x lst)) (setq lst (cons x lst))))
                                              (mapcar '(lambda (x) (cdr (assoc 8 (entget x))))
                                                      (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget)))))))
                       lst)))
       (t (while (setq dat (tblnext "layer" (null dat))) (setq pt (somefunc2 pt (cdr (assoc 2 dat)))))))
 (princ))
(defun somefunc2  (pt ln / desln)
 (entmake (list '(0 . "line") (cons 10 pt) (cons 11 (mapcar '+ pt '(2.5 0))) (cons 8 ln)))
 (entmake (list '(0 . "text") (cons 10 (mapcar '+ pt '(3.0 0))) '(40 . 0.1) (cons 1 ln) (cons 8 ln)))
 (if (zerop (strlen (setq desln (getpropertyvalue (tblobjname "layer" ln) "Description"))))
  ()
  (entmake (list '(0 . "text") (cons 10 (mapcar '+ pt '(4.5 0))) '(40 . 0.1) (cons 1 desln) (cons 8 ln))))
 (mapcar '- pt '(0 0.15)))

layer_list2.gif

 

braudpat
Mentor
Mentor

 

Hello The MAPCAR Jedi

 

THANKS, it's perfect !

 

Happy Week-End, Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes

Anonymous
Not applicable

Amazing LISP! Thank you for publishing this!

 

Any way you could adapt this to do the same for linetypes...? 

0 Likes

HARISH-WAHO
Participant
Participant

Very good stuff. I was searching exactly this.

 

Saved lots of time.

 

Thanks Bro.

Harish

0 Likes

Anonymous
Not applicable

Hey Ranjit,

 

this LISP is crazy good, but I was wondering - is it possible to add a keyword that would then draw all layers which contain that keyword? 

 

For e.g.: run the function, it asks you for a keyword, you type in ''CONSTR" and then it creates lines of layers whose name contains that keyword (''CONSTR'').

 

Thanks and kind regards.

0 Likes

aclima01
Contributor
Contributor

Beautiful! thank you

0 Likes

moon47usaco
Contributor
Contributor

Can the list be sorted by layer name?

 

They come out all disorganized.

0 Likes

dlanorh
Advisor
Advisor

Try this modified version.

 

(defun c:test ( / doc lyrs lst pt)
  (setq doc (vla-get-activedocument (vlax-get-acad-object))
        lyrs (vla-get-layers doc)
  );end_setq
  (vlax-for lyr lyrs (setq lst (cons (vlax-get lyr 'name) lst)))
  (setq lst (vl-sort lst '< )
        pt (getpoint "\nSelect insertion point: ")
  );end_setq
  (foreach lyr lst
    (entmakex (list '(0 . "line") (cons 10 pt) (cons 11 (mapcar '+ pt '(2.5 0 0))) (cons 8 lyr)))
    (entmakex (list '(0 . "text") (cons 10 (mapcar '+ pt '(3.0 0 0))) '(40 . 0.1) (cons 1 lyr) (cons 8 lyr)))
    (setq pt (mapcar '- pt '(0 0.15 0)))
  );end_foreach
  (princ)
);end_defun

I am not one of the robots you're looking for

moon47usaco
Contributor
Contributor

That is excellent.

 

Thank you very much. =]

0 Likes

Anonymous
Not applicable
Hi Ranjit, amazing lisp, almost what I was looking for 🙂 I know this is an old thread, but if you can help me with just an improvement on this routine I will appreciate a looot. Can the outcome be in the exact order than I have the layers? I am merging duplicates layers with small differences (ei: C-rubber pads and c-rubber_pads) so would be easier for me to catch them if they are organized alphabetically.
Thank you very much.
0 Likes

tranminhthong0993
Participant
Participant

I press the like button.

 

But could you add more code for sorting layer names as Anphabe?

 

Now, they come out all disorganized.

 

Thank you in advance.

0 Likes

moon47usaco
Contributor
Contributor

See message 12 by dlanorh

0 Likes

tranminhthong0993
Participant
Participant

Hi @moon47usaco,

 

Thanks for your response. However, this code which you mentioned that I found has no option "select" as above message 05 of Mr. Ranjit.Singh. 

 

So, I would like to find that has two options and with Anphabe.

 

Thanks for your time!

 

 

0 Likes

pbejse
Mentor
Mentor

@tranminhthong0993 wrote:

So, I would like to find that has two options and with Anphabe.


What is that?

 

0 Likes

tranminhthong0993
Participant
Participant

Yeah, LISP to create a line and text from a layer name which are organized alphabetically.

Something like that... Thanks...

 

tranminhthong0993_0-1632064057524.png

 

0 Likes

moon47usaco
Contributor
Contributor

To clarify I bileave tranminhthong0993 wants the best of both worlds.

 

He wants the list of layers to be alpha ordered like in post #12 and also wants the options available in #05.

 

IE:

 

 

Specify option [Select / All] <All>:

 

 

Hopefully someone can mash up the code in those two posts into one nice lisp =]

 

0 Likes

Type a product name