Version 0 of AutoCAD Automation

Updated 2005-01-27 19:23:29

I recently had the need to automate some file conversions within AutoCAD. Specifically, I needed to convert native AutoCAD drawing files (in DWG format) to the more palatable DXF format. After some research, I found that AutoCAD exposes a fairly standard COM automation interface - which seemed to make my task a perfect match for tcl coupled with the tcom extension.

So, the following is some very basic sample code to do what I needed. I'll eventually create a stand-alone application (with all the required frills), but for now, I thought this might help others:

 package require tcom

 set inFile [file join "c:/" "test"]
 set outFile [file join "c:/" "test.dxf"]

 set type(R12_dxf)  [expr 1]
 set type(R13_dxf)  [expr 5]
 set type(R14_dxf)  [expr 9]
 set type(R15_dxf)  [expr 13]
 set type(R18_dxf)  [expr 25]
 set type(2000_dxf) [expr 13]
 set type(2004_dxf) [expr 25]

 set app [::tcom::ref createobj AutoCAD.Application]
 $app Visible 0
 set docsCollection [$app Documents]
 set theDoc [$docsCollection Open $inFile]
 $theDoc SaveAs $outFile $type(2000_dxf)
 $theDoc Close