CTInterfaceOnAfterButtonFunction Event |
Namespace: ATR.CT.CTInterface
// @auto-reference {[CT_INSTALL_PATH]\CTInterface.dll} // @auto-reference {[CT_INSTALL_PATH]\Interop.CTEngineLib.dll} // @auto-reference {C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.dll} // @auto-reference {C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll} using System; using System.IO; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using System.Windows.Forms; using ATR.CT.CTInterface; using CTEngineLib; namespace CUSTOMTOOLS.APIExample { public class ManufacturingTime { private CTInterface _ctInterface; public ManufacturingTime(ATR.CT.CTInterface.CTInterface ctInterface) { _ctInterface = ctInterface; _ctInterface.OnAfterButtonFunction += _ctInterface_OnAfterButtonFunction; } /// <summary> /// Called after the dialog box associated with button function is closed by user. /// </summary> /// <param name="sender"></param> /// <param name="e">Information about button function and properties having "Allow scripting" /// set in CUSTOMTOOLS</param> void _ctInterface_OnAfterButtonFunction(object sender, CTInterface.ButtonFuncArgs e) { // We are only interested in user scope custom 1 data which has job stages defined // in CUSTOMTOOLS profile we are using if(e.ButtonFunctionResult == ButtonFunctionResult.DialogOk && e.ButtonFunction == ctButtonFunction.ctButtonFunctionGetUserScopeData) { // Job stages are in XML form in matrix data string totalManufacturingTime = GetTotalManufacturingTime(e.Property.MatrixData).ToString(); // Value needs to be changed through e.Properties in order to update it in UI ICustomProperty manuTime = e.Properties.SingleOrDefault(p => p.Property.ID == e.Property.Property.ID); if(manuTime != null && manuTime.OriginalValue != totalManufacturingTime) { manuTime.Value = totalManufacturingTime; } } } /// <summary> /// Calculates total manufacturing time for item by summing up time on each job stage row /// </summary> /// <param name="jobStages">Defined job stages as XML</param> /// <returns>Summed up manufacturing time when successful; otherwise 0</returns> private int GetTotalManufacturingTime(string jobStages) { try { int totalTime = 0; XDocument jobStagesXML = XDocument.Parse(jobStages); IEnumerable<XElement> jobTimes = from field in jobStagesXML.Descendants("field") where (string)field.Attribute("name") == "job_time" select field; foreach(var time in jobTimes) { int stageTime = 0; if(int.TryParse(time.Value, out stageTime)) { totalTime += int.Parse(time.Value); } } return totalTime; } catch(Exception ex) { MessageBox.Show(string.Format("An error occurred while calculating manufacturing time:{0}{1}", Environment.NewLine, ex.Message)); } return 0; // Default } } }
CUSTOMTOOLS 2016 SP4