Tuto

AutoLISP Tutorial: Export Block Attributes to CSV

Welcome to the AutoLISP Tutorial: Export Block Attributes to CSV. This comprehensive guide is crafted especially for beginners eager to delve into the world of AutoLISP within AutoCAD. You’ll embark on a detailed journey to understand and implement a functional LISP routine, designed to extract block attributes and save them into a CSV format. By the end of this tutorial, you will not only have a practical tool for your CAD operations but also a foundational understanding of AutoLISP programming principles.


Key Takeaways

  1. Selection Sets in AutoLISP: Master the art of selecting specific entities within AutoCAD using LISP.
  2. Interaction with AutoCAD Database: Understand the core methods to retrieve and manipulate entity data.
  3. User Input/Output in AutoLISP: Grasp the techniques to communicate with users, from gathering inputs to presenting outputs.
  4. File Operations: Delve deep into creating, reading, and writing to external files from AutoLISP.
  5. Logical Structures in LISP: Harness the power of decision-making and iteration within your LISP routines.

AutoLISP Program: Export Block Attributes to CSV

Here’s a basic AutoLISP program that will export block attributes to an external CSV file which can then be opened in Excel:

(defun c:attexport (/ ss n i blka data fname)
(if (setq ss (ssget ‘((0 . “INSERT”))))
(progn
(setq n (sslength ss)
data (list)
)
(repeat n
(setq i (1+ i)
blka (entget (ssname ss (1- i)))
)
(if (setq att (entget (cdr (assoc 66 blka)) (ssname ss i)))
(setq data (cons (cdr (assoc 1 att)) data))
)
)
(setq fname (getfiled “Save CSV File” “” “csv” 1))
(if fname
(progn
(setq f (open fname “w”))
(foreach val data
(write-line val f)
)
(close f)
(princ (strcat “\nAttributes exported to: ” fname))
)
)
)
(princ “\nNo blocks selected!”)
)
(princ)
)

How to use:

  1. Copy and paste the provided code into the AutoCAD command line or load it via Visual LISP Editor.
  2. Type attexport at the command prompt to activate the function.
  3. When prompted, select the blocks from which you wish to export attributes.
  4. Choose a file name and location for the CSV file to be saved.

Remember that this script assumes that each block has only one attribute. If a block has more than one attribute, you’ll need to enhance the script to handle multiple attributes per block.

You can then open the saved .csv file in Excel or any other compatible spreadsheet application.


AutoLISP Tutorial: Export Block Attributes to CSV

In this tutorial, we’ll go step-by-step through an AutoLISP program designed to export block attributes to a CSV file.


Breakdown:

  1. Function Declaration:

(defun c:attexport (/ ss n i blka data fname)

  • defun starts the definition of a new function.
  • c:attexport is the name of the function. The c: prefix means it’s callable from the command line.
  • The (/ ss n i blka data fname) section declares local variables. Variables inside the parentheses following the / are local to the function.
  1. Selecting Blocks:

(if (setq ss (ssget ‘((0 . “INSERT”))))

  • ssget is a function to select objects in the drawing. The selection filter '((0 . "INSERT")) specifies only block inserts (INSERT objects).
  • setq assigns the selection set to the ss variable.
  • The entire statement checks if any blocks were selected.
  1. Processing Selection Set:

(progn
(setq n (sslength ss)
data (list)
)

  • progn groups several expressions into a single compound expression.
  • sslength returns the number of items in the selection set.
  • data will store our block attributes.
  1. Iterating Over Blocks:

(repeat n

  • This loop will iterate n times, which corresponds to the number of blocks we’ve selected.
  1. Accessing Block Information:

(setq i (1+ i)
blka (entget (ssname ss (1- i)))
)

  • entget retrieves the data associated with an entity. The function ssname gets the nth item from a selection set.
  1. Checking for Attributes:

(if (setq att (entget (cdr (assoc 66 blka)) (ssname ss i)))

  • If a block has attributes, it has a 66 group with a value of 1. assoc finds this pair, and cdr returns the value, confirming the block has attributes.
  1. Storing Attribute Value:

(setq data (cons (cdr (assoc 1 att)) data))

  • We retrieve the attribute’s value (which is stored under the 1 group code) and add it to the data list.
  1. Getting File Name and Saving Data:

(setq fname (getfiled “Save CSV File” “” “csv” 1))

  • getfiled prompts the user to specify a filename.
  • If a filename is provided, we open the file, write the data, and then close it.
  1. Feedback to User:

(princ “\nNo blocks selected!”)

  • If no blocks were selected, we provide feedback to the user.
  1. End of Function:

(princ)

  • A final princ function is used to suppress any unwanted return values.

Conclusion:

Congratulations on completing the AutoLISP Tutorial: Export Block Attributes to CSV! You’ve now equipped yourself with a vital tool for your CAD operations and, more importantly, a foundational grasp of AutoLISP programming. As you continue your journey, remember that the essence of learning any programming language is practice and experimentation.

So, always stay curious, keep experimenting, and soon you’ll unlock even greater efficiencies and tools for your AutoCAD projects. Happy coding!