Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
tmullins
Posts: 154
Registered: ‎11-13-2006
Message 1 of 5 (1,536 Views)
Accepted Solution

Layer Legend

1536 Views, 4 Replies
03-23-2012 05:02 AM

I have a lisp routine that creates a layer legend in your drawing. My apologies to the source who wrote it, I simply can't recall where I ran across it. It works pretty well, and with a couple tweaks it would be great.

 

It basically reads the layer table and creates a legend in your drawing. You have to have a height assigned first to your current text style, not a big deal (I can take care of that in the code) but it would be nice to not have to remember to do that but the main issue is that it sorts (possibly) on the order the layers were created in the file. It would be great if it sorted based on name, alphabetically. So can it be sorted first before it reads tblnext "Layer"?

 

I'm not technically a programmer so any help would be greatly appreciated. Again my apologies to the source. Thanks.

 

It is attached and here is the code:

 

 

(defun c:layerlegend (/ osm ortho clayer celtype cecolor start-point line-length text-height *error*)
(defun *error* (msg)
(setvar "osmode" osm)
(setvar "orthomode" ortho)
(setvar "clayer" clayer)
(setvar "celtype" celtype)
(setvar "cecolor" cecolor)
(princ msg)
)

(defun mfp (pt pa) (list (+ (car pt) (car pa)) (+ (cadr pt) (cadr pa)) (+ (caddr pt) (caddr pa))))

(setq osm (getvar "osmode"))
(setq ortho (getvar "orthomode"))
(setq clayer (getvar "clayer"))
(setq celtype (getvar "celtype"))
(setq cecolor (getvar "cecolor"))
(setq start-point (getpoint "\nSelect point to start legend: "))
(setvar "orthomode" 1)
(setq line-length (getdist start-point "\nEnter length of line or click end point: "))
(setq text-height (cdr (assoc 40 (tblsearch "style" (getvar "textstyle")))))
(setvar "osmode" 0)
(setvar "clayer" (cdr (assoc 2 (tblnext "Layer" T))))
(setvar "celtype" "ByLayer")
(setvar "cecolor" "ByLayer")
(command "_line" start-point (mfp start-point (list line-length 0 0)) "")
(command "-text" (mfp start-point (list 0 1 0)) "0" (getvar "clayer"))
(while (setq layer (tblnext "Layer"))
(setvar "clayer" (cdr (assoc 2 layer)))
(setq start-point (mfp start-point (list 0 (- 0 (+ text-height 4)) 0)))
(command "_line" start-point (mfp start-point (list line-length 0 0)) "")
(command "-text" (mfp start-point (list 0 1 0)) "0" (getvar "clayer"))
)
(setvar "osmode" osm)
(setvar "orthomode" ortho)
(setvar "clayer" clayer)
(setvar "celtype" celtype)
(setvar "cecolor" cecolor)
)


tmullins wrote:

I have a lisp routine that creates a layer legend in your drawing. ....

 

It basically reads the layer table and creates a legend in your drawing. ....the main issue is that it sorts (possibly) on the order the layers were created in the file. It would be great if it sorted based on name, alphabetically. So can it be sorted first before it reads tblnext "Layer"?....


I don't think there's any way to sort the Layer table before stepping through it.  You would need to make a list of Layer names, sort that, and step through it, using a name from the sorted list rather than from the Layer table at each step, something like this [untested]:

 

....

