Pack and Go an Assembly (C#)
This example shows how to get the names of the path and files of an assembly
document, assign them new names, add a prefix and suffix to the new names, and save the newly named files to a different path using the Pack and Go
interface.
//------------------------------------------
// Preconditions:
// 1. Specified assembly exists.
// 2. The folder, c:\temp, exists.
// 3. Open the Immediate window.
// 4. Run the macro.
//
// Postconditions:
// 1. Names of the current path and filenames
// of the assembly documents are printed to Immediate window.
// 2. Names of the default path and filenames to which to
// save assembly documents are printed to Immediate window.
// 3. User-specified path and user-named filenames for assembly documents
// created.
// 4. Prefix and suffix added to user-named filenames.
// 5. Names of user-specified path and user-named filenames printed to
// Immediate window.
// 6. User-named files created in user-specified path using Pack and Go.
// 7. Examine c:\temp to verify.
//-------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using
System.Runtime.InteropServices;
namespace
PackAndGoCSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
ModelDoc2 swModelDoc = default(ModelDoc2);
ModelDocExtension swModelDocExt = default(ModelDocExtension);
PackAndGo swPackAndGo = default(PackAndGo);
string openFile
= null;
string
myFileName = null;
bool status =
false;
int warnings =
0;
int errors = 0;
int i = 0;
int j = 0;
int namesCount
= 0;
string myPath =
null;
int[] statuses
= null;
// Open assembly
openFile =
"C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\handle.sldasm";
swModelDoc = (ModelDoc2)swApp.OpenDoc6(openFile, (int)swDocumentTypes_e.swDocASSEMBLY,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"",
ref errors,
ref warnings);
swModelDocExt = (ModelDocExtension)swModelDoc.Extension;
// Get Pack and Go Object
Debug.Print("Pack
and Go");
swPackAndGo = (PackAndGo)swModelDocExt.GetPackAndGo();
// Get number of documents in assembly
namesCount = swPackAndGo.GetDocumentNamesCount();
Debug.Print(" Number
of model documents: " + namesCount);
// Get whether to include any drawings
and simulation results
Debug.Print(" Include
drawings: " + swPackAndGo.IncludeDrawings);
Debug.Print(" Include
simulation results: " + swPackAndGo.IncludeSimulationResults);
// Get current paths and filenames of the
assembly's documents
object
fileNames;
object[]
pgFileNames = new
object[namesCount - 1];
status = swPackAndGo.GetDocumentNames(out
fileNames);
pgFileNames = (object[])fileNames;
Debug.Print("");
Debug.Print(" Current
path and filenames: ");
if ((pgFileNames
!= null))
{
for (i = 0;
i <= pgFileNames.GetUpperBound(0); i++)
{
Debug.Print(" The
path and filename is: " + pgFileNames[i]);
}
}
// Get current save-to paths and
filenames of the assembly's documents
object
pgFileStatus;
status = swPackAndGo.GetDocumentSaveToNames(out
fileNames, out pgFileStatus);
pgFileNames = (object[])fileNames;
Debug.Print("");
Debug.Print(" Current
default save-to filenames: ");
if ((pgFileNames
!= null))
{
for (i = 0;
i <= pgFileNames.GetUpperBound(0); i++)
{
Debug.Print("
The path and filename is: " + pgFileNames[i]);
}
}
// Folder where to save the files
myPath =
"C:\\temp\\";
// Create your own filenames for the
model's documents
string[]
newFileNames = new
string[namesCount];
Debug.Print("");
Debug.Print(" My
Pack and Go path and filenames before adding prefix and suffix: ");
j = 0;
for (i = 0; i
<= namesCount - 1; i++)
{
myFileName = (string)pgFileNames[i];
// Determine type of SolidWorks file
based on file extension
if
(myFileName.EndsWith("sldprt"))
{
myFileName = j + ".sldprt";
}
else
if (myFileName.EndsWith("sldasm"))
{
myFileName = j + ".sldasm";
}
else
if (myFileName.EndsWith("slddrw"))
{
myFileName = j + ".slddrw";
}
else
{
// Only packing up SolidWorks
files
return;
}
myFileName = myPath + myFileName;
newFileNames[i] = (string)myFileName;
Debug.Print(" My
path and filename is: " + newFileNames[i]);
j = j + 1;
}
// Set document paths and filenames for
Pack and Go
BStrWrapper[]
pgSetFileNames;
pgSetFileNames = ObjectArrayToBStrWrapperArray(newFileNames);
status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames);
// Add a prefix and suffix to the
filenames
swPackAndGo.AddPrefix =
"SW";
swPackAndGo.AddSuffix = "PackAndGo";
// Verify document paths and filenames
after adding prefix and suffix
object
getFileNames;
object
getDocumentStatus;
string[]
pgGetFileNames = new
string[namesCount - 1];
status = swPackAndGo.GetDocumentSaveToNames(out
getFileNames, out
getDocumentStatus);
pgGetFileNames = (string[])getFileNames;
Debug.Print("");
Debug.Print(" My
Pack and Go path and filenames after adding prefix and suffix: ");
for (i = 0; i
<= namesCount - 1; i++)
{
Debug.Print(" My
path and filename is: " + pgGetFileNames[i]);
}
// Pack and Go
statuses = (int[])swModelDocExt.SavePackAndGo(swPackAndGo);
}
public
BStrWrapper[]
ObjectArrayToBStrWrapperArray(object[]
SwObjects)
{
int arraySize;
arraySize = SwObjects.GetUpperBound(0);
BStrWrapper[]
dispwrap = new
BStrWrapper[arraySize + 1];
int arrayIndex;
for (arrayIndex
= 0; arrayIndex < arraySize + 1; arrayIndex++)
{
dispwrap[arrayIndex] = new
BStrWrapper((string)(SwObjects[arrayIndex]));
}
return dispwrap;
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}