Hide Table of Contents

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



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Preview Bitmaps of Drawing Sheets Example (VB.NET)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.