Code Red => .NET => Topic started by: dave_illini on October 02, 2008, 07:05:49 PM

Title: Export [DWG] to WMF or BMP with .NET
Post by: dave_illini on October 02, 2008, 07:05:49 PM
Is it possible to Export a DWG to WMF or BMP with .NET?  Can anyone point me to the right code keywords?
So far, I can only find the Interop version of export by using F2 and searching for objects.
Preferences are in this order for Speed.
1. Purely in the database without loading the drawing.
2. Using the databse and loading the file
3. COM Interop.
I'm using VB 2008 Express SP1, and AutoCAD MEchanical 2009 SP1.
I'd really prefer to do it without loading the drawing for speed.  Thanks to posts from Daniel on these boards, I can extract text from 3300 DWG's in under 3 minutes.
Thanks for any help.  -Dave
Title: Re: Export to WMF or BMP with .NET
Post by: dave_illini on October 08, 2008, 04:35:50 PM
I was able to export to both WMF and BMP using COM Interop with
I still could not find a way to do this using the database, though.
I'll post my code and working application later this week.

The key function is:
Autodesk.AutoCAD.Interop.AcadDocumentClass.Export(String, String, Autodesk.AutoCAD.Interop.AcadSelectionSet)

More info:
Public Overridable Sub Export(ByVal FileName As String, ByVal Extension As String, ByVal SelectionSet As Autodesk.AutoCAD.Interop.AcadSelectionSet)
     Member of Autodesk.AutoCAD.Interop.AcadDocumentClass
Title: Re: Export to WMF or BMP with .NET
Post by: dave_illini on October 15, 2008, 03:34:23 PM
  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
Code: [Select]
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()
            AcadApp = GetObject(, "AutoCAD.Application.17.2")  '17.2 For AutoCAD 2009, differs for other versions.
            AcadApp.Visible = True
                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)
            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
                acadDoc = AcadApp.ActiveDocument
                acadDoc = AcadApp.Documents.Add
            End Try
    End Sub

    Sub ProcessDwg(ByVal ThisDwg As String)
        Dim sImage_Output_File As String = ""
        sfileName_Only_No_Path = ""
            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
            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
            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
        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"
            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
Title: Re: Export [DWG] to WMF or BMP with .NET
Post by: cadpro on January 07, 2009, 12:32:41 AM
Hi dave,

This code would be helpful to lot many around. I would like to know if this is possible with sld files. I mean, can we convert sld files to bmp in order to view as thumbnails in a ListView control? If not, any other suggession like using the slide control and the previous and next buttons to view all slides in a folder?

Title: Re: Export [DWG] to WMF or BMP with .NET
Post by: pb.Perksa on March 22, 2009, 08:12:02 PM
First things first to the OP.  Great work.  Very handy.


There are much more effective ways of doing that, as shown in this thread.

PS.  Just trying to draw the different threads about this issue to a single thread.