求一个将AUtoCAD摸个类型文字导出到excel 然后再从excel导入的程序
因为课题需要,需要将CAD中的某个类型的文本文字导出到已经打开的excel中,在excel中编辑后,输入命令,将选定的excel中的文字导入Autocad中,要求求autoCAD中文字位置不发生变化,只是内容变化。
下面的程序只能实现导出具体到excel文件功能,但是导入函数报错:
通过xyz坐标将文字导出到excel, excel文字导出到CAD的程序,现在可以从CAD导出,但是目前没法导入。最佳的方式是将文件导出到已经打开excel中,然后在excel中修改后再导回CAD中。
导出函数
(defun c:EXTCSV (/ ss i f ename text pos)
(setq ss (ssget "X" '((0 . "TEXT,MTEXT"))))
(if ss
(progn
(setq f (open "D:/autocad_text.csv" "w"))
(if f
(progn
(write-line "TextString,X,Y,Z" f)
(setq i -1)
(repeat (sslength ss)
(setq i (1+ i))
(setq ename (ssname ss i))
(setq text (cdr (assoc 1 (entget ename))))
(setq pos (cdr (assoc 10 (entget ename))))
(write-line (strcat text "," (rtos (car pos)) "," (rtos (cadr pos)) "," (rtos (caddr pos))) f)
)
(close f)
(princ "\nText has been exported to D:/autocad_text.csv")
)
(princ "\nFailed to open file for writing.")
)
)
(princ "\nNo text found in the drawing.")
)
(princ)
)
导入函数出错
(defun c:IMTCSV (/ f line fields text pos)
(setq f (open "D:/autocad_text.csv" "r"))
(if f
(progn
(read-line f) ; Skip the header line
(while (setq line (read-line f))
(setq fields (strtok line ","))
(setq text (nth 0 fields))
(setq pos (list (atof (nth 1 fields)) (atof (nth 2 fields)) (atof (nth 3 fields))))
(command "TEXT" pos "" "" text)
)
(close f)
(princ "\nText has been imported from D:/autocad_text.csv")
)
(princ "\nFailed to open file for reading.")
)
(princ)
)
CAD自带的数据链接功能可实现此功能,缺点是CAD的表格编辑功能比较弱,可能无法很好的满足格式要求 (setq fields (strtok line ","))
好像是差 strtok 函数吧 有图有真像。特殊的要求一般需要定制才能达到目的,或者自己慢慢学习 vba做个类似功能的,acad和excel表格互转。 (defun STRTOK (str del / pos)
(if (setq pos (vl-string-search del str))
(cons (substr str 1 pos)
(STRTOK (substr str (+ pos 1 (strlen del))) del)
)
(list str)
)
) 再或者随便找个CAD表格和EXCEL表格互转的插件都能实现,比如TRUETABLE yoyoho 发表于 2024-3-8 09:20
(defun STRTOK (str del / pos)
(if (setq pos (vl-string-search del str))
(cons (substr str 1...
太感谢了。非常感谢。大侠 yoyoho 发表于 2024-3-8 09:20
(defun STRTOK (str del / pos)
(if (setq pos (vl-string-search del str))
(cons (substr str 1...
您好,能否再加一个语句,让导出的文字维持原来CAD中文字的字体等格式样式呢。谢谢您了 yimiyangguang55 发表于 2024-3-8 15:34
太感谢了。非常感谢。大侠
可以共享一下吗?
页:
[1]
2