Yanjie's Blog

这个博客已通过认证

AutoLISP 开发者指南

📅 | 🏷️ ,
文章目录

引言

使用AutoLISP语言

AutoLISP基础

AutoLISP表达式

(function  
arguments
)
(fun1 (fun2
arguments) (fun3
arguments)
)

示例:

(* 2 27)

54
(* 2 (+ 5 10))

30
AutoLISP 函数语法
(foo string [number ...])

foo: 函数名
string: 必要参数
number: 可选参数

AutoLISP 数据类型

2147483647

2147483647
2147483648

2.14748e+009
(+ 2147483646 3)

-2147483647
(+ 2147483648 2)

2.14748e+009
-2147483647

-2147483647
-2147483648

-2.14748e+009
(- -2147483648 1)

-2.14748e+009
"\nEnter first point:"
(1.0 1.0 0.0)

("this" "that" "the other")

(1 "ONE")
(ssget "X")

<Selection set: 6>
(entlast)

<图元名: -16bbe0>

图元名指的是drawing中仅在当前编辑会话期间有效的对象
(setq file1(open "test.dwg" "r"))

#<file "test.dwg">
------
(close file1)

nil
Notation characters descriptions
( (Open Parenthesis)
) (Close Parenthesis)
. (Period)
' (Apostrophe)
" (Quote Symbol)
; (Semicolon)
(setq str1 "this is a string")

"this is a string"

AutoLISP 程序文件

空格
(setq test1 123 test2 456)

(setq
    test1 123
	test2 456
)

两者结果相同
注释
; This entire line is a comment
(setq area (* pi r r)) ; Compute area of circle
; 行内注释
(setq tmode ;|some note here|; (getvar "tilemode"))
; 多行注释
(setvar "orthomode" 1) ;|comment starts here
and continues to this line,
but ends way down here|; (princ "\nORTHOMODE set On.")
变量
(setq
	variable_name1 value1 [variable_name2 value2 ...]
)
(set val 3 abc 3.875)

3.875
(setq layr "EXTERIOR-WALLS")

"EXTERIOR-WALLS"
!abc

3.875
(setq val nil)

nil

数字处理

(* 2.5 13)

32.5
(/ 12 5)

2
(/ 12.0 5)

2.4

字符串处理

(strcase "This is a TEST.")

"THIS IS A TEST."
(strcase "This is a TEST." T)

"this is a test."
(setq str "BIG")(setq bigstr (strcat "This is a " str " test."))

"This is a BIG test."
(strlen bigstr)

19
(setq filnam "bigfile.txt")

"bigfile.txt"
(setq newlen (- (strlen filnam) 4))

7
------
(substr filnam 1 newlen)

"bigfile"

或者合并为一行代码

(substr filnam 1 (- (strlen filnam) 4))

"bigfile"

基本输出函数

文本输出
prin1
princ
print
prompt
(setq str "The \"allowable\" tolerance is 1\/4")

"The \"allowable\" tolerance is 1/4"

(prompt str)

The "allowable" tolerance is 1/4nil

(princ str)

The "allowable" tolerance is 1/4"The \"allowable\" tolerance is 1/4"

(prin1 str)

"The \"allowable\" tolerance is 1/4""The \"allowable\" tolerance is 1/4"

(print str)

"The \"allowable\" tolerance is 1/4" "The \"allowable\" tolerance is 1/4"
控制字符串中的字母
Code Description
\ \
" "
\e Escape character
\n Newline character
\r Return character
\t Tab character
\nnn 八进制编码nnn的字符
(princ "The \"filename\" is: /ACAD/TEST.TXT.")

The "filename" is: /ACAD/TEST.TXT."The \"filename\" is: /ACAD/TEST.TXT."
(prompt "An example of the \nnewline character.")

An example of the
newline character.nil
通配符匹配
(setq matchme "this is a string - test1 test2 the end")

"this is a string - test1 test2 the end"

(wcmatch matchme "this*")

T

; 如果包含 "test4", "test5", "test6" or "test9",将返回 T
(wcmatch matchme "*test[4-69]*")

nil

(wcmatch matchme "*test[4-61]*")

T

(wcmatch matchme "ABC,XYZ*,*end")

T

等式和条件

列表处理

(setq lst1 (list 1.0 "One" 1))

(1.0 "One" 1)
(nth 1 lst1)

"One"
(cdr lst1)

