//-------------------------------------------------------------------------- // Preconditions: // 1. Specified assembly exists. // 2. The folder, c:\temp, exists. // 3. Open the Immediate window. // 4. Run the macro. // // Postconditions: // 1. Prints names of the current path and filenames // of the assembly documents to Immediate window. // 2. Prints names of the default path and filenames to which to // save assembly documents to the Immediate window. // 3. Specifies the Pack and Go destination folder. // 4. Specifies that all files get saved to the root directory of the // Pack and Go destination folder. // 5. Adds prefix and suffix to user-named filenames. // 6. Prints names of user-specified path and user-named filenames to // Immediate window. // 7. Creates user-named files in user-specified path using Pack and Go. // 8. 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; bool status = false; int warnings = 0; int errors = 0; int i = 0; int namesCount = 0; string myPath = null; int[] statuses = null; // Open assembly openFile = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\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); // 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]); } } // Set folder where to save the files myPath = "C:\\temp\\"; status = swPackAndGo.SetSaveToName(true, myPath); // Flatten the Pack and Go folder structure; save all files to the root directory swPackAndGo.FlattenToSingleFolder = true; // 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); } /// /// The SldWorks swApp variable is pre-assigned for you. /// public SldWorks swApp; } }