/// <summary> /// Try to found an open <see cref="Autodesk.Revit.UI.UIView"/> that is referencing the specified <see cref="Autodesk.Revit.DB.View"/> element. /// </summary> /// <param name="view"></param> /// <param name="uiView"></param> /// <returns>true on succes.</returns> public static bool TryGetOpenUIView(this Autodesk.Revit.DB.View view, out Autodesk.Revit.UI.UIView uiView) { using (var uiDocument = new Autodesk.Revit.UI.UIDocument(view.Document)) { uiView = uiDocument.GetOpenUIViews().Where(x => x.ViewId == view.Id).FirstOrDefault(); return(uiView is object); } }
/// <summary> /// Try to found an open <see cref="Autodesk.Revit.UI.UIDocument"/> that is referencing the specified <see cref="Autodesk.Revit.DB.Document"/>. /// </summary> /// <param name="document"></param> /// <param name="uiDocument"></param> /// <returns>true on succes.</returns> public static bool TryGetOpenUIDocument(this Autodesk.Revit.DB.Document document, out Autodesk.Revit.UI.UIDocument uiDocument) { uiDocument = new Autodesk.Revit.UI.UIDocument(document); if (uiDocument.GetOpenUIViews().Count == 0) { uiDocument.Dispose(); uiDocument = default; return(false); } return(true); }
public static bool Release(this Document doc) { using (var uiDocument = new Autodesk.Revit.UI.UIDocument(doc)) { if (uiDocument.GetOpenUIViews().Count == 0) { return(doc.Close(false)); } } return(true); }
/// <summary> /// Gets the active Graphical <see cref="Autodesk.Revit.DB.View"/> of the provided <see cref="Autodesk.Revit.DB.Document"/>. /// </summary> /// <param name="doc"></param> /// <returns>The active graphical <see cref="Autodesk.Revit.DB.View"/></returns> public static View GetActiveGraphicalView(this Document doc) { using (var uiDocument = new Autodesk.Revit.UI.UIDocument(doc)) { var activeView = uiDocument.ActiveGraphicalView; if (activeView is null) { var openViews = Rhinoceros.InvokeInHostContext(() => uiDocument.GetOpenUIViews()). Select(x => doc.GetElement(x.ViewId) as View). Where(x => x.ViewType.IsGraphicalViewType()); activeView = openViews.FirstOrDefault(); } return(activeView); } }
public static bool Release(this Document doc) { if (doc.IsValid()) { try { using (var uiDocument = new Autodesk.Revit.UI.UIDocument(doc)) { if (uiDocument.GetOpenUIViews().Count == 0) { return(doc.Close(false)); } } } catch (Autodesk.Revit.Exceptions.ArgumentException) { } } return(false); }
/// <summary> /// Duplicates A Sheet. /// </summary> /// <param name="sheet">The Sheet to be Duplicated.</param> /// <param name="duplicateWithContents">Set to true that Duplicate sheet with contents</param> /// <param name="duplicateWithView">Set to true that Duplicate sheet with views.</param> /// <param name="viewDuplicateOption">Enter View Duplicate Option: 0 = Duplicate. 1 = AsDependent. 2 = WithDetailing.</param> /// <param name="prefix"></param> /// <param name="suffix">When prefix and suffix are both empty, suffix will set a default value - " - Copy".</param> /// <returns></returns> public static Sheet DuplicateSheet(Sheet sheet, bool duplicateWithContents = false, bool duplicateWithView = false, int viewDuplicateOption = 0, string prefix = "", string suffix = "") { if (sheet == null) { throw new ArgumentNullException(nameof(sheet)); } ViewDuplicateOption Option = 0; switch (viewDuplicateOption) { case 0: Option = ViewDuplicateOption.Duplicate; break; case 1: Option = ViewDuplicateOption.AsDependent; break; case 2: Option = ViewDuplicateOption.WithDetailing; break; default: throw new ArgumentException(Properties.Resources.ViewDuplicateOptionOutofRange); } if (String.IsNullOrEmpty(prefix) && String.IsNullOrEmpty(suffix)) { suffix = " - Copy"; } Sheet newSheet = null; try { RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(Application.Document.Current.InternalDocument); var oldElements = ElementBinder.GetElementsFromTrace <Autodesk.Revit.DB.Element>(Document); List <ElementId> elementIds = new List <ElementId>(); var newSheetNumber = prefix + sheet.SheetNumber + suffix; var newSheetName = sheet.SheetName; List <Autodesk.Revit.DB.Element> TraceElements = new List <Autodesk.Revit.DB.Element>(); if (oldElements != null) { foreach (var element in oldElements) { elementIds.Add(element.Id); if (element is ViewSheet) { var oldSheet = (element as ViewSheet).ToDSType(false) as Sheet; if (oldSheet.SheetNumber.Equals(newSheetNumber)) { if ((duplicateWithView && oldElements.Count() > 1) || (!duplicateWithView && oldElements.Count() == 1)) { newSheet = oldSheet; TraceElements.AddRange(oldElements); } if (newSheet != null) { if (duplicateWithContents) { DuplicateSheetAnnotations(sheet, newSheet); } else { DeleteSheetAnnotations(newSheet); } } } } } if (newSheet == null) { Autodesk.Revit.UI.UIDocument uIDocument = new Autodesk.Revit.UI.UIDocument(Document); var openedViews = uIDocument.GetOpenUIViews().ToList(); var shouldClosedViews = openedViews.FindAll(x => elementIds.Contains(x.ViewId)); if (shouldClosedViews.Count > 0) { foreach (var v in shouldClosedViews) { if (uIDocument.GetOpenUIViews().ToList().Count() > 1) { v.Close(); } else { throw new InvalidOperationException(string.Format(Properties.Resources.CantCloseLastOpenView, v.ToString())); } } } Document.Delete(elementIds); } } if (newSheet == null && TraceElements.Count == 0) { // Create a new Sheet with different SheetNumber var titleBlockElement = sheet.TitleBlock.First() as FamilyInstance; FamilySymbol TitleBlock = titleBlockElement.InternalFamilyInstance.Symbol; FamilyType titleBlockFamilyType = ElementWrapper.Wrap(TitleBlock, true); if (!CheckUniqueSheetNumber(newSheetNumber)) { throw new ArgumentException(String.Format(Properties.Resources.SheetNumberExists, newSheetNumber)); } var viewSheet = Autodesk.Revit.DB.ViewSheet.Create(Document, TitleBlock.Id); newSheet = new Sheet(viewSheet); newSheet.InternalSetSheetName(newSheetName); newSheet.InternalSetSheetNumber(newSheetNumber); TraceElements.Add(newSheet.InternalElement); // Copy Annotation Elements from sheet to new sheet by ElementTransformUtils.CopyElements if (duplicateWithContents) { DuplicateSheetAnnotations(sheet, newSheet); } if (duplicateWithView) { // Copy ScheduleSheetInstance except RevisionSchedule from sheet to new sheet by ElementTransformUtils.CopyElements DuplicateScheduleSheetInstance(sheet, newSheet); // Duplicate Viewport in sheet and place on new sheet TraceElements.AddRange(DuplicateViewport(sheet, newSheet, Option, prefix, suffix)); } } ElementBinder.SetElementsForTrace(TraceElements); RevitServices.Transactions.TransactionManager.Instance.TransactionTaskDone(); } catch (Exception e) { if (newSheet != null) { newSheet.Dispose(); } throw e; } return(newSheet); }
/// <summary> /// Duplicates A view. /// </summary> /// <param name="view">The View to be Duplicated</param> /// <param name="viewDuplicateOption">Enter View Duplicate Option: Duplicate, AsDependent or WithDetailing.</param> /// <param name="prefix"></param> /// <param name="suffix"></param> /// <returns></returns> public static Revit.Elements.Views.View DuplicateView(View view, string viewDuplicateOption = "Duplicate", string prefix = "", string suffix = "") { View newView = null; if (view == null) { throw new ArgumentNullException(nameof(view)); } if (view is Sheet) { throw new ArgumentException(Properties.Resources.DuplicateViewCantApplySheet); } ViewDuplicateOption Option = (ViewDuplicateOption)Enum.Parse(typeof(ViewDuplicateOption), viewDuplicateOption); try { RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(Application.Document.Current.InternalDocument); var viewElement = ElementBinder.GetElementFromTrace <Autodesk.Revit.DB.View>(Document); int count = 0; string newViewName = ""; if (!String.IsNullOrEmpty(prefix) || !String.IsNullOrEmpty(suffix)) { newViewName = prefix + view.Name + suffix; } Autodesk.Revit.UI.UIDocument uIDocument = new Autodesk.Revit.UI.UIDocument(Document); var openedViews = uIDocument.GetOpenUIViews().ToList(); if (viewElement != null) { count++; var oldViewName = viewElement.Name; if (oldViewName.Equals(newViewName)) { newView = viewElement.ToDSType(false) as View; } else { count--; } } if (count == 0) { if (!CheckUniqueViewName(newViewName)) { throw new ArgumentException(String.Format(Properties.Resources.ViewNameExists, newViewName)); } if (view.InternalView.CanViewBeDuplicated(Option)) { var viewID = view.InternalView.Duplicate(Option); var viewEle = Document.GetElement(viewID) as Autodesk.Revit.DB.View; newView = ElementWrapper.ToDSType(viewEle, false) as View; } else { throw new Exception(String.Format(Properties.Resources.ViewCantBeDuplicated, view.Name)); } if (!String.IsNullOrEmpty(newViewName)) { var param = newView.InternalView.get_Parameter(BuiltInParameter.VIEW_NAME); param.Set(newViewName); } if (viewElement != null) { var shouldClosedViews = openedViews.FindAll(x => viewElement.Id == x.ViewId); if (shouldClosedViews.Count > 0) { foreach (var v in shouldClosedViews) { if (uIDocument.GetOpenUIViews().ToList().Count() > 1) { v.Close(); } else { throw new InvalidOperationException(string.Format(Properties.Resources.CantCloseLastOpenView, viewElement.ToString())); } } } } } ElementBinder.CleanupAndSetElementForTrace(Document, newView.InternalElement); RevitServices.Transactions.TransactionManager.Instance.TransactionTaskDone(); } catch (Exception e) { //if (e is Autodesk.Revit.Exceptions.InvalidOperationException) // throw e; if (newView != null) { newView.Dispose(); } throw e; } return(newView); }