("One" 1)
; 注意:quote函数(or ')很容易将一个字符串添加进一个列表
(setq lst2 (append lst1 '("One")))

(1.0 "One" 1 "One")
(setq lst3 (cons "Ones" lst2))

("Ones" 1.0 "One" 1 "One")
(setq lst4 (subst "one" "One" lst3))

("Ones" 1.0 "one" 1 "one")
点列表
(list 3.875 1.23)

(3.875 1.23)

(list 88.0 14.77 3.14)
(setq pt1 (list 3.875 1.23))

(3.875 1.23)

(setq pt2 (list 88.0 14.77 3.14))

(88.0 14.77 3.14)

(setq abc 3.45)

3.45

(setq pt3 (list abc 1.23))

(3.45 1.23)
(setq pt1 (quote (4.5 7.5)))

(4.5 7.5)

; 功能同上
(setq pt1 '(4.5 7.5))

(4.5 7.5)
(setq pt '(1.5 3.2 2.0))

(1.5 3.2 2.0)
(setq x_val (car pt))

1.5
(setq y_val (cadr pt))

3.2
(setq z_val (caddr pt))

2.0
; 定义矩形的左下角和右上角坐标
(setq pt1 '(1.0 2.0) pt2 '(3.0 4.0))

(3.0 4.0)

; 定义坐标的左上角坐标
(setq pt3 (list (car pt1) (cadr pt2)))

(1.0 4.0)
(caar x)	; 等同于 (car (car x))

(cdar x)	; 等同于 (cdr (car x))

(cadar x)	; 等同于 (car (cdr (car x)))
点对(dotted pairs)
(setq sublist (cons 'lyr "WALLS"))

(LYR . "WALLS")
(setq wallinfo (list sublist (cons 'len 240.0) (cons 'hgt 96.0)))

((LYR . "WALLS") (LEN . 240.0) (HGT . 96.0))
(assoc 'len wallinfo)

(LEN . 240.0)

(cdr (assoc 'lyr wallinfo))

"WALLS"

(nth 1 wallinfo)

(LEN . 240.0)

(car (nth 1 wallinfo))

LEN

符号和函数处理

defun 定义一个函数
(defun symbol_name (args / local_variables) expressions)
(defun DONE () (prompt "\nbye!"))

DONE
(prompt "The value is 127.")(DONE)

The value is 127.
bye!nil
------
(prompt "The value is 127.")(DONE)(princ)

The value is 127.
bye!
C:XXX 函数
(defun C:HELLO () (princ "Hello world. \n")(princ))

C:HELLO
(defun C:LINE () (princ "Shouldn't you be using PLINE?\n") (command ".LINE")(princ))

C:LINE

(command "undefine" "line")
; 禁止输出重复信息
(defun C:LINE (/ cmdsave) (setq cmdsave (getvar "cmdecho")) (setvar "cmdecho" 0) (princ "Shouldn't you be using PLINE?\n") (command ".LINE") (setvar "cmdecho" cmdsave) (princ))

C:LINE
函数中的局部变量 local variables
; 定义带局部变量的函数
(defun LOCAL (/ aaa bbb)
(setq aaa "A" bbb "B")
(princ (strcat "\naaa has the value " aaa))
(princ (strcat "\nbbb has the value " bbb))
(princ))

LOCAL
(setq aaa 1 bbb 2)

2
(local)

aaa has the value A
bbb has the value B
带参函数
; 注意:这里不能使用 princ 函数抑制程序返回值
(defun ARGTEST (arg1 arg2 / ccc)
(setq ccc "Constant string")
(strcat ccc ", " arg1 ", " arg2))

ARGTEST
(setq newstr (ARGTEST "String 1" "String 2"))

"Constant string, String 1, String 2"

; 程序执行完成,局部变量`ccc`所占用的内存空间就被释放了
!ccc

nil
and
command
cond
defun
defun-q
foreach
function
if
lambda
or
progn
quote
repeat
setq
trace
untrace
vlax-for
while

AutoLISP 中的错误处理

*error* 函数 vl-catch-all-apply 函数

(alert "File not found")

; 将弹出一个警告框

使用AutoLISP与AutoCAD进行交流

访问命令与服务

命令提交

(command "circle" "0,0" "3,3")
(command "thickness" 1)
(setq p1 '(1.0 1.0 3.0))
(setq rad 4.5)
(command "circle" p1 rad)