using System; using System.IO; using System.Text; using System.Xml.Serialization; using TBF.BenchControl.Generic; using TBF.Resources; namespace TBF.BenchControl.Cameras.CameraRoi { public class RoiProcedureParams : IParamsProvider, IProcedureParams { public int IRoiX; public int IRoiY; public int IRoiW; public int IRoiH; public float GradPerLtr; public bool CCW; /// Clockwise = false, Counter clockwise = true public float ORoiW; public float ORoiH; public float CX; public float CY; public float R1; public float R2; public float R3; public float Ang1; public float Ang2; public float AngStep; public int MinMvmt; public int MaxMvmt; public int SequenceLen; public int SequenceTiming; public int BackgroundCorr; public float BrightnessRoi; string[] paramNames = new string[] { "IRoiX", "IRoiY", "IRoiW", "IRoiH", "Grad per liter", "CW/CCW", "ORoiW", "ORoiH", "CX", "CY", "R1", "R2", "R3", "Ang1", "Ang2", "AngStep", "Min.Mvmt.", "Max.Mvmt.", "Det.Seq.Len", "Det.Seq.Timing", "Background", "Brightness", }; public string ParamName(int i) { return paramNames[i]; } public string ToString(int i) { switch (i) { case 0: return IRoiX.ToString(); case 1: return IRoiY.ToString(); case 2: return IRoiW.ToString(); case 3: return IRoiH.ToString(); case 4: return GradPerLtr.ToString(); case 5: return (CCW ? "ccw" : "cw"); case 6: return ORoiW.ToString(); case 7: return ORoiH.ToString(); case 8: return CX.ToString(); case 9: return CY.ToString(); case 10: return R1.ToString(); case 11: return R2.ToString(); case 12: return R3.ToString(); case 13: return Ang1.ToString(); case 14: return Ang2.ToString(); case 15: return AngStep.ToString(); case 16: return MinMvmt.ToString(); case 17: return MaxMvmt.ToString(); case 18: return SequenceLen.ToString(); case 19: return SequenceTiming.ToString(); case 20: return BackgroundCorr.ToString(); case 21: return ((int)(100.0f * BrightnessRoi)).ToString(); default: return string.Empty; } } /// /// Parameterless constructor - sets default values of parameters. /// public RoiProcedureParams() { IRoiX = 60; IRoiY = 1; IRoiW = 520; IRoiH = 478; GradPerLtr = 40; CCW = true; ORoiW = 64.0f; ORoiH = 64.0f; CX = 31.0f; CY = 31.0f; R1 = 21.0f; R2 = 31.0f; R3 = 0; Ang1 = 0; Ang2 = 400.0f; AngStep = 40.0f; MinMvmt = 0; MaxMvmt = 0; SequenceLen = 8; SequenceTiming = 1; BackgroundCorr = 0; BrightnessRoi = 1.0f; } public void UpdateParam(int i, string strValue) { switch (i) { case 0: IRoiX = int.Parse(strValue); return; case 1: IRoiY = int.Parse(strValue); return; case 2: IRoiW = int.Parse(strValue); return; case 3: IRoiH = int.Parse(strValue); return; case 4: GradPerLtr = Utils.ParseUFloat(strValue); return; case 5: CCW = strValue.ToLower().Equals("ccw"); return; case 6: ORoiW = Utils.ParseUFloat(strValue); return; case 7: ORoiH = Utils.ParseUFloat(strValue); return; case 8: CX = Utils.ParseUFloat(strValue); return; case 9: CY = Utils.ParseUFloat(strValue); return; case 10: R1 = Utils.ParseUFloat(strValue); return; case 11: R2 = Utils.ParseUFloat(strValue); return; case 12: R3 = Utils.ParseSFloat(strValue); return; case 13: Ang1 = Utils.ParseSFloat(strValue); return; case 14: Ang2 = Utils.ParseSFloat(strValue); return; case 15: AngStep = Utils.ParseUFloat(strValue); return; case 16: MinMvmt = int.Parse(strValue); return; case 17: MaxMvmt = int.Parse(strValue); return; case 18: SequenceLen = int.Parse(strValue); return; case 19: SequenceTiming = int.Parse(strValue); return; case 20: BackgroundCorr = int.Parse(strValue); return; case 21: BrightnessRoi = ((float)int.Parse(strValue) / 100.0f); return; default: return; } } /// /// Parses the text in the control to verify whether it represents a valid parameter. /// /// Parameter nr. /// String to be verified /// Message to be shown when verification fails /// 'true' if parameter string is valid, 'false' otherwise public bool ValidateParam(int i, string strValue, out string message) { message = string.Empty; int iDummy; float dummy; switch (i) { case 0: case 1: case 2: case 3: case 16: case 17: case 18: case 19: case 20: if (int.TryParse(strValue, out iDummy)) return true; break; case 4: case 6: case 7: case 8: case 9: case 10: case 11: case 15: case 21: if (Utils.TryParseUFloat(strValue, out dummy)) return true; break; case 12: case 13: case 14: if (Utils.TryParseSFloat(strValue, out dummy)) return true; break; case 5: if (strValue.ToLower().Equals("cw") || strValue.ToLower().Equals("ccw")) return true; break; default: message = "Invalid index"; return false; } message = ParamName(i) + " is invalid"; return false; } #region Boilerplate code public int ParamsCount() { return paramNames.Length; } public override string ToString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ParamsCount(); i++) { if (i != 0) sb.Append("; "); sb.Append(ParamName(i)); sb.Append("="); sb.Append(ToString(i)); } return sb.ToString(); } [XmlIgnore] public Entities.ComponentProcedure ProcedureParamsEntity { get { return procedureParamsEntity; } } Entities.ComponentProcedure procedureParamsEntity; [XmlIgnore] public string ComponentName { get { return componentName; } } string componentName; [XmlIgnore] public Entities.Procedure Procedure { get { return procedure; } } Entities.Procedure procedure; public RoiProcedureParams(Entities.ComponentProcedure procedureParamsEntity, string componentName, Entities.Procedure procedure) : this() { this.procedureParamsEntity = procedureParamsEntity; this.componentName = componentName; this.procedure = procedure; } public TBF.Entities.ComponentProcedure ToDbEntity(IComponent component, Entities.Procedure procedure) { using (var writer = new StringWriter()) { /// Serialize this class with parameters (new XmlSerializer(GetType())).Serialize(writer, this); /// Create and return the ComponentProcedure entity TBF.Entities.ComponentProcedure entity = new TBF.Entities.ComponentProcedure(); entity.CmpntName = component.Cfg.Name; entity.Parameters = writer.ToString(); entity.Procedure = procedure; return entity; } } public void UpdateDbEntity() { using (var writer = new StringWriter()) { /// Serialize this class and update the entity (new XmlSerializer(GetType())).Serialize(writer, this); procedureParamsEntity.Parameters = writer.ToString(); procedureParamsEntity.CmpntName = componentName; procedureParamsEntity.Procedure = procedure; } } public static IParamsProvider GetProcedureParams(Entities.ComponentProcedure procedureParamsEntity) { if (procedureParamsEntity == null) return new RoiProcedureParams(); RoiProcedureParams procedureParams; try { procedureParams = (RoiProcedureParams)(new XmlSerializer(typeof(RoiProcedureParams))) .Deserialize(new StringReader(procedureParamsEntity.Parameters)); } catch { procedureParams = new RoiProcedureParams(); } procedureParams.procedureParamsEntity = procedureParamsEntity; procedureParams.componentName = procedureParamsEntity.CmpntName; procedureParams.procedure = procedureParamsEntity.Procedure; return procedureParams; } #endregion } }