private Boolean DocCheck() { if (swModel == null) { ExportPDFException e = new ExportPDFException("You must have a drawing document open."); return(false); } else if ((Int32)swModel.GetType() != (Int32)swDocumentTypes_e.swDocDRAWING) { ExportPDFException e = new ExportPDFException("You must have a drawing document open."); return(false); } else if (swModel.GetPathName() == String.Empty) { swModel.Extension.RunCommand((int)swCommands_e.swCommands_SaveAs, swModel.GetTitle()); if (swModel.GetPathName() == string.Empty) { return(false); } return(true); } else { return(true); } }
/// <summary> /// This thing does the saving. /// </summary> /// <param name="fl">A <see cref="System.Collections.Generic.List<>"/> of destination paths.</param> /// <returns>Returns a <see cref="System.Boolean"/>, indicating success.</returns> private Boolean SaveFiles(List <String> fl) { Int32 saveVersion = (Int32)swSaveAsVersion_e.swSaveAsCurrentVersion; Int32 saveOptions = (Int32)swSaveAsOptions_e.swSaveAsOptions_Silent; Int32 refErrors = 0; Int32 refWarnings = 0; Boolean success = true; string tmpPath = Path.GetTempPath(); ModelDocExtension swModExt = default(ModelDocExtension); ExportPdfData swExportPDFData = default(ExportPdfData); foreach (String fileName in fl) { FileInfo fi = new FileInfo(fileName); string tmpFile = tmpPath + "\\" + fi.Name; if (drawingPath != String.Empty) { swFrame.SetStatusBarText(String.Format("Checking path: '{0}'", fileName)); if (!CreatePath(fileName)) { ExportPDFException e = new ExportPDFException("Unable to save file, folder could not be created."); //e.Data.Add("who", System.Environment.UserName); //e.Data.Add("when", DateTime.Now); throw e; } String[] obj = (string[])swDraw.GetSheetNames(); object[] objs = new object[obj.Length - 1]; DispatchWrapper[] dr = new DispatchWrapper[obj.Length - 1]; for (int i = 0; i < obj.Length - 1; i++) { swDraw.ActivateSheet(obj[i]); Sheet s = (Sheet)swDraw.GetCurrentSheet(); objs[i] = s; dr[i] = new DispatchWrapper(objs[i]); } swFrame.SetStatusBarText(String.Format("Exporting '{0}'", fileName)); bool layerPrint = swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swPDFExportIncludeLayersNotToPrint); swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swPDFExportIncludeLayersNotToPrint, true); swExportPDFData = swApp.GetExportFileData((int)swExportDataFileType_e.swExportPdfData); swModExt = swModel.Extension; success = swExportPDFData.SetSheets((int)swExportDataSheetsToExport_e.swExportData_ExportAllSheets, (dr)); success = swModExt.SaveAs(tmpFile, saveVersion, saveOptions, swExportPDFData, ref refErrors, ref refWarnings); //success = swModel.SaveAs4(tmpFile, saveVersion, saveOptions, ref refErrors, ref refWarnings); swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swPDFExportIncludeLayersNotToPrint, layerPrint); try { File.Copy(tmpFile, fileName, true); } catch (UnauthorizedAccessException uae) { throw new ExportPDFException( String.Format("You don't have the reqired permission to access '{0}'.", fileName), uae); } catch (ArgumentException ae) { throw new ExportPDFException( String.Format("Either '{0}' or '{1}' is not a proper file name.", tmpFile, fileName), ae); } catch (PathTooLongException ptle) { throw new ExportPDFException( String.Format("Source='{0}'; Dest='{1}' <= One of these is too long.", tmpFile, fileName), ptle); } catch (DirectoryNotFoundException dnfe) { throw new ExportPDFException( String.Format("Source='{0}'; Dest='{1}' <= One of these is invalid.", tmpFile, fileName), dnfe); } catch (FileNotFoundException fnfe) { throw new ExportPDFException( String.Format("Crap! I lost '{0}'!", tmpFile), fnfe); } catch (IOException) { System.Windows.Forms.MessageBox.Show( String.Format("If you have the file, '{0}', selected in an Explorer window, " + "you may have to close it.", fileName), "This file is open somewhere.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return(false); } catch (NotSupportedException nse) { throw new ExportPDFException( String.Format("Source='{0}'; Dest='{1}' <= One of these is an invalid format.", tmpFile, fileName), nse); } if (!File.Exists(fileName)) { success = false; } if (success) { swFrame.SetStatusBarText(String.Format("Exported '{0}'", fileName)); if (fileName.StartsWith(Properties.Settings.Default.KPath) && APathSet.WriteToDb) { savedFile = fileName; if (fileName.EndsWith("PDF")) { InsertIntoDb(fileName, Properties.Settings.Default.PDFTable); drwKey = GetKeyCol(fi.Name); } if (fileName.EndsWith(@"EPRT") || fileName.EndsWith("EASM")) { InsertIntoDb(fileName, Properties.Settings.Default.eDrawingTable); } } if ((fileName.StartsWith(Properties.Settings.Default.MetalPath) && fileName.EndsWith("PDF")) && APathSet.WriteToDb) { _metalDrawing = true; InsertIntoDb(fileName, Properties.Settings.Default.metalTable); drwKey = GetKeyCol(fi.Name); } } else { ExportPDFException e = new ExportPDFException(String.Format("Failed to save '{0}'", fileName)); //e.Data.Add("who", System.Environment.UserName); //e.Data.Add("when", DateTime.Now); throw e; } } } if (success) { return(true); } else { return(false); } }
/// <summary> /// Opens and saves models referenced in the open drawing. /// </summary> /// <returns>Returns a <see cref="System.Boolean"/>, should you need one.</returns> public Boolean ExportEDrawings() { ModelDoc2 currentDoc = swModel; string docName = currentDoc.GetPathName(); String sourceName = Path.GetFileNameWithoutExtension(docName); String altPath = Path.GetDirectoryName(docName).Substring(3); ModelDocExtension swModExt = swModel.Extension; String Rev = GetRev(swModExt); String fFormat = String.Empty; List <string> ml = get_list_of_open_docs(); List <String> EdrwTarget = new List <string>(); List <String> STEPTarget = new List <string>(); Boolean measurable = true; Int32 options = 0; Int32 errors = 0; switch (modelType) { case swDocumentTypes_e.swDocASSEMBLY: fFormat = ".EASM"; break; case swDocumentTypes_e.swDocPART: fFormat = ".EPRT"; break; default: ExportPDFException e = new ExportPDFException("Document type error."); //e.Data.Add("who", System.Environment.UserName); //e.Data.Add("when", DateTime.Now); throw e; } swApp.ActivateDoc3(sourcePath, true, options, ref errors); swModel = (ModelDoc2)swApp.ActiveDoc; Configuration swConfig = (Configuration)swModel.GetActiveConfiguration(); swFrame.SetStatusBarText("Positioning model."); swModel.ShowNamedView2("*Dimetric", 9); swModel.ViewZoomtofit2(); if (!swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure)) { swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure, true); measurable = false; } else { measurable = true; } EdrwTarget.Add(String.Format("{0}{1}\\{2}{3}", APathSet.KPath, altPath, sourceName, fFormat)); EdrwTarget.Add(String.Format("{0}{1}\\{2}{3}{4}", APathSet.GPath, altPath, sourceName, Rev, fFormat)); if (APathSet.ExportSTEP) { STEPTarget.Add(String.Format("{0}{1}\\{2}{3}", APathSet.KPath, altPath, sourceName, @".STEP")); STEPTarget.Add(String.Format("{0}{1}\\{2}{3}{4}", APathSet.GPath, altPath, sourceName, Rev, @".STEP")); } Boolean success = SaveSTEPorEDrw(EdrwTarget); if (STEPTarget.Count > 0) { success = SaveSTEPorEDrw(STEPTarget); } if (!measurable) { swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEDrawingsOkayToMeasure, false); } if (!ml.Contains(swModel.GetPathName())) { swFrame.SetStatusBarText("Closing model."); swApp.CloseDoc(sourcePath); } else if (APathSet.SaveFirst) { swModel.SaveSilent(); } int err = 0; swApp.ActivateDoc3(currentDoc.GetTitle(), true, (int)swRebuildOnActivation_e.swDontRebuildActiveDoc, ref err); return(success); }