Lessons

Working with Entities in AutoCAD: Advanced

Welcome back! As we continue our journey into AutoLISP, we’ll delve deeper into advanced entity manipulations within AutoCAD. Entities, as we know, form the crux of our drawings, and mastering the art of managing them is pivotal. Today, we’ll expand our understanding by exploring some more advanced functions that deal with entities.

Learning Outcomes

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

  • Delete and update entities programmatically.
  • Make precise selections and understand the difference between various selection methods.
  • Convert entity names to handles and vice versa.
  • Compute the bounding box of text entities.
  • Transform coordinates between UCS and WCS.

Advanced Entity Functions

entdel

Definition: Deletes a specified entity from the drawing.

Syntax:

(entdel entity_name)

Examples:

  1. Delete a selected entity:
(entdel (car (entsel)))

Prompts the user to select an entity, which will then be deleted.

  1. Delete an entity using its handle:
(entdel (handent “12AB”))

Deletes the entity with the handle “12AB”.

  1. Remove the last created entity:
(entdel (entlast))

entupd

Definition: Updates or regenerates a specified entity, useful after modifying its properties.

Syntax:

(entupd entity_name)

Examples:

  1. Update a selected entity:
(entupd (car (entsel)))
  1. Modify the color of an entity and then update it:
(setq e_data (entget (car (entsel))))
(setq e_data_modified (subst (cons 62 3) (assoc 62 e_data) e_data))
(entmod e_data_modified)
(entupd (car e_data_modified))

Changes the color of the selected entity to green and then updates it.

  1. Regenerate the last created entity:
(entupd (entlast))

entsel

Definition: Allows the user to interactively select a single entity.

Syntax:

(entsel [prompt])

Examples:

  1. Select an entity with a custom prompt:
(setq selected_entity (entsel “\nSelect an entity to modify: “))
  1. Get the start and end points of a selected line:
(setq line_data (entget (car (entsel))))
(setq start_point (cdr (assoc 10 line_data)))
(setq end_point (cdr (assoc 11 line_data)))
  1. Change the layer of a selected entity:
(setq e_data (entget (car (entsel “\nSelect an entity to change its layer: “))))
(setq e_data_modified (subst (cons 8 “NewLayer”) (assoc 8 e_data) e_data))
(entmod e_data_modified)

nentsel

Definition: Allows the user to interactively select a single entity, even if it’s part of a complex object like a block or polyline.

Syntax:

(nentsel [prompt])

Examples:

  1. Select a part of a block reference:
(setq subentity (nentsel “\nSelect a part of a block: “))
  1. Get the properties of the selected subentity:
(setq subentity_data (entget (cadr subentity)))
  1. Modify the color of a subentity:
(setq modified_data (subst (cons 62 4) (assoc 62 subentity_data) subentity_data))
(entmod modified_data)

nentselp

Definition: Allows the user to interactively select a single entity, providing more detailed information, especially useful for nested entities.

Syntax:

(nentselp [point])

Examples:

  1. Select a nested entity:
(setq nested_entity (nentselp))
  1. Retrieve the insertion point of a selected block:
(setq block_data (entget (cadr nested_entity)))
(setq insertion_point (cdr (assoc 10 block_data)))
  1. Modify the rotation angle of a nested entity:
(setq modified_data (subst (cons 50 45.0) (assoc 50 block_data) block_data))
(entmod modified_data)

handent

Definition: Converts a handle to an entity name.

Syntax:

(handent handle_string)

Examples:

  1. Convert a handle to an entity name:
(setq entity_name (handent “1F3”))
  1. Delete an entity using its handle:
(entdel (handent “1F3”))
  1. Update the entity associated with a handle:
(entupd (handent “2A4”))

textbox

Definition: Returns the bounding box of a text or mtext entity.

Syntax:

(textbox entity_list)

Examples:

  1. Get the bounding box of a selected text:
(setq text_box (textbox (entget (car (entsel “\nSelect a text entity: “)))))
  1. Compute the width and height of the text bounding box:
EXAMPLE 1

(setq width (- (cadr (cadr text_box)) (car (cadr text_box))))
(setq height (- (caddr (cadr text_box)) (cadr (cadr text_box))))

  1. Move the text entity based on its bounding box dimensions:
EXAMPLE 1

(command “_MOVE” (car (entsel “\nSelect a text to move: “)) “” (list width height))


trans

Definition: Transforms a point from one coordinate system to another.

Syntax:

(trans point from_UCS [to_UCS [disp]])

 

Examples:

  1. Convert a point from WCS to current UCS:
(setq point_in_ucs (trans ‘(10 10 0) 0 1))
  1. Convert a point from current UCS to WCS:
(setq point_in_wcs (trans ‘(10 10 0) 1 0))
  1. Convert a point from one named UCS to another:
(setq converted_point (trans ‘(10 10 0) “UCS1” “UCS2”))

Conclusion

Advanced entity manipulations in AutoCAD using AutoLISP offer an array of possibilities, from precise selection to efficient modifications. Mastery over these functions empowers you to tackle intricate CAD challenges with ease and precision. Make sure to practice with these functions, integrating them into your projects and routines. Keep exploring, and stay tuned for more in-depth lessons on AutoLISP!