internal static bool TryGetFilterStringParam(DB.BuiltInParameter paramId, ref string pattern, out DB.ElementFilter filter)
        {
            if (pattern is string subPattern)
            {
                var inverted = false;
                var method   = Operator.CompareMethodFromPattern(ref subPattern, ref inverted);
                if (Operator.CompareMethod.Nothing < method && method < Operator.CompareMethod.Wildcard)
                {
                    var evaluator = default(DB.FilterStringRuleEvaluator);
                    switch (method)
                    {
                    case Operator.CompareMethod.Equals: evaluator = new DB.FilterStringEquals(); break;

                    case Operator.CompareMethod.StartsWith: evaluator = new DB.FilterStringBeginsWith(); break;

                    case Operator.CompareMethod.EndsWith: evaluator = new DB.FilterStringEndsWith(); break;

                    case Operator.CompareMethod.Contains: evaluator = new DB.FilterStringContains(); break;
                    }

                    var rule = new DB.FilterStringRule
                               (
                        new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                        evaluator,
                        subPattern,
                        true
                               );

                    filter  = new DB.ElementParameterFilter(rule, inverted);
                    pattern = default;
                    return(true);
                }
            }

            filter = default;
            return(false);
        }
Ejemplo n.º 2
0
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            var parameterKey = default(Types.ParameterKey);

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

            DA.DisableGapLogic();

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

                return;
            }

            var provider = new DB.ParameterValueProvider(parameterKey);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            if (rule is object)
            {
                if (Condition == ConditionType.NotEquals)
                {
                    DA.SetData("Rule", new DB.FilterInverseRule(rule));
                }
                else
                {
                    DA.SetData("Rule", rule);
                }
            }
        }