如何从 Python 连接 AutoCAD?
How to interface with AutoCAD from Python?
我有以下 AutoLISP 代码
(defun graph ( pts sls tls )
( (lambda ( l )
(foreach x l (text (cdr x) (itoa (car x)) 0.0 1))
(mapcar
'(lambda ( a b / p q r )
(setq p (cdr (assoc a l))
q (cdr (assoc b l))
r (angle p q)
)
(entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q) '(62 . 8)))
(text
(mapcar '(lambda ( x y ) (/ (+ x y) 2.0)) p q)
(rtos (distance p q) 2)
(if (and (< (* pi 0.5) r) (<= r (* pi 1.5))) (+ r pi) r)
2
)
)
sls tls
)
)
(mapcar 'cons (vl-sort (append sls tls) '<) pts)
)
)
(defun text ( p s a c )
(entmake
(list
'(0 . "TEXT")
(cons 10 p)
(cons 11 p)
(cons 50 a)
(cons 01 s)
(cons 62 c)
'(40 . 2)
'(72 . 1)
'(73 . 2)
)
)
)
并且输入是
(graph
'((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25))
'(1 1 1 1 2 2 3 4 4 5 6)
'(2 3 4 5 3 6 6 5 7 7 7)
)
从上面创建的二维几何图形从 AutoCAD 导出为 dxf 文件。
实际输入生成于Python
pts = [(75, 25), (115, 45), (90, 60), (10, 5), (45, 0), (45, 55), (0, 25)]
sls = [1 1 1 1 2 2 3 4 4 5 6]
tls = [2 3 4 5 3 6 6 5 7 7 7]
我想知道如何使用 python 数据类型作为输入并直接从 Python 与 AutoCAD 交互,将 AutoCAD 输出保存为 dxf 文件。
编辑:
我已经在 Python 中安装了 pyautocad 并且
from pyautocad import Autocad, APoint
现在我不确定如何在 Python 中计算这些 AutoCAD 表达式。
例如,输入是 Python 元组
我是否应该使用
将每个元组转换为 AutoCAD 点
p = APoint(x, y)
并且我不确定在将输入数据生成为 autocad 点后如何从这里继续。我不清楚函数中的命令如何运行
defun graph ( pts sls tls )
任何建议都会非常有用
可以使用python
的子进程库建立接口
"""
Created by Natasha 28/9/2020
-This code passes inputs from python to autocad
ref:
ref: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-interface-with-autocad-from-python/m-p/9768816/highlight/false#M404952
"""
import subprocess
from settings import\
GRAPH_LISP_FILE,\ # file paths
GRAPH_SCR_FILE,\
GRAPH_DWG_FILE,\
GRAPH_DXF_FILE
def write_src(graph):
"""
Commands sent to AutoCAD aew
:param graph:
:return:
"""
with open(GRAPH_SCR_FILE, 'w') as outfile:
outfile.write("open\n" +
f"{GRAPH_DWG_FILE}" + "\n"
"(setq *LOAD_SECURITY_STATE* (getvar 'SECURELOAD))\n" +
'(setvar "SECURELOAD" 0)\n' +
rf'(load "{GRAPH_LISP_FILE}")' + "\n"
f"{graph}\n" +
'(setvar "SECURELOAD" *LOAD_SECURITY_STATE*)\n'
"saveas dxf 16\n" +
rf"{GRAPH_DXF_FILE}" +"\n"+
"quit"
)
pass
def iter_to_lisp_string(list_or_tuple):
"""
Converts python datatypeS list and tuple to AutoCAD datatype
:param list_or_tuple:
:return:
"""
return "(" + " ".join(map(str, list_or_tuple)) + ")"
if __name__ == '__main__':
pts_string = "'" + iter_to_lisp_string(map(iter_to_lisp_string, [(75, 25), (115, 45), (90, 60), (10, 5), (45, 0), (45, 55), (0, 25)]))
sls_string = "'" + iter_to_lisp_string([1, 1, 1, 1, 2, 2, 3, 4, 4, 5, 6])
tls_string = "'" + iter_to_lisp_string([2, 3, 4, 5, 3, 6, 6, 5, 7, 7, 7])
graph_command_string = iter_to_lisp_string(["graph", pts_string, sls_string, tls_string])
write_src(graph=graph_command_string)
subprocess.run(["C:\Program Files\Autodesk\AutoCAD 2019\acad.exe", "/b", GRAPH_SCR_FILE])
print("Process Finished")
我有以下 AutoLISP 代码
(defun graph ( pts sls tls )
( (lambda ( l )
(foreach x l (text (cdr x) (itoa (car x)) 0.0 1))
(mapcar
'(lambda ( a b / p q r )
(setq p (cdr (assoc a l))
q (cdr (assoc b l))
r (angle p q)
)
(entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q) '(62 . 8)))
(text
(mapcar '(lambda ( x y ) (/ (+ x y) 2.0)) p q)
(rtos (distance p q) 2)
(if (and (< (* pi 0.5) r) (<= r (* pi 1.5))) (+ r pi) r)
2
)
)
sls tls
)
)
(mapcar 'cons (vl-sort (append sls tls) '<) pts)
)
)
(defun text ( p s a c )
(entmake
(list
'(0 . "TEXT")
(cons 10 p)
(cons 11 p)
(cons 50 a)
(cons 01 s)
(cons 62 c)
'(40 . 2)
'(72 . 1)
'(73 . 2)
)
)
)
并且输入是
(graph
'((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25))
'(1 1 1 1 2 2 3 4 4 5 6)
'(2 3 4 5 3 6 6 5 7 7 7)
)
从上面创建的二维几何图形从 AutoCAD 导出为 dxf 文件。
实际输入生成于Python
pts = [(75, 25), (115, 45), (90, 60), (10, 5), (45, 0), (45, 55), (0, 25)]
sls = [1 1 1 1 2 2 3 4 4 5 6]
tls = [2 3 4 5 3 6 6 5 7 7 7]
我想知道如何使用 python 数据类型作为输入并直接从 Python 与 AutoCAD 交互,将 AutoCAD 输出保存为 dxf 文件。
编辑: 我已经在 Python 中安装了 pyautocad 并且
from pyautocad import Autocad, APoint
现在我不确定如何在 Python 中计算这些 AutoCAD 表达式。 例如,输入是 Python 元组
我是否应该使用
将每个元组转换为 AutoCAD 点p = APoint(x, y)
并且我不确定在将输入数据生成为 autocad 点后如何从这里继续。我不清楚函数中的命令如何运行
defun graph ( pts sls tls )
任何建议都会非常有用
可以使用python
的子进程库建立接口"""
Created by Natasha 28/9/2020
-This code passes inputs from python to autocad
ref:
ref: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-interface-with-autocad-from-python/m-p/9768816/highlight/false#M404952
"""
import subprocess
from settings import\
GRAPH_LISP_FILE,\ # file paths
GRAPH_SCR_FILE,\
GRAPH_DWG_FILE,\
GRAPH_DXF_FILE
def write_src(graph):
"""
Commands sent to AutoCAD aew
:param graph:
:return:
"""
with open(GRAPH_SCR_FILE, 'w') as outfile:
outfile.write("open\n" +
f"{GRAPH_DWG_FILE}" + "\n"
"(setq *LOAD_SECURITY_STATE* (getvar 'SECURELOAD))\n" +
'(setvar "SECURELOAD" 0)\n' +
rf'(load "{GRAPH_LISP_FILE}")' + "\n"
f"{graph}\n" +
'(setvar "SECURELOAD" *LOAD_SECURITY_STATE*)\n'
"saveas dxf 16\n" +
rf"{GRAPH_DXF_FILE}" +"\n"+
"quit"
)
pass
def iter_to_lisp_string(list_or_tuple):
"""
Converts python datatypeS list and tuple to AutoCAD datatype
:param list_or_tuple:
:return:
"""
return "(" + " ".join(map(str, list_or_tuple)) + ")"
if __name__ == '__main__':
pts_string = "'" + iter_to_lisp_string(map(iter_to_lisp_string, [(75, 25), (115, 45), (90, 60), (10, 5), (45, 0), (45, 55), (0, 25)]))
sls_string = "'" + iter_to_lisp_string([1, 1, 1, 1, 2, 2, 3, 4, 4, 5, 6])
tls_string = "'" + iter_to_lisp_string([2, 3, 4, 5, 3, 6, 6, 5, 7, 7, 7])
graph_command_string = iter_to_lisp_string(["graph", pts_string, sls_string, tls_string])
write_src(graph=graph_command_string)
subprocess.run(["C:\Program Files\Autodesk\AutoCAD 2019\acad.exe", "/b", GRAPH_SCR_FILE])
print("Process Finished")