好的,我感兴趣的是最后一个函数:逃逸。
我添加了一个带有几个属性块的dwg。
命令是BLOCKS(我喜欢将客户端可访问的函数作为最后一个函数编写)
这会将数据导出到“block_export.txt”,与dwg位于同一文件夹中(因此请不要在未保存的图形上使用此选项)
要求:值中的每个双引号必须在中转义。txt文件,以便将其解析为lisp数据
- (vl-load-com)
- (defun blocks ( / iterations i block attString content_string)
- ;; select blocks (all blocks in the dwg)
- (setq
- blocks (ssget "X" (list
- '(0 . "INSERT")
- ))
- iterations (sslength blocks) ;; number of iterations
- i 0
- block nil
- content_string "" ;; this is the variable containing the content we will save to file
- )
- (repeat iterations ;; loop over blocks
- (setq block (ssname blocks i))
- ;; attributes
- (setq attString "")
- (foreach att (vlax-invoke (vlax-ename->vla-object block) 'GetAttributes)
- (setq attVal (vla-get-TextString att))
- (setq tag (vla-get-TagString att))
- (setq attVal (escape attVal))
- (setq attString (strcat attString "("" tag "" "" attval "") "))
- )
- (setq content_string (strcat content_string "(" attString ")\n" ))
- (setq i (+ 1 i))
- )
- ;; this prints the contents to file. The file is "block_export.txt" , in the same folder as the dwg.
- ;; Notice: save the dwg before using this
- (file_put_contents (strcat (getvar 'dwgprefix) "block_export.txt") content_string)
- )
- ;; http://forums.autodesk.com/t5/visual...e/td-p/3732910
- (defun replace (newstr oldstr str / localstr)
- (setq localstr str)
- (while (vl-string-search oldstr localstr)
- (setq localstr (vl-string-subst newstr oldstr localstr))
- (replace newstr oldstr localstr)
- )
- localstr
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; file IO.
- ;; made to look like the php-functions file_put_contents & file_get_contents
- ;; (Not sure where I found these and how much of it I wrote my self)
- ;; saves a string to file
- (defun file_put_contents (file_name content / result f)
- (setq result T)
- (if (setq f (open file_name "w"))
- (progn
- (write-line content f)
- (close f)
- )
- (progn
- (setq result NIL)
- )
- )
- result
- )
- ;; read file as string
- (defun file_get_contents (file_name / content)
- (setq content "")
- (if(setq fp (open file_name "r")) ; file pointer
- (progn
- (while (setq txtLine(read-line fp))
- (setq content (strcat content "\n" txtLine))
- )
- (close fp)
- content ; return content
- )
- (progn
- NIL ; return false
- )
- )
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (defun _add:slash (st)
- (if (wcmatch st "*"")
- (strcat (substr st 1 (1- (strlen st))) (chr 92) (chr 34))
- )
- st
- )
- (defun escape(str / )
- ;;(replace """ "\\"" str)
- (_add:slash str)
- )
- (defun c:blocks ( / )
- (blocks)
- (princ)
- )
注意:
-函数替换目前未使用。
-file\u get\u内容根本没有使用;但是因为我提供了file\u put\u内容,所以我也可以提供file\u get\u内容。
图纸2.dwg |