shh_188 发表于 2014-6-16 21:53 
是的,我想要的结果,就是你说描述的
只是说,因为完全没有多文档这方面的经验,再加上今天在论坛里也没 ...
 - (defun getcommandname (file / acadapp documents
- actdoc mnlfile tmpfile tmpf
- value str1 abcDoc
- )
- (vl-load-com)
- (setq acadapp (vlax-get-acad-object)
- documents (vla-get-documents acadapp)
- actdoc (vla-get-activedocument acadapp)
- )
- ;;寻找自动加载的"acad.mnl"文件,并在同目录下创建"tmpfile.lsp"文件
- (setq mnlfile (findfile "acad.mnl")
- tmpfile (strcat (vl-filename-directory mnlfile) "\\tmpfile.lsp")
- tmpf (open tmpfile "w")
- )
- ;;在tmpfile.lsp"文件里写入 findfunc 函数定义
- (princ (vl-prin1-to-string
- '(defun findfunc (file / lst1 lst2 lst3)
- (mapcar
- '(lambda (x)
- (if (or (wcmatch x "C:*")
- (wcmatch x "c:*")
- )
- (setq lst1 (cons x lst1))
- )
- )
- (atoms-family 1)
- )
- (load file (princ))
- (mapcar
- '(lambda (x)
- (if (or (wcmatch x "C:*")
- (wcmatch x "c:*")
- )
- (setq lst2 (cons x lst2))
- )
- )
- (atoms-family 1)
- )
- (mapcar
- '(lambda (x)
- (if (not (member x lst1))
- (setq lst3 (cons x lst3))
- )
- )
- lst2
- )
- (vl-bb-set 'a lst3)
- ) ;_ 结束defun
- ) ;_ 结束 vl-princ1-to-string
- tmpf
- ) ;_ 结束 princ
- ;;在文件的后面添加两行代码
- ;;(findfunc file) ;_ 执行自定义函数
- ;;(vl-load-com) ;_ 在第二个文档的名称空间里加载ActiveX支持[?这一步不知道是否需要]
- ;;(vla-Activate actDoc) ;_ 激活前一个文档,把控制权交还给它
- (princ (strcat "\n(findfunc " (vl-prin1-to-string file) " )")
- tmpf
- )
- (princ "\n(vl-load-com)" tmpf)
- (princ "\n(vla-Activate actdoc)" tmpf)
- ;;关闭"tmpfile.lsp"文件
- (close tmpf)
- ;;从头到尾依次读取指定文件的每一行,
- ;;如果文件存在指定的字符串,就返回这
- ;;条字符串,并停止搜索,否则返回nil。
- (defun readeveryline (fd string / str)
- (if (setq str (read-line fd))
- (if (/= string str)
- (readeveryline fd string)
- str
- )
- )
- )
- ;;打开"acad.mnl"文件,依次读取每一行
- ;;寻找是不是存在(load tmpfile (princ))这一行代码
- ;;若不存在,就在文件里添加
- (setq tmpf (open mnlfile "r")
- str1 (strcat "(load " (vl-prin1-to-string tmpfile) " (princ))")
- )
- (if (readeveryline tmpf str1)
- (close tmpf)
- (progn (close tmpf)
- (setq tmpf (open mnlfile "a"))
- (princ (strcat "\n" str1) tmpf)
- (close tmpf)
- )
- )
- ;;新建一个文档,同时,把控制权交给它
- (setq abcDoc (vla-add documents ""))
- ;;第二个文档执行完所有的代码后,自动把控制权交还给第一个文档
- ;;第一个文档得到控制权后,就关闭第二个文档
- (vla-close abcDoc)
- ;;从黑板读取变量值
- (setq value (vl-bb-ref 'a))
- ;;删除黑板的变量
- (vl-bb-set 'a nil)
- ;;删除文件
- (vl-file-delete tmpfile)
- value
- )
|