using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Nice3point.Revit.Toolkit.External; using Nice3point.Revit.Toolkit.External.Handlers; namespace Szmedi.RvKits.Common { [Transaction(TransactionMode.Manual)] public class AutoSaveDocumentCmd : ExternalCommand { public static AsyncEventHandler SaveHandler { get; } = new(); private readonly System.Timers.Timer timer = GlobalVariables.Timer; public override void Execute() { if (string.IsNullOrEmpty(Document.PathName)) { ErrorMessage = "当前文件尚未保存,请先保存文件"; Result = Result.Failed; return; } TimerSetterWin win = new(); win.ShowDialog(); if (win.DialogResult == true) { if (Properties.Settings.Default.AutoSave) { if (Properties.Settings.Default.AutoSaveIntervalTime >= 1) { timer.Interval = Properties.Settings.Default.AutoSaveIntervalTime * 60 * 1000; Properties.Settings.Default.Save(); } //timer.Enabled = Properties.Settings.Default.AutoSave; timer.Start(); } else { timer.Stop(); } } Result = Result.Succeeded; } // private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) // { // SaveHandler.RaiseAsync(app => // { //#if REVIT2020 // if (!app.ActiveUIDocument.Document.IsBackgroundCalculationInProgress() && app.ActiveUIDocument.Document.IsModified) // { // app.ActiveUIDocument.Document.Save(); // } //#elif REVIT2018 // if (app.ActiveUIDocument.Document.IsModified) //文档做了修改才需要保存 // { // app.ActiveUIDocument.Document.Save(); // } //#endif // }); // } //public void EnableAutoSave(doc doc) //{ // // 注册文档的修改事件,每次文档修改后都会触发该事件 // doc.Application.DocumentChanged += OnDocumentChanged; //} //private void OnDocumentChanged(object sender, DocumentChangedEventArgs e) //{ // // 在文档修改后进行自动保存 // e.GetDocument().Save(); //} //public void EnableAutoSave(doc doc, int intervalMinutes) //{ // // 创建定时器,每隔一定时间自动保存文档 // Timer timer = new Timer(); // timer.Interval = intervalMinutes * 60 * 1000; // 定时器间隔时间,单位:毫秒 // timer.AutoReset = true; // timer.Elapsed += (sender, e) => OnTimerElapsed(doc); // 注册定时器事件 // timer.Start(); //} //private void OnTimerElapsed(doc doc) //{ // // 在定时器事件中进行自动保存 // doc.Save(); //} } }