Lisp Codes

16
substr - This function retrieves part of a string. Syntax : (substr "string" startPoint numberOfCharacters)  strcase - This function converts to upper or lower case. Syntax : (strcase "string" flag) flag = T or nil strlen - This function returns the length of a string in characters. Syntax : (strlen "string") strcat - This function adds multiple strings together to form one string. Syntax : (strcat "string1" "string2" "string3" ....ect. )  abs - This function returns the absolute value of a number. Syntax : (abs number) atoi - This function converts a string to a integer. Syntax : (atoi "string") itoa - This function converts an integer to a string. Syntax : (itoa integer) atof - This function converts a string to real. Syntax : (atof "string") fix - This function converts a real to an integer. Syntax : (fix real) float - This function takes a number (integer or real) and converts it to a real number. Syntax : (float integer) (float real) car - This function retur ns the first item of a lis t. The item can be a list as shown in ex ample 3 below. Syntax : (car list) cdr - This function returns a list that includes ev erything but the first item in a list. The item can be a list as shown in example 3 below.

Transcript of Lisp Codes

Page 1: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 1/16

substr - This function retrieves part of a string.

Syntax : (substr "string" startPoint numberOfCharacters) 

strcase - This function converts to upper or lower case.

Syntax : (strcase "string" flag) 

flag = T or nil

strlen - This function returns the length of a string in characters.

Syntax : (strlen "string") 

strcat - This function adds multiple strings together to form one string.

Syntax : (strcat "string1" "string2" "string3" ....ect. ) 

abs - This function returns the absolute value of a number.

Syntax : (abs number) 

atoi - This function converts a string to a integer.

Syntax : (atoi "string") 

itoa - This function converts an integer to a string.

Syntax : (itoa integer) 

atof - This function converts a string to real.

Syntax : (atof "string") 

fix - This function converts a real to an integer.

Syntax : (fix real) 

float - This function takes a number (integer or real) and converts it to a real number.

Syntax : (float integer) (float real) 

car - This function returns the first item of a list. The item can be a list as shown in example 3 below.

Syntax : (car list) 

cdr - This function returns a list that includes everything but the first item in a list. The item can be a list as shown

in example 3 below.

Page 2: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 2/16

Syntax : (cdr list) 

cadr - This function returns the second item in a list. The item can be a list as shown in example 3 below.

Syntax : (cadr list) 

caddr - This function returns the third item in a list. The item can be a list as shown in example 3 below.

Syntax : (caddr list) 

caar - This function returns the first item of the first item in a list. The item can be a list as shown in example 3

 below.

Syntax : (caar list) 

cddr - This function returns a list that includes everything after the second item in a list.

Syntax : (cddr list) 

foreach - This function steps through each item in the list and returns the last value.

Syntax : (foreach varName list yourFunctions) 

list - This function creates a list.

Syntax : (list Item) 

Syntax : (list firstItem secondItem) 

Syntax : (list firstItem secondItem thirdItem...) 

cons - This function takes an item and a list, and returns the addition of that item to the beginning of the list. The

first item can be an atom or a list.

Syntax : (cons Item list) ;thanks tim! 

nth - This function returns the Nth item in a list. The first item in a list is item zero.

Syntax : (nth integer list) 

fix - This function returns an integer. (NOTE: This function does not round numbers off!)

Syntax : (fix number) 

float - This function returns a real number.

Syntax : (float number) 

Page 3: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 3/16

rtos - This function converts numbers to a formatted string representing a distance.

Syntax : (rtos number mode precision)

mode = 1 = Scientific

with precision = 1 (rtos 456.5 1 1) returns "4.6E+02"

mode = 2 = Engineering

with precision = 1 (rtos 456.5 2 1) returns "456.5"

mode = 3 = Decimal

with precision = 1 (rtos 456.5 3 1) returns "38'-0.5""

mode = 4 = Architectural

with precision = 1 (rtos 37.7071 4 1) returns "3'-1 1/2""

mode = 5 = Inch Fractional

with precision = 1 (rtos 37.7071 5 1) returns "371/2""

angtos - This function converts a number to a formatted string representing an angle.

Syntax : (angtos number mode precision)

mode = 0 = Decimal Degrees

with precision = 1 (angtos 0.627102 0 1) returns "35.9"

mode = 1 = Degrees Minutes Seconds

with precision = 1 (angtos 0.627102 1 1) returns " 35d56' "

mode = 2 = Grads

with precision = 1 (angtos 0.627102 2 1) returns "39.9g"

mode = 3 = Radians

with precision = 1 (angtos 0.627102 3 1) returns "0.6r"

mode = 4 = Surveyor 

with precision = 1 (angtos 0.627102 0 1) returns "N 54d4' E"

+ - Addition.

Page 4: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 4/16

Syntax : (+ number number) 

- - Subtraction.

Syntax : (- number number) 

/ - Division.

Syntax : (/ number number) 

* - Multiplication.

Syntax : (* number number) 

1+ - Returns value increased by one.

Syntax : (1+ number) 

1- - Returns value decreased by one.

Syntax : (1- number) 

cos - Returns the cosine of an angle expressed in radians.

(Note: Radians are AutoCads angular units. A circle = 2*pi or 180 degrees = pi )

Syntax : (cos number) 

atan - Returns the arctangent of a number in radians.

(Note: Radians are AutoCads angular units. A circle = 2*pi or 180 degrees = pi )

Syntax : (atan number1 ....optional number2) 

sin - Returns the sine of an angle expressed in radians.

(Note: Radians are AutoCads angular units. A circle = 2*pi or 180 degrees = pi )

Syntax : (sin number) 

sqrt - Returns the square root of a number.

Syntax : (sqrt number) 

expt - Returns a number raised to a power.

Page 5: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 5/16

Syntax : (expt number power) 

entsel - This function prompts for the user to pick one entity.

Syntax : (entsel) 

Returns the entity's name and the point selected in a list.

Example: (<Entity name: 3b40a08> (230.666 285.862 0.0))

ssget - This function prompts the user to select entities.

Syntax : (ssget) ;prompts the user for a selection set.

Syntax : (ssget mode) ;prompts the user for a selection set and sets mode to "W", "C", "L", and "P",

corresponding to the Window, Crossing, Last, and Previous selection methods. Another optional mode value is "X", which selects the entire database. There are other modes, but thislist contains the modes that will be used most often.

Syntax : (ssget "W" ) ;prompts the user for a selection set using the Window mode.

Syntax : (ssget "C" ) ;prompts the user for a selection set using the Crossing mode.

Syntax : (ssget "L" ) ;Selects the last entity selected.

Syntax : (ssget "P" ) ;Selects the previously selected entities.

Syntax : (ssget "X" ) ;Selects all entities in the drawing.

Syntax : (ssget filter_List) or (ssget mode filter_List) 

filter_List - an association list containing valid filters for entities corresponding to the Entity DXF GroupCodes. What did I just say? Entity DXF Group Codes? What the heck is that?Click  here to find out.

You can filter out enitites that do no match your criteria using the filter_List. If you onlywanted entities that were on layer "STR" then you could add a filter list that looks like this:

(setq myFilter(list (cons 8 "STR"))) ;returns ((8 . "STR")) 

(ssget "X" myFilter) ;returns a selection set containing only entities on layer "STR"

or 

(setq myFilter(list (cons 0 "LINE")(cons 8 "STR"))) 

;returns ((0 . "LINE")(8 . "STR")) 

(ssget "X" myFilter)

;returns a selection set containing only line entities on layer "STR"

Page 6: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 6/16

 

Returns a selection set of entity names.

ssadd - This function does one of three things depending on how many arguments you send with it.

If you use the ssadd function with no arguments it will return a new selection set with no members.

Example : (setq myNewSet(ssadd)) 

If you use ssadd with a name of an entity it will return a new selection set with only that one member.

Example : (setq myNewSet(ssadd entityName)) 

If you use ssadd with a name of an entity and name of an selection set, it will add that entity to the set.

Example : (setq myOldSet(ssadd entityName selectionSetName)) 

And return the selection set with the entity added to the set. 

Syntax : (ssadd) (ssadd entityName) (ssadd entityName SelectionSetName) 

Where entityName is a valid entity name returned by ssname or entsel.

Where selectionSetName is a valid name of a selection set returned by setq.

ssdel - This function does the opposite of ssadd. SSDEL removes an entity from a selection set.

Syntax : (ssdel entityName selectionSetName) 

Where entityName is a valid entity name returned by ssname or entsel.

Where selectionSetName is a valid name of a selection set returned by setq.

Returns a new selection set without the entity.

Assuming en1 is a valid entity and en2 is not.

Assuming ss1 is a valid selection set and ss2 is not.

Assuming en3 is a valid entity name but is not part of any selection set.

(ssdel en1 ss1) ;returns selection set without entity en.

(ssdel en1 ss2) ;returns ERROR - Bad Argument Type

(ssdel en2 ss1) ;returns ERROR - Bad Argument Type

(ssdel en2 ss2) ;returns ERROR - Bad Argument Type

(ssdel en3 ss1) ;returns nil

Page 7: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 7/16

  Note: If you filter correctly using the ssget "X" function, you shouldn't need ssdel.

sslength - This function returns the number of entities inside a selection set.

Syntax : (sslength selectionSetName) 

Where selectionSetName is a valid name of a selection set returned by setq.

Returns a real number if the number is greater than 32,767, otherwise it returns an integer.

The number, real or integer, represents the quantity of items inside the selection set.

Assuming ss1 is a valid selection set containing 15 entities.

Assuming ss2 is a valid selection set containing 1 entity.

Assuming ss3 is a valid selection set containing no entities.

Assuming ss4 is a valid selection set containing 40,000 entities.

Assuming ss5 is not a valid selection set.

(sslength ss1) ;returns 15 as an integer 

(sslength ss2) ;returns 1 as an integer 

(sslength ss3) ;returns 0 as an integer 

(sslength ss4) ;returns 40000.0 as a real number 

(sslength ss5) ;returns ERROR - Bad Argument Type

ssname - This function returns an entity name contained inside a selection set.

Syntax : (ssname selectionSetName nthItem) 

Where selectionSetName is a valid name of a selection set returned by setq.

Where nthItem is an integer representing an index into the set.

Note:  nthItem needs to be a real if the set contains more than 32,767 items.

Returns an entity's name on success and nil on error.

Assuming ss1 is a valid selection set containing 15 entities.

Assuming ss2 is a valid selection set containing 40,000 entities.

Assuming ss3 is not a valid selection set.

(ssname ss1 0) ;returns the first entity in the selection set.

Page 8: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 8/16

  (ssname ss1 3) ;returns the fourth entity.

(ssname ss1 24) ;returns nil

(ssname ss1 -3) ;returns nil

(ssname ss2 34555) ;returns ERROR - Bad Argument Type (needs real)

(ssname ss2 34555.0) ;returns the 34555th entity

entget - This function returns the DXF Group Codes of the entity.

Syntax : (entget ename) 

ename - a valid entity name returned by entsel, ssname, entlast, or the entnext function.

Returns an Entity's DXF Group Codes as an association list.

(setq en(car(entsel))) returns entity name as eg. <Entity name: 3b40a60>

(entget en) returns :

((-1 . <Entity name: 3b40a60>) (0 . "LINE") (5 . "FC") (100 . "AcDbEntity") (67 . 0) (8 . "DIM")(100 . "AcDbLine") (10 252.578 68.25 0.0) (11 188.379 31.5 0.0) (210 0.0 0.0 1.0))

entlast - This function returns the name of the last non-deleted entity in the drawing.

Syntax : (entlast) 

This function is mainly used to get the last entity added to the drawing.

Returns an entity name.

(entlast) returns <Entity name: 3b40a60>

