Get PNG Preview Bitmap and Stream for Configuration Example (VB.NET)
This example shows how to
get the name of the stream and the PNG preview bitmap for a configuration in a part or assembly.
NOTE: To get the name of the stream and a preview bitmap of the sheet
active when the drawing was last saved, use ISwDMDocument10::GetPreviewBitmap and
ISwDMDocument10::PreviewStreamName. To get the name of the streams and
the preview bitmaps for all sheets in a drawing, use
ISwDMSheet2::GetPreviewPNGBitmap and ISwDMSheet2::PreviewPNGStreamName.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Read the SolidWorks Document Manager API Getting Started
'
topic
and ensure that the required DLLs
are registered.
' 2. Copy and paste this module into a VB.NET console application
' in Microsoft
Visual Studio.
' 3. Ensure that the specified document exists
' (or point to another
document).
' 4. Add the SolidWorks.Interop.swdocumentmgr.dll reference
' to the
project:
' a. Right-click the solution in Solution Explorer.
' b. Select Add Reference.
' c. Click the Browse tab.
' d. Load:
' <SolidWorks_install_dir>\api\redist\SolidWorks.Interop.swdocumentmgr.dll
' 5. Add the following references to the project: System.Drawing.dll,
' stdole.dll, Microsoft.VisualBasic.Compatibility.VB6.dll.
' 6. Substitute <your_license_code> with your SolidWorks
' Document Manager license key.
' 7. Compile and run this program in Debug mode.
'
' Postconditions: Inspect the Output Window and the code to see
' how the API is used.
'---------------------------------------------------------------------------
Imports
SolidWorks.Interop.swdocumentmgr
Imports
Microsoft.VisualBasic.Compatibility.VB6
Imports
stdole
Imports
System.Drawing.Image
Module
Module1
Sub
main()
' Substitute the path and name of
your file for the following path and file name
Const
sDocFileName As
String =
"C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\multibody\multi_inter.sldprt"
'Specify document
Const
sExtractDir As
String =
"c:\temp\"
Dim
swClassFact As
SwDMClassFactory
Dim
swDocMgr As
SwDMApplication
Dim
swDoc As
SwDMDocument
Dim
swCfgMgr As
SwDMConfigurationMgr
Dim
vCfgNameArr As
Object
Dim
vCfgName As
Object
Dim
swCfg As
SwDMConfiguration7
Dim
pPreview As
IPictureDisp
Dim
nDocType As
Long
Dim
nRetVal As
Long
Dim
image As
Drawing.Image
' 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
'
Because drawing documents do not have configurations,
'
only continue running the macro if the document
'
is a part or assembly document
If
(nDocType <> SwDmDocumentType.swDmDocumentDrawing)
Then
swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory")
swDocMgr = swClassFact.GetApplication("<your_license_code>")
'Specify license key
swDoc =
swDocMgr.GetDocument(sDocFileName, nDocType,
False, nRetVal) :
Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone = nRetVal)
swCfgMgr = swDoc.ConfigurationManager
Debug.Print("File = "
& swDoc.FullName)
Debug.Print(" Active
configuration name = " &
swCfgMgr.GetActiveConfigurationName)
vCfgNameArr = swCfgMgr.GetConfigurationNames
For
Each
vCfgName In
vCfgNameArr
swCfg = swCfgMgr.GetConfigurationByName(vCfgName)
pPreview = swCfg.GetPreviewPNGBitmap(nRetVal)
image = Support.IPictureDispToImage(pPreview)
image.Save(sExtractDir & vCfgName &
".PNG")
Debug.Print(" "
& vCfgName)
Debug.Print(" PNG
preview stream = " & swCfg.PreviewPNGStreamName)
Debug.Print(" ")
Next
swDoc.CloseDoc()
End
If
End
Sub
End Module