private void InitializeOpenTransaction(string name) { m_Transaction.Start(Resources.IFCOpenReferenceFile); FailureHandlingOptions options = m_Transaction.GetFailureHandlingOptions(); //options.SetFailuresPreprocessor(Log); options.SetForcedModalHandling(true); options.SetClearAfterRollback(true); }
//---------------------------------------------------------- public void PurgeUnusedType(Document doc) { var allElementsInView = new FilteredElementCollector(doc, doc.ActiveView.Id).ToElements(); List <string> typesName = new List <string>(); List <string> familysName = new List <string>(); foreach (Element ele in allElementsInView) { typesName.Add(ele.Name); string familyName = ele.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString(); familysName.Add(familyName); } List <string> typesName_Unique = typesName.Distinct().ToList(); List <string> familysName_Unique = familysName.Distinct().ToList(); var elementType = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).ToList(); var elementType1 = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).ToList(); List <ElementId> idDelete = new List <ElementId>(); foreach (ElementType type in elementType) { if (DocumentValidation.CanDeleteElement(doc, type.Id) && typesName_Unique.Contains(type.Name) == false && familysName_Unique.Contains(type.FamilyName)) { idDelete.Add(type.Id); } } foreach (FamilySymbol type in elementType1) { if (DocumentValidation.CanDeleteElement(doc, type.Id) && typesName_Unique.Contains(type.Name) == false && familysName_Unique.Contains(type.FamilyName)) { idDelete.Add(type.Id); } } foreach (ElementId type in idDelete) { Transaction transaction = new Transaction(doc); FailureHandlingOptions failure = transaction.GetFailureHandlingOptions(); failure.SetClearAfterRollback(true); failure.SetForcedModalHandling(false); failure.SetFailuresPreprocessor(new RollbackIfErrorOccurs()); transaction.SetFailureHandlingOptions(failure); transaction.Start("PurgeUnusedType"); try { doc.Delete(type); transaction.Commit(); } catch (Exception) { transaction.Dispose(); } } }