AutoCAD Automation

JAG - 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

 # --- the file (extensions are assumed based on filetype)
 set file [file join "c:/" "test"]

 # --- various dxf output formats
 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]

 # --- open AutoCAD
 set app [::tcom::ref createobj AutoCAD.Application]
 
 # --- keep it hidden
 $app Visible 0

 # --- get a handle to the Documents collection
 set docsCollection [$app Documents]

 # --- open our file as a new document
 set theDoc [$docsCollection Open $file]
 
 # --- save our file as a specific DXF format
 $theDoc SaveAs $file $type(2000_dxf)

 # --- close the file
 $theDoc Close

 # --- quit from AutoCAD
 $app Quit