CTInterfaceOnPostFileConversion Event |
Namespace: ATR.CT.CTInterface
/// <summary> /// A helper class to define some public variables to be used with the script add-in. /// </summary> public class CDocumentValues { public ModelDoc2 poDoc = null; public string psRevision { get; set; } public string psQty { get; set; } public string psMaterial { get; set; } public string psConfigurationName { get; set; } public string psFilename { get; set; } public string psOutputFileExt { get; set; } public string psConfDrawingName { get; set; } public List<string> pslSelectedOpts = new List<string>(); } /// <summary> /// A method to init the event handlers for the events this script should be attached to. /// </summary> /// <param name="ctInterface">CustomTools API</param> public CTFileConversion(ATR.CT.CTInterface.CTInterface ctInterface) { _ctInterface = ctInterface; // Catch the CUSTOMTOOLS File conversion events (post). _ctInterface.OnPostFileConversion += new EventHandler<ATR.CT.CTInterface.CTInterface.PostFileConversionArgs>(_ctInterface_OnPostFileConversion); } #region PostFileConversion /// <summary> /// Method to be executed on the post file conversion event. /// </summary> /// <param name="sender"></param> /// <param name="e">CustomTools OnPostFileConversion event arguments.</param> void _ctInterface_OnPostFileConversion(object sender, ATR.CT.CTInterface.CTInterface.PostFileConversionArgs e) { // Define a rule to restrict the script add-in usage on file conversion to the specific File Conversion rules only. In this example the File Conversion rules the script add-in applies to, have name which starts with string [Scripted]. if (!e.BatchConversionRule.Name.StartsWith(@"[Scripted]", StringComparison.OrdinalIgnoreCase)) { return; } ModelDoc2 oSrcDoc = (ModelDoc2)e.SourceModelDoc; CDocumentValues cCurrent = lConvCopys.Find(x => x.poDoc == oSrcDoc); // Clean up the list to ensure that there are no un-necessary records for the next run. if (cCurrent != null) lConvCopys.Remove(cCurrent); int iConvertedFilesCount = 0; // Check if any processing options are selected for the component. If no actions are selected, copy converted file in to the CT Conversion folder. // Otherwise follow the existing rules to determine the target path for the converted documents. if (cCurrent.pslSelectedOpts.Count > 0) { // Some processing options were selected for the component so save the converted file into the pre-defined folder based on the rules. foreach (string sOption in cCurrent.pslSelectedOpts) { // CHECKBOX: Copy to ERP if (sOption.Equals("CopyToERP", StringComparison.OrdinalIgnoreCase)) { // Define the desired output folder for the converted file and create it, if it does not already exist. string sTargetPath = @"C:\Workspace\ERPFolder\"; CreateMissingFolder(sTargetPath); // Generate the final output filenames using the CustomProperties read from the source document. string sOutputFilename = ""; if (cCurrent.psOutputFileExt.Equals(@".pdf", StringComparison.OrdinalIgnoreCase)) sOutputFilename = string.Format(@"{0}{1}{2}", sTargetPath, cCurrent.psFilename, cCurrent.psOutputFileExt); else if(cCurrent.psRevision == "") sOutputFilename = string.Format(@"{0}{1}{2}", sTargetPath, cCurrent.psConfDrawingName, cCurrent.psOutputFileExt); else sOutputFilename = string.Format(@"{0}{1} {2}{3}", sTargetPath, cCurrent.psConfDrawingName, cCurrent.psRevision, cCurrent.psOutputFileExt); // Copy converted temp file to its final destination. if (CopyFile(e.TargetFileName, sOutputFilename)) iConvertedFilesCount++; } } } // Any processing options were not selected for the component so save the converted document into the CT Conversion folder. else { // If the file without process is converted to PDF, copy the output file to the CT Conversion folder. Otherwise do nothing. if (cCurrent.psOutputFileExt.Equals(@".pdf", StringComparison.OrdinalIgnoreCase)) { string sTargetPath = @"C:\Workspace\CT Conversion\"; CreateMissingFolder(sTargetPath); string sOutputFilename = string.Format(@"{0}{1}{2}", sTargetPath, cCurrent.psFilename, cCurrent.psOutputFileExt); if (CopyFile(e.TargetFileName, sOutputFilename)) iConvertedFilesCount++; } } // Check if the converted files' count was the same as the list length. If all files were successfully copied, delete the temp files. if(iConvertedFilesCount == cCurrent.pslSelectedOpts.Count) DeleteFile(e.TargetFileName); } #endregion #region WinAPI - File and Folder Handling /// <summary> /// Create a path that does not exist at the moment. /// </summary> /// <param name="sPath">Folder's path to be created if currently missing.</param> private void CreateMissingFolder(string sPath) { if (!Directory.Exists(sPath)) { Directory.CreateDirectory(sPath); } } /// <summary> /// Copy file to the output path specified. /// </summary> /// <param name="sSrcFile">Source file.</param> /// <param name="sTgtFile">Target file.</param> /// <returns>Return TRUE if the copying succeeds. Otherwise return FALSE.</returns> private bool CopyFile(string sSrcFile, string sTgtFile) { try { File.Copy(sSrcFile, sTgtFile, true); } catch (Exception) { return false; } return true; } /// <summary> /// Delete the temp file which is now obsolete. /// </summary> /// <param name="sFile">File to be deleted.</param> private void DeleteFile(string sFile) { File.Delete(sFile); } #endregion
CUSTOMTOOLS 2016 SP1