protected static bool TryGetFilterIntegerParam(DB.BuiltInParameter paramId, int pattern, out DB.ElementFilter filter)
        {
            var rule = new DB.FilterIntegerRule
                       (
                new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                new DB.FilterNumericEquals(),
                pattern
                       );

            filter = new DB.ElementParameterFilter(rule, false);
            return(true);
        }
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            var types = new List <DB.ElementType>();

            if (!DA.GetDataList("Types", types))
            {
                return;
            }

            var inverted = false;

            if (!DA.GetData("Inverted", ref inverted))
            {
                return;
            }

            if (types.Any())
            {
                var provider = new DB.ParameterValueProvider(new DB.ElementId(DB.BuiltInParameter.ELEM_TYPE_PARAM));

                var typeIds = types.Select(x => x?.Id ?? DB.ElementId.InvalidElementId).ToArray();
                if (typeIds.Length == 1)
                {
                    var rule   = new DB.FilterElementIdRule(provider, new DB.FilterNumericEquals(), typeIds[0]);
                    var filter = new DB.ElementParameterFilter(rule, inverted) as DB.ElementFilter;

                    if (typeIds[0] == DB.ElementId.InvalidElementId)
                    {
                        filter = new DB.LogicalAndFilter
                                 (
                            new DB.ElementClassFilter(typeof(DB.PropertySetElement), inverted),
                            filter
                                 );
                    }

                    DA.SetData("Filter", filter);
                }
                else
                {
                    if (inverted)
                    {
                        var rules = typeIds.Select(x => new DB.FilterInverseRule(new DB.FilterElementIdRule(provider, new DB.FilterNumericEquals(), x))).ToArray();
                        DA.SetData("Filter", new DB.ElementParameterFilter(rules));
                    }
                    else
                    {
                        var filters = typeIds.Select(x => new DB.FilterElementIdRule(provider, new DB.FilterNumericEquals(), x)).Select(x => new DB.ElementParameterFilter(x)).ToArray();
                        DA.SetData("Filter", new DB.LogicalOrFilter(filters));
                    }
                }
            }
        }
        protected static bool TryGetFilterLengthParam(DB.BuiltInParameter paramId, double pattern, out DB.ElementFilter filter)
        {
            var rule = new DB.FilterDoubleRule
                       (
                new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                new DB.FilterNumericEquals(),
                pattern,
                Revit.VertexTolerance
                       );

            filter = new DB.ElementParameterFilter(rule, false);
            return(true);
        }
        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);
        }
Example #5
0
        public static IDictionary MergeElementTypes(DynaElem FromType, DynaElem ToType)
        {
            //  Name of Transaction
            string transactionName = "Merge Element Type";

            // Get the Revit elements from the Dynamo Elements
            RevitDB.ElementType rFromType = (RevitDB.ElementType)FromType.InternalElement;
            RevitDB.ElementType rToType   = (RevitDB.ElementType)ToType.InternalElement;

            RevitDoc document = rToType.Document;

            // Collect all instances of FromType
            RevitDB.FilteredElementCollector collector        = new RevitDB.FilteredElementCollector(document);
            RevitDB.BuiltInParameter         parameterId      = RevitDB.BuiltInParameter.ELEM_TYPE_PARAM;
            RevitDB.FilterNumericEquals      filterNumberRule = new RevitDB.FilterNumericEquals();
            RevitDB.ParameterValueProvider   provider         = new RevitDB.ParameterValueProvider(new RevitDB.ElementId(parameterId));
            RevitDB.FilterRule    filterRule      = new RevitDB.FilterElementIdRule(provider, filterNumberRule, rFromType.Id);
            RevitDB.ElementFilter filterParameter = new RevitDB.ElementParameterFilter(filterRule, false);

            Type instanceType = Select.InstanceClassFromTypeClass(rFromType.GetType());

            if (instanceType != null)
            {
                collector.OfClass(instanceType);
            }

            IEnumerable <RevitDB.Element> instances = collector
                                                      .WhereElementIsNotElementType()
                                                      .WherePasses(filterParameter)
                                                      .ToElements();

            // Intialize list for elements that are successfully merged and failed to merge.
            List <DynaElem> elements       = new List <DynaElem>();
            List <DynaElem> elementsFailed = new List <DynaElem>();

            // Define Function to change instances types.
            Action <IEnumerable <RevitDB.Element> > _SetType = (isntances) =>
            {
                foreach (RevitDB.Element elem in instances)
                {
                    // If Element is in a group, put the element in the failed list
                    int groupId = elem.GroupId.IntegerValue;
                    if (groupId == -1)
                    {
                        //elem.TextNoteType = rToType;
                        RevitDB.Parameter param = elem.get_Parameter(RevitDB.BuiltInParameter.ELEM_TYPE_PARAM);
                        param.Set(rToType.Id);
                        DynaElem dElem = elem.ToDSType(true);
                        elements.Add(dElem);
                    }
                    else
                    {
                        DynaElem dElem = elem.ToDSType(true);
                        elementsFailed.Add(dElem);
                    }
                }

                // Check if there are any instances of FromType left
                int count = collector.Count();
                if (count == 0)
                {
                    document.Delete(rFromType.Id);
                }
            };

            if (document.IsModifiable)
            {
                TransactionManager.Instance.EnsureInTransaction(document);
                _SetType(instances);
                TransactionManager.Instance.TransactionTaskDone();
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(document))
                {
                    trans.Start(transactionName);
                    _SetType(instances);
                    trans.Commit();
                }
            }

            return(new Dictionary <string, object>
            {
                { "Merged", elements },
                { "Failed", elementsFailed }
            });
        }