Get Preview Bitmaps of Drawing Sheets Example (VB.NET)
This example shows how to get PNG preview bitmaps of the sheets in a drawing
document.
'---------------------------------------------------------------------------
' Preconditions:
' 1.
Specified drawing document exists.
' 2.
References to Microsoft.VisualBasic.Compatiblity,
' System.Drawing,
stdole, and
' SolidWorks.Interop.swdocumentmgr
exist in Project References
' (click
Project > Add Reference in
the
' SolidWorks
Visual Studio Tools for Applications IDE
' to
add them if needed).
'
' Postconditions:
' 1.
Preview bitmaps of the sheets in the
' drawing
document are created in c:\temp.
' 2.
Right-click a just-created preview bitmap
' file,
and select Preview.
'---------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports SolidWorks.Interop.swdocumentmgr
Imports Microsoft.VisualBasic.Compatibility.VB6
Imports System.Drawing.Image
Imports stdole
Partial Class SolidWorksMacro
Public
Sub main()
Dim
swClassFact As SwDMClassFactory
Dim
swDocMgr As SwDMApplication
Dim
swDoc As SwDMDocument10
Dim
sDocFileName As String
Dim
nDocType As SwDmDocumentType
Dim
nRetVal As SwDmDocumentOpenError
Dim
sLicenseKey As String
'
Specify your license key
sLicenseKey
= "your_license_key"
'
If the following drawing document doesn't exist on your system,
'
then substitute the name of a drawing document that does
sDocFileName
= "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\foodprocessor.slddrw"
nDocType
= SwDmDocumentType.swDmDocumentDrawing
swClassFact
= CreateObject("SwDocumentMgr.SwDMClassFactory")
swDocMgr
= swClassFact.GetApplication(sLicenseKey)
swDoc
= swDocMgr.GetDocument(sDocFileName,
nDocType, True, nRetVal)
If
(swDoc Is Nothing) Then
MsgBox("Unable
to open document.")
End
If
Dim
pPreview As stdole.IPictureDisp
Dim
image As Drawing.Image
Dim
Sheets As Object
Dim
Sheet As SwDMSheet2
Dim
nError As Long
Dim
PreviewPNGByteArray As Object
'
Get the sheets in the drawing document
Sheets
= swDoc.GetSheets
Dim
nbrSheets As Long
nbrSheets
= CLng(UBound(Sheets)) + 1
Dim
snbrSheets As String = Convert.ToString(nbrSheets)
Debug.Print("Number
of sheets: " + snbrSheets)
Debug.Print("
")
Dim
i As Integer
For
i = 0 To UBound(Sheets)
Sheet
= Sheets(i)
Debug.Print("Name
of sheet: " + (Sheet.Name))
Debug.Print("Name
of preview PNG's stream: " + (Sheet.PreviewPNGStreamName))
pPreview
= Sheet.GetPreviewPNGBitmap(nError)
If
nError = 0 Then
'
For each sheet, convert the picture to an
'
image and save it as .png file
image
= Support.IPictureDispToImage(pPreview)
image.Save("c:\temp\"
+ Sheet.Name + ".png", Drawing.Imaging.ImageFormat.Png)
'
Get the preview's PNG byte array
PreviewPNGByteArray
= Sheet.GetPreviewPNGBitmapBytes(nError)
Dim
nbrPreviewPNGBitmapBytes As Long
nbrPreviewPNGBitmapBytes
= CLng(UBound(PreviewPNGByteArray))
nbrPreviewPNGBitmapBytes
= nbrPreviewPNGBitmapBytes + 1
Dim
snbrPreviewPNGBitmapBytes As String = Convert.ToString(nbrPreviewPNGBitmapBytes)
Debug.Print("Number
of bytes in preview's PNG byte array: " + snbrPreviewPNGBitmapBytes)
Debug.Print("")
Else
Select
Case nError
Case
2
Debug.Print("Error:
No preview data stored with document.")
Case
4
Debug.Print("Error:
Failed to make the bitmap.")
End
Select
End
If
Next
swDoc.CloseDoc()
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class