Autolisp to list blocks in particular layer to excel with X,Y & Rotation angle

Autolisp to list blocks in particular layer to excel with X,Y & Rotation angle

Anonymous
Not applicable
2,974 Views
12 Replies
Message 1 of 13

Autolisp to list blocks in particular layer to excel with X,Y & Rotation angle

Anonymous
Not applicable

Dear All,

I'm new to Autolisp and would like some help from you.

 

I am looking for a Autolisp, which lists all the blocks name placed in a particular layer to excel with other information like its layer name, x, y, z co-ordinates and mainly its rotation angle.

 

Request you all to share your ideas or the existing post in the forum that fulfills my above requirement, if any.

 

Thank you all..

 

Regards,

Harish

0 Likes
Accepted solutions (4)
2,975 Views
12 Replies
Replies (12)
Message 2 of 13

S.Faris
Advisor
Advisor

You can do that with DATAEXTRACTION.

Refer this : To Create a New Data Extraction Table

Data Extraction: Exploring the Features and Benefits of AutoCAD

 

Sample

SN0ZoRWqWK.png

SALMANUL FARIS

Message 3 of 13

Anonymous
Not applicable

Hi 

Thank you for your reply.

I am aware of DATAEXTRACTION command in AutoCAD, But this takes many step to get the required information.

 

I am looking for a single step/command by Autolisp code, which will create an excel with required information.

Also i have come across an Autolisp code called "Blocks2Excel" in this forum, which fulfills my requirement partially.

 

Attached the same for your reference, if at all if you find any solution please let me know.

 

Thank you again..

 

Regards,

Harish

0 Likes
Message 4 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> But this takes many step to get the required information

Can you describe which of these steps are not necessary?

  • Don't you need to select by blockname or layer?
  • Don't you need to specify which attributes to export?
  • Don't you need to specify which sort order?
  • Don't you need to specify which columns to export to Excel?
  • Don't you need to specify the filename to create?

 

I agree that all that could be done with one command, but in that case you have to define the criteria and export settings anywhere else (DATAEXTRACTION allows you to use a template with all that settings and so you can pass the wizard to the last page to define the export filename).

 

I guess this information could help to find a tool for you.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 5 of 13

dlanorh
Advisor
Advisor
Accepted solution

The count column is superfluous as each block will have different x, y and z coordinates.

 

Try this: 

 

(vl-load-com)

