// this is an attempt to get rid of the extra command line step for printing pdfs. // it also uses the display names inside the idw for the pdf names rather than getting it from a Vault property. public Boolean printToPDF(string idw, string outputFolder, string pdfPrinterName, string psToPdfProgName, string ghostScriptWorkingFolder, ref string errMessage, ref string logMessage) { { try { // set log file location XmlConfigurator.Configure(); log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(); foreach (IAppender a in h.Root.Appenders) { if (a is FileAppender) { FileAppender fa = (FileAppender)a; // Programmatically set this to the desired location here string logFileLocation = outputFolder + "PDFPrint.log"; // Uncomment the lines below if you want to retain the base file name // and change the folder name... //FileInfo fileInfo = new FileInfo(fa.File); //logFileLocation = string.Format(@"C:\MySpecialFolder\{0}", fileInfo.Name); fa.File = logFileLocation; fa.ActivateOptions(); break; } } InventorApprentice.ApprenticeServerComponent oApprentice = new ApprenticeServerComponent(); InventorApprentice.ApprenticeServerDrawingDocument drgDoc; oApprentice.Open(idw); drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; int pageCount = 1; List <string> assemblyFileNameList = new List <string>(); idwFile idwFileToPrint = new idwFile(); idwFileToPrint.sheetNames = new List <string>(); idwFileToPrint.idwName = idw; idwFileToPrint.pageCount = drgDoc.Sheets.Count; foreach (Sheet sh in drgDoc.Sheets) { if (sh.DrawingViews.Count > 0) { string modelName; modelName = sh.DrawingViews[1].ReferencedDocumentDescriptor.DisplayName; // this doesn't work right on files with special characters. //modelName = Path.GetFileNameWithoutExtension(modelName); if (modelName.EndsWith(".ipt") || modelName.EndsWith(".iam")) { int index = modelName.LastIndexOf('.'); modelName = index == -1 ? modelName: modelName.Substring(0, index); } idwFileToPrint.sheetNames.Add(modelName); pageCount++; } } log.Info("Sheet Names All Read When Printing " + idwFileToPrint.idwName); string printer = pdfPrinterName; string pdfConverter = psToPdfProgName; string workingDir = ghostScriptWorkingFolder; string psFileName = ""; string pdfFileName = ""; try { InventorApprentice.ApprenticeDrawingPrintManager pMgr; drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; pMgr = (InventorApprentice.ApprenticeDrawingPrintManager)drgDoc.PrintManager; pMgr.Printer = printer; int actualSheetIndex = 1; int modifiedSheetIndex = 1; foreach (Sheet sh in drgDoc.Sheets) { string modelName; //string modelExtension; if (sh.DrawingViews.Count > 0) // added to make sure sheet has at least one drawing view { modelName = sh.DrawingViews[1].ReferencedDocumentDescriptor.DisplayName; // this doesn't work right on files with special characters. //modelName = Path.GetFileNameWithoutExtension(modelName); if (modelName.EndsWith(".ipt") || modelName.EndsWith(".iam")) { int index = modelName.LastIndexOf('.'); modelName = index == -1 ? modelName : modelName.Substring(0, index); } string newName = ""; switch (sh.Orientation) { case PageOrientationTypeEnum.kLandscapePageOrientation: pMgr.Orientation = PrintOrientationEnum.kLandscapeOrientation; break; case PageOrientationTypeEnum.kDefaultPageOrientation: pMgr.Orientation = PrintOrientationEnum.kDefaultOrientation; break; case PageOrientationTypeEnum.kPortraitPageOrientation: pMgr.Orientation = PrintOrientationEnum.kPortraitOrientation; break; } pMgr.SetSheetRange(actualSheetIndex, actualSheetIndex); pMgr.PrintRange = PrintRangeEnum.kPrintSheetRange; pMgr.ScaleMode = InventorApprentice.PrintScaleModeEnum.kPrintBestFitScale; //if (more than one matching pdf name) if (idwFileToPrint.sheetNames.Where(x => x.Equals(idwFileToPrint.sheetNames[modifiedSheetIndex - 1])).Count() > 1) { newName = outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + ".pdf"; if (System.IO.File.Exists(outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + ".pdf")) { assemblyFileNameList.Add(newName); newName = outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + "~" + 1 + ".pdf"; if (System.IO.File.Exists(newName)) { System.IO.File.Delete(newName); } System.IO.File.Move(outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + ".pdf", newName); assemblyFileNameList.Add(newName); } } psFileName = outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + ".ps"; pdfFileName = outputFolder + idwFileToPrint.sheetNames[modifiedSheetIndex - 1] + ".pdf"; // for some reason if a ps filename contains a comma it doesn't want to print. // we'll replace it with a tilde. if (psFileName.Contains(",")) { psFileName = psFileName.Replace(',', '~'); log.Warn("One or more characters replaced with '~' in " + pdfFileName); //logMessage += "One or more characters replaced with '~' in " + pdfFileName + "\r\n"; } if (psFileName.Contains("°")) { psFileName = psFileName.Replace('°', '~'); log.Warn("One or more characters replaced with '°' in " + pdfFileName); //logMessage += "One or more characters replaced with '°' in " + pdfFileName + "\r\n"; } pMgr.PrintToFile(psFileName); if (System.IO.File.Exists(psFileName)) { log.Info("PS file generated for " + psFileName); } else { log.Warn("PS file for " + psFileName + "could not be generated."); continue; // skip trying to create a pdf if we couldn't generate a ps } // notice: // gs doesn't seem to be able to handle the degree symbol // all filenames with a degree symbol will lose it when run through this script Process oProc = new Process(); // need the full path to the program if we want to set UseShellExecute to false ProcessStartInfo startInfo = new ProcessStartInfo(pdfConverter); startInfo.WorkingDirectory = workingDir; startInfo.Arguments = @"""" + psFileName + @"""" + " " + @"""" + pdfFileName + @""""; startInfo.CreateNoWindow = true; oProc.StartInfo = startInfo; oProc.StartInfo.UseShellExecute = false; oProc.Start(); oProc.WaitForExit(); if (assemblyFileNameList != null) { if (assemblyFileNameList.Count > 1) // combine multiple assembly drawings into one pdf file { // Open the input files PdfDocument inputDocument1 = new PdfDocument(); PdfDocument inputDocument2 = new PdfDocument(); if (System.IO.File.Exists(assemblyFileNameList[0])) { inputDocument1 = PdfReader.Open(assemblyFileNameList[0], PdfDocumentOpenMode.Import); } if (System.IO.File.Exists(assemblyFileNameList[1])) { inputDocument2 = PdfReader.Open(assemblyFileNameList[1], PdfDocumentOpenMode.Import); } // Create the output document PdfDocument outputDocument = new PdfDocument(); // Show consecutive pages facing. Requires Acrobat 5 or higher. outputDocument.PageLayout = inputDocument1.PageLayout; int count = Math.Max(inputDocument1.PageCount, inputDocument2.PageCount); for (int idx = 0; idx < count; idx++) { PdfPage page1 = new PdfPage(); PdfPage page2 = new PdfPage(); if (inputDocument1.PageCount > idx) { page1 = inputDocument1.Pages[idx]; page1 = outputDocument.AddPage(page1); } if (inputDocument2.PageCount > idx) { page2 = inputDocument2.Pages[idx]; page2 = outputDocument.AddPage(page2); } } if (System.IO.File.Exists(assemblyFileNameList[0])) { System.IO.File.Delete(assemblyFileNameList[0]); } // Save the document... while (!(System.IO.File.Exists(assemblyFileNameList[0]))) { string filename = assemblyFileNameList[0]; outputDocument.Save(filename); } // delete the temp file and clear the list if (System.IO.File.Exists(assemblyFileNameList[1])) { System.IO.File.Delete(assemblyFileNameList[1]); } assemblyFileNameList.Clear(); } } System.IO.File.Delete(psFileName); actualSheetIndex++; modifiedSheetIndex++; } else { actualSheetIndex++; // still need to increment sheet index, even if no drawing view was found // on current sheet... } // double check to make sure file got generated and saved properly. if (!System.IO.File.Exists(pdfFileName)) { log.Warn("No PDF Generated for " + pdfFileName); //logMessage += "No PDF Generated for " + pdfFileName + "\r\n"; } else { log.Info("PDF Generated for " + pdfFileName); } } } catch (Exception ex) { //errMessage += "PDF Generation Error in printToPDF\r\n"; //errMessage += ex.Message + "\r\n"; log.Error("PDF Generation Error in printToPDF"); log.Error(ex.Message); return(false); } } catch (Exception ex) { //errMessage += "IDW File Read Error in printToPDF\r\n"; //errMessage += ex.Message + "\r\n"; log.Error("IDW File Read Error in printToPDF"); log.Error(ex.Message); return(false); } return(true); } }
// this routine was coded to use the ERP names instead of the part names // it needs to have a list of ERP names passed to it, each one matching its corresponding sheet in the IDW. // the routine actually doesn't care whether it gets passed Vault Names or Epicor Numbers, // it'll just name the pdfs accordingly // as of November 1 2017, it is no longer being used. public Boolean printToPDFNew(string idw, System.Collections.Generic.Dictionary <VDF.Vault.Currency.Entities.IEntity, Autodesk.DataManagement.Client.Framework.Vault.Currency.Properties.PropertyValue> propDict, string outputFolder, ref string errMessage, ref string logMessage) { { try { InventorApprentice.ApprenticeServerComponent oApprentice = new ApprenticeServerComponent(); InventorApprentice.ApprenticeServerDrawingDocument drgDoc; drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; oApprentice.Open(idw); drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; int pageCount = 1; List <string> assemblyFileNameList = new List <string>(); idwFile idwObject = new idwFile(); idwObject.sheetNames = new List <string>(); idwObject.idwName = idw; idwObject.pageCount = drgDoc.Sheets.Count; foreach (Sheet sh in drgDoc.Sheets) { if (sh.DrawingViews.Count > 0) { string modelName; string modelExtension; modelName = sh.DrawingViews[1].ReferencedDocumentDescriptor.DisplayName; modelExtension = System.IO.Path.GetExtension(modelName); bool matchFound = false; foreach (KeyValuePair <VDF.Vault.Currency.Entities.IEntity, VDF.Vault.Currency.Properties.PropertyValue> pair in propDict) { if (pair.Key.EntityMasterId != 0) { if (pair.Key.EntityName.ToString() == modelName) { modelName = System.IO.Path.GetFileNameWithoutExtension(pair.Value.Value.ToString()); //logMessage+= "\nModel Name: " + modelName + "\n\r"; matchFound = true; idwObject.sheetNames.Add(modelName); break; } } } if (!matchFound) { logMessage += @" " + "\r\n" + @" " + "No corresponding model found for " + modelName + @" " + "\r\n" + @" "; idwObject.sheetNames.Add("unmatchedfile"); pageCount++; //continue; } else { pageCount++; } } } string debugFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PrintPDFCommandLine\debug.txt"; using (StreamWriter writetext = new StreamWriter(debugFileName)) { writetext.WriteLine(AppSettings.Get("PrintPDFWorkingFolder").ToString()); } Process myProcess = new Process(); myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.WorkingDirectory = AppSettings.Get("PrintPDFWorkingFolder").ToString(); myProcess.StartInfo.FileName = AppSettings.Get("PrintPDFExecutable").ToString(); myProcess.StartInfo.Arguments = @"""" + AppSettings.Get("PrintPDFPrinter").ToString() + @"""" + " " + @"""" + outputFolder + @"""" + " " + @"""" + AppSettings.Get("PrintPDFPS2PDF").ToString() + @"""" + " " + @"""" + AppSettings.Get("GhostScriptWorkingFolder").ToString() + @"""" + " " + @"""" + idw + @"""" + " " + (pageCount - 1) + " "; foreach (string sheetName in idwObject.sheetNames) { myProcess.StartInfo.Arguments += " " + @"""" + sheetName + @""""; } myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); myProcess.WaitForExit(); string argumentsFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PrintPDFCommandLine\arguments.txt"; using (StreamWriter writetext = new StreamWriter(argumentsFileName)) { writetext.WriteLine(myProcess.StartInfo.Arguments); } string returnFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PrintPDFCommandLine\" + AppSettings.Get("PrintPDFreturnFile").ToString(); int lineCount = System.IO.File.ReadLines(returnFileName).Count(); // after a successful run of PrintPDF, the file 'return.txt' should contain a // list of files printed, the number of lines matching the number of sheets in the idw if (!(lineCount == pageCount)) // if the drawing set has a sheet with no drawing views on it, for example if a sheet has only a BOM // this test will not be accurate. The routine will return an error even though the pages all printed ok // but because the page count does not equal the sheet count it will return a false error. // so I decided for now I will still log the error, but we won't return a fail status. { errMessage = System.IO.File.ReadAllText(returnFileName); } else { logMessage += idw + " printed successfully"; } errMessage = logMessage; return(true); } catch (Exception ex) { errMessage += "Unknown Error in printToPDF\r\n"; errMessage += ex.Message + "\r\n"; return(false); } } }
static int Main(string[] args) { // the first parameter passed to this program needs to be the name of the printer to print to. // the second one is the path to the folder to print the pdfs to // the third one is the path and name of the executable of the postscript to pdf converter // the rest of the parameters need to be in sets as follows: // 1. the full path to the idw file // 2. the number of pages in the idw file // 3. a list of filenames (the count matching the number of pages), one for each page in the idw // this program writes to a text file called return.txt that is created in the %appdata%\PrintPDFCommandLine directory. // it outputs each pdf filename on a line by itself as soon as it has successfully written it. string printer = ""; string outputFolder = ""; string pdfConverter = ""; string workingDir = ""; string IOFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PrintPDFCommandLine\"; string returnFileName = IOFolder + "return.txt"; string callingParamsFileName = IOFolder + "callingparams.txt"; List <idwFile> idwFiles = new List <idwFile>(); try { if (System.IO.File.Exists(returnFileName)) { System.IO.File.Delete(returnFileName); } idwFile idw = new idwFile(); idw.sheetNames = new List <string>(); if (args == null) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(returnFileName, true)) { file.WriteLine("Error: No arguments specified, aborting operation"); } Console.WriteLine("No arguments specified, aborting operation\n"); return(1); // printer not specified, nothing to print } printer = args[0]; // first argument is name of printer to print to outputFolder = args[1]; pdfConverter = args[2]; workingDir = args[3]; for (int argIndex = 4; argIndex < args.Count();) // the rest of the arguments are the pdf names to use for printing the idw sheets { idw.idwName = args[argIndex]; idw.pageCount = int.Parse(args[argIndex + 1]); argIndex += 2; int i = 0; for (i = argIndex; i < (argIndex + idw.pageCount); i++) { idw.sheetNames.Add(args[i]); using (System.IO.StreamWriter file = new System.IO.StreamWriter(callingParamsFileName, true)) { file.WriteLine(args[i]); if (System.IO.File.Exists(outputFolder + args[i] + ".pdf")) { System.IO.File.Delete(outputFolder + args[i] + ".pdf"); // delete any outdated files } } } argIndex = i; idwFiles.Add(idw); idw.sheetNames = new List <string>(); } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(returnFileName, true)) { file.WriteLine("Error: Problems parsing arguments, aborting operation"); } Console.WriteLine("Problems parsing arguments, aborting operation\n"); return(1); } try { foreach (idwFile idw in idwFiles) { InventorApprentice.ApprenticeServerComponent oApprentice = new ApprenticeServerComponent(); InventorApprentice.ApprenticeServerDrawingDocument drgDoc; InventorApprentice.ApprenticeDrawingPrintManager pMgr; drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; oApprentice.Open(idw.idwName); drgDoc = (InventorApprentice.ApprenticeServerDrawingDocument)oApprentice.Document; pMgr = (InventorApprentice.ApprenticeDrawingPrintManager)drgDoc.PrintManager; pMgr.Printer = printer; int actualSheetIndex = 1; int modifiedSheetIndex = 1; foreach (Sheet sh in drgDoc.Sheets) { string modelName; string modelExtension; if (sh.DrawingViews.Count > 0) // added to make sure sheet has at least one drawing view { modelName = sh.DrawingViews[1].ReferencedDocumentDescriptor.DisplayName; modelExtension = System.IO.Path.GetExtension(modelName); List <string> assemblyFileNameList = new List <string>(); string newName = ""; switch (sh.Orientation) { case PageOrientationTypeEnum.kLandscapePageOrientation: pMgr.Orientation = PrintOrientationEnum.kLandscapeOrientation; break; case PageOrientationTypeEnum.kDefaultPageOrientation: pMgr.Orientation = PrintOrientationEnum.kDefaultOrientation; break; case PageOrientationTypeEnum.kPortraitPageOrientation: pMgr.Orientation = PrintOrientationEnum.kPortraitOrientation; break; } pMgr.SetSheetRange(actualSheetIndex, actualSheetIndex); pMgr.PrintRange = PrintRangeEnum.kPrintSheetRange; pMgr.ScaleMode = InventorApprentice.PrintScaleModeEnum.kPrintBestFitScale; //if (more than one matching pdf name) if (idw.sheetNames.Where(x => x.Equals(idw.sheetNames[modifiedSheetIndex - 1])).Count() > 1) { newName = outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + ".pdf"; if (System.IO.File.Exists(outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + ".pdf")) { assemblyFileNameList.Add(newName); newName = outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + "~" + 1 + ".pdf"; if (System.IO.File.Exists(newName)) { System.IO.File.Delete(newName); } System.IO.File.Move(outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + ".pdf", newName); assemblyFileNameList.Add(newName); } } string psFileName = outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + ".ps"; string pdfFileName = outputFolder + idw.sheetNames[modifiedSheetIndex - 1] + ".pdf"; // for some reason if a ps filename contains a comma it doesn't want to print. // we'll replace it with a tilde. if (psFileName.Contains(",")) { psFileName = psFileName.Replace(',', '~'); } if (psFileName.Contains("°")) { psFileName = psFileName.Replace('°', '~'); } pMgr.PrintToFile(psFileName); // notice: // gs doesn't seem to be able to handle the degree symbol // all filenames with a degree symbol will lose it when run through this script Process oProc = new Process(); // need the full path to the program if we want to set UseShellExecute to false ProcessStartInfo startInfo = new ProcessStartInfo(pdfConverter); //startInfo.WorkingDirectory = @"C:\Program Files\gs\gs9.18\bin\"; startInfo.WorkingDirectory = workingDir; startInfo.Arguments = @"""" + psFileName + @"""" + " " + @"""" + pdfFileName + @""""; startInfo.CreateNoWindow = true; oProc.StartInfo = startInfo; oProc.StartInfo.UseShellExecute = false; oProc.Start(); oProc.WaitForExit(); if (assemblyFileNameList != null) { if (assemblyFileNameList.Count > 1) // combine multiple assembly drawings into one pdf file { // Open the input files PdfDocument inputDocument1 = new PdfDocument(); PdfDocument inputDocument2 = new PdfDocument(); if (System.IO.File.Exists(assemblyFileNameList[0])) { inputDocument1 = PdfReader.Open(assemblyFileNameList[0], PdfDocumentOpenMode.Import); } if (System.IO.File.Exists(assemblyFileNameList[1])) { inputDocument2 = PdfReader.Open(assemblyFileNameList[1], PdfDocumentOpenMode.Import); } // Create the output document PdfDocument outputDocument = new PdfDocument(); // Show consecutive pages facing. Requires Acrobat 5 or higher. outputDocument.PageLayout = inputDocument1.PageLayout; int count = Math.Max(inputDocument1.PageCount, inputDocument2.PageCount); for (int idx = 0; idx < count; idx++) { PdfPage page1 = new PdfPage(); PdfPage page2 = new PdfPage(); if (inputDocument1.PageCount > idx) { page1 = inputDocument1.Pages[idx]; page1 = outputDocument.AddPage(page1); } if (inputDocument2.PageCount > idx) { page2 = inputDocument2.Pages[idx]; page2 = outputDocument.AddPage(page2); } } if (System.IO.File.Exists(assemblyFileNameList[0])) { System.IO.File.Delete(assemblyFileNameList[0]); } // Save the document... while (!(System.IO.File.Exists(assemblyFileNameList[0]))) { string filename = assemblyFileNameList[0]; outputDocument.Save(filename); } // delete the temp file and clear the list if (System.IO.File.Exists(assemblyFileNameList[1])) { System.IO.File.Delete(assemblyFileNameList[1]); } assemblyFileNameList.Clear(); } } using (System.IO.StreamWriter file = new System.IO.StreamWriter(returnFileName, true)) { file.WriteLine(pdfFileName); } System.IO.File.Delete(psFileName); actualSheetIndex++; modifiedSheetIndex++; } else { actualSheetIndex++; // still need to increment sheet index, even if no drawing view was found // on current sheet... } } } return(0); } catch (Exception x) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(returnFileName, true)) { file.WriteLine("Error: Problems printing pdfs, aborting operation"); file.WriteLine(x.ToString()); } Console.WriteLine("Problems printing pdfs, aborting operation\n"); return(1); } }