说明.txt

CAD中导入导出制表符文件

命令:it - 导入制表符表格,excel表格另存为制表符格式文件即可使用此命令
命令:ot - 导出制表符表格
备注:此程序包含config.ini配置文件,使用前把配置文件拷贝到当前CAD文件所在目录

配置文件说明:

;导入文件设置
[input]
fileDir=./ - 要导入文件所在目录
colWidth=140,320,1200,140,140,305.52 - 表格各列宽度,用逗号分开各宽度
rowHeight=95.17 - 表格的行高
fontHeight=30 - 文本的高度
left=40 - 文本左边空白
bottom=32 - 文本底部空白

;导出文件设置
[output]
fileDir=./ - 要导出文件的保存目录
colWidth=140,320,1200,140,140,305.52 - 表格各列宽度,用逗号分开各宽度
rowHeight=95.17 - 表格的行高
rowCount=30 - 表格的行数

cad加载自定义命令方法:

输入命令ap->回车->添加->选择命令文件->选择刚添加的命令文件->加载->确定

作者:xenice
日期:2014-10-28

配置文件config.ini

[input]
fileDir=./
colWidth=140,320,1200,140,140,305.52
rowHeight=95.17
fontHeight=30
left=40
bottom=32

[output]
fileDir=./
colWidth=140,320,1200,140,140,305.52
rowHeight=95.17
rowCount=30

源码文件it_ot.lsp

;载入文本,返回行列表
(defun loadTxt(strPath / filePath fp result row)
   (setq filePath (getfiled "选择数据文件" strPath "txt" 0));显示选择文件窗口并得到文件路径
   (setq fp (open filePath "r"));以只读方式打开文件
   (setq result nil)
   (while (setq row (read-line fp))
       (setq result (cons row result))
   )
   (close fp) ;关闭文件
   (setq result (reverse result))
)


;保存文本
(defun saveTxt(fileDir rows / filePath fp)
   (setq filePath (getfiled "文件存为" fileDir "txt" 1));显示保存文件窗口并得到文件路径
   (setq fp (open filePath "w"));以写读方式打开文件
   (while (car rows)
        (write-line (car rows) fp)
        (setq rows (cdr rows))
   )
   (close fp) ;关闭文件
)


;按分割符将字符串拆分成列表
(defun split(str dim / start pos field result)
    (setq start 0)
    (setq result nil)
    (while (setq pos (vl-string-search dim str start))
        (setq field (substr str (+ start 1) (- pos start)))
        (setq result (cons field result))
        (setq start (+ pos 1))
    )
    (setq field (substr str (+ start 1)))
    (setq result (cons field result))
    (setq result (reverse result))
)

;获取配置信息
(defun getConfig(node / fp result row lineList start)
    (setq fp (open "config.ini" "r"));只读方式打开文件
    (if (= fp nil) (out "使用前请把配置文件config.ini拷贝到CAD文件所在目录\n"))
    (setq start nil)
    (setq result nil)
    (while (setq row (read-line fp))
        (if (= row (strcat "[" node "]"))
            (setq start T)
        )
        (if start
            (cond
                ((and (= "[" (substr row 1 1)) (/= row (strcat "[" node "]")))
                    (setq start nil)
                )
                ((and (/= "[" (substr row 1 1)) (/= row (strcat "[" node "]")) (/= "" row))
                    (progn
                    (setq lineList (split row "="))
                    (setq result (cons lineList result))
                    )
                )
            )
        )
    )
    (close fp) ;关闭文件
    (setq result (reverse result))
)


;中止程序并输出错误信息
(defun out(str / *error*)
    (defun *error* (msg)
        (princ "错误: ")
        (princ str)      ;_ 打印错误信息
        (princ)         
    )
    (exit)
)



;将列表中的字符串元素转为实数元素
(defun list_atof(oldList / e newList)
    (setq newList nil)
    (foreach e oldList (setq newList (cons (atof e) newList)))
    (setq newList (reverse newList))
)


;去除字符串首尾引号
(defun delq(str / start end len result)
    (if (/= (strlen str) 0)
        (progn
            (setq len (strlen str))
            (setq start (substr str 1 1))
            (setq end (substr str len 1))
            (if (and (=  start "\"") (= end "\""))
                (setq result (substr str 2 (- len 2)))
                (setq result str)
            )
        )
        (setq result str)
    )
)

;导入表格(命令)
(defun c:it()
    (inputTable)
)


