一.AutoLISP的程序结构
1.程序结构 (函数 参数 ……) 如:(setq x 25.0) (setq y 12.2) ( + (* x y) x )
AutoLISP程序由一个或一系列按顺序排列的标准表组成。 2. AutoLISP书写格式的特点:
(1)左右括号匹配; (2)从左到右读程序;
(3)函数放在第一个元素的位置; (4)一表可占多行,一行可写多表; (5)用分号“;”作注释;
二.AutoLISP的变量与类型
1.整形常量Int
整数由数字组成,不包含小数点。AutoLISP 的整数是 32 位带符号的数,取值范围从 +2,147,483,7 到 -2,147,483,8 2.实数常量Real
实数是带有小数点的数。以双精度浮点格式存储的,可以提供至少 14 位精度。可以用科学记数法表示。如数字 3.1、0.23、-56.123 和 21,000,000.0 都是有效的 AutoLISP 实数
3.字符串常量 Str
字符串是在双引号中的一组字符。如“string 1”和“\\nEnter first point:”都是有效的字符串。 4. 表
ListAutoLISP 的表是在括号中以空格分隔的一组相关值。表提供了一种存储大量相关值的有效方法。AutoCAD 将三维点表示为三个实数组成的表。
(1.0 1.0 0.0)、(\"this\" \"that\" \"the other\") 和 (1 \"ONE\") 都是有效的表 5. 其它
Sym(符号和变量),Select(选择集),Entity(实体名),filepointer(文件描述符),VLA对像(Visual LISP ActiveX)。
三.数值函数
1.计算函数
(1)+、-、*、/
AutoLISP 中,所有运算符号均采用前缀方式。一般格式为: (运算符 数字 数字……)。 (+ a b);(- a b);(* a b);(/ a b); 如:(* a b c)即为a*b*c
(+ a (- b (* c d)))即为a+(b-c*d) command:(+ 4.7 2) 6.7 command:(/ 8 5) 1 command:(* 2 5 6) 60
(2)最大数,最小数
(max num1 num2 ……) (min num1 num2 ……) 如:command:(max 4.7 2) 4.7
command:(min 3.2 -1) -1
(3)平方根,绝对值
平方根:(sqrt num) 绝对值:(abs num) 如:command:(sqrt 4) 2 command:(abs -1) 1
(4)三角函数
反正切(atan a)或(atan a1 a2) 正弦 (sin angle); 余弦 (cos angle); 注:angle用弧度制
如:command:(atan 1.5) 0.982794
a1
a2
a=a1/a2
command:(atan 3.0 2.0) 0.982794 command:(sin 1) 0.841471 command:(cos pi) 1.0
2.逻辑运算
(and a b );(or a b ); (not a );-----其中a,b为逻辑表达式。 如:command:(and nil 10) nil command: (or nil 1) T command: (not 10) nil
3.关系运算
(< a b );(/ = a b );(> = a b ) -----条件为真返回T,否则返回nil 如:command:(< 3 5) T command: (< 3 5 4 2) nil command: (< 2 3 4 5) T
四.几何实用函数
1.angle函数
(angle pt1 pt2)
说明:pt1、pt2为两点的坐标,计算这两点连线与x轴的夹角(按逆时针计算),返回弧度值。
如:command:( angle ’(0 0) ’ (100 100)) 0.785398 2.distance函数
(distance pt1 pt2)
如:command:(distance ’(0 0) ’(100 0)) 100 3.polar函数
(polar pt angle distance) 如:command:(polar '(0 0) 0.78 100)) (71.0914 70.3279)
pt distance angle 五.表处理函数
1.表长函数
(length list)
如:command:(length ’(a b c)) 3 command: (length ’(a (b c))) 2 2.表颠倒函数
(reverse list)
如:command:(reverse ’( 3 5)) (5 3) command: (reverse ’(3 5 4 2)) (2 4 5 3) 3.返回表中元素
(cad list) (cdr list) (cadr list) 如:command:(cad ’(a b c)) a command: (cdr ’(a b c)) b command: (cadr ’(a b c)) c command: (cdr ’(a (b c))) (b c)
六.赋值函数
(setq sym1 expr1 sym2 expr2 sym3 expr3 ……) 如:command:(setq d ’(a b c)) (A B C) command: (setq a 10 b 12.5 c 23) 23 command: !a 10 command: !c 23
七.用户输入函数
1.getint、getreal、getstring函数
(get××× 如:command:(setq d (getint “Enter a number:\\n”)) Enter a number:12 12 command:(setq s (getstring “Enter a string:”)) Enter a string:autolisp autolisp 2.getpoint函数 (getpoint 如:command:(setq d (getpoint “Enter a point:”)) Enter a number:鼠标点取 (100,100) 八.command函数 (comment 如:(command \"circle\" '(200 200) 20) (command \"circle\" \"2p\" '(100 100) '(80 150)) 说明: 1.本函数用于执行一条AutoCAD命令,即向AutoCAD的“command:”提示直接发 送AutoCAD命令; 2.变元 须与命令对应的参数需求相适应,且顺序一致; 3.空字符串(“ ”)等效于回车; 4.调用AutoCAD内部的参数要加“ ”,自定义的参数不加“ ”; 5.command 函数中需要的参数必需执行在command函数前定义,command函数中不能使用get×××等函数。 实例: command:(setq p1 (getpoint)) (300.572 245.05 0.0) command:(setq r1 (getint)) command:(command \"circle\" p1 r1) command:(setq a (entlast)) command:(command \"hatch\" \"u\" 45 10 \"\" a \"\") 50 九. 函数的定义 (defun sym(全局变量/局部变量)) 如:(defun plus (/ ans) (setq ans (+ 2 3));令ans=2+3 ) 运行:加载→输入“(plus)”或在AutoCAD命令中输入“(plus)” ?问题:plus不是AutoCAD的内部函数,如何使得plus函数数能像AutoCAD内部函数那样被调用? (defun c: sym (全局变量/局部变量)) 如:(defun c:plus (/ ans) (setq ans (+ 2 3)) ;令ans=2+3 ) 运行:加载→输入“(c:plus)”或在AutoCAD命令中输入“(c:plus)或“plus” 十.实例1 (defun c:ring() (setq r1 2.5 r2 4.0 r3 5.5 r 1.0) (setq pc (getpoint\"\\n输入圆心\")) (command \"circle\" pc r1 \"circle\" pc r2 \"circle\" pc r3) (command \"circle\" (polar pc 0 r2) r) (command \"array\" \"L\" \"\" \"p\" pc 8 360 \"N\") ) 实例2 (defun c:akey (/ d h w bp ag p1 p2) (setq d (/ (getint \"\\n d=?\" ) 2.0) h (getint \"\\n h=?\") w ( / (getint \"\\n w=?\" ) 2.0) bp (getpoint\"\\n center=?\") ag (atan ( / w (sqrt(- (* d d) (* w w))))) ) (command\"pline\" (setq p1 (polar bp (- ag) d)) (setq p2 (list(+(car bp)(- d h)) (cadr p1))) (polar p2 ( / pi 2.0)(* w 2)) (polar bp ag d) \"a\" \"ce\" bp p1 \"\" ) (setq ss (entlast)) (command \"hatch\" \"u\" 45 10 \"\" ss \"\") ) 第二部分 AutoCAD菜单开发 一.菜单文件 MNU 原始菜单文件,文本格式。 MNS 加载.MNU后产生,文本格式。 MNC 将.MNS编译为二进制格式。 MNR 二进制文件,包含由菜单使用的位图。 MNL 与菜单配套的 AutoLISP 程序。这些文件包含由菜单文 式件使用的 AutoLISP 表达式。当加载同名的菜单文件 时,这些文件也载入内存。 二.菜单文件结构 1.段标题 常用段标题 菜单区域 ***POPN 下拉/快捷菜单区域 ***TOOLBARS ***IMAGE 工具栏定义 图像控件菜单区域 屏幕菜单区域 ***SCREEN ***MENUGROUP 菜单文件组名 2.菜单项 菜单项由下列元素组成:(名称标记)、标签和菜单宏。 3.菜单宏 ^C^C 可确保不存在尚未完成的命令。这与在键盘上连续按两次 ESC 一样。并 从 DIM 命令返回命令提示。 说明:(1)注释用 //; (2)程序最后要回车。 三.下拉菜单 1.段标题 ***popN (N:1~499) 2.子菜单标题 **+子菜单名(可省略) 3.菜单项 [菜单项名]—下拉菜单总标题 [菜单项1] [菜单项2] 4.菜单宏 [菜单项n]^c^c(load “源程序文件名”)(函数名) 例: ***pop1 [关键零件] [法兰]^c^c((load \"ring.lsp\")(c:ring)) [键槽] ^c^c(load \"key.lsp\")(c:akey) 四.图像菜单 1.菜单组名 menugroup=菜单组名 2.下拉菜单段 ***popN [菜单项名]——下拉菜单总标题 [图像菜单项n]$I=菜单组名.图像菜单子菜单名 $I=* (其中,第一个$I为查找图像菜单。第二个$I为了显示当前加载的图像菜单。) 3.图像菜单项 ***image **+图像菜单子菜单名(不可省略) [图像菜单标题] [幻灯片1,菜单项1] ^c^c(load “源程序文件名”)(函数名) [幻灯片2,菜单项2] ^c^c(load “源程序文件名”)(函数名) 例: ***menugroup=part ***pop1 [关键零件] [主要零配件]$I=part.image_part $I=* ***image **image_part [主要零配件] [ring,法兰]^c^c(load \"ring.lsp\")(c:ring) [key,键槽]^c^c(load \"key.lsp\")(c:akey) 第三部分 AutoCAD对话框设计 一.DCL基本语言 在VisualLISP中,对话框的格式以对话框控制语言(DCL)定义在.dcl文件中。 1.语法格式 对话框名称:dialog {label=标题名; {:组件1 key=value1; label=value2; attribute=value;} {:组件2 key=value1; label=value2; attribute2=value2;} ok_cancel} 2.说明 (1)属性定义语句后用“;”结尾; (2)注释符用“//”; (3)对话框显示预览:VisualLISP编辑器→工具→界面工具→预览编辑器中的DCL。 3.实例 dcl:dialog {label=\"我的对话框\"; ok_cancel; } 二.对话框常用组件的定义 1.编辑框(edit_box) radia:dialog {label=\"半径值\"; :edit_box {label=\"请输入圆的半径r1\"; key=\"a1\";} :edit_box {label=\"请输入圆的半径r2\"; key=\"a2\";} ok_cancel; } 2.图像框(image) ring:dialog {label=\"法兰幻灯片\"; :image {key=\"ringslide\"; height=20; width=50; color=-3;} ok_cancel; } } 3.下拉列表框(popup_list) season:dialog {label=\"季节\"; :popup_list {key=\"season\"; label=\"一年四季\"; list=\"春\\n夏\\n秋\\n冬\";} ok_cancel; } 4.列表框(list_box) season:dialog {label=\"季节\"; :list_box {key=\"season\"; label=\"一年四季\"; list=\"春\\n夏\\n秋\\n冬\";} ok_cancel; } 5.对话框区域定义 boxed_column(列);boxed_row(行) :boxed_row {label=\"第一组半径值\"; :edit_box {label=\"r1\"; key=\"a1\";} :edit_box {label=\"r2\"; key=\"a2\";} } :boxed_column {label=\"第二组半径值\"; :edit_box {label=\"r3\"; key=\"a3\";} :edit_box {label=\"r \"; key=\"a\";} } ok_cancel; } 三.对话框管理 1.调用 load_dialog 来加载 DCL 文件 格式: (load_dialog dclfile) 为便于后面的管理,给对话框文件一个文件识别码,则格式改为: (setq dcl_id (load_dialog dclfile)) 注:dcl_id文件识别码 2.调用 new_dialog 来显示特定的对话框。 格式:(new_dialog dlgname dcl_id) 如须检查 new_dialog 的返回值,则格式改为: (if (not (new_dialog \"dlgname\" dcl_id))(exit)) 3.通过设置控件值、列表和图像来初始化对话框。 (1)action_tile函数 格式:(action_tile key action-expression) 例:(action_tile “edit1” “(setq a1 $value)\") $value—引用控件的当前值 (2)图像框处理函数 a.获取图像框尺寸 (dimx_tile key) 如:(setq width (dimx_tile key) (dimy_tile key) 如:(setq heigth (dimy_tile key) b.激活图像框 (start_image key) c.显示幻灯片图像 (slide_image x y width heigth sldname) x,y—图像与图像框左上角偏移量 (3)done_dialog函数,用于结束对话框 格式:(done_dialog [status]) status 默认值:1,表示用户按下“ok”按钮; 0,表示用户按下“cancel”按钮; 4. 调用 start_dialog 将控制权转交给对话框,以便用户进行输入 格式:(start_dialog) 5.调用 unload_dialog 来卸载该 DCL 文件。 格式:(unload_dialog dcl_id) 第四部分 AutoCAD二次开发综合实例 (defun c:ring() (setq dcl_id(load_dialog \"ring.dcl\")) (if (not (new_dialog \"ring\"dcl_id)) (exit)) (setq x (dimx_tile \"ring_image\")) (setq y (dimy_tile \"ring_image\")) (start_image \"ring_image\") (slide_image 0 0 x y \"ring\") (end_image) (action_tile \"a1\" \"(setq r1 $value)\") (action_tile \"a2\" \"(setq r2 $value)\") (action_tile \"a3\" \"(setq r3 $value)\") (action_tile \"a\" \"(setq r $value)\") (action_tile \"accept\" \"(done_dialog)\") (start_dialog) (unload_dialog dcl_id) (setq pc (getpoint\"\\n输入圆心\")) (command \"circle\" pc (atoi r1) \"circle\" pc (atoi r2) \"circle\" pc (atoi r3)) (command \"circle\" (polar pc 0 (atoi r2))(atoi r)) (command \"array\" \"L\" \"\" \"p\" pc 8 360 \"N\") ) 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务