(while (setq lay (cdadr (tblnext "layer" (not lay)))); make list of Layer names
  (setq laylist (cons lay laylist))
)
(foreach
  lay
  (vl-sort laylist '<); sorted alphabetically
  (setvar 'clayer lay)
  (setq start-point (mfp start-point (list 0 (- 0 (+ text-height 4)) 0)))
  (command

    "_line" start-point (mfp start-point (list line-length 0 0)) ""
    "-text" (mfp start-point (list 0 1 0)) "0" lay ; [could leave (getvar 'clayer) in there]

  )
); foreach

....

*Expert Elite*
Kent1Cooper
Posts: 5,788
Registered: ‎09-13-2004
Message 2 of 5 (1,532 Views)

Re: Layer Legend

03-23-2012 06:02 AM in reply to: tmullins

tmullins wrote:

I have a lisp routine that creates a layer legend in your drawing. ....

 

It basically reads the layer table and creates a legend in your drawing. ....the main issue is that it sorts (possibly) on the order the layers were created in the file. It would be great if it sorted based on name, alphabetically. So can it be sorted first before it reads tblnext "Layer"?....


I don't think there's any way to sort the Layer table before stepping through it.  You would need to make a list of Layer names, sort that, and step through it, using a name from the sorted list rather than from the Layer table at each step, something like this [untested]:

 

....

(while (setq lay (cdadr (tblnext "layer" (not lay)))); make list of Layer names
  (setq laylist (cons lay laylist))
)
(foreach
  lay
  (vl-sort laylist '<); sorted alphabetically
  (setvar 'clayer lay)
  (setq start-point (mfp start-point (list 0 (- 0 (+ text-height 4)) 0)))
  (command

    "_line" start-point (mfp start-point (list line-length 0 0)) ""
    "-text" (mfp start-point (list 0 1 0)) "0" lay ; [could leave (getvar 'clayer) in there]

  )
); foreach

....

Kent Cooper
Distinguished Contributor
tmullins
Posts: 154
Registered: ‎11-13-2006
Message 3 of 5 (1,519 Views)

Re: Layer Legend

03-23-2012 07:11 AM in reply to: Kent1Cooper

Thanks Kent, worked great!

 

Here is the revised code if anyone wants it:

 

 

 

(defun c:layerlegend (/ osm ortho clayer celtype cecolor start-point line-length text-height *error*)
(defun *error* (msg)
(setvar "osmode" osm)
(setvar "orthomode" ortho)
(setvar "clayer" clayer)
(setvar "celtype" celtype)
(setvar "cecolor" cecolor)
(princ msg)
)

(defun mfp (pt pa) (list (+ (car pt) (car pa)) (+ (cadr pt) (cadr pa)) (+ (caddr pt) (caddr pa))))


(setq osm (getvar "osmode"))
(setq ortho (getvar "orthomode"))
(setq clayer (getvar "clayer"))
(setq celtype (getvar "celtype"))
(setq cecolor (getvar "cecolor"))
(setq start-point (getpoint "\nSelect point to start legend: "))
(setvar "orthomode" 1)
(setq line-length (getdist start-point "\nEnter length of line or click end point: "))
(setq text-height (cdr (assoc 40 (tblsearch "style" (getvar "textstyle")))))
(setvar "osmode" 0)
(setvar "celtype" "ByLayer")
(setvar "cecolor" "ByLayer")
(command "_line" start-point (mfp start-point (list line-length 0 0)) "")
(command "-text" (mfp start-point (list 0 1 0)) "0" (getvar "clayer"))
(while (setq lay (cdadr (tblnext "layer" (not lay)))); make list of Layer names
  (setq laylist (cons lay laylist))
)
(foreach
  lay
  (vl-sort laylist '<); sorted alphabetically
  (setvar 'clayer lay)
  (setq start-point (mfp start-point (list 0 (- 0 (+ text-height 4)) 0)))
  (command
    "_line" start-point (mfp start-point (list line-length 0 0)) ""
    "-text" (mfp start-point (list 0 1 0)) "0" lay ; [could leave (getvar 'clayer) in there]
  )
); foreach


(setvar "osmode" osm)
(setvar "orthomode" ortho)
(setvar "clayer" clayer)
(setvar "celtype" celtype)
(setvar "cecolor" cecolor)
)

Distinguished Contributor lai
Distinguished Contributor
lai
Posts: 123
Registered: ‎08-15-2011
Message 4 of 5 (1,492 Views)

Re: Layer Legend

03-23-2012 06:46 PM in reply to: tmullins

hi, just come across this topic, and i give it a try to the lisp..after load the lisp and at command prompt, i type "layerlegend", somehow i see the end resut is just a line and a text above it..i just dun see any purpose or maybe i have misunderstand something..

*Expert Elite*
Kent1Cooper
Posts: 5,788
Registered: ‎09-13-2004
Message 5 of 5 (1,470 Views)

Re: Layer Legend

03-24-2012 07:24 PM in reply to: lai

lai wrote:

hi, just come across this topic, and i give it a try to the lisp..after load the lisp and at command prompt, i type "layerlegend", somehow i see the end resut is just a line and a text above it..i just dun see any purpose or maybe i have misunderstand something..


I haven't loaded it and run it, but some possibilities come to mind:

 

Does your drawing have more than one Layer in it?  Is your current Text Style defined with 0 height?  [I think the code is built for a fixed-height style.]  Or, could it be a metric-vs.-imperial units issue?  Is it truly "just a line," or might it be multiple lines very close together?  And is it just one Text entity, or multiples on top of each other, or nearly so?

Kent Cooper
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!