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
- Selection Sets in AutoLISP: Master the art of selecting specific entities within AutoCAD using LISP.
- Interaction with AutoCAD Database: Understand the core methods to retrieve and manipulate entity data.
- User Input/Output in AutoLISP: Grasp the techniques to communicate with users, from gathering inputs to presenting outputs.
- File Operations: Delve deep into creating, reading, and writing to external files from AutoLISP.
- 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:
- Copy and paste the provided code into the AutoCAD command line or load it via Visual LISP Editor.
- Type
attexport
at the command prompt to activate the function. - When prompted, select the blocks from which you wish to export attributes.
- 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:
- 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. Thec:
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.
- 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 thess
variable.- The entire statement checks if any blocks were selected.
- 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.
- Iterating Over Blocks:
(repeat n
- This loop will iterate
n
times, which corresponds to the number of blocks we’ve selected.
- Accessing Block Information:
(setq i (1+ i)
blka (entget (ssname ss (1- i)))
)
entget
retrieves the data associated with an entity. The functionssname
gets the nth item from a selection set.
- 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, andcdr
returns the value, confirming the block has attributes.
- 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.
- 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.
- Feedback to User:
(princ “\nNo blocks selected!”)
- If no blocks were selected, we provide feedback to the user.
- 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!