102 lines
3.9 KiB
C#
102 lines
3.9 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using UnitsNet;
|
|||
|
|
using UnitsNet.Units;
|
|||
|
|
|
|||
|
|
namespace CaeGlobals
|
|||
|
|
{
|
|||
|
|
public class StringFrequencyConverter : TypeConverter
|
|||
|
|
{
|
|||
|
|
// Variables
|
|||
|
|
protected static FrequencyUnit _frequencyUnit = FrequencyUnit.Hertz;
|
|||
|
|
|
|||
|
|
|
|||
|
|
// Properties
|
|||
|
|
public static string SetUnit
|
|||
|
|
{
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
if (value == "") _frequencyUnit = (FrequencyUnit)MyUnit.NoUnit;
|
|||
|
|
else _frequencyUnit = Frequency.ParseUnit(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// Constructors
|
|||
|
|
public StringFrequencyConverter()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// Methods
|
|||
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
|||
|
|
{
|
|||
|
|
if (sourceType == typeof(string)) return true;
|
|||
|
|
else return base.CanConvertFrom(context, sourceType);
|
|||
|
|
}
|
|||
|
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
|||
|
|
{
|
|||
|
|
// Convert from string
|
|||
|
|
if (value is string valueString) return MyNCalc.ConvertFromString(valueString, ConvertToCurrentUnits);
|
|||
|
|
else return base.ConvertFrom(context, culture, value);
|
|||
|
|
}
|
|||
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
|||
|
|
{
|
|||
|
|
// Convert to string
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (destinationType == typeof(string))
|
|||
|
|
{
|
|||
|
|
if (value is double valueDouble)
|
|||
|
|
{
|
|||
|
|
string valueString = valueDouble.ToString();
|
|||
|
|
if ((int)_frequencyUnit != MyUnit.NoUnit) valueString += " " + Frequency.GetAbbreviation(_frequencyUnit);
|
|||
|
|
return valueString;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//
|
|||
|
|
public static double ConvertToCurrentUnits(string valueWithUnitString)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Frequency frequency = Frequency.Parse(valueWithUnitString);
|
|||
|
|
if ((int)_frequencyUnit != MyUnit.NoUnit) frequency = frequency.ToUnit(_frequencyUnit);
|
|||
|
|
return frequency.Value;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
throw new Exception(ex.Message + Environment.NewLine + Environment.NewLine + SupportedUnitAbbreviations());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static string SupportedUnitAbbreviations()
|
|||
|
|
{
|
|||
|
|
string abb;
|
|||
|
|
string supportedUnitAbbreviations = "Supported frequency abbreviations: ";
|
|||
|
|
var allUnits = Frequency.Units;
|
|||
|
|
for (int i = 0; i < allUnits.Length; i++)
|
|||
|
|
{
|
|||
|
|
abb = Frequency.GetAbbreviation(allUnits[i]);
|
|||
|
|
if (abb != null) abb.Trim();
|
|||
|
|
if (abb.Length > 0) supportedUnitAbbreviations += abb;
|
|||
|
|
if (i != allUnits.Length - 1) supportedUnitAbbreviations += ", ";
|
|||
|
|
}
|
|||
|
|
supportedUnitAbbreviations += ".";
|
|||
|
|
//
|
|||
|
|
return supportedUnitAbbreviations;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|