使用Python自动化AutoCAD [英] Using Python to Automate AutoCAD

查看:519
本文介绍了使用Python自动化AutoCAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python和AutoCAD还是很陌生,所以请多多包涵。我正在尝试使用Python在AutoCAD中自动化设计周期。到目前为止,我已经能够通过Python在AutoCAD中添加点和线,但是我希望能够将线型更改为虚线。我知道可以在AutoCAD的命令栏中使用一些简单的命令来完成此操作,但是我无法弄清楚如何通过Python进行操作。

I'm very new to Python and AutoCAD so please bear with me. I'm trying to use Python to automate a design cycle in AutoCAD. So far I am able to add points and lines in AutoCAD via Python but I want to be able to change the linetype to a dotted line. I understand that this can be done in the command bar in AutoCAD with a few simple commands but I can't work out how to do it through Python.

import array
from _ast import If
import comtypes.client
import pyautocad

#Get running instance of the AutoCAD application
acad = comtypes.client.GetActiveObject("AutoCAD.Application")

# Document object
doc = acad.ActiveDocument

#Get the ModelSpace object
ms = doc.ModelSpace

#In ModelSpace
#Set up A4 Space
pt1 = array.array('d', [0.0, 0.0, 0])
pt2 = array.array('d', [210.0, 0.0, 0])
pt3 = array.array('d', [210.0, 297.0, 0])
pt4 = array.array('d', [0.0, 297.0, 0])
line1 = ms.AddLine(pt1, pt2)
line2 = ms.AddLine(pt2, pt3)
line3 = ms.AddLine(pt3, pt4)
line4 = ms.AddLine(pt4, pt1)

#Add central fold
pt5 = array.array('d', [105.0, 0.0, 0])
pt6 = array.array('d', [105.0, 297.0, 0])

line5 = ms.AddLine(pt5, pt6)

print("Done.")

这会产生一个A4大小的矩形,并带有一条实心的分隔线,但我要一条虚线。您知道在哪里可以找到有关在AutoCAD中使用Python的一些信息吗?任何帮助是极大的赞赏!

This produces an A4 sized rectangle with a solid dividing line, but I want a dotted line. Any idea where I can find some information on using Python with AutoCAD? Any help is greatly appreciated!

推荐答案

尝试执行此操作以更改 line5 的行类型:

Try doing this to change the line type of line5:

line5.linetype = 'dot'

请确保在线型管理器列表中添加(加载)使用的线型(在这种情况下,即DOT)。

Please make sure to add (load) the line type used (i.e. DOT in this case) in the list of linetype manager.

您可以通过以下方式更改线型比例:

You can change the linetype scale by:

line5.linetypescale = '0.01'

这篇关于使用Python自动化AutoCAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