Here's the simple version I had working last week to batch process DWG files to BMP or WMF images.
It is VB.Net but using COM Interop. This version of code is light on error checking. Although no changes are saved to the DWG file, use at your own risk.
I have an improved version that also can load the images that were created and slideshow thru them, with a custom dialog box prompting yes or no if the drawing is ok. The results are stored in Excel with Hyperlinks to the CAD files for later editing. If anyone is interested in that let me know on this board.
CODE: VB.Net Express 2008 sp1
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Windows.Forms
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Object
'CMA Notes: AutoCAD and AutoDesk are copyrighted names by their owners, and any and all code used is for learning
' and demonstartion purposes. The user of this code assumes any and all risk resulting from it's use.
Public Class DWG_to_WMF
Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
Dim acadSelectset As Autodesk.AutoCAD.Interop.AcadSelectionSet
Dim temp_complete_filename_path As String
Dim Top_Level_Folder As String
Dim sImage_Type As String
Dim sSave_Folder_Path As String
Dim sfileName_Only_No_Path As String
Public Sub GetAcadApplication()
Try
AcadApp = GetObject(, "AutoCAD.Application.17.2") '17.2 For AutoCAD 2009, differs for other versions.
AcadApp.Visible = True
Catch
Try
AcadApp = CreateObject("AutoCAD.Application.17.2")
AcadApp.Visible = True
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
Exit Sub
End Try
End Try
End Sub
Private Sub Pick_Folder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pick_Folder.Click
If Folder_Source_DWG.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Top_Level_Folder = Folder_Source_DWG.SelectedPath
End If
Dim fileNames As String()
If Include_Subfolders.Checked = True Then
fileNames = Directory.GetFiles(Top_Level_Folder, "*.dwg", SearchOption.AllDirectories)
Else
fileNames = Directory.GetFiles(Top_Level_Folder, "*.dwg", SearchOption.TopDirectoryOnly)
End If
' This grabs all *.dwg files in either the top level folder or top level folder and all sub folders
' depending on the value of Include_Subfolders checkbox.
Call GetAcadApplication()
For Each fileName As String In fileNames
Try
acadDoc = AcadApp.ActiveDocument
Catch
acadDoc = AcadApp.Documents.Add
End Try
ProcessDwg(fileName)
Next
End Sub
Sub ProcessDwg(ByVal ThisDwg As String)
Dim sImage_Output_File As String = ""
sfileName_Only_No_Path = ""
Try
acadDoc.Application.Documents.Open(ThisDwg) 'Opens file in variable ThisDwg Ex c:\test.dwg
Catch ex As Exception
MsgBox("Error Opening drawing." & vbCrLf & ex.ToString)
End Try
acadDoc = AcadApp.ActiveDocument
AcadApp.ZoomExtents() 'may not be needed since selection set selects "ALL"
Dim ssetObj As AcadSelectionSet
ssetObj = acadDoc.SelectionSets.Add("SS02") ' creates selection set object
ssetObj.Select(AcSelect.acSelectionSetAll) 'puts everything into the selection set object
' To Do : Add selection set option to pick specific Layer(s), & only their contents will go to the WMF.
If File_Output_Different_Than_dwg.Checked = True Then ' handles logic of where to save image files
'Image files will be saved to a different folder than where the original DWG files are located.
sfileName_Only_No_Path = Path.GetFileName(ThisDwg)
sImage_Output_File = sSave_Folder_Path & "\" & sfileName_Only_No_Path
Else
sImage_Output_File = ThisDwg 'Image files will be saved in same folder as DWG files.
End If
acadDoc.Export(sImage_Output_File, sImage_Type, ssetObj)
' use sImage_Output_File as path and filename to save the Image file
' use sImage_Type variable to chose WMF or BMP. This adds a .WMF or .BMP to sImage_Output_File
' use ssetObj as the items from drawing that will appear in image file.
' Example test.dwg will be exported as test.dwg.wmf
' Example c:\test.dwg will now have a WMF saved at c:\test.dwg.wmf
Try
acadDoc.Close(False, ThisDwg)
Catch ex As Exception
MsgBox("Error closing the drawing." & vbCrLf & ex.ToString)
End Try
End Sub
Private Sub Close_AutoCAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Close_AutoCAD.Click
Try
AcadApp.Quit()
Catch ex As Exception
MsgBox("Error closing AutoCAD." & vbCrLf & ex.ToString)
End Try
End Sub
Private Sub DWG_to_WMF_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub WMF_option_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WMF_option.CheckedChanged
sImage_Type = "WMF"
End Sub
Private Sub BMP_Option_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BMP_Option.CheckedChanged
sImage_Type = "BMP"
End Sub
Private Sub File_output_same_as_dwg_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles File_output_same_as_dwg.CheckedChanged
File_Output_Location.Text = ""
End Sub
Private Sub File_Output_Different_Than_dwg_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles File_Output_Different_Than_dwg.CheckedChanged
If File_Output_Different_Than_dwg.Checked = True Then
Folder_Alternate_Save_Location.Description = "Select Folder to save image files"
Folder_Alternate_Save_Location.ShowDialog()
If System.Windows.Forms.DialogResult.OK Then
sSave_Folder_Path = Folder_Alternate_Save_Location.SelectedPath.ToString()
File_Output_Location.Text = sSave_Folder_Path
ElseIf System.Windows.Forms.DialogResult.Cancel Then
File_Output_Location.Text = ""
End If
End If
End Sub
End Class