Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autosave #1692

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions extensions/pyRevitTools.extension/hooks/doc-opened.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
##### Autosave
import time
from pyrevit import script
from pyrevit.userconfig import user_config

doc = __eventargs__.Document
try:
title = doc.Title
except:
sys.exit()

# Does not save detached, family, or unsaved documents
# TBD adding regex for shared template/resource folders or drives
is_project = not doc.IsDetached and not doc.IsFamilyDocument and len(doc.PathName) > 0

# Hash pathname to avoid overlap between two documents with the same name in different folders
if user_config.autosave.get_option("enabled") and is_project:
with open(script.get_document_data_file(str(hash(doc.PathName)),"txt"), "w") as f:
f.write(str(title) + "|" + str(time.time()))
##### Autosave
20 changes: 20 additions & 0 deletions extensions/pyRevitTools.extension/hooks/doc-saved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
##### Autosave
import time
from pyrevit import script
from pyrevit.userconfig import user_config

doc = __eventargs__.Document
try:
title = doc.Title
except:
sys.exit()

# Does not save detached, family, or unsaved documents
# TBD adding regex for shared template/resource folders or drives
is_project = not doc.IsDetached and not doc.IsFamilyDocument and len(doc.PathName) > 0

# Hash pathname to avoid overlap between two documents with the same name in different folders
if user_config.autosave.get_option("enabled") and is_project:
with open(script.get_document_data_file(str(hash(doc.PathName)),"txt"), "w") as f:
f.write(str(title) + "|" + str(time.time()))
##### Autosave
40 changes: 40 additions & 0 deletions extensions/pyRevitTools.extension/hooks/view-activated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
##### Autosave
import time, os
from pyrevit import forms
from pyrevit import script
from pyrevit.userconfig import user_config

doc = __eventargs__.Document
try:
title = doc.Title
except:
sys.exit()

savestatus_datafile = script.get_document_data_file(str(hash(doc.PathName)),"txt")

# Does not save detached, family, or unsaved documents
# TBD adding regex for shared template/resource folders or drives
is_project = not doc.IsDetached and not doc.IsFamilyDocument and len(doc.PathName) > 0

# Check if file exists before trying to read from
savefile_exists = os.path.exists(savestatus_datafile) and os.stat(savestatus_datafile).st_size > 0

def read():
with open(savestatus_datafile, "r") as f:
for line in f:
x = line.split("|")
return x

# Hash pathname to avoid overlap between two documents with the same name in different folders
if user_config.autosave.get_option("enabled") and is_project and savefile_exists:
x = read()
if str(title) == x[0] and time.time() - float(x[1]) >= user_config.autosave.get_option("interval"):
try:
with forms.ProgressBar(
title="Autosaving...", indeterminate=True, cancellable=False
) as pb:
pb.update_progress(1, 1)
doc.Save()
except:
pass
##### Autosave
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,7 @@ def cleanup_sheetnumbers(doc):

# verify model is printable
forms.check_modeldoc(exitscript=True)
# ensure there is nothing selected
revit.selection.get_selection().clear()
selection = revit.uidoc.Selection.SetElementIds(List[DB.ElementId]([revit.DB.ElementId(2147483647)]))

# TODO: add copy filenames to sheet list
if __shiftclick__: #pylint: disable=E0602
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
layout:
- toggles1
- toggles2
- toggles2
- ">>>>>"
- toggles3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title:
en_us: Autosave
tooltip:
en_us: >-
Automatically saves the current document upon changing views if the document has not been saved sinced the configured interval.

Shift+Click:

Customize interval between autosaves.
min_revit_version: 2017
context: zero-doc
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from pyrevit.coreutils.ribbon import ICON_MEDIUM
from pyrevit import script
from pyrevit.userconfig import user_config
from pyrevit import forms

__context__ = "zero-doc"
__title__ = "Autosave"
__author__ = "Alex D\'Aversa"


def __selfinit__(script_cmp, ui_button_cmp, __rvt__):
if user_config.has_section("autosave") and user_config.autosave.has_option(
"enabled"
):
button_icon = script_cmp.get_bundle_file(
"on.png" if user_config.autosave.get_option("enabled") else "off.png"
)
else:
user_config.add_section("autosave")
user_config.autosave.interval = 900
user_config.autosave.enabled = False
user_config.save_changes()
button_icon = script_cmp.get_bundle_file(
"on.png" if user_config.autosave.get_option("enabled") else "off.png"
)
ui_button_cmp.set_icon(button_icon, icon_size=ICON_MEDIUM)


def toggle_autosave():
if user_config.autosave.enabled == False:
user_config.autosave.enabled = True
else:
user_config.autosave.enabled = False
user_config.save_changes()
return user_config.autosave.enabled


def config_autosave_interval():
default_interval = user_config.autosave.interval / 60
interval = forms.GetValueWindow.show(
None,
value_type="slider",
default=default_interval,
prompt="Set autosave interval in minutes:",
title="Autosave Interval",
min=5,
max=240,
)
if interval:
user_config.autosave.interval = int(interval * 60)
user_config.save_changes()

if __name__ == "__main__":
if __shiftclick__:
config_autosave_interval()
else:
is_active = toggle_autosave()
script.toggle_icon(is_active)