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;
}
}