Lessons

Working with Entities in AutoCAD: Basics

Today, we are going to embark on a journey into the world of entities in AutoCAD. Entities are the core elements you work with inside AutoCAD, ranging from lines, arcs, and circles to more complex objects like polylines, blocks, and hatches. Being able to manipulate these entities programmatically using AutoLISP is a vital skill for every aspiring AutoCAD developer. Let’s get started!

Learning Outcomes

By the end of this lesson, you’ll be able to:

  • Understand the basic concepts behind entities in AutoCAD.
  • Manipulate and interact with entities using AutoLISP functions.
  • Implement these functions into your own AutoLISP scripts to enhance your CAD projects.

Introduction to Entities in AutoCAD

In AutoCAD, an entity is any object that you can draw or modify. Think of entities as the building blocks of your drawings. Whether you’re drawing a simple line or creating a complex 3D model, you’re working with entities.

Diving Deep into Entity Functions

entget

Definition: This function retrieves the properties of an entity in the form of a list.

Syntax:

(entget entity_name [application])

Examples:

  1. Retrieve properties of an entity:
(setq entity_data (entget (car (entsel))))

Prompts the user to select an entity and stores its properties in the entity_data variable.

  1. Fetch properties of a block reference entity:
(setq block_data (entget (car (entsel)) “ACAD”))

This will retrieve the properties specific to the block reference.

  1. Extract the layer of a selected entity:
(setq entity_layer (cdr (assoc 8 (entget (car (entsel))))))

This will store the layer name of the selected entity in the entity_layer variable.


entlast

Definition: Returns the name of the last entity created in the drawing.

Syntax:

(entlast)

Examples:

  1. Fetch the last entity created:
(setq last_entity (entlast))

Stores the name of the most recently created entity in the last_entity variable.

  1. Retrieve the properties of the last entity:
(setq last_entity_data (entget (entlast)))

Fetches the properties of the most recently created entity.

  1. Draw a line starting from the endpoint of the last drawn line:
(setq last_line_end (cdr (assoc 11 (entget (entlast)))))
(command “_LINE” last_line_end)

This will start drawing a new line from the endpoint of the last drawn line.


entmake

Definition: Creates a new entity based on a list of properties.

Syntax:

(entmake property_list)

Examples:

  1. Create a simple line:
(entmake ‘((0 . “LINE”) (10 0.0 0.0 0.0) (11 10.0 10.0 0.0)))

This will draw a line from (0,0,0) to (10,10,0).

  1. Create a circle with a radius of 5 units at the origin:
(entmake ‘((0 . “CIRCLE”) (10 0.0 0.0 0.0) (40 . 5.0)))
  1. Draw a text entity:
(entmake ‘((0 . “TEXT”) (10 10.0 10.0 0.0) (40 . 1.0) (1 . “Hello, AutoLISP!”)))

This will create a text entity saying “Hello, AutoLISP!” at the point (10,10,0) with a height of 1 unit.


entmod

Definition: Modifies the properties of an existing entity.

Syntax:

(entmod modified_property_list)

Examples:

  1. Modify the endpoint of a line:
(setq entity_data (entget (car (entsel))))
(setq modified_data (subst (cons 11 (list 20.0 20.0 0.0)) (assoc 11 entity_data) entity_data))
(entmod modified_data)

Allows the user to select a line and modifies its endpoint to (20,20,0).

  1. Change the color of an entity to red:
(setq entity_data (entget (car (entsel))))
(setq modified_data (subst (cons 62 1) (assoc 62 entity_data) entity_data))
(entmod modified_data)
  1. Update the text of a text entity:
(setq text_data (entget (car (entsel))))
(setq updated_data (subst (cons 1 “Updated Text”) (assoc 1 text_data) text_data))
(entmod updated_data)

Allows the user to select a text entity and updates its content to “Updated Text”.


entnext

Definition: Retrieves the name of the next entity in the drawing database sequence.

Syntax:

(entnext [entity_name])

Examples:

  1. Fetch the next entity after a selected entity:
(setq next_entity (entnext (car (entsel))))
  1. Retrieve properties of the next entity:
(setq next_entity_data (entget (entnext (car (entsel)))))
  1. Highlight the next entity after a selected entity:
(command “_HIGHLIGHT” (entnext (car (entsel))))

Conclusion

Working with entities in AutoCAD using AutoLISP provides immense power and flexibility to CAD professionals. By understanding these basic entity functions, you can automate mundane tasks, reduce errors, and significantly improve your CAD workflows. Always remember, practice makes perfect. So, keep experimenting with these functions and integrate them into your CAD projects. See you in the next lesson!