protected override void SolveInstance(IGH_DataAccess DA)
        {
            var parameterId = ElementId.InvalidElementId;

            if (!DA.GetData("ParameterKey", ref parameterId))
            {
                return;
            }

            DA.DisableGapLogic();

            if (!TryGetParameterDefinition(Revit.ActiveDBDocument, parameterId, out var storageType, out var parameterType))
            {
                if (parameterId.TryGetBuiltInParameter(out var builtInParameter))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to found parameter '{LabelUtils.GetLabelFor(builtInParameter)}' in Revit document.");
                }
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to found parameter '{parameterId.IntegerValue}' in Revit document.");
                }

                return;
            }

            var provider = new ParameterValueProvider(parameterId);

            Autodesk.Revit.DB.FilterRule rule = null;
            if (storageType == StorageType.String)
            {
                FilterStringRuleEvaluator ruleEvaluator = null;
                switch (Condition)
                {
                case ConditionType.NotEquals:
                case ConditionType.Equals:          ruleEvaluator = new FilterStringEquals(); break;

                case ConditionType.Greater:         ruleEvaluator = new FilterStringGreater(); break;

                case ConditionType.GreaterOrEqual:  ruleEvaluator = new FilterStringGreaterOrEqual(); break;

                case ConditionType.Less:            ruleEvaluator = new FilterStringLess(); break;

                case ConditionType.LessOrEqual:     ruleEvaluator = new FilterStringLessOrEqual(); break;
                }

                var goo = default(GH_String);
                if (DA.GetData("Value", ref goo))
                {
                    rule = new FilterStringRule(provider, ruleEvaluator, goo.Value, true);
                }
            }
            else
            {
                FilterNumericRuleEvaluator ruleEvaluator = null;
                switch (Condition)
                {
                case ConditionType.NotEquals:
                case ConditionType.Equals:          ruleEvaluator = new FilterNumericEquals(); break;

                case ConditionType.Greater:         ruleEvaluator = new FilterNumericGreater(); break;

                case ConditionType.GreaterOrEqual:  ruleEvaluator = new FilterNumericGreaterOrEqual(); break;

                case ConditionType.Less:            ruleEvaluator = new FilterNumericLess(); break;

                case ConditionType.LessOrEqual:     ruleEvaluator = new FilterNumericLessOrEqual(); break;
                }

                switch (storageType)
                {
                case StorageType.Integer:
                {
                    var goo = default(GH_Integer);
                    if (DA.GetData("Value", ref goo))
                    {
                        rule = new FilterIntegerRule(provider, ruleEvaluator, goo.Value);
                    }
                }
                break;

                case StorageType.Double:
                {
                    var goo = default(GH_Number);
                    if (DA.GetData("Value", ref goo))
                    {
                        if (Condition == ConditionType.Equals || Condition == ConditionType.NotEquals)
                        {
                            if (parameterType == ParameterType.Length || parameterType == ParameterType.Area || parameterType == ParameterType.Volume)
                            {
                                rule = new FilterDoubleRule(provider, ruleEvaluator, ToHost(goo.Value, parameterType), ToHost(Revit.VertexTolerance, parameterType));
                            }
                            else
                            {
                                rule = new FilterDoubleRule(provider, ruleEvaluator, ToHost(goo.Value, parameterType), 1e-6);
                            }
                        }
                        else
                        {
                            rule = new FilterDoubleRule(provider, ruleEvaluator, ToHost(goo.Value, parameterType), 0.0);
                        }
                    }
                }
                break;

                case StorageType.ElementId:
                {
                    switch (parameterType)
                    {
                    case (ParameterType)int.MaxValue: // Category
                    {
                        var value = default(Types.Category);
                        if (DA.GetData("Value", ref value))
                        {
                            rule = new FilterElementIdRule(provider, ruleEvaluator, value);
                        }
                    }
                    break;

                    case ParameterType.FamilyType:
                    {
                        var value = default(Types.ElementType);
                        if (DA.GetData("Value", ref value))
                        {
                            rule = new FilterElementIdRule(provider, ruleEvaluator, value);
                        }
                    }
                    break;

                    default:
                    {
                        var value = default(Types.Element);
                        if (DA.GetData("Value", ref value))
                        {
                            rule = new FilterElementIdRule(provider, ruleEvaluator, value);
                        }
                    }
                    break;
                    }
                }
                break;
                }
            }

            if (rule is object)
            {
                if (Condition == ConditionType.NotEquals)
                {
                    DA.SetData("Rule", new FilterInverseRule(rule));
                }
                else
                {
                    DA.SetData("Rule", rule);
                }
            }
        }
