Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to get layer description in lisp

15 REPLIES 15
Reply
Message 1 of 16
Anonymous
2689 Views, 15 Replies

how to get layer description in lisp

Using autolisp - how can I obtain the layer description of a selected
drawing or xreference object?
15 REPLIES 15
Message 2 of 16
Anonymous
in reply to: Anonymous

what do you mean with layer description?
all layer names of a open drawing
all layer names of a selected xref


KDispoto wrote:

> Using autolisp - how can I obtain the layer description of a selected
> drawing or xreference object?
Message 3 of 16
Anonymous
in reply to: Anonymous

Ultimately I'd like to add the user defined layer "description" (as it
appears at the far right side of the layer properties dialog box) to the
LID.lsp routine below so that when I select a single object, whether it be a
base dwg object or an xreference object, the description would show in the
screen message.

(defun C:LID/)
(setvar "cmdecho" 0)
(setq AA (cdr (assoc 8
(entget (car (entsel "Select entity to set layer: "))))))


(setq EL (tblsearch "layer" AA))
(setq A2 (cdr(assoc 2 EL)))
(setq A62(cdr(assoc 62 EL)))

(setq LYRNM A2)
(setq LYRCLR (rtos A62 2 0))

(setq MSG (strcat "Layer: " LYRNM "\n" "\nLayer color: " LYRCLR))
(alert MSG)

)

"gert" wrote in message
news:5747887@discussion.autodesk.com...
what do you mean with layer description?
all layer names of a open drawing
all layer names of a selected xref


KDispoto wrote:

> Using autolisp - how can I obtain the layer description of a selected
> drawing or xreference object?
Message 4 of 16
Anonymous
in reply to: Anonymous

here's a few snippets of vlisp to get you going.

(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(setq layObj (vla-item theLayers AA)) ;; the layer name you selected
(vla-put-description layObj "This is the layer description")

- the OF

"KDispoto" wrote in message
news:5747872@discussion.autodesk.com...
Ultimately I'd like to add the user defined layer "description" (as it
appears at the far right side of the layer properties dialog box) to the
LID.lsp routine below so that when I select a single object, whether it be a
base dwg object or an xreference object, the description would show in the
screen message.

(defun C:LID/)
(setvar "cmdecho" 0)
(setq AA (cdr (assoc 8
(entget (car (entsel "Select entity to set layer: "))))))


(setq EL (tblsearch "layer" AA))
(setq A2 (cdr(assoc 2 EL)))
(setq A62(cdr(assoc 62 EL)))

(setq LYRNM A2)
(setq LYRCLR (rtos A62 2 0))

(setq MSG (strcat "Layer: " LYRNM "\n" "\nLayer color: " LYRCLR))
(alert MSG)

)

"gert" wrote in message
news:5747887@discussion.autodesk.com...
what do you mean with layer description?
all layer names of a open drawing
all layer names of a selected xref


KDispoto wrote:

> Using autolisp - how can I obtain the layer description of a selected
> drawing or xreference object?
Message 5 of 16
Anonymous
in reply to: Anonymous

I appreciate the help - Being that I barely get by with lisp - I am not
familiar with vlisp (although I won't mind trying it here).

Using your code - what is the variable that contains the layer description
(vla-put-description layObj )?


"the Other Frank" wrote in message
news:5747942@discussion.autodesk.com...
here's a few snippets of vlisp to get you going.

(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(setq layObj (vla-item theLayers AA)) ;; the layer name you selected
(vla-put-description layObj "This is the layer description")

- the OF

"KDispoto" wrote in message
news:5747872@discussion.autodesk.com...
Ultimately I'd like to add the user defined layer "description" (as it
appears at the far right side of the layer properties dialog box) to the
LID.lsp routine below so that when I select a single object, whether it be a
base dwg object or an xreference object, the description would show in the
screen message.

(defun C:LID/)
(setvar "cmdecho" 0)
(setq AA (cdr (assoc 8
(entget (car (entsel "Select entity to set layer: "))))))


(setq EL (tblsearch "layer" AA))
(setq A2 (cdr(assoc 2 EL)))
(setq A62(cdr(assoc 62 EL)))

(setq LYRNM A2)
(setq LYRCLR (rtos A62 2 0))

(setq MSG (strcat "Layer: " LYRNM "\n" "\nLayer color: " LYRCLR))
(alert MSG)

)

"gert" wrote in message
news:5747887@discussion.autodesk.com...
what do you mean with layer description?
all layer names of a open drawing
all layer names of a selected xref


KDispoto wrote:

> Using autolisp - how can I obtain the layer description of a selected
> drawing or xreference object?
Message 6 of 16
Anonymous
in reply to: Anonymous

Agreed, with the years all of us
are become by others

~'J'~
Message 7 of 16
Anonymous
in reply to: Anonymous

Oops, I just re-read your request. Sorry, I thought you wanted to add a
description. You can't get to the description thru regular AutoLisp, you
have to use Vlisp (activeX calls to the object model itself)

It's almost the same code to get the description: (use a get instead of a
put)
the following would be inserted just after your setting of AA

