/// <summary> /// We define the various unit strings (long and short), as well as the default (internal) units /// for each unit type in a resource-based XML file (FactoryUnits.xml). /// This method loads the above lists from that file, for use in the methods that follow. /// </summary> static void InitializeUnitsData() { const string FactoryUnitsXmlResource = "DbxUtils.Units.FactoryUnits.xml"; sUnitsTypeData = new Dictionary <UnitsType, UnitsTypeData>(); Assembly thisAssembly = Assembly.GetExecutingAssembly(); using (Stream unitsDataStream = thisAssembly.GetManifestResourceStream(FactoryUnitsXmlResource)) { XPathDocument unitsDoc = new XPathDocument(unitsDataStream); XPathNavigator nav = unitsDoc.CreateNavigator(); // grab data for each known unit type (i.e., UnitsType enum values) foreach (UnitsType unitType in Enum.GetValues(typeof(UnitsType))) { // Skip Unknown if (unitType == UnitsType.Unknown) { continue; } string unitTypeName = Enum.GetName(typeof(UnitsType), unitType); Type unitTypeEnum = thisAssembly.GetType("Autodesk.Factory.Core.Utilities." + unitTypeName + "Unit"); var subNav = nav.SelectSingleNode("/FactoryUnits/UnitGroup[@type='" + unitTypeName + "']"); var unitsTypeData = new UnitsTypeData(unitType); string internalUnit = (string)subNav.Evaluate("string(@default)"); object internalUnitValue = Enum.Parse(unitTypeEnum, internalUnit); unitsTypeData.InternalUnits = (int)internalUnitValue; // "default" attribute identifies internal unit for the group // "Unit" children provide type/display string mappings var unitGroupItems = subNav.Select("Unit"); while (unitGroupItems.MoveNext()) { var unitNode = unitGroupItems.Current; // "type" attribute identifies the particular xxxUnit enum value (as a string) // "res" attribute identifies the resource string string resId = (string)unitNode.Evaluate("string(@res)"); string shortResId = (string)unitNode.Evaluate("string(@shortRes)"); string typeName = (string)unitNode.Evaluate("string(@type)"); string displayString = Resources.ResourceManager.GetString(resId); string shortDisplayString = String.IsNullOrEmpty(shortResId) ? String.Empty : Resources.ResourceManager.GetString(shortResId); object typeValue = Enum.Parse(unitTypeEnum, typeName); unitsTypeData.UnitsData.Add(new UnitsData() { Value = (int)typeValue, DisplayString = displayString, ShortDisplayString = shortDisplayString }); } sUnitsTypeData[unitType] = unitsTypeData; } } }
/// <summary> /// Get the internal units string for the provided units type /// </summary> /// <param name="unitType"></param> /// <returns></returns> public static string GetInternalUnits(UnitsType unitType) { try { UnitsTypeData unitsData = GetUnitsData()[unitType]; int internalUnitsValue = unitsData.InternalUnits; return(unitsData.UnitsData.Single(data => data.Value == internalUnitsValue).DisplayString); } catch (InvalidOperationException) { throw new ArgumentOutOfRangeException("unitType"); } }