(defun c:test (/ *error* c_doc ss blk_name b_lst)
  
  (defun *error* ( msg )
    (if fp (close fp))
    (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nAn Error : " msg " occurred.")))
    (princ)
  );end_*error*_defun
	
  (setq c_doc (vla-get-activedocument (vlax-get-acad-object))
        csv_file (strcat (getvar "dwgprefix") (getvar "dwgname") " - Block Data" ".csv");construct csv file name
        fp (open csv_file "w");open csv file
        ss (ssget "_X" '((0 . "INSERT")))
  );end_setq
  
  (write-line "Block Name, Rotation, Layer , X , Y , Z" fp)
  
  (vlax-for blk (vla-get-activeselectionset c_doc)
    (setq blk_name (vlax-get-property blk (if (vlax-property-available-p blk 'effectivename) 'effectivename 'name))
          b_lst (cons (list blk_name (vlax-get-property blk 'rotation) (vlax-get-property blk 'layer) (vlax-get blk 'insertionpoint)) b_lst)
    );end_setq      
  );end_for
  (setq s_lst (vl-sort b_lst '(lambda (x y) (if (= (car x) (car y)) (< (cadr x) (cadr y)) (< (car x) (car y))))))
  (foreach blk s_lst
    (write-line (strcat (nth 0 blk) "," (angtos (nth 1 blk) 0 0) "," (nth 2 blk) "," (rtos (car (nth 3 blk)) 2 3) "," (rtos (cadr (nth 3 blk)) 2 3) "," (rtos (caddr (nth 3 blk)) 2 3)) fp)
  );end_foreach
  (close fp);close file
  (princ)
);end_defun

It writes the results into a csv file which can be opened in excel. It is placed in the same directory as the drawing.

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

Message 6 of 13

Anonymous
Not applicable

Dear  dlanorh,

 

Thank you so much for your magic codes... Its working like Charm..

 

I need one small changes in below code.. as of now the code listing all the blocks in the drawing with its properties. But I need to list only the blocks from particular layer.

Example: Code to list the blocks, which are placed only in layer name called "Plans".

 

Please need your extended help on this requirement.

 

Thank you again..

 

Regards,

Harish Kumar M

0 Likes
Message 7 of 13

dlanorh
Advisor
Advisor
Accepted solution

Try this. It is untested as I have no access to full AutoCAD at the moment.

 

(vl-load-com)

(defun c:test (/ *error* c_doc csv_file fp ent lyr ss blk_name b_lst)
  
  (defun *error* ( msg )
    (if fp (close fp))
    (if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
    (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nAn Error : " msg " occurred.")))
(princ) );end_*error*_defun (setq c_doc (vla-get-activedocument (vlax-get-acad-object)) csv_file (strcat (getvar "dwgprefix") (getvar "dwgname") " - Block Data" ".csv");construct csv file name fp (open csv_file "w");open csv file lyr nil );end_setq (while (not lyr) (setq ent (car (entsel "\nSelect Entity on Layer to Report : ")) lyr (vlax-get-property (vlax-ename->vla-object ent) 'layer) );end_setq );end_while (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 8 lyr)))) (write-line "Block Name, Rotation, Layer , X , Y , Z" fp) (vlax-for blk (vla-get-activeselectionset c_doc) (setq blk_name (vlax-get-property blk (if (vlax-property-available-p blk 'effectivename) 'effectivename 'name)) b_lst (cons (list blk_name (vlax-get-property blk 'rotation) (vlax-get-property blk 'layer) (vlax-get blk 'insertionpoint)) b_lst) );end_setq );end_for (setq s_lst (vl-sort b_lst '(lambda (x y) (if (= (car x) (car y)) (< (cadr x) (cadr y)) (< (car x) (car y)))))) (foreach blk s_lst (write-line (strcat (nth 0 blk) "," (angtos (nth 1 blk) 0 0) "," (nth 2 blk) "," (rtos (car (nth 3 blk)) 2 3) "," (rtos (cadr (nth 3 blk)) 2 3) "," (rtos (caddr (nth 3 blk)) 2 3)) fp) );end_foreach (close fp);close file (princ) );end_defun

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

Message 8 of 13

Anonymous
Not applicable

Hi dlanorh,

 

Thank you so much for your support.. Your code is doing my work like a magic..

Appreciate your quick response Sir..

If at all any options to like your post 'n' number of times.. i would have given 1000.. LIKES ... Smiley Happy 

 

Thank you again.

 

Regards,

Harish

0 Likes
Message 9 of 13

dlanorh
Advisor
Advisor
Accepted solution
Thank you. The code needs a bit of a tidy up, as I started to do something then changed my mind, but left spurious code in the lisp. I will sort and post the tweaked code.

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

Message 10 of 13

dlanorh
Advisor
Advisor
Accepted solution

Please find attached the tidied up lisp.

 

I have re-inserted the option to process ALL the blocks or only ByLayer. This impliments the dynamic mode and dynamic prompt so the choices should appear at the cursor so you can select with the mouse. The default is ByLayer so you can quickly move past this by pressing enter or right mouse click if it is set up for this.

 

If all is selected It will sort the blocks by layer, rotation then block name, in that order.

 

Enjoy

 

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

Message 11 of 13

Anonymous
Not applicable

 dlanorh,

 

Thank you so much for your effort.. support.. tidied up lisp Sir..

Many Thanks.. have a wonderful weekend Sir..

 

Regards,

Harish Kumar M

 

 

 

0 Likes
Message 12 of 13

Anonymous
Not applicable

Hi, i've droped your lisp file into autocad, but can't seem to invoke it by typing in the name blkdata into the command line 

0 Likes
Message 13 of 13

dlanorh
Advisor
Advisor

The actual command to run the lisp is "test". If you open blkdata.lsp in a text editor you will find this is the start of the second line of code.

 

(defun c:test 

The defun name starts with a "c:" which indicates the name you must type on the command line to run it. If you wish to change it to something more memorable, just change the "test" part to whatever you want.

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

0 Likes