Hide Table of Contents

Get Preview Bitmaps of Drawing Sheets Example (C#)

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.

//--------------------------------------------------------------------------

 

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Diagnostics;

using SolidWorks.Interop.swdocumentmgr;

using Microsoft.VisualBasic.Compatibility.VB6;

using System.Drawing;

using stdole;

using System.Windows.Forms;

 

namespace PNGBitmapsISwDMSheet2CSharp.csproj

{

    partial class SolidWorksMacro

    {

        public void Main()

        {

            SwDMClassFactory swClassFact;

            SwDMApplication swDocMgr;

            SwDMDocument10 swDoc;

            string sDocFileName;

            SwDmDocumentType nDocType;

            SwDmDocumentOpenError nRetVal;

            string sLicenseKey = null;

 

            // 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 = new SwDMClassFactory();

            swDocMgr = swClassFact.GetApplication(sLicenseKey);

            swDoc = (SwDMDocument10)swDocMgr.GetDocument(sDocFileName, nDocType, true, out nRetVal);

            if ((swDoc == null))

            {

                MessageBox.Show("Unable to open document.");

            }

 

            stdole.IPictureDisp pPreview;

            Image image;

            object Sheets;

            object[] oSheets;

            SwDMSheet2 Sheet;

            SwDmPreviewError nErrors;

            int i;

 

            // Get the sheets in the drawing document

            Sheets = (object[])swDoc.GetSheets();

            oSheets = (object[])Sheets;

string nbrSheets;

nbrSheets = System.Convert.ToString(oSheets.Length);

Debug.Print ("Number of sheets: " + nbrSheets);

Debug.print (" ");

 

         

 for (i = 0; i < oSheets.Length; i++)

            {

                Sheet = (SwDMSheet2)oSheets[i];

                Debug.Print(Sheet.Name);

                Debug.Print(Sheet.PreviewPNGStreamName);

                pPreview = (stdole.IPictureDisp)Sheet.GetPreviewPNGBitmap(out nErrors);

                if (nErrors == 0)

                {

                    // 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", System.Drawing.Imaging.ImageFormat.Png);

                    byte[] PreviewPNGByteArray;

                    string nbrBytes;

                    PreviewPNGByteArray = (byte[])Sheet.GetPreviewPNGBitmapBytes(out nErrors);

                    nbrBytes = System.Convert.ToString(PreviewPNGByteArray.Length);

                    Debug.Print ("Number of bytes in preview's PNG byte array: " + nbrBytes);

                }

                else

                {

                    switch (nErrors)

                    {

                        case SwDmPreviewError.swDmPreviewErrorNoPreview:

                            Debug.Print("Error: No preview data stored wiht document.");

                            break;

                        case SwDmPreviewError.swDmPreviewErrorMakeBmpFail:

                            Debug.Print("Error: Failed to make the bitmap.");

                            break;

                    }

            }

            swDoc.CloseDoc();

 

        }

 

        /// <summary>

        /// The SldWorks swApp variable is pre-assigned for you.

        /// </summary>

        public SldWorks swApp;

 

    }

}



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 (C#)
*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.