public async Task <double> ConvertLength (LengthEnum from, LengthEnum to, double value, CancellationToken cancellationToken) { if (value == 0) { return(0); } var units = await UnitFactorService.GetUnitList(cancellationToken); var fromFactor = units .FirstOrDefault(x => x.Symbol == from.ToString())?.RatioToTheBaseUnit ?? 0; var toFactor = units? .FirstOrDefault(x => x.Symbol == to.ToString())?.RatioToTheBaseUnit ?? 0; if (fromFactor.Equals(toFactor)) { return(value); } return((value * fromFactor) / toFactor); }
public static dynamic GetTSObject(LengthEnum dynEnum) { var tsType = TSActivator.CreateInstance("Tekla.Structures.Model.RebarSpacingZone.LengthEnum").GetType(); switch (dynEnum) { case LengthEnum.ABSOLUTE: return(System.Enum.Parse(tsType, "ABSOLUTE")); case LengthEnum.RELATIVE: return(System.Enum.Parse(tsType, "RELATIVE")); default: throw new DynamicAPIException(dynEnum.ToString() + "- enum value is not implemented"); } }
/// <summary> /// Converts from one unit of length to another. /// </summary> /// <param name="value"></param> /// <param name="convertTo"></param> /// <param name="convertFrom"></param> /// <returns></returns> public static double LengthConvertTo(double value, LengthEnum convertTo, LengthEnum convertFrom) { switch (convertTo) { case LengthEnum.IN: if (convertFrom == LengthEnum.FT) { value *= 12; } else if (convertFrom == LengthEnum.YD) { value *= 36; } else if (convertFrom == LengthEnum.CM) { value /= 2.54; } break; case LengthEnum.FT: if (convertFrom == LengthEnum.IN) { value /= 12; } else if (convertFrom == LengthEnum.YD) { value *= 3; } else if (convertFrom == LengthEnum.CM) { value /= 30.48; } break; case LengthEnum.YD: if (convertFrom == LengthEnum.FT) { value /= 3; } else if (convertFrom == LengthEnum.IN) { value /= 36; } else if (convertFrom == LengthEnum.CM) { value /= 91.44; } break; case LengthEnum.CM: if (convertFrom == LengthEnum.IN) { value *= 2.54; } else if (convertFrom == LengthEnum.FT) { value *= 30.48; } else if (convertFrom == LengthEnum.YD) { value *= 91.44; } break; } return(value); }