Tushar Suradkar [ www.geocities.com/cadgurucool ]


Convert AutoCAD DWG file to BMP using VB 6

This is 4th in the series of 17 tutorials that show you how to create a menu with bitmaps,
display it in the system tray right click as shown in figure and further
run 15 AutoCAD utilities from the menu.

All the tutorials are available for FREE at :

www.geocities.com/cadgurucool ( Click Tutorials 4U )


All VB6 source code and related ocx files are included with the zip file that contained this tutorial and is also available at the above site.
The entire series of AutoCAD - VB tutorials cover :

  • Create menu with bitmaps using VB 6

  • Put the menu with bitmaps in the system tray using VB 6


  • Drawing to DXF - Convert DWG to DXF without opening AutoCAD

  • Drawing to BMP - Convert DWG to Bitmap format Without Opening AutoCAD

  • Drawing to JEPG - Convert DWG to Joint Pictures Expert Group format - W.O.A.

  • Drawing to GIF - Convert DWG to Graphics Interchange Format - W.O.A.

  • Drawing to TIFF - Convert DWG to Tagged Image File Format - W.O.A.

  • Drawing to CNC - Generate M-G-code from milling profiles in AutoCAD - W. O. A.

  • Drawing to CNC (html) - Generate html file showing the milling profile and CNC program - without opening AutoCAD

  • Attribute Tree List - Display block attributes from a drawing in tree view and list view control just by right clicking on the dwg file in explorer.

  • Attribut to HTML - Generate a html file showing tables of block attributes in a drawing just by right clicking on the dwg file in explorer.

  • Attribut to PDF - Generate a Portable Document Format file showning tables of block attributes in a drawing just by right clickingon the dwg file in explorer.

  • Drawing List - Create a list of all drawings in a folder using the Browse for folder dialog.

  • Slide Library - Create a slide library of all slides ( AutoCAD sld files ) just by right clicking in windows explorer.

  • Slide Library Viewer - Learn to build a small utitlity that displays a list of all slides with preview from a slide library (AutoCAD slb file)


  • All of the above using Visual Basic 6 and without opening AutoCAD .

    Two more that fit in the above series but do not use VB or AutoCAD.

  • Drawing List - Add an item Create DWG List to the right click menu for dwg files in explorer and then quickly create a list of all drawings in the folder.

  • Drawing List - Add an item Create DWG List to the right click menu for all folders in explorer and then quickly create a list of all drawings in the folder.


  • Let us begin

    It is assumed that :

    you have a basic understanding of Visual Basic 6.
    AutoCAD 2000 / 2000i or 2002 is installed.

    Start Visual Basic and open the AcadTray project.
    The acadtray.zip file, this tutorial and the related zip and project files are available (for FREE) at
    www.geocities.com/cadgurucool - Click Tutorials 4U

    Also, another slide.ocx file that has nothing to do with the current tutorial, but is part of the aforesaid series of tutorials, is present in the acadtray.zip file.

    When you open the acadtray.vbp project, an error mesaage will appear regarding the slide.ocx file.

    Ignoring the error message, proceed to VB.

    OR

    You can easily register the slide.ocx file as follows : (first cancel project loading and exit VB)

    1. On the windows taskbar, click Start > Run.
    2. The Run dialog box appears.
    3. Type regsvr32 and then Drag and Drop the slide.ocx file in front of the word regsvr32 that you just typed.
    4. The filename with its fullpath will appear as shown in figure. OK or press Enter.
    6. A message will appear telling you that the registration succeeded.

    Once in VB, select Project > References and observe that reference to AutoCAD 2000 Type Library is added.

    This will enable us to contact AutoCAD for conversion of dwg file to bmp.

    Select Project > Components

    Observe the different components added to the project:
    The various components are relevant to the other 17 tutorials in this series.

    Next, click the Acadtray.frm and observe the various controls placed.

    The form itself is never going to appear at any point of time in any of the 17 tutorials.

    But the controls are still required, for example, the picture1 control that has the prism icon is for displaying in the Windows taskbar tray. See fig1 at the top of this page which shows the small tray icon along with the menu.

    The menu is also present on the form as indicated by the arrow in the figure besides.

    Press F5 and observe the system tray.

    The icon in the picture1 control will appear.

    If you right click on the tray icon, the menu will appear, but with bitmaps added - which is again one of the 17 tutorials in this series.









    Declaring the Variables

    Double click to open dwg2bmp.bas module.
     
      Dim strFileName As String
      Dim strDwgFile As String
      Dim strExportFile As String
      Dim intFLen As Integer
      Dim objAcad As New AutoCAD.AcadApplication
      Dim objAcadDoc As New AcadDocument
      Dim ss1 As AcadSelectionSet
     
    The Sub is called from one of the menu items.

    The strFileName will come from windows common dialog - open.
    The strDwgFile variable is for the drawing file to convert and strExportFile is for the bmp file.

    intFLen is for filename length and objAcad is an object variable, courtsey, the reference to AutoCAD Type Library, discussed above.
    And also the variables objAcadDoc and objAcadSelectionSet.

    Get the drawing file

    Double click to open dwg2bmp.bas module.
     
      Form1.FileDlg.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist Or _
                              cdlOFNExplorer Or cdlOFNLongNames
    
      Form1.FileDlg.Filter = "Drawing Files (*.dwg)|*.dwg"
      Form1.FileDlg.FilterIndex = 1
      Form1.FileDlg.ShowOpen
    
      strFileName = Form1.FileDlg.FileName
     
    Get the strFileName by filtering out dwg files.

    Check the File

    If the dialog was cancelled, the string is empty.
     
      If strFileName = "" Then
        MsgBox ("Error : No File Specified !")
     
    Proceed only if the string is not empty.

    The Meaty Part - Lick Lick

    Trim off the dwg extension :
     
      strDwgFile = strFileName
      intFLen = Len(strDwgFile)
      strExportFile = Left(strDwgFile, intFLen - 4)
     
    First assign the filename from the user to strDwgFile.
    Find its length.
    Take only the filename part without the file extrension and the dot .dwg that is 4 characters total.
    Assign it to strExportFile

    The Meaty Part - Wake up AutoCAD

    We must contact AutoCAD now.
     
      Set objAcadDoc = objAcad.Documents.Open(strDwgFile)
      objAcad.Visible = False
     
    Use the Open method of the Documents collection of the AutoCAD appplication object to open the user specified file.
    Keep AutoCAD hidden.

    The Meaty Part - Export the drawing

    Give AutoCAD a run for its money.
     
      Set ss1 = objAcadDoc.SelectionSets.Add("SS1")
      ss1.Select acSelectionSetAll
      objAcadDoc.Export strExportFile, "BMP", ss1
      ss1.Delete
      objAcadDoc.Close
      objAcad.Quit
      Set objAcadDoc = Nothing
      Set objAcad = Nothing
      MsgBox "Created file :" & vbCrLf & vbCrLf & strExportFile & ".bmp"
     
    Set ss1 to the newly added selection set SS1. Wait... ! The two are different species.
    Whereas ss1 is an object variable, SS1 is a dummy selection set.

    Add all the objects in the drawing to the selection set.

    Use the Export method of the AutoCAD document object, which requires a filename without extension, the format BMP and the selection set, all of which were ready.

    Leave it better than you found it - Thats our motto.
    Clean up the memory garbage.

    Delete the selection set ss1.
    Close the drawing ie . the AutocAD document.
    Quit AutoCAd in the background.
    Return back as if Nothing has happened.
    Finally, notify the user where the bmp file is.

    The Tailpiece

    We kept objAcad.Visible = False

    How do we know, AutoCAD actually was fired.

    Just before starting the whole thing, Right-Click the Window 2000 taskbar and select Task Manager.

    In the task manager dialog, take the Processes tab at the top and click on the Image Name coloumn header twice ( do not double click ) to list the processes alphabetically.

    acad.exe should be one of them, but only until the objects are set to nothing.

    You can also see the Mem Usage figure flare up at key time intervals, especially when the dwg file - AcadDocument - is opened and the program is doing the export selection set stuff.