目录
一、文件操作
1.1 选择文件
- 代码示例
;getfiled函数:对话框的名称,默认文件夹路径,过滤后缀名,flag(1是创建、4是选择) (getfiled "" "" "" 1) (getfiled "" "" "" 4) (setq filepath (getfiled "选择文件" "C:\\Users\\Administrator\\Desktop\\" "dwg" 4))返回值:仅返回绝对路径的字符串,没做其他任何操作,需配合open函数使用
- 对话框效果



1.1 写文件
- 代码示例
(defun c:writefile() ;getfiled函数:对话框的名称,默认文件夹路径,过滤后缀名,flag(1是创建、4是选择) ;getfiled返回的是文件的绝对路径,是字符串 (setq filepath (getfiled "选择要预览的对话框所在的文件" "C:\\Users\\" "txt" 4)) ;返回文件的操作符 ;w:有则覆盖,没有则新建。a:有则追加,无则新建 (setq file (open filepath "w")) (write-line "0,0" file) (write-line "100,0" file) (close file) ) - 对话框选择

1.2 读文件
- 代码示例
(defun c:readfile() ;getfiled返回的是文件的绝对路径,是字符串 (setq filepath (getfiled "选择要预览的对话框所在的文件" "C:\\Users\\" "txt" 2)) ;返回文件的操作符 (setq file (open filepath "r")) ;操作文件操作符 (setq fdata (read-line file)) ;注意:这里演示可以把一个命令拆分成几块 (command "pline") (while fdata (command fdata) (setq fdata (rade-line file)) ) (command "") (close file) (print) ) - 对话框选择

1.3 查找文件、目录
1.3.1 函数说明
- 在搜索目录下检索并返回文件或目录全路径
; 查找已有系统级文件 命令: (findfile "dim.shx") "C:\\program files\\autodesk\\autocad 2014\\fonts\\dim.shx" ; 查找已有系统级目录 命令: (findfile "fonts") "C:\\Program Files\\Autodesk\\AutoCAD 2014\\fonts" ; 查找自己新建的配置或中间临时文件 命令: (findfile "配置文件.ini") "C:\\tangent\\tarcht20v6\\sys\\配置文件.ini"搜索目录:
(getenv "ACAD"),命令可查看,
功能:返回值可用于open函数调用
1.3.2 应用举例
- 代码示例
(defun C:demo () (setq path (getfiled "new" "1" "txt" 1)) ; 存在则追加,不存在则创建 (if (findfile path) (setq file (open path "a")) (setq file (open path "w")) ) (write-line "123" file) (close file) (princ) )
1.4 删除文件
- 代码示例
(defun c:demo () (setq filepath (getfiled "选择文件" "C:\\Users\\Administrator\\Desktop\\" "txt" 4 ) ) ; 删除成功返回T,否则返回nil,如只读,打开 (setq res (vl-file-delete filepath)) (princ) )
1.5 与Excel文件交互
-
代码示例
(defun c:demo () (setq str_list nil) ; 选择文件 (if (setq filepath (getfiled "选择文件" "C:\\Users\\Administrator\\Desktop\\" "csv" 4 ) ) (progn (setq file (open filepath "r")) ; 循环读取每一行并存储在表中 (while (setq str (read-line file)) (setq str_list (append str_list (list str))) ) (print str_list) ) (print "未选择文件") ) (princ) ) -
准备被读取文件:工作簿1.csv

-
csv文件用记事本打开

csv文件:即逗号分隔值(Comma-Separated Values)文件,是一种常见的数据交换格式,它以纯文本形式存储表格数据
-
输出
( "序号,项目,坐标,备注" "1,纽约帝国大厦,\"(1000,1000,0)\",炸了" "2,华盛顿白宫,\"(2000,2000,0)\",轰了" )
二、对话框DCL
- DCL:Dialog Control Language,对话框控制语言,后缀名dcl,定义对话框样式布局。
- 重点:一定别忘了
分号,一定要用英文标点
2.1 初识对话框
- 显示效果
按确认:会在命令行输出“确认”,temp.lsp中定义
- temp.dcl
// 冒号后面为控件名字 // 对话框的名字:demo1_dcl demo1_dcl:dialog{ // 中括号内为属性 label="对话框名"; // 中括号内也可以包含空间 :text{ // 控件中的属性 label="对话框内容"; } // cad封装好的控件 ok_cancel; }
- temp.lsp(完全版,可以作为模版)
(defun C:demo1 () ;选择dcl对话框文件:一个对话框文件中可以包含多个对话框 (setq dlg_file (getfiled "选择要预览的对话框所在的文件" "C:\\Users\\" "dcl" 2)) (if (= dlg_file nil) (exit)) ;选择对话框文件中的demo1_dcl命名的对话框:有此提示时候输入demo1_dcl (setq dlg_name (getstring "\n输入对话框名称:")) (if (= dlg_name "") (exit)) ;加载dcl对话框文件:返回加载标识符,如果只有文件名,则搜索默认路径,见代码下注释 (setq dlg_id (load_dialog dlg_file)) ;加载失败退出 (if (< dlg_id 0) (exit)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;用于保存对话框操作状态 (setq std 0) ;创建对话框对象,此时还没显示对话框,可以向对话框中添加东西 (if (not (new_dialog dlg_name dlg_id))(exit)) ;此处有两个key值:accept,cancel,是系统封装的确定取消按钮控件的key名 ;确认是accept,取消是cancel ;done_dialog函数:结束当前对话框并指定一个结束码, ;通常用来判断用户点击了哪个控件 (action_tile "accept" "(done_dialog 1)") (action_tile "cancel" "(done_dialog 0)") ;启动对话框:开始接受使用者输入,对话框获得控制权 (setq std (start_dialog)) ;卸载对话框:控制权还给cad主程序 (unload_dialog dlg_id) ;根据std做相应动作 (

本文详细介绍了在AutoCAD环境中,如何通过Lisp编程实现文件操作(读写文件)、对话框的设计(包括DCL语法、常用控件如复选框、列表框、下拉框和文字输入框),以及对话框按钮的拾取功能和图片的加载,包括幻灯片的制作。
最低0.47元/天 解锁文章
889

被折叠的 条评论
为什么被折叠?



