Hi Guys,
I'm in the process of writing a .NET app that shows a preview image of the drawing sheets for a selected file. I am using the code below which I got from here: 2017 SOLIDWORKS API Help - Get Preview Bitmaps of Drawing Sheets Example (VB.NET) and here: 2017 SOLIDWORKS API Help - Get PNG Preview Bitmap and Stream for Configuration Example (VB.NET). The issue I am having I will get blank sheets previews for some sheets but not others. Any help or advice is appreciated.
<p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> Public Function SheetImages(SelectionName As String) As Image<br> Dim swClassFact As SwDMClassFactory<br> Dim swDocMgr As ISwDMApplication<br> Dim swDoc As SwDMDocument10<br> Dim sDocFileName As String = BrowseTB.Text<br> Dim nDocType As SwDmDocumentType<br> Dim nRetVal As SwDmDocumentOpenError<br> Dim sLicenseKey As String = My.Settings.swDocMgrKey<br> Dim swCfgMgr As SwDMConfigurationMgr<br> Dim vCfgNameArr As Object<br> Dim vCfgName As Object<br> Dim swCfg As SwDMConfiguration7</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory")<br> swDocMgr = swClassFact.GetApplication(sLicenseKey)</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> If InStr(LCase(sDocFileName), "sldprt") > 0 Then<br> nDocType = SwDmDocumentType.swDmDocumentPart<br> ElseIf InStr(LCase(sDocFileName), "sldasm") > 0 Then<br> nDocType = SwDmDocumentType.swDmDocumentAssembly<br> ElseIf InStr(LCase(sDocFileName), "slddrw") > 0 Then<br> nDocType = SwDmDocumentType.swDmDocumentDrawing<br> Else<br> nDocType = SwDmDocumentType.swDmDocumentUnknown<br> Return Nothing<br> Exit Function<br> End If</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> If nDocType = SwDmDocumentType.swDmDocumentDrawing Then<br> swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)<br> If (swDoc Is Nothing) Then<br> MsgBox("Unable to open document.")<br> End If<br> Dim Sheets As Object<br> Dim Sheet As SwDMSheet2<br> Dim nError As Integer<br> Sheets = swDoc.GetSheets<br> Dim nbrSheets As Integer<br> nbrSheets = CLng(UBound(Sheets)) + 1<br> Dim snbrSheets As String = Convert.ToString(nbrSheets)<br> Dim i As Integer<br> For i = 0 To UBound(Sheets)<br> Sheet = Sheets(i)<br> If Sheet.Name = SelectionName Then<br> Dim objBitMap As Object = Sheet.GetPreviewPNGBitmap(nError)<br> If nError = 0 Then<br> Dim imgPreview As System.Drawing.Image = PictureDispConverter.Convert(objBitMap)<br> Return imgPreview<br> imgPreview.Dispose()<br> Exit Function<br> Else<br> Select Case nError<br> Case 2<br> MsgBox("Error: No preview data stored with document.")<br> Case 4<br> MsgBox("Error: Failed to make the bitmap.")<br> End Select<br> End If<br> End If<br> Next<br> swDoc.CloseDoc()<br> ElseIf nDocType = SwDmDocumentType.swDmDocumentAssembly Or nDocType = SwDmDocumentType.swDmDocumentPart Then<br> swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)<br> swCfgMgr = swDoc.ConfigurationManager<br> vCfgNameArr = swCfgMgr.GetConfigurationNames<br> For Each vCfgName In vCfgNameArr<br> If vCfgName = SelectionName Then<br> swCfg = swCfgMgr.GetConfigurationByName(vCfgName)<br> Dim objBitMap As Object = swCfg.GetPreviewBitmap(nRetVal)<br> Dim imgPreview As System.Drawing.Image = PictureDispConverter.Convert(objBitMap)<br> Return imgPreview<br> imgPreview.Dispose()<br> Exit Function<br> End If<br> Next<br> swDoc.CloseDoc()<br> Else<br> Return Nothing<br> Exit Function<br> End If</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> Return Nothing<br> End Function</span></p><p></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;">Public Class PictureDispConverter</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> Inherits AxHost</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> Public Sub New()<br> MyBase.New("56174C86-1546-4778-8EE6-B6AC606875E7")<br> End Sub</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;"> Public Shared Function Convert(ByVal objIDispImage As Object) As System.Drawing.Image<br> Dim objPicture As Image<br> objPicture = GetPictureFromIPicture(objIDispImage)<br> Return objPicture<br> End Function</span></p><p><span style="color: #0000ff; font-family: Consolas; font-size: 10pt;">End Class</span></p><p></p>SolidworksApi macros