/// <summary> /// 模型打包 /// </summary> /// <param name="suffix">后缀</param> /// <param name="swApp">SW程序</param> /// <param name="modelPath">模型地址</param> /// <param name="itemPath">目标地址</param> /// <returns></returns> public static string PackAndGoFunc(string suffix, SldWorks swApp, string modelPath, string itemPath) { swApp.CommandInProgress = true; int warnings = 0; int errors = 0; ModelDoc2 swModelDoc = default(ModelDoc2); ModelDocExtension swModelDocExt = default(ModelDocExtension); PackAndGo swPackAndGo = default(PackAndGo); //打开需要pack的模型 swModelDoc = (ModelDoc2)swApp.OpenDoc6(modelPath, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings); swModelDocExt = (ModelDocExtension)swModelDoc.Extension; 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 = false; swPackAndGo.IncludeSimulationResults = false; swPackAndGo.IncludeToolboxComponents = false; // Set folder where to save the files,目标存放地址 swPackAndGo.SetSaveToName(true, itemPath); //将文件展开到一个文件夹内,不要原始模型的文件夹结构 // 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 = "_" + suffix; try { // Pack and Go,执行PackAndGo swModelDocExt.SavePackAndGo(swPackAndGo); } catch (Exception ex) { throw new Exception("PackandGo过程中出现异常:" + ex.Message); } finally { swApp.CloseDoc(swModelDoc.GetTitle()); swModelDoc = null; swApp.CommandInProgress = false;//及时关闭外部命令调用,否则影响SolidWorks的使用 } string modelPathName = modelPath.Substring(modelPath.LastIndexOf(@"\") + 1); //返回packandgo后模型的地址 return(itemPath + @"\" + modelPathName.Substring(0, modelPathName.LastIndexOf(".")) + "_" + suffix + ".sldasm"); }
public void SetUp() { _projectContext = A.Fake <IProjectContext>(); _workbenchViewModel = A.Fake <IWorkbenchViewModel>(); A.CallTo(() => _workbenchViewModel.ProjectContext).Returns(_projectContext); _fileDialogManager = A.Fake <IFileDialogManager>(); _modelExporter = A.Fake <IModelExporter>(); _gtfCopier = A.Fake <IGtfCopier>(); _batchCopier = A.Fake <IBatchCopier>(); _zipper = A.Fake <IZipper>(); _monitorCopier = A.Fake <IMonitorCopier>(); var fileService = A.Fake <IFileService>(); _schedulerProvider = A.Fake <ITaskSchedulerProvider>(); A.CallTo( () => _schedulerProvider.PoolScheduler.InvokeAsync( A <Action> ._, A <CancellationToken> ._, A <ExecutionMode> ._)).Invokes(oc => oc.GetArgument <Action>(0)()); _packAndGo = new PackAndGo(_fileDialogManager, fileService, _modelExporter, _gtfCopier, _batchCopier, _zipper, _monitorCopier, _schedulerProvider); }
static void Main() { ModelDoc2 swModelDoc = default(ModelDoc2); ModelDocExtension swModelDocExt = default(ModelDocExtension); PackAndGo swPackAndGo = default(PackAndGo); //SldWorks swApp = new SldWorks(); //GetSolidworks getSW = new GetSolidworks(); Tuple <SldWorks, Process> processes = GetSolidworks.Solidworks(1); swApp = processes.Item1; Process = processes.Item2; //swApp.Visible = false; //add another line to get item 2 which is the process which you can then use to kill when it derps /* To be used to more easily choose which assemblies to pack and go * List<string> trNames = new List<string>(new string[] { "500", "525"}); * List<string> cmtNames = new List<string>(new string[] { "500", "525" }); * * for (int i = 0; i <= trNames.Count - 1; i++) * { * trNames[i] = "TR-34-20-" + trNames[i]; * } * for (int i = 0; i <= cmtNames.Count - 1; i++) * { * cmtNames[i] = "CMT-34-20-" + cmtNames[i]; * } */ List <string> modelNames = new List <string>(new string[] { "TR-34-20-400", "TR-34-20-425", "TR-34-20-500", "TR-34-20-525", "TR-34-20-530", "TR-34-20-600", "TR-34-20-625", "TR-34-20-630", "TR-34-20-700", "TR-34-20-725", "TR-34-20-730", "TR-34-20-900", "TR-34-20-925", "TR-34-20-930", "CMT-34-20-400", "CMT-34-20-425", "CMT-34-20-500", "CMT-34-20-525", "CMT-34-20-530", "CMT-34-20-600", "CMT-34-20-625", "CMT-34-20-630", "CMT-34-20-700", "CMT-34-20-725", "CMT-34-20-730", "CMT-34-20-900", "CMT-34-20-925", "CMT-34-20-930" }); //List<string> modelNames = new List<string>(new string[] { "TR-34-20-400", "TR-34-20-425", "TR-34-20-500" }); int modelCount = modelNames.Count; while (modelNames.Count > 0) { SetCheckTimer(); int j = 0; try { string openFile = null; bool status = false; int warnings = 0; int errors = 0; int i = 0; int namesCount = 0; string savePath = null; int[] statuses = null; openFile = @"C:\Configurator\" + modelNames[j] + ".sldasm"; Debug.Print("Performing pack and go on " + modelNames[j]); Console.WriteLine("Performing pack and go on " + modelNames[j]); swModelDoc = swApp.OpenDoc6(openFile, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings); swModelDoc.Visible = false; swApp.Visible = false; //opendoc6 seems to remove the visiblility setting swModelDocExt = (ModelDocExtension)swModelDoc.Extension; swPackAndGo = (PackAndGo)swModelDocExt.GetPackAndGo(); savePath = @"C:\Configurator\PackandGoTest\test.zip"; status = swPackAndGo.SetSaveToName(false, savePath); swPackAndGo.FlattenToSingleFolder = true; namesCount = swPackAndGo.GetDocumentNamesCount(); Debug.Print(" Number of model documents: " + namesCount); Console.WriteLine(" 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 = false; //Debug.Print(" Include SOLIDWORKS Simulation results: " + swPackAndGo.IncludeSimulationResults); swPackAndGo.IncludeToolboxComponents = false; //Debug.Print(" Include SOLIDWORKS Toolbox components: " + swPackAndGo.IncludeToolboxComponents); swPackAndGo.IncludeSuppressed = true; // Verify document paths and filenames after adding prefix and suffix object getFileNames; object getDocumentStatus; status = swPackAndGo.GetDocumentSaveToNames(out getFileNames, out getDocumentStatus); string[] pgGetFileNames = (string[])getFileNames; if (pgGetFileNames.Length == 0) { status = swPackAndGo.GetDocumentSaveToNames(out getFileNames, out getDocumentStatus); pgGetFileNames = (string[])getFileNames; } for (int z = 0; z < pgGetFileNames.Length; z++) { if (pgGetFileNames[z].StartsWith(@"c:\configurator")) { } else { Debug.Print(pgGetFileNames[z]); Console.WriteLine("Please Remove This File and Try Again: " + pgGetFileNames[z]); } pgGetFileNames[z] = GetProperFilePathCapitalization(pgGetFileNames[z]); } swPackAndGo.SetDocumentSaveToNames(pgGetFileNames); //for (int z = 0;z< ) //pgGetFileNames = (string[])getFileNames; /*This section is unnecessary and clutters the debug window, add back in at own preference * 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); //swApp.CloseDoc(modelNames[j]); checkTimer.Stop(); swApp.CloseAllDocuments(true); modelNames.RemoveAt(0); //removes as the very last step to ensure successful execution } catch (Exception e) { /* * Process swProcess = new Process(); * int processID = swApp.GetProcessID(); * swProcess = Process.GetProcessById(processID); * swProcess.Kill(); * while (swProcess.HasExited == false) { } */ checkTimer.Stop(); swApp = null; try { Process.Kill(); } catch { } Debug.Print("Solidworks Crash: Restarting Now"); Console.WriteLine("Solidworks Crash: Restarting Now"); Debug.Print(e.StackTrace); while (swApp == null) { Tuple <SldWorks, Process> tuple = GetSolidworks.Solidworks(1); swApp = tuple.Item1; Process = tuple.Item2; } } } swApp.Visible = true; swApp.SendMsgToUser2("Pack and Go Is Complete!", 2, 2); }