Exemple #2
0
        private ElementFilter CreateParamFilter(int paramId, string strOperator, string paramValue, bool emptyValue)
        {
            try
            {
                ParameterProperties    pp  = selectedParameters[paramId];
                ParameterValueProvider pvp = new ParameterValueProvider(new ElementId(pp.ParamId));

                switch (pp.ParamStorageType)
                {
                case StorageType.ElementId:
                    if (pp.ParamName == "Phase Created" || pp.ParamName == "Phase Demolished")
                    {
                        FilterNumericRuleEvaluator fnre1 = new FilterNumericEquals();

                        var       phaseId   = from phase in phases where phase.Value.Name == paramValue select phase.Key;
                        int       pId       = phaseId.First();
                        ElementId ruleValId = new ElementId(pId);

                        FilterRule filterRule1 = new FilterElementIdRule(pvp, fnre1, ruleValId);

                        if (strOperator == "does not equal")
                        {
                            return(new ElementParameterFilter(filterRule1, true));
                        }
                        else
                        {
                            return(new ElementParameterFilter(filterRule1));
                        }
                    }
                    return(null);

                case StorageType.Double:
                    if (emptyValue)
                    {
                        FilterNumericRuleEvaluator fnre       = new FilterNumericGreater();
                        FilterDoubleRule           doubleRule = new FilterDoubleRule(pvp, fnre, 0, double.Epsilon);
                        return(new ElementParameterFilter(doubleRule, true));
                    }

                    FilterNumericRuleEvaluator fnre2 = FindFilterNumericRuleEvaluator(strOperator);
                    double dblValue = 0;
                    if (!string.IsNullOrEmpty(paramValue))
                    {
                        double.TryParse(paramValue, out dblValue);
                    }

                    FilterRule filterRule2 = new FilterDoubleRule(pvp, fnre2, dblValue, double.Epsilon);

                    if (strOperator == "does not equal")
                    {
                        return(new ElementParameterFilter(filterRule2, true));
                    }
                    else
                    {
                        return(new ElementParameterFilter(filterRule2));
                    }

                case StorageType.Integer:
                    if (emptyValue)
                    {
                        FilterNumericRuleEvaluator fnre        = new FilterNumericGreater();
                        FilterIntegerRule          integerRule = new FilterIntegerRule(pvp, fnre, 0);
                        return(new ElementParameterFilter(integerRule, true));
                    }
                    FilterNumericRuleEvaluator fnre3 = FindFilterNumericRuleEvaluator(strOperator);

                    int intValue = 0;
                    if (pp.ParamName == "Workset")
                    {
                        var worksetId = from ws in worksets where ws.Value.Name == paramValue select ws.Key;
                        intValue = worksetId.First();
                    }
                    else if (!string.IsNullOrEmpty(paramValue))
                    {
                        int.TryParse(paramValue, out intValue);
                    }

                    FilterRule filterRule3 = new FilterIntegerRule(pvp, fnre3, intValue);

                    if (strOperator == "does not equal")
                    {
                        return(new ElementParameterFilter(filterRule3, true));
                    }
                    else
                    {
                        return(new ElementParameterFilter(filterRule3));
                    }

                case StorageType.String:
                    if (emptyValue)
                    {
                        FilterStringRuleEvaluator fsre       = new FilterStringGreater();
                        FilterStringRule          stringRule = new FilterStringRule(pvp, fsre, "", false);
                        return(new ElementParameterFilter(stringRule, true));
                    }

                    FilterStringRuleEvaluator fnre4 = FindFilterStringRuleEvaluator(strOperator);
                    string           strValue       = paramValue;
                    FilterStringRule filterRule4    = new FilterStringRule(pvp, fnre4, strValue, false);

                    if (strOperator.Contains("does not"))
                    {
                        return(new ElementParameterFilter(filterRule4, true));
                    }
                    else
                    {
                        return(new ElementParameterFilter(filterRule4, false));
                    }

                default:
                    return(null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create parameter filters.\n" + ex.Message, "CreateParamFilter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(null);
        }