/// <summary> /// Executed upon exiting value rule /// </summary> /// <param name="context">the value context</param> public override void ExitValue([NotNull] ParamExprGrammarParser.ValueContext context) { base.ExitValue(context); object value = null; string valueStr = context.GetChild(0).GetText(); if (context.GetChild(0) is ParamExprGrammarParser.StringliteralContext) { valueStr = valueStr.Trim().Replace("\'", "").Replace("\"", ""); value = (string)valueStr; } else if (context.GetChild(0) is ParamExprGrammarParser.RealliteralContext) { UnitType convertUnit = UnitType.UT_Length; bool hasUnit = false; ParamExprGrammarParser.RealliteralContext realCtx = context.GetChild(0) as ParamExprGrammarParser.RealliteralContext; if (realCtx.UNITTYPEENUM() != null) { hasUnit = Enum.TryParse <UnitType>(realCtx.UNITTYPEENUM().GetText(), true, out convertUnit); } valueStr = realCtx.signed_number().GetText(); int valueInt; if (Int32.TryParse(valueStr, out valueInt)) { value = (int)valueInt; } else { double valueDbl; if (Double.TryParse(valueStr, out valueDbl)) { if (hasUnit) { value = (double)valueDbl / UnitUtil.ScaleDouble(convertUnit, 1.0); } else { value = (double)valueDbl; } } } } NodeProperty nodeP = new NodeProperty(); nodeP.originalNodePropertyValue = valueStr; nodeP.nodePropertyValue = value; SetNodePropertyValue(context, nodeP); }
/// <summary> /// Executed upon exiting value rule /// </summary> /// <param name="context">the value context</param> public override void ExitValue([NotNull] ParamExprGrammarParser.ValueContext context) { base.ExitValue(context); object value = null; ForgeTypeId convertUnit = SpecTypeId.Number; string valueStr = context.GetChild(0).GetText(); if (context.GetChild(0) is ParamExprGrammarParser.StringliteralContext) { valueStr = valueStr.Trim().Replace("\'", "").Replace("\"", ""); value = (string)valueStr; } else if (context.GetChild(0) is ParamExprGrammarParser.RealliteralContext) { ParamExprGrammarParser.RealliteralContext realCtx = context.GetChild(0) as ParamExprGrammarParser.RealliteralContext; valueStr = realCtx.signed_number().GetText(); int valueInt; if (Int32.TryParse(valueStr, out valueInt)) { value = (int)valueInt; } else { double valueDbl; if (Double.TryParse(valueStr, out valueDbl)) { value = (double)valueDbl; } } } else if (context.GetChild(0) is ParamExprGrammarParser.Value_with_unitContext) { ParamExprGrammarParser.Value_with_unitContext vwunitCtx = context.GetChild(0) as ParamExprGrammarParser.Value_with_unitContext; if (vwunitCtx.UNITTYPE() != null) { try { if (vwunitCtx.GetChild(2) is ParamExprGrammarParser.Atomic_paramContext) { NodeProperty val = GetNodePropertyValue(vwunitCtx.GetChild(2)); if (val.nodePropertyValue is double) { value = (double)val.nodePropertyValue; } } else if (vwunitCtx.GetChild(2) is ParamExprGrammarParser.Signed_numberContext) { ParamExprGrammarParser.Signed_numberContext signedNum = vwunitCtx.GetChild(2) as ParamExprGrammarParser.Signed_numberContext; valueStr = signedNum.GetText(); double valueDbl; if (Double.TryParse(valueStr, out valueDbl)) { value = (double)valueDbl; } } string unitTypeName = vwunitCtx.UNITTYPE().GetText(); System.Reflection.PropertyInfo unitType = typeof(Autodesk.Revit.DB.SpecTypeId).GetProperty(unitTypeName); if (unitType != null) { convertUnit = unitType.GetValue(null, null) as ForgeTypeId; } } catch { } } } NodeProperty nodeP = new NodeProperty(); nodeP.originalNodePropertyValue = valueStr; nodeP.nodePropertyValue = value; nodeP.uomTypeId = convertUnit; SetNodePropertyValue(context, nodeP); }