/// <summary> /// Executed upon existing the param_expr rule /// </summary> /// <param name="context">the param_expr context</param> public override void ExitParam_expr([NotNull] ParamExprGrammarParser.Param_exprContext context) { base.ExitParam_expr(context); NodeProperty paramExprNodeProp = GetNodePropertyValue(context.expr()); object parValue = paramExprNodeProp.nodePropertyValue; // Unique parameter value can only be "enforced" for a string datatype by appending a running number: <parValue> (#), starting with 2 if (isUnique && parValue is string) { Tuple <string, object> key = new Tuple <string, object>(RevitParameterName, parValue); int counter = 0; if (UniqueParameterValue.TryGetValue(key, out counter)) { counter++; parValue = parValue.ToString() + " (" + counter.ToString() + ")"; UniqueParameterValue[key] = counter; } else { UniqueParameterValue.Add(key, 1); } } FinalParameterValue = parValue; UnitType = paramExprNodeProp.uomTypeId; if (paramExprNodeProp.uomTypeId != null) { if (FinalParameterValue is double) { double?paramValueDouble = FinalParameterValue as double?; formattedValue = UnitFormatUtils.Format(RevitElement.Document.GetUnits(), paramExprNodeProp.uomTypeId, paramValueDouble.Value, false); FinalParameterValue = UnitUtils.ConvertToInternalUnits(paramValueDouble.Value, paramExprNodeProp.uomTypeId); } } }
/// <summary> /// Resolve "(expr)^power" /// </summary> /// <param name="expr">the expr NodeProperty</param> /// <param name="powerOp">the power operator</param> /// <returns>NodeProperty</returns> public static NodeProperty ResolveExprPowerOp(NodeProperty expr, int powerOp) { NodeProperty ret = new NodeProperty(); ret.originalNodePropertyValue = "(" + expr.originalNodePropertyValue + ")^" + powerOp.ToString(); if (expr.nodePropertyValue is double || expr.nodePropertyValue is int) { ret.nodePropertyValue = Math.Pow((double)expr.nodePropertyValue, (double)powerOp); } return(ret); }
/// <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 expr rule /// </summary> /// <param name="context">the expr context</param> public override void ExitExpr([NotNull] ParamExprGrammarParser.ExprContext context) { base.ExitExpr(context); NodeProperty retExpr = new NodeProperty(); // value if (context.GetChild(0) is ParamExprGrammarParser.ValueContext && context.children.Count == 1) { retExpr = GetNodePropertyValue(context.GetChild(0)); } // | atomic_param else if (context.GetChild(0) is ParamExprGrammarParser.Atomic_paramContext && context.children.Count == 1) { retExpr = GetNodePropertyValue(context.GetChild(0)); } // | unary_operator expr else if (context.GetChild(0) is ParamExprGrammarParser.Unary_operatorContext && context.ChildCount == 2) { retExpr = ParamExprResolver.ResolveExprUnaryOperator(context.GetChild(0).GetText(), GetNodePropertyValue(context.GetChild(1))); } // | expr ops expr else if (context.GetChild(1) is ParamExprGrammarParser.OpsContext && context.ChildCount == 3) { retExpr = ParamExprResolver.ResolveExprOpsExpr(context.GetChild(0) as ParamExprGrammarParser.ExprContext, GetNodePropertyValue(context.GetChild(0)), context.GetChild(1) as ParamExprGrammarParser.OpsContext, GetNodePropertyValue(context.GetChild(2))); } // | '(' expr ')' (power_op)? else if (context.GetChild(0) is ITerminalNode && context.GetChild(2) is ITerminalNode) { int powerOp = +1; if (context.ChildCount == 4) { powerOp = ParamExprResolver.GetPowerOp(context.GetChild(3) as ParamExprGrammarParser.Power_opContext); } retExpr = ParamExprResolver.ResolveExprPowerOp(GetNodePropertyValue(context.GetChild(1) as ParamExprGrammarParser.ExprContext), powerOp); } else { // Not valid rule, return nothing? or the original text? } SetNodePropertyValue(context, retExpr); }
/// <summary> /// Resolve unary operator for expr /// </summary> /// <param name="unaryOp">the unary operator</param> /// <param name="expr">the expr</param> /// <returns>NodeProperty</returns> public static NodeProperty ResolveExprUnaryOperator(string unaryOp, NodeProperty expr) { NodeProperty ret = new NodeProperty(); ret.originalNodePropertyValue = unaryOp + " " + ret.originalNodePropertyValue; if (expr.nodePropertyValue == null) { return(ret); } if (expr.nodePropertyValue is Int32) { if (unaryOp.Equals("-")) { ret.nodePropertyValue = -1 * ((int)expr.nodePropertyValue); } else { ret.nodePropertyValue = (int)expr.nodePropertyValue; } } else if (expr.nodePropertyValue is double) { if (unaryOp.Equals("-")) { ret.nodePropertyValue = -1 * ((double)expr.nodePropertyValue); } else { ret.nodePropertyValue = (double)expr.nodePropertyValue; } } else { ret.nodePropertyValue = unaryOp + " " + expr.nodePropertyValue.ToString(); } return(ret); }
/// <summary> /// Executed upon exiting atomic_param rule /// </summary> /// <param name="context">the atomic_param context</param> public override void ExitAtomic_param([NotNull] ParamExprGrammarParser.Atomic_paramContext context) { base.ExitAtomic_param(context); NodeProperty nodeP = new NodeProperty(); nodeP.originalNodePropertyValue = context.GetText(); // Handle special_param if (context.GetChild(0) is ParamExprGrammarParser.Special_paramContext) { var spParCtx = context.GetChild(0) as ParamExprGrammarParser.Special_paramContext; if (spParCtx.ELEMENTID() != null) { nodeP.nodePropertyValue = RevitElement.Id.IntegerValue; } else if (spParCtx.RUNNINGNUMBER() != null) { var key = new Tuple <string, string>(RevitElement.Category.Name, RevitParameterName); int lastNumber; if (LastRunningNumberCollection.ContainsKey(key)) { lastNumber = ++LastRunningNumberCollection[key]; } else { lastNumber = 1; LastRunningNumberCollection.Add(key, lastNumber); } nodeP.nodePropertyValue = lastNumber; } else if (spParCtx.RUNNINGNUMBERINSTANCE() != null) { var key = new Tuple <string, string>(RevitElement.Id.ToString(), RevitParameterName); int lastNumber; if (LastRunningNumberCollection.ContainsKey(key)) { lastNumber = ++LastRunningNumberCollection[key]; } else { lastNumber = 1; LastRunningNumberCollection.Add(key, lastNumber); } nodeP.nodePropertyValue = lastNumber; } //else if (spParCtx.AUTOCALCULATE() != null) //{ //} else { // Not supported } } // handle objref param_name (',' param_name) else { object parValue = null; var thisObj = context.GetChild(0) as ParamExprGrammarParser.ObjrefContext; var paramNames = context.param_name(); if (paramNames.Count() >= 1) { // This is the first level reference if (thisObj.THIS() != null) { parValue = GetValueFromParam_nameContext(RevitElement, paramNames[0]); } else if (thisObj.TYPE() != null) { parValue = GetValueFromParam_nameContext(RevitElement.Document.GetElement(RevitElement.GetTypeId()), paramNames[0]); } } if (paramNames.Count() > 1 && parValue != null && parValue is ElementId) { // Parameter should be obtained from the second level reference if the parameter is of ElementId type parValue = GetValueFromParam_nameContext(RevitElement.Document.GetElement(parValue as ElementId), paramNames[1]); } nodeP.nodePropertyValue = parValue; } SetNodePropertyValue(context, nodeP); }
/// <summary> /// Set NodeProperty /// </summary> /// <param name="node">the node</param> /// <param name="value">value</param> public void SetNodePropertyValue(Antlr4.Runtime.Tree.IParseTree node, NodeProperty value) { nodeProp.Put(node, value); }
/// <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); }
/// <summary> /// Resolve "expr operator expr" /// </summary> /// <param name="expr1Ctx">context of expr 1</param> /// <param name="expr1">NodeProperty of expr 1</param> /// <param name="ops">operator</param> /// <param name="expr2">NodeProperty of expr 2</param> /// <returns>NodeProperty</returns> public static NodeProperty ResolveExprOpsExpr(ParamExprGrammarParser.ExprContext expr1Ctx, NodeProperty expr1, ParamExprGrammarParser.OpsContext ops, NodeProperty expr2) { NodeProperty ret = new NodeProperty(); ret.originalNodePropertyValue = expr1.originalNodePropertyValue + " " + ops.GetText() + " " + expr2.originalNodePropertyValue; if (expr1.nodePropertyValue == null && expr2.nodePropertyValue == null) { return(ret); } if (expr1.nodePropertyValue == null) { // expr1 is null, the ops is undefined, returns only expr2 as it is ret.nodePropertyValue = expr2.nodePropertyValue; return(ret); } if (expr2.nodePropertyValue == null) { // expr2 is null, ops is undefined, returns only expr1 as it is ret.nodePropertyValue = expr1.nodePropertyValue; return(ret); } if (expr1.nodePropertyValue is ElementId || expr2.nodePropertyValue is ElementId) { // For ElementId to be in this oper, the Name of the Element will be used and the rest will be converted to strings string expr1Str = null; if (expr1.nodePropertyValue is ElementId) { expr1Str = _element.Document.GetElement((ElementId)expr1.nodePropertyValue).Name; } else { expr1Str = expr1.nodePropertyValue.ToString(); } string expr2Str = null; if (expr2.nodePropertyValue is ElementId) { expr2Str = _element.Document.GetElement((ElementId)expr2.nodePropertyValue).Name; } else { expr2Str = expr2.nodePropertyValue.ToString(); } //ret.nodePropertyValue = expr1Str + " " + ops.GetText() + " " + expr2Str; ret.nodePropertyValue = expr1Str + expr2Str; } else if (expr1.nodePropertyValue is string || expr2.nodePropertyValue is string) { // one of the expr is a string, and therefore the entire expr will returns string //ret.nodePropertyValue = expr1.nodePropertyValue.ToString() + " " + ops.GetText() + " " + expr2.nodePropertyValue.ToString(); ret.nodePropertyValue = expr1.nodePropertyValue.ToString() + expr2.nodePropertyValue.ToString(); } else if (expr1.nodePropertyValue is double || expr2.nodePropertyValue is double) { if (ops.MULTIPLY() != null) { ret.nodePropertyValue = (double)expr1.nodePropertyValue * (double)expr2.nodePropertyValue; } if (ops.DIVIDE() != null) { ret.nodePropertyValue = (double)expr1.nodePropertyValue / (double)expr2.nodePropertyValue; } if (ops.ADDITION() != null) { ret.nodePropertyValue = (double)expr1.nodePropertyValue + (double)expr2.nodePropertyValue; } if (ops.SUBTRACT() != null) { ret.nodePropertyValue = (double)expr1.nodePropertyValue - (double)expr2.nodePropertyValue; } } else { if (ops.MULTIPLY() != null) { ret.nodePropertyValue = (int)expr1.nodePropertyValue * (int)expr2.nodePropertyValue; } if (ops.DIVIDE() != null) { ret.nodePropertyValue = (double)((int)expr1.nodePropertyValue / (int)expr2.nodePropertyValue); } if (ops.ADDITION() != null) { ret.nodePropertyValue = (int)expr1.nodePropertyValue + (int)expr2.nodePropertyValue; } if (ops.SUBTRACT() != null) { ret.nodePropertyValue = (int)expr1.nodePropertyValue - (int)expr2.nodePropertyValue; } } return(ret); }