Entity properties are accessed by using two AutoLISP data types: entity names and selection set. Entity names are similar to symbols, that is, they are symbolic representations of entities. An entity name is device used to point to a record in drawing database. This database record holds all the information regarding the particular entity. Once you know the entity name, you can retrieve the information stored in entities record. To get entity name, get the number of entities by sslength and then get each name by while or repeat.
ssname: Returns the object (entity) name of the indexed element of a selection set. (ssname ss index). The index argument must be an integer. If index is negative or greater than the highest numbered entity in the selection set, nil is returned. The first element in the set has an index of zero. Entity names in selection sets obtained with ssget always are names of main entities. Subentities (attributes and polyline vertices) are not returned. (The entnext function allows access to them.)
(setq sset (ssget)) Creates a selection set named sset
(setq ent1 (ssname sset 0)) Gets name of first entity in sset
(setq ent4 (ssname sset 3)) Gets name of fourth entity in sset
To access entities beyond the 32767th one in a selection set, you must supply the index argument as a real. For example:
(setq entx (ssname sset 50843.0)) Gets name of 50844th entity in sset
entnext: Returns the name of the next object (entity) in the drawing. (entnext [ename]). If entnext is called with no arguments, it returns the entity name of the first nondeleted entity in the database. If entnext is called with an entity name argument ename, it returns the entity name of the first nondeleted entity following ename in the database. If there is no next entity in the database, it returns nil. The entnext function returns both main entities and subentities. The entities selected by ssget are main entities, not attributes of blocks or vertices of polylines. You can access the internal structure of these complex entities by walking through the subentities with entnext. Once you obtain a subentity's name, you can operate on it like any other entity. If you obtain the name of a subentity with entnext, you can find the parent entity by walking forward with entnext until a seqend entity is found, then extracting the -2 group from that entity, which is the main entity's name.
(setq e1 (entnext)) Sets e1 to the name of the first entity in the drawing.
(setq e2 (entnext e1)) Sets e2 to the name of the entity following e1.
entsel: Prompts the user to select a single object (entity) by specifying a point. (entsel [msg]). entsel function returns a list whose first element is the entity name of the chosen object and whose second element is coordinates (in terms of the current UCS) of the point used to pick the object. If a string is specified for msg, that string is used to ask the user for the object. Otherwise, the prompt defaults to Select object. Note that the pick point returned by entsel does not represent a point that lies on the selected object. The point returned is the location of the crosshairs at the time of selection. The relationship between the pick point and the object will vary depending on the size of the pickbox and the current zoom scale. A list of the form returned by entsel can be supplied to AutoCAD in response to any of its object selection prompts. It is treated by AutoCAD as a pick of the designated object by pointing to the specified point.
entlast: Returns the name of the last nondeleted main object (entity) in the drawing. (entlast). entlast function is frequently used to obtain the name of a new entity that has just been added with the command function. The entity need not be on the screen or on a thawed layer to be selected.
entget: Retrieves an object's (entity's) definition data. (entget ename [applist]). entget function returns a list containing the entity definition data of the entity ename. This applies to both graphical and nongraphical entities. If you supply applist, an optional list of registered application names, entget also returns the extended data associated with the specified applications. The data returned by entget is coded as an association list, from which you can extract items by using the assoc function. Objects in the list are assigned AutoCAD DXF group codes for each part of the entity data.
Assume that the last object created in the drawing is a line drawn from point (1,2) to point (6,5). You can retrieve the entity name of the last object with the entlast function, and pass that name to entget.
(entget (entlast))
This might return the following:
((-1 .
) Entity name
(0 . "LINE") ;Object type
(8 . "0") ;Layer
(10 1.0 2.0 0.0) ;Start point
(11 6.0 6.0 0.0) ;Endpoint
)
assoc: Searches an association list for an element and returns that association list entry. (assoc item alist). Searches the association list alist for item as the key element and returns that alist entry. If assoc does not find item as a key in alist, it returns nil.
For example, given the alist:
((-1 . ) (0 . "LINE") (8 . "0") (10 1.0 2.0 0.0) (11 6.0 6.0 0.0))
then
(cdr (assoc -1 alist)) will return Entity name
(cdr (assoc 0 alist)) will return "LINE" Object type
(cdr (assoc 8 alist)) will return "0" Layer
(cdr (assoc 10 1.0 2.0 0.0 alist)) will return Start point
(cdr (assoc 11 6.0 6.0 0.0 alist)) will return End point
Association lists are frequently used for storing data that can be accessed by a key. The subst function provides a convenient means of replacing the value associated with one key in an association list.
How to get a point on line, by selecting a line and draw a valve at angle.
(setq e0 (entsel “Select line”)
entity_name (car e0)
p1 (cdr e0))
(if (= (cdr (assoc 0 (entget entity_name))) "LINE")
(setq layer_name (cdr (assoc 8 (entget entity_name)))
p101 (cdr (assoc 10 (entget entity_name)))
p102 (cdr (assoc 11 (entget entity_name)))
i1 (angle p101 p102)
p1 (polar p101 i1 (distance p101 p1))
)
)
(setq p2 (polar p1 (+ i1 pi) 5.0)
p3 (polar p1 i1 5.0)
p4 (polar p2 (/ pi 2.0) 3.0)
p5 (polar p2 (* (/ pi 2.0) 3.0) 3.0)
p6 (polar p3 (/ pi 2.0) 3.0)
p7 (polar p3 (* (/ pi 2.0) 3.0) 3.0)
)
(command “break” e0 “f” p2 p3
“line” p4 p5 p6 p7 p4 “”
)
entdel: Deletes objects (entities) or undeletes previously deleted objects. (entdel ename). The entity specified by ename is deleted if it is currently in the drawing. The entdel function undeletes the entity (restores it to the drawing) if it has been deleted previously in this editing session. Deleted entities are purged from the drawing when the drawing is exited. entdel function can delete both graphical and non-graphical entities.
(setq e1 (entnext)) Sets e1 to the name of the first entity in the drawing
(entdel e1) Deletes entity e1
(entdel e1) Undeletes (restores) deleted entity e1
entmake: Creates a new entity (graphical object) in the drawing. (entmake [elist])
The following code creates a red circle, centered at (4,4) with a radius of 1. The optional layer and linetype fields have been omitted and therefore assume default values.
(entmake '((0 . "CIRCLE") (62 . 1) (10 4.0 4.0 0.0) (40 . 1.0)))
entmod: Modifies the definition data of an object (entity). (entmod elist). entmod function is passed with a list (elist) in the format returned by entget, and it updates the database information for the entity name specified by the -1 group in elist. The primary mechanism through which AutoLISP updates the database is by retrieving entities with entget, modifying the list defining an entity, and updating the entity in the database with entmod. The entmod function can modify both graphical and nongraphical objects.
(setq en (entnext)) Sets en to the name of the first entity in the drawing
(setq ed (entget en)) Sets ed to the entity data of entity en
(setq ed
(subst (cons 8 "0")
(assoc 8 ed) Changes the layer group in ed to layer 0
ed
)
)
(entmod ed) Modifies entity en's layer in drawing
entupd: Updates the screen image of an object (entity). (entupd ename)
(entupd ed) Regenerates the entity ed.
subst: Searches a list for an old item and returns a copy of the list with a new item substituted in place of every occurrence of the old item (subst newitem olditem lst)
If olditem is not found in lst, subst returns lst unchanged.
(setq sample '(a b (c d) b))
(subst 'qq 'b sample) ;;returns (A QQ (C D) QQ)
(subst 'qq 'z sample) ;;returns (A B (C D) B)
(subst 'qq '(c d) sample) ;;returns (A B QQ B)
(subst '(qq rr) '(c d) sample) ;returns (A B (QQ RR) B)
(subst '(qq rr) 'z sample) ;;returns (A B (C D) B)
When used in conjunction with assoc, subst provides a convenient means of replacing the value associated with one key in an association list. The subst function provides a convenient means of replacing the value associated with one key in an association list.