(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(setq layObj (vla-item theLayers AA)) ;; the layer name you selected
(setq descriptString (vla-get-description layObj))

then you can add the string to your strcat at the end.

Of course, if you want, you can access the other properties of the layer at
the same time with:
(setq layColor (vla-get-color layObj)) ;; same as your A62
or
(setq layLT (vla-get-linetype layObj)) etc.

bytheway, your vars A2 and LYRNAME are not necessary, as AA already is bound
to the name.

If you want to get nested layers, look into using nentsel instead of entsel.

oh, and your defun statement first line should read: (defun C:LID ( / )
[with your variable names after the slash]

good luck,

- the OF


"KDispoto" wrote in message
news:5748046@discussion.autodesk.com...
I appreciate the help - Being that I barely get by with lisp - I am not
familiar with vlisp (although I won't mind trying it here).

Using your code - what is the variable that contains the layer description
(vla-put-description layObj )?


"the Other Frank" wrote in message
news:5747942@discussion.autodesk.com...
here's a few snippets of vlisp to get you going.

(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(setq layObj (vla-item theLayers AA)) ;; the layer name you selected
(vla-put-description layObj "This is the layer description")

- the OF

"KDispoto" wrote in message
news:5747872@discussion.autodesk.com...
Ultimately I'd like to add the user defined layer "description" (as it
appears at the far right side of the layer properties dialog box) to the
LID.lsp routine below so that when I select a single object, whether it be a
base dwg object or an xreference object, the description would show in the
screen message.

(defun C:LID/)
(setvar "cmdecho" 0)
(setq AA (cdr (assoc 8
(entget (car (entsel "Select entity to set layer: "))))))


(setq EL (tblsearch "layer" AA))
(setq A2 (cdr(assoc 2 EL)))
(setq A62(cdr(assoc 62 EL)))

(setq LYRNM A2)
(setq LYRCLR (rtos A62 2 0))

(setq MSG (strcat "Layer: " LYRNM "\n" "\nLayer color: " LYRCLR))
(alert MSG)

)

"gert" wrote in message
news:5747887@discussion.autodesk.com...
what do you mean with layer description?
all layer names of a open drawing
all layer names of a selected xref


KDispoto wrote:

> Using autolisp - how can I obtain the layer description of a selected
> drawing or xreference object?
Message 8 of 16
Anonymous
in reply to: Anonymous

"the Other Frank" wrote

>> You can't get to the description thru regular AutoLisp,
>> you have to use Vlisp (activeX calls to the object model itself)


(cdr (assoc 4 (entget (tblobjname "block" "blockname"))))

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 9 of 16
Anonymous
in reply to: Anonymous

Tony,
Wasn't he referring to layer descriptions rather than block descriptions?

Athough may be possible using regular AutoLisp as shown below:

;;This works on my system for 2006, 2008
(defun layerdescription (layname)
(cdr(last(cadr(assoc -3 (entget (tblobjname "layer" layname)
'("AcAecLayerStandard")))))))

that technique would make some large assumptions about application xdata order.

It certainly would be more reliable to get the description by ActiveX right?

Doug

"Tony Tanzillo" wrote in message
news:5748726@discussion.autodesk.com...
"the Other Frank" wrote

>> You can't get to the description thru regular AutoLisp,
>> you have to use Vlisp (activeX calls to the object model itself)


(cdr (assoc 4 (entget (tblobjname "block" "blockname"))))

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 10 of 16
Anonymous
in reply to: Anonymous

I am looking for the layer description. I'd like to display the user defined
layer "description" (as it is entered in the layer properties dialog box)
from of a single selected object, whether it be on the base dwg object or
from an xreference object.

Hope this clears it up:)
Message 11 of 16
Tom Smith
in reply to: Anonymous

What Doug posted will return the layer description for a given layer name. It's kept as extended data in the layer table. You'd have to feed Doug's function the layer of a selected object obtained (for instance) from entsel or nentsel.

Are you sure you need this? Layer descriptions are relatively new, appearing in 2005 for the first time. We haven't yet found a need for them, because we've been muddling along just fine without them for umpteen years. None of your old legacy drawings will include a description. Unless your users have a lot of time on their hands, I'd be surprised if they're actually entering a "user defined layer description" for each layer they use.

If we used them, it would only be as a training tool for new hires, and the standard descriptions would be in the standard layers which come from our standard drawing templates. We discourage users from creating new layers that aren't in the standards.
Message 12 of 16
Anonymous
in reply to: Anonymous

Hi Doug.

Ooops you're right, he was talking about the Layer
description (duh).

And that's also easily obtainable with regular
AutoLISP as you've shown, thanks.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Doug Broad" wrote in message news:5749331@discussion.autodesk.com...
Tony,
Wasn't he referring to layer descriptions rather than block descriptions?

Athough may be possible using regular AutoLisp as shown below:

;;This works on my system for 2006, 2008
(defun layerdescription (layname)
(cdr(last(cadr(assoc -3 (entget (tblobjname "layer" layname)
'("AcAecLayerStandard")))))))

that technique would make some large assumptions about application xdata order.

It certainly would be more reliable to get the description by ActiveX right?

Doug

"Tony Tanzillo" wrote in message
news:5748726@discussion.autodesk.com...
"the Other Frank" wrote

>> You can't get to the description thru regular AutoLisp,
>> you have to use Vlisp (activeX calls to the object model itself)


(cdr (assoc 4 (entget (tblobjname "block" "blockname"))))

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 13 of 16
Anonymous
in reply to: Anonymous

On a similar note ...
From time to time I use a script to create/update a layer list (name, color,
lineweight..) in a dwg.
Can a script (or other method be used) to create/import a layer list in a
dwg which woud include user defined layer descriptions?
btw: using v2008
Thanks,
Message 14 of 16
3dwannab
in reply to: Anonymous

Here's something the ChatGPT wrote to about 90%. A lot has changed since 2007 since this thread was started.

 

 

(vl-load-com) ; Load the ActiveX Automation support

;; Program below allows to export the layer name and descriptions to a .csv
;; where you can then modify it and import it back it.
;; See the very bottom of this code.

(defun compare-layer-info (layer-info-1 layer-info-2) 
  ; "Compare layer information for sorting."
  (if (equal (car layer-info-1) (car layer-info-2)) 
    nil
    (< (car layer-info-1) (car layer-info-2))
  )
)

;; Credit to dlanorh. See, https://www.cadtutor.net/forum/topic/66221-how-to-split-a-string-by-character/?do=findComment&comment=543921
(defun rh:str_split (str char lst / pos) 
  (setq lst (cons (substr str 1 (setq pos (vl-string-position (ascii char) str))) lst)
        str (substr str (+ 2 pos))
  )
  (if (vl-string-position (ascii char) str) (rh:str_split str char lst) (setq lst (reverse (cons str lst))))
)

;;
;; export-layer-info-to-csv function
;;
(defun export-layer-info-to-csv (output-file / file layers layer-desc layer-info layer-name) 

  (setq layer-info '()) ; Initialize a list to store layer information

  (setq layers (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))))

  ; Iterate through each layer and store its name and description
  (vlax-for layer layers 
    (setq layer-name (vla-get-Name layer))
    (setq layer-desc (vla-get-Description layer))
    (setq layer-info (cons (list layer-name layer-desc) layer-info))
  ) ; Sort the layer information based on layer names without string-lessp and string<
  (setq layer-info (vl-sort layer-info 'compare-layer-info)) ; Write the sorted layer information to the CSV file
  (setq file (open output-file "w"))
  (write-line "Layer Name, Description" file)
  (foreach info layer-info 
    (setq layer-name (car info))
    (setq layer-desc (cadr info))
    (write-line (strcat "\"" layer-name "\",\"" layer-desc "\"") file)
  )
  (close file)
  (princ (strcat "Layer information exported to " output-file))
) ;; end export-layer-info-to-csv function

;;
;; import-layer-descriptions function
;;
(defun import-layer-descriptions (csv-file-path / csv-file data layer-description layer-info layer-name layer-obj) 
  ;; Read CSV file
  (setq csv-file (open csv-file-path "r"))
  (setq data (read-line csv-file)) ;; Skip header if exists
  (setq layer-cntOK 0)
  (setq layer-cntNOTOK 0)

  ;; Iterate through CSV data
  (while (setq data (read-line csv-file)) 
    (setq layer-info (rh:str_split data "," nil))
    (setq layer-name (read (nth 0 layer-info)))
    (setq layer-description (read (nth 1 layer-info)))
    ;; Check if layer exists
    (if (tblsearch "layer" layer-name) 
      ;; Layer exists, update description
      (progn 
        (setq layer-obj (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
        (setq layer-cntOK (+ layer-cntOK 1))
        (vla-put-description layer-obj layer-description)
        (princ (strcat "Updated description for layer: " layer-name " with description " layer-description "\n"))
      )
      ;; Layer doesn't exist, create new layer
      (progn 
        (setq layer-cntNOTOK (+ layer-cntNOTOK 1))
        ; (vla-Add (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))) layer-name)
        ; (setq layer-obj (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
        ; (vla-put-description layer-obj layer-description)
        ; (princ (strcat "Created new layer: " layer-name " with description " layer-description "\n"))
      )
    )
  )

  (if layer-info 
    (progn 
      (princ "\nREPORT")
      (princ (strcat "\n" (itoa layer-cntOK) " layers successfully changed :)"))
      (princ (strcat "\n" (itoa layer-cntNOTOK) " layers unsuccessfully changed :(\n"))
    )
  )

  ;; Close CSV file
  (close csv-file)
) ;; end import-layer-descriptions function

;; Set your path to the .csv file here
(setq csvFile "C:\\Users\\username\\Desktop\\OutputFileNew.csv")

;; Example usage for exporting:
(export-layer-info-to-csv csvFile)

;; Example usage for importing:
; (import-layer-descriptions csvFile)

 

Message 15 of 16
Sea-Haven
in reply to: Anonymous

Why not export a lot more color & linetype come to mind. Could use this as a defacto layer manager.

Message 16 of 16
3dwannab
in reply to: Sea-Haven

I use a cad standards .dws file. The issue with it is that it doesn't merge descriptions. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost