Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
George Omura. Lisp programing tutorial for AutoCAD customization / 620.0.The ABC's of AutoLISP - Omura, George.pdf
Скачиваний:
146
Добавлен:
02.05.2014
Размер:
1.55 Mб
Скачать

The ABC’s of AutoLISP by George Omura

(defun C:SEQ (/ pt1 currnt last)

(setq pt1

(getpoint "\nPick start point: "))

(setq spc

(getdist pt1 "\nEnter number spacing: "))

(setq currnt

(getint "\nEnter first number: "))

(setq last

(getint "\nEnter last number: "))

(setq stspc

(rtos spc 2 2))

(setq stspc

(strcat "@" stspc "<0" ))

(command "text" pt1 "" "" currnt) (repeat (- last currnt)

(setq currnt (1+ currnt))

(command "text" stspc "" "" currnt)

)

)

Figure 7.8: The sequential number program

Converting a Number to a String

Exit AutoCAD and open the AutoLISP file seq.lsp. Make the changes shown in bold face type in figure 7.10. Save and exit the seq.lsp file and return to the file chapt7. Load the C:SEQ program and do the following:

1.Enter seq at the command prompt.

2.At the prompt:

Pick start point:

pick a point at coordinate 1,3.

3. At the prompt:

Enter spacing:

enter .5.

4. At the prompt:

Enter first number:

enter 4.

147

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

5. At the last prompt:

Enter last number:

enter 12.

The numbers 4 through 12 will appear beginning at your selected start point and spaced at 0.5 unit intervals.

The program starts by prompting the user to pick a starting point:

(defun C:SEQ (/ pt1 pt2 currnt last)

(setq pt1 (getpoint "\nPick start point: "))

A new prompt is added that obtains the spacing for the numbers:

(setq spc (getdist pt2 "\nEnter number spacing: "))

The spacing is saved as the symbol spc. The program continues by prompting the user to enter starting and ending value:

(setq currnt (getint "\nEnter first number: "))

(setq last (getint "\nEnter last number: "))

Next, the function rtos is used to convert the value of spc to a string:

(setq stspc (rtos spc 2 2))

the syntax for rtos is:

(rtos [real or integer value][unit style code][precision])

The first argument to rtos is the number being converted. It can be a real or integer. The next argument is the unit style code. Table shows a listing of these codes and their meaning.

Code

Format

1

Scientific

2

Decimal

3

Feet and decimal inches

4

Feet and inches

5

Fractional units

148

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

This code determines the style the number will be converted to. For example, if you want a number to be converted to feet and inches, you would use the code 4. The third argument, precision, determines to how many decimal places to convert. In our example, we use the code 2 for unit style and 2 for the number of decimal places to convert to a string.

The next expression combines the converted number with the strings "@" and "<0" to form a string that can be used in with the command function:

(setq stspc (strcat "@" stspc "<0" ))

The next two expressions set up the location of the beginning of the text:

(command "text" pt1 "" "" currnt)

This is done because in the next set of expressions, The string that locates the text, "@distance<0", gives a distance and direction rather than a point. The previous expressions locate a point which will cause the Text command in the next expression to place the text in the proper place:

(repeat (- last currnt)

(setq currnt (1+ currnt))

(command "text" stspc "" "" currnt)

)

)

In the last three expressions, the repeat function is used to issue the text command, enter the number and advance to the next number repeatedly until the last number is in place (see figure 7.9).

Figure 7.9: Writing the numbers into the AutoCAD file

149

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Converting Other Data Types

Before we continue, we should briefly look at several other functions that offer data type conversion. These functions are listed in table .

Function

angtos ascii atoi itoa

chr

Uses

Converts real numbers (radians) into string values. Converts a string into its ASCII character code. Converts a string a string into an integer. Converts an integer to a string.

Converts an integer representing an ASCII character code into a string.

Angtos works in a similar way to rtos. It accepts a unit style code and a precision value. Its syntax is:

(angtos [angle value][unit style code][precision])

All of the other functions listed in table take a single item, the value to be converted, as their argument. For example, to convert an integer into a string, you could use the following expression:

(itoa 55)

The resulting value is "55".

The functions ascii and chr convert ASCII character codes. These are numeric values that represent letters, numbers and symbols. Figure 7.10 shows these codes and their meaning.

150

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Code Meaning

Code Meaning

Code

Meaning

Code Meaning

Code Meaning

Code Meaning

07

Beep

46 .

65

A

84

T

103

g

122

z

09

Tab

47

/

66

B

85

U

104

h

123

{

10

New line

48

0

67

C

86

V

105

i

124

|

13

Return

49

1

68

D

87

W

106

j

125

}

27

Escape

50

2

69

E

88

X

107

k

126

~

32

Space

51

3

70

F

89

Y

108

l

 

 

33

!

52

4

71

G

90

Z

109

m

 

 

34

"

53

5

72

H

91

[

110

n

 

 

35

#

54

6

73

I

92

\

111

o

 

 

36

$

55

7

74

J

93

]

112

p

 

 

37

%

56

8

75

K

94

^

113

q

 

 

38

&

57

9

76

L

95

_

114

r

 

 

39

'

58

:

77

M

96

`

115

s

 

 

40

(

59

;

78

N

97

a

116

t

 

 

41

)

60

<

79

O

98

b

117

u

 

 

42

*

61

=

80

P

99

c

118

v

 

 

43

+

62

>

81

Q

100

d

119

w

 

 

44

,

63

?

82

R

101

e

120

x

 

 

45

-

64

@

83

S

102

f

121

y

 

 

Figure 7.10: The ASCII character codes

151

Copyright © 2001 George Omura,,World rights reserved