一、处理AutoCad模块 -pyautocad
1.安装
pip install pyautocad
注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块
2.如要使用tables 命令,要另外安装xlrd 和 tablib
2.使用准备
pyautocad 通过调用autocad的com接口,具体可以参考AutoCAD ActiveX documentation。
-
-
acad_aag.chm
- ActiveX and VBA Developer’s Guideacadauto.chm
- ActiveX and VBA Reference
-
二、连接AutoCad
使用pyautocad连接cad 会自动打开电脑上安装的cad软件,所以必须安装autocad,任意版本均可。
#导入模块
from pyautocad import Autocad, APoint
#创建cad实例
acad = Autocad(create_if_not_exists=True)
#在cad控制面板中输出
acad.prompt("Hello, Autocad from Python\n")
#输出文件的名字
print(acad.doc.Name)
python会自动连接上cad,只要cad是开着的,就创建了一个
create_if_not_exists=True:如果此时还没有打开cad,将会创建一个新的dwg文件,并自动开启cad软件
使用ActiveX接口简单操作cad,具体参考原文文档:
To work with AutoCAD documents and objects we can use ActiveX interface,
Autocad
(from pyautocad) contains some methods to simplify common Automation tasks, such as object iteration and searching, getting objects from user’s selection, printing messages.There are shortcuts for current
ActiveDocument
-Autocad.doc
andActiveDocument.ModelSpace
-Autocad.model
二、pyautocad使用方式
调用方式主要有如下几种:
1.Autocad.app :对应Autocad Active 接口中的 Application
method:
- Eval
- GetAcadState
- GetInterfaceObject
- ListARX
- LoadARX
- LoadDVB
- Quit
- RunMacro
- StatusID
- UnloadARX
- UnloadDVB
- Update
- ZoomAll
- ZoomCenter
- ZoomExtents
- ZoomPickWindow
- ZoomPrevious
- ZoomScaled
- ZoomWindow
2.Autocad.doc :对应Autocad Active 接口中的 Document(
ActiveDocument)
method:
- Activate
- AuditInfo
- Close
- CopyObjects
- EndUndoMark
- Export
- GetVariable
- HandleToObject
- Import
- LoadShapeFile
- ObjectIDToObject
- Open
- PurgeAll
- Regen
- Save
- SaveAs
- SendCommand
- SetVariable
- StartUndoMark
- WBlock
2.Autocad.model :对应Autocad Active 接口中的 Document.ModelSpace(
ActiveDocument
.ModelSpace
)
method:
- Add3DFace
- Add3DMesh
- Add3DPoly
- AddArc
- AddAttribute
- AddBox
- AddCircle
- AddCone
- AddCustomObject
- AddCylinder
- AddDim3PointAngular
- AddDimAligned
- AddDimAngular
- AddDimArc
- AddDimDiametric
- AddDimOrdinate
- AddDimRadial
- AddDimRadialLarge
- AddDimRotated
- AddEllipse
- AddEllipticalCone
- AddEllipticalCylinder
- AddExtrudedSolid
- AddExtrudedSolidALongPath
- AddHatch
- AddLeader
- AddLightWeightPolyline
- AddLine
- AddMLeader
- AddMLine
- AddMText
- AddPoint
- AddPolyfaceMesh
- AddPolyline
- AddRaster
- AddRay
- AddRegion
- AddRevolvedSolid
- AddShape
- AddSolid
- AddSphere
- AddSpline
- AddTable
- AddText
- AddTolerance
- AddTorus
- AddTrace
- AddWedge
- AddXLine
- AttachExternalReference
- InsertBlock
- Item
三、结语
pyautocad仅仅封装了一下接口,加入了一些迭代方式和table操作,可以导入导出excel等文本数据,官方文档也比较简陋,只有一两个简单实例,并未接受所有的方法,而这些方法主要是一个接口,所以这些方法所要求的参数并不太清楚,只能自己测试。本文只做简单介绍,更多实例会在以后更新。