Exemplo n.º 1
0
        /// <summary>
        /// Given the name of the design option set, returns a list of set's design options.
        /// </summary>
        /// <param name="DesignOptionSetName">The name of the Design Option Set</param>
        /// <param name="document">A Autodesk.Revit.DB.Document object.  This does not work with Dynamo document objects.</param>
        /// <returns name="DesignOptions">A list of design options that belong to the DesignOptionSet</returns>
        public static IList <dynElem> GetDesignOptionsBySet(string DesignOptionSetName,
                                                            [DefaultArgument("Synthetic.Revit.Document.Current()")] revitDoc document)
        {
            revitDB.ElementCategoryFilter    catFilter   = new revitDB.ElementCategoryFilter(revitDB.BuiltInCategory.OST_DesignOptionSets);
            revitDB.FilteredElementCollector collectSets = new revitDB.FilteredElementCollector(document);
            IList <revitElem> sets = collectSets
                                     .WherePasses(catFilter)
                                     .Cast <revitDB.Element>()
                                     .Where(elem => elem.Name == DesignOptionSetName)
                                     .ToList();

            IList <revitDB.ElementFilter> paramFilters = new List <revitDB.ElementFilter>();

            foreach (revitElem set in sets)
            {
                revitDB.ElementId id = set.Id;
                revitDB.FilterNumericRuleEvaluator eval     = new revitDB.FilterNumericEquals();
                revitDB.ParameterValueProvider     provider = new revitDB.ParameterValueProvider(new revitDB.ElementId(revitDB.BuiltInParameter.OPTION_SET_ID));
                revitDB.FilterRule filterRule = new revitDB.FilterElementIdRule(provider, eval, id);

                paramFilters.Add(new revitDB.ElementParameterFilter(filterRule));
            }

            revitDB.ElementFilter orFilter = new revitDB.LogicalOrFilter(paramFilters);

            catFilter = new revitDB.ElementCategoryFilter(revitDB.BuiltInCategory.OST_DesignOptions);

            revitDB.LogicalAndFilter filter = new revitDB.LogicalAndFilter(new List <revitDB.ElementFilter> {
                orFilter, catFilter
            });


            revitDB.FilteredElementCollector collectOptions = new revitDB.FilteredElementCollector(document);
            IList <revitDB.Element>          elements       = collectOptions.WherePasses(filter).ToElements();

            IList <dynElem> dynamoElements = new List <dynElem>();

            foreach (revitDB.Element elem in elements)
            {
                try
                {
                    dynamoElements.Add(elem.ToDSType(true));
                }
                catch { }
            }

            return(dynamoElements);
        }
Exemplo n.º 2
0
        static DB.ElementFilter ElementCategoriesFilter(DB.Document doc, DB.ElementId[] ids)
        {
            var list = new List <DB.ElementFilter>();

            if (ids.Length == 1)
            {
                list.Add(new DB.ElementCategoryFilter(ids[0]));
            }
            else if (ids.Length > 1)
            {
                list.Add(new DB.ElementMulticategoryFilter(ids));
            }

            if (doc.IsFamilyDocument)
            {
                foreach (var id in ids)
                {
                    using (var provider = new DB.ParameterValueProvider(new DB.ElementId(DB.BuiltInParameter.FAMILY_ELEM_SUBCATEGORY)))
                        using (var evaluator = new DB.FilterNumericEquals())
                            using (var rule = new DB.FilterElementIdRule(provider, evaluator, id))
                                list.Add(new DB.ElementParameterFilter(rule));
                }
            }

            if (list.Count == 0)
            {
                var nothing = new DB.ElementFilter[] { new DB.ElementIsElementTypeFilter(true), new DB.ElementIsElementTypeFilter(false) };
                return(new DB.LogicalAndFilter(nothing));
            }
            else if (list.Count == 1)
            {
                return(list[0]);
            }
            else
            {
                return(new DB.LogicalOrFilter(list));
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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 }
            });
        }