entnext - Let's look at AutoDesk's explanation: 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 argumentename, 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 canaccess 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 nameof 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.

Page 9: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 9/16

Syntax : (entnext) or (entnext ename) 

ename - a valid entity name returned by entsel, ssname, entlast, or the entnext function.

(entnext) returns the name of the first entity in the drawing.

(entnext ename) returns the name of the next entity following ename.

entdel - This function deletes an entity from the drawing if it exist. If it does not exist and has been previously

deleted, it will undelete the entity.

Syntax : (entdel ename) 

ename - a valid entity name returned by entsel, ssname, entlast, or the entnext function.

Returns an entity name.

(setq en(car(entsel))) returns the name of selected entity.

(entdel en) deletes the entity and returns the name of the deleted entity.

(entdel en) undeletes the entity and returns the name of the undeleted entity.

entmod - Modifies an entity in the AutoCAD database.

Syntax :(entmod elist)

 

elist - a list that is returned from an entget statement. (DXF Group Codes of an entity)

Returns an elist if successful otherwise it returns nil.

(setq en(car(entsel))) returns the name of selected entity.

(setq elist(entget en)) returns the DXF Group Codes of the entity.

(setq elist(subst (cons 8 "DIM")(assoc 8 elist) elist)) 

The line above Replaces existing layer name in the elist with new layer name "DIM"

(entmod elist) updates the AutoCAD database and replaces the old layer name with the new one.

entupd - Updates the screen image of an entity.

Syntax : (entupd ename) 

Page 10: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 10/16

ename - a valid entity name returned by entsel, ssname, entlast, or the entnext function.

Returns an entity name.

(setq en(car (entsel))) returns an entity name.

(entupd ename) refreshes the entity on the graphics screen.

 Note: If you modify the DXF Group Codes of an entity, such as changing the layer (8 . "LAyerName"), youwill not notice the change until you regenerate the entire drawing or use entupd to regenerate the entity.

open/close - Open a file and close a file. That simple.

Syntax : (open "filename" mode) (close fileVarName) 

Where filename is any valid file or resource name.

Where mode is either "r" , "w" , or  "a".

"r" - Read mode. Reads a file with either read-char or read-line.

"w" - Write mode. Writes to a file with either write-char, write-line, princ, or print.

"a" - Append mode. Appends to an existing file using write-char, write-line, princ, or print.

 Note: If the existing file is not found in append mode, autolisp will create a new empty file.

Open Returns a file descriptor.

 Note: You must use (setq varNam(open "file" mode)) to enable closing of the file.

 Note: Writing the file does not occur until you use the close statement..

Example:

