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)); } } } }
/// <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); }
/// <summary> /// Creates a ElementFilter that passes elements with an ElementId based parameter that match the provided FilterNumericRuleEvaluator. The filter should then be passed to a Collector node and the Collector retrieves elements that pass the filter. /// </summary> /// <param name="parameterId">The ElementId as an int of the parameter to search.</param> /// <param name="value">Value of the parameter to search for.</param> /// <param name="filterNumberRule">Revit FilterNumberRuleEvaluator that determines how to search the parameter.</param> /// <param name="inverted">If true, the filter elements NOT matching the filter criteria are chosen.</param> /// <returns name="ElementFilter">An Element Filter. The filter should then be passed to a Collector node and the Collector retrieves elements that pass the filter.</returns> public static revitDB.ElementFilter FilterElementIdParameter( int parameterId, int value, revitDB.FilterNumericRuleEvaluator filterNumberRule, [DefaultArgument("false")] bool inverted) { revitDB.ParameterValueProvider provider = new revitDB.ParameterValueProvider(new revitDB.ElementId(parameterId)); if (filterNumberRule != null) { revitDB.FilterRule filterRule = new revitDB.FilterElementIdRule(provider, filterNumberRule, new revitDB.ElementId(value)); return(new revitDB.ElementParameterFilter(filterRule, inverted)); } else { return(null); } }
/// <summary> /// Creates a ElementFilter that passes elements with a number based parameter that match the provided FilterNumericRuleEvaluator. The filter should then be passed to a Collector node and the Collector retrieves elements that pass the filter. /// </summary> /// <param name="parameterId">The ElementId as an int of the parameter to search.</param> /// <param name="value">Value of the parameter to search for.</param> /// <param name="tolerance">Tolerance on how close tot he value a match should be.</param> /// <param name="filterNumberRule">Revit FilterNumberRuleEvaluator that determines how to search the parameter.</param> /// <param name="inverted">If true, the filter elements NOT matching the filter criteria are chosen.</param> /// <returns name="ElementFilter">An Element Filter. The filter should then be passed to a Collector node and the Collector retrieves elements that pass the filter.</returns> public static revitDB.ElementFilter FilterElementNumberParameter( int parameterId, double value, [DefaultArgument("0.0001")] double tolerance, revitDB.FilterNumericRuleEvaluator filterNumberRule, [DefaultArgument("false")] bool inverted) { revitDB.ParameterValueProvider provider = new revitDB.ParameterValueProvider(new revitDB.ElementId(parameterId)); if (filterNumberRule != null) { revitDB.FilterRule filterRule = new revitDB.FilterDoubleRule(provider, filterNumberRule, value, tolerance); return(new revitDB.ElementParameterFilter(filterRule, inverted)); } else { return(null); } }
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)); } }
protected override void TrySolveInstance(IGH_DataAccess DA) { var typeIds = new List <DB.ElementId>(); if (!DA.GetDataList("Types", typeIds)) { return; } var inverted = false; if (!DA.GetData("Inverted", ref inverted)) { return; } var provider = new DB.ParameterValueProvider(new DB.ElementId(DB.BuiltInParameter.ELEM_TYPE_PARAM)); if (typeIds.Count == 1) { var rule = new DB.FilterElementIdRule(provider, new DB.FilterNumericEquals(), typeIds[0]); DA.SetData("Filter", new DB.ElementParameterFilter(rule, inverted)); } 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 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); } } }
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 } }); }