;导入表格
(defun inputTable(/ config fileDir colWidths colCount colCount1 rowHeight rowCount fontHeight lineList point x0 y0)
    ;获取配置文件信息
    (setq config (getConfig "input"))
    (setq fileDir (cadr (assoc "fileDir" config))) ;获取要导入文件所在目录
    (setq colWidths (list_atof (split (cadr (assoc "colWidth" config)) ","))) ;获取各列宽列表
    (setq colCount (length colWidths)) ;获得列数量
    (setq rowHeight (atof (cadr (assoc "rowHeight" config)))) ;获取行高
    (setq fontHeight (atof (cadr (assoc "fontHeight" config)))) ;获取文本高度
    (setq left (atof (cadr (assoc "left" config)))) ;获取文本左边空白距离
    (setq bottom (atof (cadr (assoc "bottom" config)))) ;获取文本底部空白距离
    
    ;获取要导入的文件信息
    (setq lineList (loadTxt fileDir))
    (setq rowCount (length lineList))
    (setq colCount1 (length (split (car lineList) "\t"))) ;获取列数量
    (if (/= colCount colCount1) 
        (out "列数量配置不一致,请修改配置文件colWidth的值\n")
    )
    
    ;开始导入
    (setq point (getpoint "选择基点\n")) ;表的基点坐标
    (paintTable point colWidths rowHeight rowCount) ;绘制表格
    (setq x0 (+ left (car point))) ;第一行文本x坐标
    (setq y0 (+ bottom (cadr point))) ;第一行文本y坐标
    (writeContent lineList (list x0 y0) colWidths rowHeight fontHeight) ;写入内容
)


;绘制表格
(defun paintTable(point colWidths rowHeight rowCount / x0 y0 x y count height colWidth)
    (setq x0 (car point))
    (setq y0 (cadr point))
    (setq height (* rowHeight rowCount))
    (setq y (+ y0 height))
    
    ;绘制垂直线
    (command "line" point (list x0 y) "")
    (setq x x0)
    (while (setq colWidth (car colWidths))
        (setq x (+ x colWidth))
        (command "line" (list x y0) (list x y) "")
        (setq colWidths (cdr colWidths))
    )
    
    ;绘制水平线
    (command "line" point (list x y0) "")
    (setq y y0)
    (setq count rowCount)
    (while (> count 0)
        (setq y (+ y rowHeight))
        (command "line" (list x0 y) (list x y) "")
        (setq count (- count 1))
    )
)


;写入内容
(defun writeContent(lineList point colWidths rowHeight fontHeight / x0 y0 x y line fields index)
    (setq x0 (car point))
    (setq y0 (cadr point))
    (setq x x0)
    (setq y y0)
    (while (setq line (car lineList))
        (setq fields (split line "\t"))
        (setq index 0)
        (while (setq field (car fields))
            (command "text" (list x y  0) fontHeight 0 (delq field))
            (setq x (+ x (nth index colWidths))) ;指向下个字段
            (setq index (+ index 1))
            (setq fields (cdr fields))
        )
        (setq x x0)
        (setq y (+ y rowHeight))
        (setq lineList (cdr lineList))
    )
)

;导出表格(命令)
(defun c:ot()
    (outputTable)
)


;导出表格
(defun outputTable(/ config fileDir colWidths rowHeight rowCount point x0 y0 rows)
    ;获取配置文件信息
    (setq config (getConfig "output"))
    (setq fileDir (cadr (assoc "fileDir" config))) ;获取要导出文件所在目录
    (setq colWidths (list_atof (split (cadr (assoc "colWidth" config)) ","))) ;获取各列宽列表
    (setq rowHeight (atof (cadr (assoc "rowHeight" config)))) ;获取行高
    (setq rowCount (atof (cadr (assoc "rowCount" config)))) ;获取行高
    
    ;获得要导出的信息
    (setq point (getpoint "获得表格基点\n"))
    (setq x0 (car point))
    (setq y0 (cadr point))
    (setq rows (getRowStrs x0 y0 colWidths rowHeight rowCount))
    
    ;保存信息
    (saveTxt fileDir rows)
    
)


;获得表格行,行为字符串
(defun getRowStrs(x0 y0 colWidths rowHeight rowCount / rows)
    (setq rows nil)
    (setq rows (cons (getRowStr x0 y0 colWidths rowHeight) rows))
    (while (> rowCount 1)
        (setq y0 (+ y0 rowHeight))
        (setq rows (cons (getRowStr x0 y0 colWidths rowHeight) rows))
        (setq rowCount (1- rowCount))
    )
    (setq rows (reverse rows))
)

;获得单行字符串,字段间用制表符分割
(defun getRowStr(x0 y0 colWidths rowHeight / x y i str tmp)
    (setq x (+ x0  (car colWidths)))
    (setq y (+ y0  rowHeight))
    (setq str (strcat (getVal (list x0 y0) (list x y))))
    (setq i 0)
    (while (< i (1- (length colWidths)))
        (setq x0 (+ x0 (nth i colWidths)))
        (setq x (+ x (nth (1+ i) colWidths)))
        (setq tmp (getVal (list x0 y0) (list x y)))
        (if tmp
            (setq str (strcat str "\t" tmp))
            (setq str (strcat str "\t"))
        )
        (setq i (1+ i))
    )
    (setq str str)
)


;获得两点间文本
(defun getVal(point1 point2 / ss i val result)
    (setq ss (ssget "C"  point1 point2))
    (setq i (sslength ss))
    (setq val nil)
    (while (> i 0)
        (setq i (1- i))
        (if (= "TEXT" (cdr (assoc 0 (entget (ssname ss i)))))
            (setq val (cdr (assoc 1 (entget (ssname ss i)))))
        )
    )
    (setq result val)
)