(if (setq f(open "c:/acad/myfile.txt" "r")) 

(progn 

(while (setq txtLine(read-line f)) 

(princ txtLine) 

(close f) 

(princ "\n Error - File was not opened.") 

Page 11: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 11/16

read-line - This function reads a line of text from an open file.

Syntax : (read-line fileVar) 

Where fileVar is a valid variable name that represents an open file descriptor.

Returns a text string.

Assuming (setq f(open "text.txt" "r")) returned sucessfully.

Assuming (setq g(open "text8.txt" "r")) failed.

(read-line f) ;returns string from file

(read-line g) ;returns Error - Bad Argument Type

write-line - This function writes a line of text to an open file.

Syntax : (write-line fileVar) 

Where fileVar is a valid variable name that represents an open file descriptor.

Returns a text string.

Assuming (setq f(open "text.txt" "r")) returned successfully.

Assuming (setq g(open "text8.txt" "r")) failed.

(write-line "Hello World" f) ;writes text to file and returns string "Hello World"

(write-line "Hello World" g) ;returns Error - Bad Argument Type

Entity DXF Group Code Table 

Entity Types: (Group Code 0)

Page 12: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 12/16

LN = Line CI = Circle AR = Arc TR = Trace SD = Solid TX = Text 3DF = 3DFaceAD = AttDef 

PT = Point PL = PolyLine VT = Vertex DM = Dimension AT = Attribute IN = Insert SQ = Seqend

|--------------------- Entity Type (Group Code 0) -----------------------|

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

-1Entity Name

(Primary)-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

-2Entity Name

(Secondary)

-2

1 Primary Text Value 1 1 1 1

2 Name: Shape,

Block, Tag2 2 2 2 2

3 Prompt String 3

5

Handle

(Hexadecimal

String)

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

6 LineType Name 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

7 Text Style Name 7 7 7

8 Layer Name 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

10

X of Start or Insert

Point 10 10 10 10 10 10 10 10

X of Center Point 10 10

X of Corner Point 10 10 10

X of Definition

Point10

X of Elev. Point

(2D Poly)10

Page 13: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 13/16

11

X of End or 

Insertion Point11

X of Corner Point 11 11 11

X of Alignment

Point11 11 11

X of Middle Point

of Dimension11

12

X of Corner Point 12 12 12

X of Insertion Point 12

13

X of Corner Point 13 13 13

X of Definition

Point13

14X of Definition

Point14

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

15X of Definition

Point15

16X of Definition

Point16

20

Y of Start or 

Insertion Point20 20 20 20 20 20 20 20

Y of Center Point 20 20

Y of Corner Point 20 20 20

Y of Definition

Point20

Y of Elev. Point

(2D Poly)20

21

Y of End or Insert

Point21

Y of Corner Point 21 21 21

Y of Alignment

Point21 21 21

Y of Middle Point

of Dimension21

Page 14: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 14/16

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

22

Y of Corner Point 22 22 22

Y of Insert Point 22

23

Y of Corner Point 23 23 23

Y of Definition

Point23

24Y of Definition

Point24

25Y of Definition

Point25

26Y of Definition

Point26

30

Z of Start or Insert

Point30 30 30 30 30 30 30 30

Z of Center Point 30 30

Z of Corner Point 30 30 30

Z of Definition

Point30

Z of Elevation Point

(2D Poly)30

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

31

Z of End Point 31

Z of Corner Point 31 31 31

Z of Alignment

Point31 31 31

Z of Middle Point

of Dimension31

32Z of Corner Point 32 32 32

Z of Insert Point 32

33

Z of Corner Point 33 33 33

Z of Definition

Point33

34 Z of Definition 34

Page 15: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 15/16

Point

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

35Z of Definition

Point35

36Z of Definition

Point36

38 Entity Elevation 38 38 38 38 38 38 38 38 38 38 38 38 38 38

39 Entity Thickness 39 39 39 39 39 39 39 39 39 39 39 39 39 39

40

Radius 40 40

Height, Size, or 

Width40 40 40 40 40

Leader Length 40

41X Scale Factor or 

Width41 41 41 41 41 41

42Y Scale Factor or 

Bulge42 42

43 Z Scale Factor 43

44 Column Spacing 44

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

45 Row Spacing 45

50

Rotation Angle 50 50 50 50 50 50 50

Start Angle 50

Curve Fit Tangent 50

51

End Angle 51

Obliquing Angle 51 51 51 51

Angle fromHorizontal

51

62 Color 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62

66Entities Follow

Flag66 66

70

Dimension Type 70

Vertex or Polyline

Flag70 70

Attribute Flag 70 70

Page 16: Lisp Codes

7/28/2019 Lisp Codes

http://slidepdf.com/reader/full/lisp-codes 16/16

Column Count 70

Invisible Edges

Flag70

CODE  DESCRIPTION LN  PT  CI  AR   TR   SD  TX  SH  IN  AD  AT  PL  VT  DM  3DF  SQ 

71

Text Generation

Flag71 71 71

Row Count 71

Mesh M Vertex

Count71

72

Text Justification 72 72 72

Mesh N Vertex

Count72

73

Field Length 73 73

Smooth Surface M

Density73

74Smooth Surface N

Denstiy74

75Smooth Surface

Type75

210X of Extrusion

Point210 210 210 210 210 210 210 210 210 210 210 210 210

220Y of Extrusion

Point210 210 210 210 210 210 210 210 210 210 210 210 210