Autolisp

Extracting Block Entities with autolisp in AutoCAD

Just as (entnext) works with old style polylines and attributes in a block, it may also be used to extract the entities that make up a block. This is done through the block definition table.

Explanation: (entnext) with blocks

Command:(setq B1 (tblnext “BLOCK” T))
((0 . “BLOCK”) (2 . “PART99”) (70 . 64) (10 0.0 0.0 0.0)
(-2 . <Entity name:40000018>))

The entity name associated with DXF code -2 is the first subentity of the block.

Command: (setq SE1 (cdr (assoc -2 B1)))
(Entity name: <40000018>)

The definition list returned by (entnext) will be the same as for the original subentity:

Command: (entget SE1)
(-1 . <Entity name:40000018>) (0 . “CIRCLE”) (8 . “0”) (10 3.0 3.0 0.0) (40 . 1.0) (210 0.0 0.0 1.0))

(entnext) will find the name of the next subentity in the block:
Command:(entnext SE1)
(Entity name 40000031)

Article inline ad #2
  • There is no SEQEND in the case of entities that comprise a block. (entnext) with the last subentity of a block as its argument returns NIL.

PRACTICE I

Create SUBEN.LSP, which includes two functions, (suben) and (nextsub), that will give information about subentities in polylines and blocks. They are printed below. Estimated time for completion: 10 minutes.

;;; SUBEN.LSP
;;; SUBEN – returns entity definition list
;;; of first subentity of the
;;; selected complex entity
(load “GET”) ;a file we created earlier that
;;;includes the routine GETN
(defun SUBEN ()
(setq SUB (entnext (GETN))) (entget SUB)
)
;** NEXTSUB – returns entity list of next subentity
(defun NEXTSUB ()
(setq SUB (entnext SUB))
(entget SUB)
)

  • A complete solution to this exercise is on your class disk as SUBEN-A.LSP.

Now try the routine:

1. Start a new drawing.

2. Change the PLINETYPE system variable to 0. This allows you to draw old style polylines.

3. Draw a polyline of two line segments and insert a block named PARTB.

4. Load SUBEN.LSP. Type in (SUBEN) and select the polyline.

Article inline ad #4

5. Type in (NEXTSUB). Repeat (NEXTSUB) until the entity type is “SEQEND” rather than “VERTEX.”

6. Type in (SUBEN) again, select the block insert, and repeat the procedure.

  • Processing subentities is often accomplished by creating a loop that begins with the main entity name and steps through the subentities checking for an entity type SEQEND, which will appear associated with DXF code 0. The next practice uses this technique.

PRACTICE II

Create a function that places a point at each vertex of a polyline. Use the subroutine technique discussed earlier. Estimated time for completion: 10 minutes.

Steps

1. First create a subroutine that returns a list of subentity names for a selected entity.

2. The main function will process that list and place the points.

Solution (PTPL.LSP)

(defun SUBENT ()
(setq SUBLIST nil)
(setq SUBNAME (car (entsel)))
(setq SUBTYPE (cdr (assoc 0 (entget (entnext SUBNAME)))))
(while (not (= SUBTYPE “SEQEND”))
(setq SUBNAME (entnext SUBNAME))
(setq SUBLIST (cons SUBNAME SUBLIST))
(setq SUBTYPE (cdr (assoc 0 (entget (entnext SUBNAME)))))
) )

(defun C:PTPLINE ()
(setvar “cmdecho” 0) (SUBENT)
(setvar “PDMODE” 34)
(foreach SUBNAME SUBLIST
(princ SUBNAME)
(setq ENXY (cdr (assoc 10 (entget subname))))
(command “point” ENXY)
)
(setvar “cmdecho” 0)
)

  • A complete solution to this exercise is on your class disk as PTPL-A.LSP.

PRACTICE III

Create the following routine in the file SUBBLK.LSP, that will give the entity definition list of the first entity in a block. Estimated time for completion: 5 minutes.

;** SUBBLK.LSP
;** SUBBLK-returns the entity definition list of the 1st
;** subentity of a block in the block definition table
(defun SUBBLK ()
(setq B1 (tblnext “BLOCK” T))
(princ (cdr (assoc 2 B1)))
(setq SUB (cdr (assoc -2 B1)))
(entget SUB)
)

Load and execute the above routine, and type in (NEXTSUB) several times.

  • A complete solution to this exercise is on your class disk as SUBBLK-A.LSP.

R. Khouri

With over 30 years of experience in the CAD industry as an instructor, developer, and manager, I have a wealth of knowledge in the field. My background in computer engineering has given me a solid foundation for understanding the complexities of CAD softwares. AutoCAD is my go-to tool, and I'm passionate about all forms of computer-aided design (CAD) and design in general.
Back to top button

Adblock Detected

Please disable your ad blocker to view the page content. For an independent site with free content, it's a matter of life and death to have advertising. Thank you for your understanding!