This example shows how
to write a Parasolid partition stream to a file for a part or assembly using the SolidWorks Document Manager API.
NOTE: To get the name of the stream and a preview
bitmap of the sheet active when the drawing was last saved, use ISwDMDocument10::GetPreviewBitmap and ISwDMDocument10::PreviewStreamName. To get the name of the
streams and the preview bitmaps for all sheets in a drawing, use ISwDMSheet2::GetPreviewPNGBitmap and
ISwDMSheet2::PreviewPNGStreamName.
//--------------------------------------------------------------------------
// Preconditions:
// 1. Read the SolidWorks Document Manager API Getting Started
// topic and ensure that the required DLLs have been registered.
// 2. Copy and paste this module into a C# console application
// in Microsoft
Visual Studio.
// 3. Ensure that the specified part exists (or point to
// another part or
assembly).
// 4. Add the SolidWorks.Interop.swdocumentmgr.dll reference
// to the
project:
// a. Right-click the solution in Solution Explorer.
// b. Select Add Reference.
// c. Click the Browse tab.
// d. Load:
//
<SolidWorks_install_dir>\api\redist\CLR2\SolidWorks.Interop.swdocumentmgr.dll
// 5. Add the following references to the project:
// System.Drawing.dll,
stdole.dll,
// and Microsoft.VisualBasic.Compatibility.VB6.dll.
// 6. Substitute <your_license_code> with your SolidWorks
// Document Manager license key.
// 7. Compile and run this program in Debug mode.
//
// Postconditions: Inspect the Output Window and the code
// to see how the API is used.
//--------------------------------------------------------------------------
using
System;
using
System.Diagnostics;
using
SolidWorks.Interop.swdocumentmgr;
using
Microsoft.VisualBasic.Compatibility.VB6;
using
System.Drawing;
using
stdole;
public
class
Program
{
public
enum
SwDmDocumentVersion_e
{
SwDmDocumentVersionSolidWorks_2000 = 1500,
SwDmDocumentVersionSolidWorks_2001 = 1750,
SwDmDocumentVersionSolidWorks_2001Plus = 1950,
SwDmDocumentVersionSolidWorks_2003 = 2200,
SwDmDocumentVersionSolidWorks_2004 = 2500,
SwDmDocumentVersionSolidWorks_2005 = 2800,
SwDmDocumentVersionSolidWorks_2006 = 3100,
SwDmDocumentVersionSolidWorks_2007 = 3400,
SwDmDocumentVersionSolidWorks_2008 = 3800,
SwDmDocumentVersionSolidWorks_2009 = 4100,
SwDmDocumentVersionSolidWorks_2010 = 4400
}
static
void
Main(string[]
args)
{
const
string
sLicenseKey = "<your_license_code>";
//Specify license key
const
string
sDocFileName = "C:\\Program Files\\SolidWorks
Corp\\SolidWorks\\samples\\tutorial\\multibody\\multi_inter.sldprt";
//Specify document
const
string
sExtractDir = "c:\\temp\\";
SwDMClassFactory
swClassFact = default(SwDMClassFactory);
SwDMApplication
swDocMgr = default(SwDMApplication);
SwDMDocument
swDoc = default(SwDMDocument);
SwDMConfigurationMgr
swCfgMgr = default(SwDMConfigurationMgr);
string[]
vCfgNameArr = null;
SwDMConfiguration2
swCfg = default(SwDMConfiguration2);
IPictureDisp
pPreview = default(IPictureDisp);
SwDmDocumentType
nDocType = 0;
SwDmDocumentOpenError
nRetVal = 0;
SwDmBodyError
nRetVal2 = 0;
SwDmPreviewError
nRetVal3 = 0;
int
i = 0;
Image
image = default(Image);
// Determine type of SolidWorks
file based on file extension
if
(sDocFileName.EndsWith("sldprt"))
{
nDocType = SwDmDocumentType.swDmDocumentPart;
}
else
if (sDocFileName.EndsWith("sldasm"))
{
nDocType = SwDmDocumentType.swDmDocumentAssembly;
}
else
if (sDocFileName.EndsWith("slddrw"))
{
nDocType = SwDmDocumentType.swDmDocumentDrawing;
}
else
{
// Not a SolidWorks file
nDocType =
SwDmDocumentType.swDmDocumentUnknown;
// So cannot open
return;
}
// Because drawing documents do
not have configurations,
//
only continue running the macro if the document
//
is a part or assembly document
if
((nDocType != SwDmDocumentType.swDmDocumentDrawing))
{
swClassFact = new
SwDMClassFactory();
swDocMgr = swClassFact.GetApplication(sLicenseKey);
swDoc = (SwDMDocument)swDocMgr.GetDocument(sDocFileName,
nDocType, false,
out
nRetVal);
Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone
== nRetVal);
swCfgMgr = swDoc.ConfigurationManager;
Debug.Print("File
= " + swDoc.FullName);
Debug.Print("
ActiveCfgName = " +
swCfgMgr.GetActiveConfigurationName());
Debug.Print("");
Debug.Print("
Version = " + swDoc.GetVersion());
Debug.Print("
Author = " + swDoc.Author);
Debug.Print("
Comments = " + swDoc.Comments);
Debug.Print("
CreationDate = " + swDoc.CreationDate);
Debug.Print("
Keywords = " + swDoc.Keywords);
Debug.Print("
LastSavedBy = " + swDoc.LastSavedBy);
Debug.Print("
LastSavedDate = " + swDoc.LastSavedDate);
Debug.Print("
Subject = " + swDoc.Subject);
Debug.Print("
Title = " + swDoc.Title);
vCfgNameArr = (string[])swCfgMgr.GetConfigurationNames();
foreach
(string
vCfgName in
vCfgNameArr)
{
swCfg = (SwDMConfiguration2)swCfgMgr.GetConfigurationByName(vCfgName);
//
SwDMConfiguration.GetPreviewBitmap code throws an unmanaged COM exception
//
for out-of-process C# console applications.
//
Try the following code in VSTA C# macros and SolidWorks add-ins only.
//pPreview
= (IPictureDisp)swCfg.GetPreviewBitmap(out nRetVal3);
//Debug.Assert(SwDmPreviewError.swDmPreviewErrorNone
== nRetVal3);
//image
= Support.IPictureDispToImage(pPreview);
//image.Save(sExtractDir
+ vCfgName + ".bmp");
Debug.Print("
" + vCfgName);
Debug.Print("
Description = " + swCfg.Description);
Debug.Print("
ParentCfgName = " + swCfg.GetParentConfigurationName());
Debug.Print("
Preview stream = " + swCfg.PreviewStreamName);
switch
((SwDmDocumentVersion_e)swDoc.GetVersion())
{
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2000:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2001:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2001Plus:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2003:
Debug.Print("
Body stream = " + swCfg.StreamName);
Debug.Print("
Body count = " + (swCfg.GetBodiesCount()
- 1));
for
(i = 0; i <= swCfg.GetBodiesCount() - 1; i++)
{
nRetVal2 = swCfg.GetBody(i, sExtractDir +
vCfgName + "_"
+ i.ToString() + ".x_b");
Debug.Assert(SwDmBodyError.swDmBodyErrorNone
== nRetVal2);
}
break;
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2004:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2005:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2006:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2007:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2008:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2009:
case
SwDmDocumentVersion_e.SwDmDocumentVersionSolidWorks_2010:
Debug.Print("
Partition stream = " + swCfg.StreamName);
//
SwDMConfiguration2::GetPartitionStream takes filename as an input and writes
//
to it. *.xmp_bin (for NTFS) and *.p_b (for FAT) are the valid extensions
//
of a partition file. PK_PARTITION_receive_u will takes this file name as
//
input to get partitions and bodies in it.
//
Refer to Parasolid documentation for details.
nRetVal2 = swCfg.GetPartitionStream(sExtractDir
+ vCfgName + ".xmp_bin");
Debug.Assert(SwDmBodyError.swDmBodyErrorNone
== nRetVal2);
break;
default:
Debug.Assert(false);
break;
}
}
}
}
}