- 积分
- 1737
- 论坛币
- 1737
- 大富大贵币
- 5
- 性别
- 男
康人师三
普通的我
- 性别
- 男
- 威望
- 3
- 注册时间
- 06-5-3
|
入门学习AutoLISP与VisualLISP的朋友全部进来!
本人刚在论坛上弄了个芝麻官当当,也当为论坛作点贡献!
即日起,教大家学习AutoLISP与VisualLISP,从最基本的入门开始到完成一个完整的程序。程序以步步深入的方式逐渐引入,使学习者能使用AutoLISP开发标准件,标准图符等参数化绘图工具,实现快速绘图的关键一步。
第一步:学习要点(a)程序架构(b)用程序在坐标原点画一个直径为5.1的圆
;;分号开头的文字为说明文字 (defun c:luowen(/ ) ;;defun为标准函数用来定义新函数 (setvar "cmdecho" 0);;关闭命令回显 (command "circle" '(0 0) 2.55);;调用AutoCAD内部命令circle来画圆 (setvar "cmdecho" 1);;打开命令回显 (princ);;静默退出 ) ;;LISP是表语言,程序是由左右括号成对出现
第二步:学习要点(a)局部变量(b)给变量赋值(c)由用户交互输入点(d)换行符号
(defun c:luowen(/ pt);;增加局部变量pt (setvar "cmdecho" 0) (setq pt (getpoint "\n拾取螺纹孔中心点:")) ;;setq是给变量赋值函数,getpoint是交互的由用户输入点函数,\n为换行符号 (command "circle" pt 2.55);;把用户拾取的点作为螺纹孔中心点 (setvar "cmdecho" 1) (princ) )
第三步:增加变量dia,完成螺纹底孔圆的参数化绘制
(defun c:luowen(/ pt dia);;增加局部变量rad (setvar "cmdecho" 0) (setq pt (getpoint "\n拾取螺纹孔中心点:")) (setq dia (getdist "\n输入螺纹公称直径:")) ;;给公称径dia赋值 (command "circle" pt (* 0.425 dia)) ;;把直径除2作为半径输入画圆,不要用除法,除数为0会出错,用乘法代替除法会更好 ;;螺纹孔底孔与牙线的比例0.85,一半是0.425 (setvar "cmdecho" 1) (princ) )
;;第四步:以公称直径的值画出圆弧,作为牙线
(defun c:luowen(/ pt dia osm)
(setvar "cmdecho" 0)
(setq osm (getvar "osmode"));;保存对象捕捉系统变量
(setq pt (getpoint "\n拾取螺纹孔中心点:"))
(setq dia (getdist "\n输入螺纹公称直径:"))
(setvar "osmode" 0);;关闭对象捕捉,防止圆心与圆弧不按输入的点作图
(command "circle" pt (* 0.425 dia))
(command "arc" "c" pt (polar pt (* 1.44 pi) (* 0.5 dia)) "a" 290)
;;用圆心起点与包含角画圆弧,求起点polar用的是弧度,包含角用的是十进制度数
(setvar "osmode" osm)
(setvar "cmdecho" 1)
(princ) )
;;第五步:画十字中心线
(defun c:luowen(/ pt dia osm)
(setvar "cmdecho" 0)
(setq osm (getvar "osmode"))
(setq pt (getpoint "\n拾取螺纹孔中心点:"))
(setq dia (getdist "\n输入螺纹公称直径:"))
(setvar "osmode" 0)
(command "circle" pt (* 0.425 dia))
(command "arc" "c" pt (polar pt (* 1.44 pi) (* 0.5 dia)) "a" 290)
(command "line" (polar pt pi (* 0.6 dia)) (polar pt 0 (* 0.6 dia)) "")
(command "line" (polar pt (* 0.5 pi) (* 0.6 dia)) (polar pt (* 1.5 pi) (* 0.6 dia)) "")
(setvar "osmode" osm)
(setvar "cmdecho" 1)
(princ)
)
;;在图形中新建1、BOU粗实线 2、CON细实线 3、CEN中心线 三个图层
;;第六步:确定线型与线宽
(defun c:luowen(/ pt dia osm lay)
(setvar "cmdecho" 0)
(setq osm (getvar "osmode"))
(setq lay (getvar "clayer"))
(setq pt (getpoint "\n拾取螺纹孔中心点:"))
(setq dia (getdist "\n输入螺纹公称直径:"))
(setvar "osmode" 0)
(setvar "clayer" "BOU");切换到粗实线图层
(command "circle" pt (* 0.425 dia))
(setvar "clayer" "CON");切换到细实线图层
(command "arc" "c" pt (polar pt (* 1.44 pi) (* 0.5 dia)) "a" 290)
(setvar "clayer" "CEN");切换到中心线图层
(command "line" (polar pt pi (* 0.6 dia)) (polar pt 0 (* 0.6 dia)) "")
(command "line" (polar pt (* 0.5 pi) (* 0.6 dia)) (polar pt (* 1.5 pi) (* 0.6 dia)) "")
(setvar "osmode" osm)
(setvar "clayer" lay)
(setvar "cmdecho" 1)
(princ)
)
;;第七步:根据圆的大小调整中心线的线型比例以让点划线相交在“划”上
(defun c:luowen(/ pt dia osm lay clt)
(setvar "cmdecho" 0)
(setq osm (getvar "osmode"))
(setq lay (getvar "clayer"))
(setq clt (getvar "celtscale"))
(setq pt (getpoint "\n拾取螺纹孔中心点:"))
(setq dia (getdist "\n输入螺纹公称直径:"))
(setvar "osmode" 0)
(setvar "clayer" "BOU");切换到粗实线图层
(command "circle" pt (* 0.425 dia))
(setvar "clayer" "CON");切换到细实线图层
(command "arc" "c" pt (polar pt (* 1.44 pi) (* 0.5 dia)) "a" 290)
(setvar "clayer" "CEN");切换到中心线图层
(setvar "celtscale" (* dia 0.5 0.1));线型比例等于半径的十分之一
(command "line" (polar pt pi (* 0.6 dia)) (polar pt 0 (* 0.6 dia)) "")
(command "line" (polar pt (* 0.5 pi) (* 0.6 dia)) (polar pt (* 1.5 pi) (* 0.6 dia)) "")
(setvar "osmode" osm)
(setvar "clayer" lay)
(setvar "celtscale" clt)
(setvar "cmdecho" 1)
(princ)
) |
-
|