private List <ElementId> PurgeMaterials() { List <Autodesk.Revit.DB.ElementId> materials = new FilteredElementCollector(this.InternalDocument) .OfClass(typeof(Autodesk.Revit.DB.Material)) .ToElementIds() .ToList(); if (materials == null) { return(new List <ElementId>()); } List <Autodesk.Revit.DB.Element> elementTypes = new FilteredElementCollector(this.InternalDocument) .WhereElementIsElementType() .ToElements() .ToList(); if (elementTypes == null) { return(new List <ElementId>()); } var nonPurgedElements = new HashSet <ElementId>(); for (int i = 0; i < elementTypes.Count; i++) { nonPurgedElements.UnionWith(GetUsedMaterialIds(elementTypes[i])); } List <ElementId> purgeableMaterials = materials.Except(nonPurgedElements).ToList(); if (purgeableMaterials.Count > 0) { this.InternalDocument.Delete(purgeableMaterials); } List <ElementId> purgedMaterialAssets = PurgeMaterialAssets(nonPurgedElements.ToList()); return(purgeableMaterials.Concat(purgedMaterialAssets).ToList()); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; bool permission = false; List <ViewSheet> sheets = new FilteredElementCollector(doc) .OfClass(typeof(ViewSheet)) .WhereElementIsNotElementType() .Cast <ViewSheet>() .ToList(); List <ElementId> usedViews = new List <ElementId>(); foreach (var sheet in sheets) { usedViews.AddRange(sheet.GetAllPlacedViews().ToList()); } usedViews = usedViews.Distinct().ToList(); if (usedViews.Count == 0) { TaskDialog.Show("Error", "Cannot let that happen - at least one view needs to remain in the project. (No views are placed on sheets.)"); return(Result.Failed); } List <ElementId> delete = new FilteredElementCollector(doc) .OfCategory(BuiltInCategory.OST_Views) .OfClass(typeof(Autodesk.Revit.DB.View)) .WhereElementIsNotElementType() .Cast <Autodesk.Revit.DB.View>() .Where(x => !x.IsTemplate) .Where(sc => (sc as ViewSchedule) == null) .Select(x => x.Id) .ToList(); TaskDialog.Show("Warning", "Be careful with that though .. "); string msg = "You want to delete all those hard drawn views. You certain?"; System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(msg, "Are you really sure?", System.Windows.Forms.MessageBoxButtons.YesNo); if (dialogResult == System.Windows.Forms.DialogResult.Yes) { permission = true; } else if (dialogResult == System.Windows.Forms.DialogResult.No) { permission = false; } if (!permission) { return(Result.Cancelled); } using (Transaction t = new Transaction(doc, "Delete Views not on Sheets")) { t.Start(); doc.Delete(delete.Except(usedViews).ToArray()); t.Commit(); } return(Result.Succeeded); }
public void Execute(UpdaterData data) { if (!Paused) { Document doc = data.GetDocument(); List <ElementId> list = data.GetAddedElementIds().ToList(); list.AddRange(data.GetModifiedElementIds()); IList <Element> list2 = (from ElementId id in list select doc.GetElement(id)).ToList(); Definition parameterDefinition = LODapp.GetParameterDefinition(doc, "Zone"); ElementId val = LODapp.GetLODparameter(doc, parameterDefinition).get_Id(); ParameterValueProvider val2 = new ParameterValueProvider(val); FilterStringRuleEvaluator val3 = new FilterStringContains(); FilterStringRuleEvaluator val4 = new FilterStringEquals(); FilterStringRuleEvaluator val5 = new FilterStringBeginsWith(); foreach (Element item in list2) { string text = item.LookupParameter("Name").AsString(); if (!string.IsNullOrWhiteSpace(text)) { FilterRule[] array = (FilterRule[])new FilterRule[3] { new FilterStringRule(val2, val4, text, true), new FilterStringRule(val2, val5, text + ", ", true), new FilterStringRule(val2, val3, ", " + text, true) }; ElementParameterFilter val6 = new ElementParameterFilter((IList <FilterRule>)array); IList <Element> list3 = new FilteredElementCollector(doc).WhereElementIsNotElementType().WherePasses(val6).ToElements(); BoundingBoxIntersectsFilter val7 = new BoundingBoxIntersectsFilter(ToOutline(item.get_BoundingBox(null))); IList <Element> list4 = new FilteredElementCollector(doc).WhereElementIsNotElementType().WherePasses(val7).ToElements(); IEnumerable <Element> enumerable = list3.Except(list4, new EqualUniqueId()); IEnumerable <Element> enumerable2 = list4.Except(list3, new EqualUniqueId()); foreach (Element item2 in enumerable) { Parameter val8 = item2.get_Parameter(parameterDefinition); if (val8 != null) { string text2 = val8.AsString() ?? string.Empty; string text3; if (text2.Length > text.Length) { int num = text2.IndexOf(text); text3 = ((num < 2 || text2[num - 2] != ',') ? text2.Remove(num, text.Length + 2) : text2.Remove(num - 2, text.Length + 2)); } else { text3 = string.Empty; } val8.Set(text3); } } foreach (Element item3 in enumerable2) { Parameter val9 = item3.get_Parameter(parameterDefinition); string text4 = (((int)val9 != 0) ? val9.AsString() : null) ?? string.Empty; string text5 = (text4.Length <= 0) ? text : (text4 + ", " + text); Parameter val10 = item3.get_Parameter(parameterDefinition); if ((int)val10 != 0) { val10.Set(text5); } } } } } }