From d80bf665087915098bf0f4c6c4fff4df5588365d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20D=C3=A9ri?= Date: Sat, 2 Dec 2023 01:08:28 +0100 Subject: [PATCH 1/3] improved view handling --- .../Toggles.panel/sync.pushbutton/config.py | 30 +++-- .../Toggles.panel/sync.pushbutton/script.py | 111 +++++++++++++----- 2 files changed, 107 insertions(+), 34 deletions(-) diff --git a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py index f77ec3dd6..e397b5fda 100644 --- a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py +++ b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py @@ -6,13 +6,29 @@ def close_inactive_views(): - form = forms.SelectFromList.show(["Yes", "No"], title = "Close all inactive views before synchronisation", height = 200) - if form=="Yes": - setattr(my_config, "close_inactive_views", True) - script.save_config() - else: - setattr(my_config, "close_inactive_views", False) - script.save_config() + try: + current_setting = getattr(my_config, "view_handling") + except: + current_setting = "nothing" + options = { + "nothing": "Don't touch my views", + "reopen": "Close them, but reopen them right after", + "close": "Close them, and call it a day" + } + + selection = forms.ask_for_one_item( + items=options.values(), + default=options.get(current_setting), + title="View Handling", + prompt="Specify what to do with open views" + ) + + setattr( + my_config, + "view_handling", + next((k for k in options if options[k] == selection), None) + ) + if __name__ == "__main__": close_inactive_views() \ No newline at end of file diff --git a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py index 41a7d7a32..5967fd377 100644 --- a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py +++ b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py @@ -4,46 +4,93 @@ doc = revit.doc +logger = script.get_logger() + my_config = script.get_config() try: - close = getattr(my_config, "close_inactive_views") + view_handling = getattr(my_config, "view_handling") except: - close = False + view_handling = "nothing" + +view_cache = [] -def close_inactive_views(closing_config=close, document=doc): - """Closes all inactive views except for the starting view. +def close_inactive_views(view_handling="nothing", document=doc): + """ + Close inactive views in the Autodesk Revit project based on specified handling. + + Args: + view_handling (str, optional): Specifies how to handle inactive views. + Options are: + - "nothing": Do nothing and return. + - anything else: Add closed views to the view cache for potential reopening. + document (Autodesk.Revit.DB.Document, optional): The Revit document to perform + the operation on. Default is the active document. + + Returns: + None + + Example: + >>> close_inactive_views(view_handling="reopen") + """ + if view_handling == "nothing": + return + starting_view_id = DB.StartingViewSettings.GetStartingViewSettings(document).ViewId + starting_view = document.GetElement(starting_view_id) + if starting_view: + uidoc = revit.uidoc + HOST_APP.uidoc.RequestViewChange(starting_view) + uidoc.ActiveView = starting_view + all_open_views = uidoc.GetOpenUIViews() + for ui_view in all_open_views: + if view_handling == "reopen": + view_cache.append(ui_view.ViewId) + doc_view = document.GetElement(ui_view.ViewId) + if doc_view.Name != starting_view.Name : + ui_view.Close() + else: + forms.show_balloon( + "No Starting View Set", + "No Starting View Set", + ) + + +def set_active_view(view): + """ + Set the active view in the Autodesk Revit project to the specified view. - Parameters: - closing_config (bool): Flag indicating whether to close inactive views or not. - document (Document): The Revit document. Default is the active document. + Args: + view (Autodesk.Revit.DB.View): The view to be set as the active view. Returns: - None + str: The name of the activated view. + + Raises: + TypeError: If the input element is not a View. + + Example: + >>> active_view_name = set_active_view(my_view) """ - if closing_config: - starting_view_id = DB.StartingViewSettings.GetStartingViewSettings(document).ViewId - starting_view = document.GetElement(starting_view_id) - if starting_view: - uidoc = revit.uidoc - HOST_APP.uidoc.RequestViewChange(starting_view) - uidoc.ActiveView = starting_view - all_open_views = uidoc.GetOpenUIViews() - for ui_view in all_open_views : - doc_view = document.GetElement(ui_view.ViewId) - if doc_view.Name != starting_view.Name : - ui_view.Close() - else: - forms.show_balloon( - "No Starting View Set", - "No Starting View Set", - ) + if not isinstance(view, DB.View): + raise TypeError( + 'Element [{}] is not a View!'.format(view.Id)) + name = view.Name + if view.ViewType != DB.ViewType.Internal and \ + view.ViewType != DB.ViewType.ProjectBrowser: + revit.uidoc.ActiveView = view + logger.debug('Active View is: {}'.format(view.Name)) + return name + else: + logger.info('View {} ({}) cannot be activated.'.format( + name, view.ViewType)) + return 'INTERNAL / PB: ' + name + def sync_document(): """Synchronizes the current document with the central model. - This function checks if the document is workshared + This function checks if the document is workshared and not a family document or a linked document. If it meets the criteria, it performs the following steps: @@ -62,7 +109,7 @@ def sync_document(): if doc.IsWorkshared and not doc.IsFamilyDocument and not doc.IsLinked: timer = coreutils.Timer() - close_inactive_views(close) + close_inactive_views(view_handling) trans_options = DB.TransactWithCentralOptions() sync_options = DB.SynchronizeWithCentralOptions() @@ -79,6 +126,16 @@ def sync_document(): doc.ReloadLatest(reload_latest_options) doc.Save(save_options) doc.SynchronizeWithCentral(trans_options , sync_options) + + if view_handling == "reopen": + for v_id in view_cache: + view = doc.GetElement(v_id) + try: + set_active_view(view) + except: + logger.warn( + "Failed to reopen view {}".format(v_id.IntegerValue)) + endtime = timer.get_time() endtime_hms = str(datetime.timedelta(seconds=endtime).seconds) endtime_hms_claim = "Synchronisation took {}s.".format(endtime_hms) From 060ea13922fc6f9b34ca82cc872b8a04c9b054d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20D=C3=A9ri?= Date: Wed, 6 Dec 2023 09:42:21 +0100 Subject: [PATCH 2/3] config adjustments --- .../pyRevit.tab/Toggles.panel/sync.pushbutton/config.py | 1 + .../pyRevit.tab/Toggles.panel/sync.pushbutton/script.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py index e397b5fda..b89aa8303 100644 --- a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py +++ b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/config.py @@ -28,6 +28,7 @@ def close_inactive_views(): "view_handling", next((k for k in options if options[k] == selection), None) ) + script.save_config() if __name__ == "__main__": diff --git a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py index 5967fd377..d09bdf637 100644 --- a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py +++ b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py @@ -11,6 +11,12 @@ view_handling = getattr(my_config, "view_handling") except: view_handling = "nothing" + setattr( + my_config, + "view_handling", + "nothing" + ) + script.save_config() view_cache = [] From f6fc9da1cb6b2d8872c6a70b10d8751933ca95e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20D=C3=A9ri?= Date: Wed, 6 Dec 2023 15:08:36 +0100 Subject: [PATCH 3/3] comparing views by id instead of name --- .../pyRevit.tab/Toggles.panel/sync.pushbutton/script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py index d09bdf637..52276197b 100644 --- a/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py +++ b/extensions/pyRevitTools.extension/pyRevit.tab/Toggles.panel/sync.pushbutton/script.py @@ -52,7 +52,7 @@ def close_inactive_views(view_handling="nothing", document=doc): if view_handling == "reopen": view_cache.append(ui_view.ViewId) doc_view = document.GetElement(ui_view.ViewId) - if doc_view.Name != starting_view.Name : + if doc_view.Id != starting_view.Id : ui_view.Close() else: forms.show_balloon(