大家都知道在Solidworks中使用Pack and Go打包时有三种打包类型可以选择,分别是“平展到单一文件夹”、“平展到最少文件夹”、以及“保留完整的文件夹结构”三种类型,如下图所示:
在使用Pack and Go打包时的代码如下:
public static void SavePackAndGo(SldWorks swApp, string Path, string Path1)
{
ModelDoc2 swModelDoc = default(ModelDoc2);
ModelDocExtension swModelDocExt = default(ModelDocExtension);
PackAndGo swPackAndGo = default(PackAndGo);
string openFile = null;
bool status = false;
int warnings = 0;
int errors = 0;
int i = 0;
int namesCount = 0;
string myPath = null;
int[] statuses = null;
// Open assembly
openFile = Path;
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);
// Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components
swPackAndGo.IncludeDrawings = true;
Debug.Print(" Include drawings: " + swPackAndGo.IncludeDrawings);
swPackAndGo.IncludeSimulationResults = true;
Debug.Print(" Include SOLIDWORKS Simulation results: " + swPackAndGo.IncludeSimulationResults);
swPackAndGo.IncludeToolboxComponents = true;
Debug.Print(" Include SOLIDWORKS Toolbox components: " + swPackAndGo.IncludeToolboxComponents);
// 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]);
}
}
swPackAndGo.FlattenToSingleFolder = false;
// Set folder where to save the files
myPath = Path1 + "\\";
status = swPackAndGo.SetSaveToName(true, myPath);
// Flatten the Pack and Go folder structure; save all files to the root directory
//swPackAndGo.FlattenToSingleFolder = true;
//swPackAndGo.
// 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);
}
在上述代码中有如下图所示的一行代码是控制打包类型的:
当你赋予bool值为false时,打包类型为第三种“保留完整的文件夹结构”,当你赋予bool值为true时,打包类型为第一种“平展到单一文件夹”。
这时细心的大家就会发现为啥没有第二种呢,这就是我今天和大家分享的。我现在也没有发现在使用Pack and Go打包时如何才能选择第二种类型“平展到最少文件夹”进行打包,如果有大神知道,可以私聊我。大家共同进步。