Process Autocad by python


一、處理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 Guide
      • acadauto.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是開着的,就創建了一個<pyautocad.api.Autocad> 對象。這個對象連接最近打開的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 and ActiveDocument.ModelSpace - Autocad.model

  

 二、pyautocad使用方式

    調用方式主要有如下幾種:

       1.Autocad.app  :對應Autocad Active 接口中的 Application

           method:

      

       2.Autocad.doc  :對應Autocad Active 接口中的 Document(ActiveDocument)

           method:

       2.Autocad.model  :對應Autocad Active 接口中的 Document.ModelSpace(ActiveDocument.ModelSpace)

           method:           

三、結語

  pyautocad僅僅封裝了一下接口,加入了一些迭代方式和table操作,可以導入導出excel等文本數據,官方文檔也比較簡陋,只有一兩個簡單實例,並未接受所有的方法,而這些方法主要是一個接口,所以這些方法所要求的參數並不太清楚,只能自己測試。本文只做簡單介紹,更多實例會在以后更新。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM