2 Replies Latest reply on Apr 4, 2018 2:49 PM by Matt Bieringer

    Preview Sheet Bitmap Blank Image

    Matt Bieringer

      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.

       

       

       

          Public Function SheetImages(SelectionName As String) As Image
              Dim swClassFact As SwDMClassFactory
              Dim swDocMgr As ISwDMApplication
              Dim swDoc As SwDMDocument10
              Dim sDocFileName As String = BrowseTB.Text
              Dim nDocType As SwDmDocumentType
              Dim nRetVal As SwDmDocumentOpenError
              Dim sLicenseKey As String = My.Settings.swDocMgrKey
              Dim swCfgMgr As SwDMConfigurationMgr
              Dim vCfgNameArr As Object
              Dim vCfgName As Object
              Dim swCfg As SwDMConfiguration7
              swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory")
              swDocMgr = swClassFact.GetApplication(sLicenseKey)
              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
                  nDocType = SwDmDocumentType.swDmDocumentUnknown
                  Return Nothing
                  Exit Function
              End If
              If nDocType = SwDmDocumentType.swDmDocumentDrawing Then
                  swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)
                  If (swDoc Is Nothing) Then
                      MsgBox("Unable to open document.")
                  End If
                  Dim Sheets As Object
                  Dim Sheet As SwDMSheet2
                  Dim nError As Integer
                  Sheets = swDoc.GetSheets
                  Dim nbrSheets As Integer
                  nbrSheets = CLng(UBound(Sheets)) + 1
                  Dim snbrSheets As String = Convert.ToString(nbrSheets)
                  Dim i As Integer
                  For i = 0 To UBound(Sheets)
                      Sheet = Sheets(i)
                      If Sheet.Name = SelectionName Then
                          Dim objBitMap As Object = Sheet.GetPreviewPNGBitmap(nError)
                          If nError = 0 Then
                              Dim imgPreview As System.Drawing.Image = PictureDispConverter.Convert(objBitMap)
                              Return imgPreview
                              imgPreview.Dispose()
                              Exit Function
                          Else
                              Select Case nError
                                  Case 2
                                      MsgBox("Error: No preview data stored with document.")
                                  Case 4
                                      MsgBox("Error: Failed to make the bitmap.")
                              End Select
                          End If
                      End If
                  Next
                  swDoc.CloseDoc()
              ElseIf nDocType = SwDmDocumentType.swDmDocumentAssembly Or nDocType = SwDmDocumentType.swDmDocumentPart Then
                  swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)
                  swCfgMgr = swDoc.ConfigurationManager
                  vCfgNameArr = swCfgMgr.GetConfigurationNames
                  For Each vCfgName In vCfgNameArr
                      If vCfgName = SelectionName Then
                          swCfg = swCfgMgr.GetConfigurationByName(vCfgName)
                          Dim objBitMap As Object = swCfg.GetPreviewBitmap(nRetVal)
                          Dim imgPreview As System.Drawing.Image = PictureDispConverter.Convert(objBitMap)
                          Return imgPreview
                          imgPreview.Dispose()
                          Exit Function
                      End If
                  Next
                  swDoc.CloseDoc()
              Else
                  Return Nothing
                  Exit Function
              End If
              Return Nothing
          End Function
      

       

       

       

       

      Public Class PictureDispConverter
          Inherits AxHost
          Public Sub New()
              MyBase.New("56174C86-1546-4778-8EE6-B6AC606875E7")
          End Sub
          Public Shared Function Convert(ByVal objIDispImage As Object) As System.Drawing.Image
              Dim objPicture As Image
              objPicture = GetPictureFromIPicture(objIDispImage)
              Return objPicture
          End Function
      End Class