Layer names to Excel

Layer names to Excel

Philip-John
Advocate Advocate
1,483 Views
7 Replies
Message 1 of 8

Layer names to Excel

Philip-John
Advocate
Advocate

Hi,
I,m new in this Autolisp world. It shall be appreciated if somebody can help me on a project of mine.
I have an AutoCAD project which has more than 140 layers. I'm looking for lisp codes that will collect layer names from selected objects and export to an Excel sheet.

Thanks in advance....

0 Likes
Accepted solutions (1)
1,484 Views
7 Replies
Replies (7)
Message 2 of 8

ВeekeeCZ
Consultant
Consultant

Copy-paste selected objects into a new drawing, run Layer properties dialog there and select all the layers, and copy-paste that into excel. 

It's a 10-sec job, no need for a lisp. 

Message 3 of 8

hak_vz
Advisor
Advisor
Accepted solution

 

(defun c:ExpLayToExcel(/ *error* Table _openfile laylist i ss f fname)
	(vl-load-com)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(if f (close f))
		(princ)
	)
	(defun _openfile (file / sh)
		(setq sh (vlax-get-or-create-object "Shell.Application"))
		(vlax-invoke-method sh 'open (findfile file))
		(vlax-release-object sh)
	)
	(princ "\nSelect objects to extract layer names to Excel >")
	(setq ss (ssget))
	(cond 
		((and ss (setq fname (getfiled "Output file:" (getvar "dwgprefix") "csv" 3)))
			(setq f (open fname "w") i -1)
			(while (< (setq i (1+ i))(sslength ss))
				(setq lay (cdr (assoc 8 (entget (ssname ss i)))))
				(if (not (member lay laylist))(setq laylist (cons lay laylist)))
			)
			(foreach layer (vl-sort laylist'<) (write-line layer f))
			(close f)
			(_openfile fname) 
		)
	)
	(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.
Message 4 of 8

Philip-John
Advocate
Advocate

@hak_vz 

Thanks a lot Bro.

Working perfectly..

 

.... and just a funny (.. or even a serious) request:

Can you make it possible for inverse selection.

I mean, collect non-selected objects layers and export to Excel sheet.

0 Likes
Message 5 of 8

hak_vz
Advisor
Advisor

This exports all non-selected layers

(defun c:ExpLayToExcelReversed(/ *error* Table _openfile laylist all_layers i ss f fname)
	(vl-load-com)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(if f (close f))
		(princ)
	)
	(defun _openfile (file / sh)
		(setq sh (vlax-get-or-create-object "Shell.Application"))
		(vlax-invoke-method sh 'open (findfile file))
		(vlax-release-object sh)
	)
	(defun Table (s / d r)
		(while (setq d (tblnext s (null d)))
			(setq r (cons (cdr (assoc 2 d)) r))
		)
	)
	(princ "\nSelect objects to extract layer names to Excel >")
	(setq ss (ssget))
	(cond 
		((and ss (setq fname (getfiled "Output file:" (getvar "dwgprefix") "csv" 3)))
			(setq f (open fname "w") i -1)
			(while (< (setq i (1+ i))(sslength ss))
				(setq lay (cdr (assoc 8 (entget (ssname ss i)))))
				(if (not (member lay laylist))(setq laylist (cons lay laylist)))
			)
			(setq all_layers (Table "layer"))
			(foreach layer laylist
				(if (member layer all_layers) (setq all_layers (vl-remove layer all_layers)))
			)
			(foreach layer all_layers
				(write-line layer f)
			)
			(close f)
			(_openfile fname) 
		)
	)
	(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.
Message 6 of 8

Philip-John
Advocate
Advocate

@hak_vz ,

Thanks again.

Perfect..

God bless you..

Message 7 of 8

longnp.water
Explorer
Explorer

TỐT

0 Likes
Message 8 of 8

longnp.water
Explorer
Explorer

https://forum.dwg.ru/showthread.php?t=81587

Can anyone help me or edit it. Lisp is perfect but can't export the layer to any file. I can't find the .txt .csv file in the selected export folder.

0 Likes