This AutoLISP Tutorial: Drawing Statistics in AutoCAD serves as a stepping stone for those eager to harness the potential of custom scripts to automate and enhance their CAD workflows. Dive deep into a programmatic approach to fetch drawing statistics, understand object type distributions, layers, and block instances.
If you’re a budding CAD professional or an enthusiast wanting to tailor AutoCAD to your needs, this guide is your passport to a more efficient drafting experience. Harness the power of AutoLISP and make AutoCAD truly your own!
Key Takeaways
- Understanding AutoLISP: Grasp the foundational elements of the AutoLISP programming language, setting the stage for advanced CAD customization.
- Program Breakdown: Familiarize yourself with the nuts and bolts of the
DWGSTATS
program, gaining insights into how to extract drawing statistics. - Real-world Application: Learn how to implement this program in your projects, making information retrieval efficient and hassle-free.
- Customization Potential: Recognize the versatility of AutoLISP, opening doors to endless possibilities in terms of drawing automation and customization.
AutoLISP program: Drawing Statistics in AutoCAD
Creating an AutoLISP program to provide a summary of objects, layers, block instances, etc. in an AutoCAD drawing can be a bit complex depending on how detailed the summary needs to be. Here’s a simple program to get started:
(defun C:DWGSTATS (/ sset count objtype obj data layers blocks)
;; Select all entities in the drawing
(setq sset (ssget “X”))
(setq count (sslength sset))
(setq data (list))
(setq layers ‘())
(setq blocks ‘())
;; Loop through the selection set to get object data
(repeat count
(setq obj (ssname sset (setq count (1- count))))
(setq objtype (cdr (assoc 0 (entget obj))))
;; Check and collect layer data
(if (not (member (cdr (assoc 8 (entget obj))) layers))
(setq layers (cons (cdr (assoc 8 (entget obj))) layers))
)
;; Check and collect block data if object type is INSERT (block instance)
(if (= objtype “INSERT”)
(if (not (member (cdr (assoc 2 (entget obj))) blocks))
(setq blocks (cons (cdr (assoc 2 (entget obj))) blocks))
)
)
;; Increment the count for this object type in the data list
(if (assoc objtype data)
(setq data (subst (cons objtype (1+ (cdr (assoc objtype data)))) (assoc objtype data) data))
(setq data (cons (cons objtype 1) data))
)
)
;; Print the results
(princ “\nDrawing Statistics:\n”)
(foreach item data
(princ (strcat “\n” (car item) “: ” (itoa (cdr item))))
)
(princ (strcat “\nTotal Layers: ” (itoa (length layers))))
(princ (strcat “\nTotal Block Definitions: ” (itoa (length blocks))))
(princ “\n”)
(princ)
)
When you run the DWGSTATS
command in AutoCAD after loading this LISP, it’ll provide a summary of the objects in the drawing by type, the number of different layers, and the number of block instances.
Note: This is a basic program, and there are many ways to expand upon it. You can extract more details, such as dimensions, texts, etc. Adjust and expand the code as needed!
AutoLISP Tutorial: Drawing Statistics in AutoCAD
The primary aim of the DWGSTATS
program is to offer a brief summary of the drawing. This includes the number of objects based on their type, the total count of distinct layers, and the number of block instances in the drawing.
Step-by-step Explanation:
Program Initialization
defun
: This command defines a new function in AutoLISP. The name of our function/command isDWGSTATS
.- The
/
symbol: Variables following this symbol are local to the function. This means their values won’t interfere with any variables outside the function with the same names.
Selection of All Drawing Entities
ssget
: A function to create a selection set. The “X” argument tells it to select everything in the drawing.setq
: Sets the value of the variablesset
to the selection set created byssget
.
Initialization of Variables
- Here, we initialize and set various variables:
count
: The number of objects in the selection set.data
: An empty list that will store data about the types and counts of objects.layers
&blocks
: Empty lists to store unique layers and blocks.
Loop Through the Selection Set
- The
repeat
function loops a set of commands a specified number of times. We’re looping for each object in our selection set.
Getting the Current Object
ssname
: This function retrieves an object’s name from the selection set.(1- count)
: This decreases the count by one for each loop iteration, effectively progressing through the selection set.
Identifying the Object Type
entget
: Fetches the data of the entity. Returns a list of pairs, where the0
code represents the entity type.assoc
: Finds the pair with the given code (0 in our case).cdr
: Returns the second item of a pair.
Collect Layer Data
- Here, we’re checking if the layer of the current object is already in our
layers
list. If not, we add it.
Collect Block Data
- This checks if the current object is a block instance (
INSERT
). If so, and if its name isn’t already in ourblocks
list, we add it.
Updating Data Counts
- Here, we’re updating our
data
list with counts for each object type. If the type already exists in our list, we increment its count. If not, we add it with a count of 1.
Printing the Results
- We’re using
princ
to print results to the command line. This block of code essentially formats and displays our collected data to the user.
Conclusion:
Upon running the DWGSTATS
command in AutoCAD, the user will receive a summary of the objects based on type, total distinct layers, and total block instances in the drawing.
This program serves as a foundational understanding of data extraction in AutoCAD through AutoLISP.