Measurements of units were added
This commit is contained in:
47
Infrastructure/UI/Converters/CommonOperation.cs
Normal file
47
Infrastructure/UI/Converters/CommonOperation.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Strings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelper.Infrastructure.UI.Converters
|
||||
{
|
||||
internal static class CommonOperation
|
||||
{
|
||||
public static double ConvertToDoubleChangeComma(string s)
|
||||
{
|
||||
double result;
|
||||
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
|
||||
{
|
||||
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static IStringDoublePair DivideIntoStringDoublePair(string s)
|
||||
{
|
||||
string digitPattern = @"\d+(\.?|\,?)\d+";
|
||||
string textPattern = @"[0-9]|\.|\,";
|
||||
string caracterPattern = "[a-z]+$";
|
||||
string target = "";
|
||||
Regex regexText = new Regex(textPattern);
|
||||
string textString = regexText.Replace(s, target);
|
||||
var textMatch = Regex.Match(textString, caracterPattern, RegexOptions.IgnoreCase);
|
||||
if (textMatch.Success) {textString = textMatch.Value.ToLower();}
|
||||
var match = Regex.Match(s, digitPattern);
|
||||
if (match.Success)
|
||||
{
|
||||
string digitString = match.Value;
|
||||
double digit = ConvertToDoubleChangeComma(digitString);
|
||||
return new StringDoublePair() { Digit = digit, Text = textString };
|
||||
}
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Infrastructure/UI/Converters/IStringDoublePair.cs
Normal file
14
Infrastructure/UI/Converters/IStringDoublePair.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelper.Infrastructure.UI.Converters
|
||||
{
|
||||
internal interface IStringDoublePair
|
||||
{
|
||||
double Digit { get; }
|
||||
string Text { get; }
|
||||
}
|
||||
}
|
||||
14
Infrastructure/UI/Converters/StringDoublePair.cs
Normal file
14
Infrastructure/UI/Converters/StringDoublePair.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelper.Infrastructure.UI.Converters
|
||||
{
|
||||
internal class StringDoublePair : IStringDoublePair
|
||||
{
|
||||
public double Digit { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
internal class Area : UnitBase
|
||||
{
|
||||
public override string unitName { get => "Area"; }
|
||||
|
||||
public override string MeasureUnit => throw new NotImplementedException();
|
||||
|
||||
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
@@ -24,12 +27,15 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
if (value != null)
|
||||
try
|
||||
{
|
||||
var strVal = value as string;
|
||||
double.TryParse(strVal, out val);
|
||||
val = CommonOperation.ConvertToDoubleChangeComma(strVal);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val /= (UnitConstatnts.Length * UnitConstatnts.Length);
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
|
||||
public override string unitName { get => "Force"; }
|
||||
|
||||
public override string MeasureUnit => throw new NotImplementedException();
|
||||
|
||||
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
@@ -26,12 +28,15 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
if (value != null)
|
||||
try
|
||||
{
|
||||
var strVal = value as string;
|
||||
double.TryParse(strVal, out val);
|
||||
val = CommonOperation.ConvertToDoubleChangeComma(strVal);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val /= coeffficient;
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Strings;
|
||||
using StructureHelperCommon.Services.Units;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -10,28 +14,60 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
{
|
||||
internal class Length : UnitBase
|
||||
{
|
||||
public override string unitName { get => "Length"; }
|
||||
private IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
|
||||
private UnitTypes type = UnitTypes.Length;
|
||||
private IUnit currentUnit => units.Where(u => u.UnitType == type & u.Name == "mm").Single();
|
||||
public override string MeasureUnit => currentUnit.Name;
|
||||
private double coeffficient => currentUnit.Multiplyer;
|
||||
|
||||
public override string unitName { get => "Length"; }
|
||||
|
||||
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
if (value != null) { val = (double)value; }
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val *= UnitConstatnts.Length;
|
||||
return val;
|
||||
val *= coeffficient;
|
||||
string strValue = $"{val} {MeasureUnit}";
|
||||
return strValue;
|
||||
}
|
||||
|
||||
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
if (value != null)
|
||||
try
|
||||
{
|
||||
var strVal = value as string;
|
||||
double.TryParse(strVal, out val);
|
||||
IStringDoublePair pair = CommonOperation.DivideIntoStringDoublePair(strVal);
|
||||
double multy;
|
||||
try
|
||||
{
|
||||
multy = coeffficient / GetMultiplyer(units, type, pair.Text);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
multy = 1d;
|
||||
}
|
||||
val = pair.Digit * multy;
|
||||
}
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val /= UnitConstatnts.Length;
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
val /= coeffficient;
|
||||
return val;
|
||||
}
|
||||
|
||||
private double GetMultiplyer(IEnumerable<IUnit> units, UnitTypes unitType, string unitName)
|
||||
{
|
||||
try
|
||||
{
|
||||
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single().Multiplyer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
|
||||
public override string unitName { get => "Stress"; }
|
||||
|
||||
public override string MeasureUnit => throw new NotImplementedException();
|
||||
|
||||
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
@@ -26,12 +28,15 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
double val;
|
||||
if (value != null)
|
||||
try
|
||||
{
|
||||
var strVal = value as string;
|
||||
double.TryParse(strVal, out val);
|
||||
val = CommonOperation.ConvertToDoubleChangeComma(strVal);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val /= coeffficient;
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace StructureHelper.Infrastructure.UI.Converters.Units
|
||||
internal abstract class UnitBase : IValueConverter
|
||||
{
|
||||
public abstract string unitName { get;}
|
||||
public abstract string MeasureUnit { get; }
|
||||
public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
|
||||
public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user