根据我的另一个线程,您应该能够从 DCL 文件生成密钥列表。我今天下午开始工作,我很困惑。我收到一个非常特殊的错误:在(读取文件)命令上关闭了流。我的意图是让文件成为一个全局变量 fe,它始终是打开的。
这是相关代码:
这是发生错误的地方:
(defun readline (fe) (setq curline nextline nextline (read-line fe) lineindex (+ lineindex 1)))
让我们看看这个函数在哪里被调用:
(defun find (str fe / entry) ;Returns next instance of string str in a line of the document. (while (and (not(= (getline) nil)) (= (wcmatch (getline) str) nil)) (readline fe) ) (princ (getline)))(defun findnext (str fe / entry) ;Returns next instance of string str in a line of the document. Excludes current line. (readline fe) (while (and (not(= (getline) nil)) (= (wcmatch (getline) str) nil)) (readline fe) ) (princ (getline)))(defun skipto (indexno fe) ;Takes index no > 0, sets pointer to that line of document. (repeat (- indexno (getindex)) (readline fe) ))
如果没有明确的 fe,这些都不应被调用。 我们来看看代码中唯一的地方(close)被调用:
(defun startinstance (/) (if (/= fe nil) (close fe) ) (setq fe (open fileloc "r") nextline (read-line fe) lineindex 0 ) (readline fe) (princ fe))
该文件应关闭,然后立即重新打开。readline 函数仅在明确定义 fe 时调用。
这是完整的 lisp:
(defun find (str fe / entry) ;Returns next instance of string str in a line of the document. (while (and (not(= (getline) nil)) (= (wcmatch (getline) str) nil)) (readline fe) ) (princ (getline)))(defun findnext (str fe / entry) ;Returns next instance of string str in a line of the document. Excludes current line. (readline fe) (while (and (not(= (getline) nil)) (= (wcmatch (getline) str) nil)) (readline fe) ) (princ (getline)))(defun skipto (indexno fe) ;Takes index no > 0, sets pointer to that line of document. (repeat (- indexno (getindex)) (readline fe) ))(defun maintain() (setq prevpov (getindex)))(defun return() (startinstance) (skipto prevpov fe))(defun c:getfile () (setq fileloc (findfile (getfiled "Select a .txt file:" "" "txt" 8))))(defun startinstance (/) (if (/= fe nil) (close fe) ) (setq fe (open fileloc "r") nextline (read-line fe) lineindex 0 ) (readline fe) (princ fe))(defun getline () (princ curline))(defun getnext () (princ nextline))(defun getindex () (princ lineindex))(defun readline (fe) (setq curline nextline nextline (read-line fe) lineindex (+ lineindex 1)))(defun startparse (instance searchlst / toplevel p1) (setq outputlst (list )) (while (/= (getnext) nil) (setq outputlst (append outputlst (list (find (nth 0 searchlst) instance)))) (setq p1 (getindex)) (findnext (nth 0 searchlst) instance) (setq toplevel (getindex)) (setq outputlst (append outputlst (list (recurseparse (startinstance) (cdr searchlst) p1 (getindex) )))) (skipto toplevel instance) ))(defun recurseparse (instance searchlst startbound endbound / storage p1 outputlist) (setq outputlist (list)) (skipto startbound instance) (setq tempval (find (nth 0 searchlst) instance)) (setq outputlist (append outputlist (list tempval))) (if (/= searchlst nil) (if (/= tempval nil) (while (< (getindex) endbound) (setq p1 (getindex)) (findnext (nth 0 searchlst) instance) (setq storage (getindex)) (setq outputlist (append outputlist (list (recurseparse (startinstance) (cdr searchlst) p1 (getindex) )))) (skipto storage instance) ;append to list ) ) ) (princ outputlist))
它仍然是一个WIP,但最终目标是获取以下形式的列表列表(dialog1 (row 1 (key1 key2...) row2 (key1 key2....))dialog2 (row 1 (key1 key2. ..) row2 (key1 key2....))...)。
要运行代码,请在 autocad 中运行 GETFILE,选择您保存为 TXT 文件的 DCL 文件,然后输入:
(startparse (startinstance) (list "*: dialog*" "*key*=*"))
在命令行中。这应该从您选择的 DCL 中将全局变量“outputlst”设置为 (dialog (key...) dialog (key...)...) 形式的列表。 希望有大佬帮帮我啊,谢谢了