Skip to content

Commit

Permalink
Merge pull request #2014 from thumDer/feature/sync-open-views
Browse files Browse the repository at this point in the history
improved view handling
  • Loading branch information
jmcouffin committed Dec 6, 2023
2 parents fc5b662 + f6fc9da commit e05ec5a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@

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)
)
script.save_config()


if __name__ == "__main__":
close_inactive_views()
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,99 @@

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"
setattr(
my_config,
"view_handling",
"nothing"
)
script.save_config()

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.Id != starting_view.Id :
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:
Expand All @@ -62,7 +115,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()
Expand All @@ -79,6 +132,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)
Expand Down

0 comments on commit e05ec5a

Please sign in to comment.