public static void SelectAllLines(UIDocument uiDoc) { Document currentDoc = uiDoc.Document; Selection Sel = uiDoc.Selection; ElementId v = uiDoc.ActiveView.Id; ISelectionFilter selectionFilter = new LineSelectionFilter(); Reference ChangedObject = Sel.PickObject(ObjectType.Element, selectionFilter); CurveElement ChangedElement = currentDoc.GetElement(ChangedObject.ElementId) as CurveElement; FilteredElementCollector lineCollector = new FilteredElementCollector(currentDoc, v); ICollection <ElementId> lines = lineCollector.OfCategory(BuiltInCategory.OST_Lines).ToElementIds().ToList(); List <ElementId> linelist = new List <ElementId>(); foreach (ElementId eid in lines) { CurveElement ce = currentDoc.GetElement(eid) as CurveElement; if (ce.LineStyle.Name == ChangedElement.LineStyle.Name) { linelist.Add(eid); } } Sel.SetElementIds(linelist); }
public static void EditLinetypes(UIDocument uiDoc) { Document currentDoc = uiDoc.Document; using (TransactionGroup tgroup = new TransactionGroup(currentDoc, "EditLineTypes")) { tgroup.Start(); Selection sel = uiDoc.Selection; ISelectionFilter selectionFilter = new LineSelectionFilter(); Reference ChangedObject = sel.PickObject(ObjectType.Element, selectionFilter); CurveElement ChangedElement = currentDoc.GetElement(ChangedObject.ElementId) as CurveElement; Reference NewObject = sel.PickObject(ObjectType.Element, selectionFilter); CurveElement NewElement = currentDoc.GetElement(NewObject.ElementId) as CurveElement; GraphicsStyle style = NewElement.LineStyle as GraphicsStyle; Category linesCat = currentDoc.Settings.Categories.get_Item("Lines"); Category cat = linesCat.SubCategories.Cast <Category>().Where(c => c.Name == ChangedElement.LineStyle.Name).First(); ElementId OldLinestyleID = cat.Id; FilteredElementCollector lineCollector = new FilteredElementCollector(currentDoc); List <ElementId> Lines = lineCollector.OfCategory(BuiltInCategory.OST_Lines).ToElementIds().ToList(); foreach (ElementId EID in Lines) { CurveElement CE = currentDoc.GetElement(EID) as CurveElement; if (CE.LineStyle.Name == cat.Name) { if (CE.GroupId == ElementId.InvalidElementId) { using (Transaction ungroupedtrans = new Transaction(currentDoc, "ungrouped transaction")) { ungroupedtrans.Start(); CE.LineStyle = style; ungroupedtrans.Commit(); } } else { Group group = currentDoc.GetElement(CE.GroupId) as Group; string gn = group.GroupType.Name; if (group.GroupId == ElementId.InvalidElementId) { Dictionary <ElementId, GroupType> groupadjustments = new Dictionary <ElementId, GroupType>(); ElementId GroupTypeId = group.GroupType.Id; if (groupadjustments.Keys.Contains(GroupTypeId)) { using (Transaction GroupTrans = new Transaction(currentDoc, "grouped transaction")) { GroupTrans.Start(); FailureHandlingOptions options = GroupTrans.GetFailureHandlingOptions(); options.SetFailuresPreprocessor(new DiscardWarnings()); GroupTrans.SetFailureHandlingOptions(options); group.GroupType = groupadjustments[GroupTypeId]; GroupTrans.Commit(); } } else { using (Transaction GroupTrans = new Transaction(currentDoc, "grouped transaction")) { GroupTrans.Start(); FailureHandlingOptions options = GroupTrans.GetFailureHandlingOptions(); options.SetFailuresPreprocessor(new DiscardWarnings()); GroupTrans.SetFailureHandlingOptions(options); ICollection <ElementId> set = group.UngroupMembers(); GroupTrans.Commit(); using (Transaction Group2Trans = new Transaction(currentDoc, "grouped transaction 2")) { Group2Trans.Start(); FailureHandlingOptions SubGroupOptions = Group2Trans.GetFailureHandlingOptions(); SubGroupOptions.SetFailuresPreprocessor(new DiscardWarnings()); Group2Trans.SetFailureHandlingOptions(SubGroupOptions); CE.LineStyle = style; Group newgroup = currentDoc.Create.NewGroup(set); newgroup.GroupType.Name = gn + "_New"; Group2Trans.Commit(); groupadjustments.Add(GroupTypeId, newgroup.GroupType); } } } } } } } using (Transaction deletetrans = new Transaction(currentDoc, "Delete Transaction")) { deletetrans.Start(); currentDoc.Delete(OldLinestyleID); deletetrans.Commit(); } tgroup.Assimilate(); } }