137 lines
5.1 KiB
C#
137 lines
5.1 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Globalization;
|
|||
|
|
|
|||
|
|
namespace TBF
|
|||
|
|
{
|
|||
|
|
public class Utils
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Parse unsigned float
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool TryParseUFloat(string text, out float result)
|
|||
|
|
{
|
|||
|
|
return float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result) ||
|
|||
|
|
float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result);
|
|||
|
|
}
|
|||
|
|
public static float ParseUFloat(string text, float dfltVal)
|
|||
|
|
{
|
|||
|
|
float result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result)) return result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result)) return result;
|
|||
|
|
return dfltVal;
|
|||
|
|
}
|
|||
|
|
public static float ParseUFloat(string text)
|
|||
|
|
{
|
|||
|
|
return ParseUFloat(text, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Parse float, sign allowed
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool TryParseSFloat(string text, out float result)
|
|||
|
|
{
|
|||
|
|
return float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result) ||
|
|||
|
|
float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result);
|
|||
|
|
}
|
|||
|
|
public static float ParseSFloat(string text, float dfltVal)
|
|||
|
|
{
|
|||
|
|
float result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result)) return result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result)) return result;
|
|||
|
|
return dfltVal;
|
|||
|
|
}
|
|||
|
|
public static float ParseSFloat(string text)
|
|||
|
|
{
|
|||
|
|
return ParseSFloat(text, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Parse float, sign allowed, exponent allowed
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool TryParseEFloat(string text, out float result)
|
|||
|
|
{
|
|||
|
|
return float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result) ||
|
|||
|
|
float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result);
|
|||
|
|
}
|
|||
|
|
public static float ParseEFloat(string text, float dfltVal)
|
|||
|
|
{
|
|||
|
|
float result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result)) return result;
|
|||
|
|
if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result)) return result;
|
|||
|
|
return dfltVal;
|
|||
|
|
}
|
|||
|
|
public static float ParseEFloat(string text)
|
|||
|
|
{
|
|||
|
|
return ParseEFloat(text, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Get a list of IValve-s from an entity implementing IHasValves
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="entity">Entity implementing IHasValves</param>
|
|||
|
|
/// <returns>A list of IValves</returns>
|
|||
|
|
public static IList<BenchControl.GenericDevices.IValve> ValvesOpen(Entities.IHasValves entity)
|
|||
|
|
{
|
|||
|
|
var valvesOpen = new List<BenchControl.GenericDevices.IValve>();
|
|||
|
|
if (entity != null && entity.ValvesOpen != null && entity.ValvesOpen != string.Empty)
|
|||
|
|
{
|
|||
|
|
string[] vOpen = entity.ValvesOpen.Split(new char[] { ';' });
|
|||
|
|
foreach (var vName in vOpen)
|
|||
|
|
{
|
|||
|
|
valvesOpen.Add((BenchControl.GenericDevices.IValve)BenchControl
|
|||
|
|
.TbfComponents.FindComponent(vName));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return valvesOpen;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Get a list of IValve-s from an entity implementing IHasValves
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="entity">Entity implementing IHasValves</param>
|
|||
|
|
/// <returns>A list of IValves</returns>
|
|||
|
|
public static IList<BenchControl.GenericDevices.IValve> ValvesClose(Entities.IHasValves entity)
|
|||
|
|
{
|
|||
|
|
var valvesClose = new List<BenchControl.GenericDevices.IValve>();
|
|||
|
|
if (entity != null && entity.ValvesClose != null && entity.ValvesClose != string.Empty)
|
|||
|
|
{
|
|||
|
|
string[] vClose = entity.ValvesClose.Split(new char[] { ';' });
|
|||
|
|
foreach (var vName in vClose)
|
|||
|
|
{
|
|||
|
|
valvesClose.Add((BenchControl.GenericDevices.IValve)BenchControl
|
|||
|
|
.TbfComponents.FindComponent(vName));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return valvesClose;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Get an array of RegulValve positions from a TransitionStep entity
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="entity">TransitionStep entity</param>
|
|||
|
|
/// <returns>float[] of regulation valve positions from 0 to 100.0f</returns>
|
|||
|
|
public static float[] RegulValvesPositions(Entities.TransitionStep step)
|
|||
|
|
{
|
|||
|
|
int regvCount = BenchControl.Sequences.SequenceBase.RegulValves.Count;
|
|||
|
|
float[] result = new float[regvCount];
|
|||
|
|
|
|||
|
|
string[] rvPosStr = (step.RegulValvesPct != null) ? step.RegulValvesPct.Split(new char[] { ';' }) : new string[0];
|
|||
|
|
for (int i = 0; i < regvCount; i++)
|
|||
|
|
{
|
|||
|
|
float fdummy;
|
|||
|
|
if ((i < rvPosStr.Length) && Utils.TryParseUFloat(rvPosStr[i], out fdummy) &&
|
|||
|
|
(fdummy >= 0) && (fdummy <= 100.0f))
|
|||
|
|
{
|
|||
|
|
result[i] = fdummy;
|
|||
|
|
}
|
|||
|
|
else // if (rvPosStr[i].Equals("---"))
|
|||
|
|
{
|
|||
|
|
result[i] = -1.0f; /// Negative value means no regulation valve position change
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|