Get Preview Bitmap of Drawing Sheet Example (VB.NET)
This example shows how to get a BMP preview bitmap of the sheet that
was active when then the drawing was last saved.
'-----------------------------------------------------------------------------
' Preconditions:
' 1. Specify your license key for sLicenseKey.
' 2.
In the SolidWorks Visual Studio Tools for
' Applications
IDE, add references for
' Microsoft.VisualBasic.Compatibility,
System.Drawing,
' stdole,
and SolidWorks.Interop.swdocumentmgr.
' (click
Project > Add Reference).
'
' Postconditions: Navigate to your c:\temp
folder and right-click
'
foodprocessor.bmp
and select Preview from the shortcut
menu to
' view
the bitmap.
'---------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.swdocumentmgr
Imports Microsoft.VisualBasic.Compatibility.VB6
Imports System.Drawing.Image
Imports stdole
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public
Sub main()
Const
sLicenseKey As String = "your_license_key"
'Specify license key
Const
sDocFileName As
String = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\foodprocessor.slddrw"
' Or specify your document
Const
sExtractDir As String = "c:\temp\" ' Or specify your disk and
folder location
Const
sFilename As
String = "foodprocessor" ' Or specify your filename
Dim
swClassFact As SwDMClassFactory
Dim
swDocMgr As SwDMApplication
Dim
swDoc As SwDMDocument
Dim
swDoc10 As SwDMDocument10
Dim
pPreview As stdole.IPictureDisp
Dim
image As Drawing.Image
Dim
nDocType As Long
Dim
nRetVal As Long
'
Determine type of SolidWorks file based on file extension
If
InStr(LCase(sDocFileName), "sldprt") > 0 Then
nDocType
= SwDmDocumentType.swDmDocumentPart
ElseIf
InStr(LCase(sDocFileName), "sldasm") > 0 Then
nDocType
= SwDmDocumentType.swDmDocumentAssembly
ElseIf
InStr(LCase(sDocFileName), "slddrw") > 0 Then
nDocType
= SwDmDocumentType.swDmDocumentDrawing
Else
'
Probably not a SolidWorks file,
'
so cannot open
nDocType
= SwDmDocumentType.swDmDocumentUnknown
Exit
Sub
End
If
swClassFact
= CreateObject("SwDocumentMgr.SwDMClassFactory")
swDocMgr
= swClassFact.GetApplication(sLicenseKey)
swDoc
= swDocMgr.GetDocument(sDocFileName,
nDocType, False, nRetVal) : Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone
= nRetVal)
Debug.Print("File
= " & swDoc.FullName)
Debug.Print("
Version
=
" & swDoc.GetVersion)
Debug.Print("
Author
=
" & swDoc.Author)
Debug.Print("
Comments
=
" & swDoc.Comments)
Debug.Print("
CreationDate
=
" & swDoc.CreationDate)
Debug.Print("
Keywords
=
" & swDoc.Keywords)
Debug.Print("
LastSavedBy
=
" & swDoc.LastSavedBy)
Debug.Print("
LastSavedDate
=
" & swDoc.LastSavedDate)
Debug.Print("
Subject
=
" & swDoc.Subject)
Debug.Print("
Title =
" & swDoc.Title)
swDoc10
= swDoc
pPreview
= swDoc10.GetPreviewBitmap(nRetVal)
image
= Support.IPictureDispToImage(pPreview)
image.Save(sExtractDir
+ sFilename + ".bmp", Drawing.Imaging.ImageFormat.Bmp)
Debug.Print("
Preview
stream =
" & swDoc10.PreviewStreamName)
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class