0% found this document useful (0 votes)
27 views6 pages

AutoLISP Tutorial Drawing Statistics in AutoCAD

The AutoLISP Tutorial on Drawing Statistics in AutoCAD provides a guide for users to automate the extraction of drawing statistics, including object types, layers, and block instances. It introduces the DWGSTATS program, which summarizes these elements when executed in AutoCAD. The tutorial emphasizes the potential for customization and efficiency in CAD workflows through the use of AutoLISP.

Uploaded by

nebiyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

AutoLISP Tutorial Drawing Statistics in AutoCAD

The AutoLISP Tutorial on Drawing Statistics in AutoCAD provides a guide for users to automate the extraction of drawing statistics, including object types, layers, and block instances. It introduces the DWGSTATS program, which summarizes these elements when executed in AutoCAD. The tutorial emphasizes the potential for customization and efficiency in CAD workflows through the use of AutoLISP.

Uploaded by

nebiyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CADpedia.ca KhouriCAD.com AutoCAD-Mac.com CanadaCAD.ca CADdikt.com AutoCAD-Gratuit.com BlocsAutoCAD.com AutoLISPcourse.

com

HOME COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 

Tuto

AutoLISP Tutorial: Drawing Statistics in


AutoCAD
1 year ago • by R. Khouri

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
1. Understanding AutoLISP: Grasp the foundational elements of the AutoLISP programming
language, setting the stage for advanced CAD customization.
2. Program Breakdown: Familiarize yourself with the nuts and bolts of the DWGSTATS program,
gaining insights into how to extract drawing statistics.
3. Real-world Application: Learn how to implement this program in your projects, making
information retrieval efficient and hassle-free.
4. 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 getHOME
started: COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 
(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”) HOME COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 
(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 C:DWGSTATS (/ sset count objtype obj data layers blocks)
defun: This command defines a new function in AutoLISP. The name of our
function/command is DWGSTATS.
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


(setq sset (ssget “X”))
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 variable sset to the selection set created by ssget.

Initialization of Variables
(setq count (sslength sset)) (setq data (list)) (setq layers ‘()) (setq blocks ‘())
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.
HOME COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 
Loop Through the Selection Set
(repeat count
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


(setq obj (ssname sset (setq count (1- count))))
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


(setq objtype (cdr (assoc 0 (entget obj))))
entget: Fetches the data of the entity. Returns a list of pairs, where the 0 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


(if (not (member (cdr (assoc 8 (entget obj))) layers)) (setq layers (cons (cdr (assoc 8 (entget obj)))
layers)) )
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


(if (= objtype “INSERT”) (if (not (member (cdr (assoc 2 (entget obj))) blocks)) (setq blocks (cons
(cdr (assoc 2 (entget obj))) blocks)) ) )
This checks if the current object is a block instance (INSERT). If so, and if its name isn’t
already in our blocks list, we add it.

Updating Data Counts


(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)) )
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


(princ “\nDrawing Statistics:\n”)
HOME COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 
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.

AutoLISP Tutorial: Sequential Auto-


Numbering of Blocks and Text

Lessons

HOME COURSE TUTORIALS BLOG DOWNLOAD SUBSCRIBE 


Introduction to AutoLISP

Basic Arithmetic Operations

Comparisons

Boolean Operations

Number Manipulation: Basic

Number Manipulation: Advanced

String Manipulation: Basics

String Manipulation: Conversions

Introduction to Lists: Basics

Introduction to Lists: Advanced

Basic Programming & Control Flow

Advanced Programming & Control Flow

User Interaction: Input

User Interaction: Configurations

Working with Entities in AutoCAD: Basics

Working with Entities in AutoCAD: Advanced

Selection Sets Management

Graphics & Display Functions

Dictionaries, Tables & Records

File & Memory Management Functions

Canada Media, editor AT AutoLISPcourse.com


1555, boulevard de l’avenir, Suite 306, Laval, Québec
H7S 2N5, Canada
Privacy Policy - Terms of Use - About me

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy