Automatically Synchronize With Central every hour

I don’t know why Erik wants to call this FaceMelter, but here is an example of how to use the Idling event and 2014’s new SynchronizeWithCentral method to automatically Synchronize With Central at a desired interval.

For testing purposes I set it to auto-synch every 3 minutes. You would probably want an interval a bit longer 🙂

autosynch

public static class myCommand
{
    // store the time when the last save occurred
    static DateTime lastSaveTime;

    public static void idleUpdate(object sender, IdlingEventArgs e)
    {
        // set an initial value for the last saved time
        if (lastSaveTime == DateTime.MinValue)
            lastSaveTime = DateTime.Now;

        // check the current time
        DateTime now = DateTime.Now;

        TimeSpan elapsedTime = now.Subtract(lastSaveTime);
        double minutes = elapsedTime.Minutes;

        UIApplication uiApp = sender as UIApplication;
        // write a comment to the journal file for diagnostic purposes
        uiApp.Application.WriteJournalComment("Idle check. Elapsed time = " + minutes,true);

        // don't do anything if less than 3 minutes since last auto-save
        if (minutes < 3)
            return;

        Document doc = uiApp.ActiveUIDocument.Document;
        if (!doc.IsWorkshared)
            return;

        TransactWithCentralOptions transact = new TransactWithCentralOptions();
        SynchronizeWithCentralOptions synch = new SynchronizeWithCentralOptions();
        synch.Comment = "Autosaved by the API at " + DateTime.Now;
        RelinquishOptions relinquishOptions = new RelinquishOptions(true);
        relinquishOptions.CheckedOutElements = true;
        synch.SetRelinquishOptions(relinquishOptions);

        uiApp.Application.WriteJournalComment("AutoSave To Central", true);
        doc.SynchronizeWithCentral(transact, synch);

        // update the last saved time
        lastSaveTime = DateTime.Now;
    }
}

class rtcApplication : IExternalApplication
{
    public static FailureDefinitionId failureDefinitionId = new FailureDefinitionId(new Guid("E7BC1F65-781D-48E8-AF37-1136B62913F5"));
    public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
    {
        // register the idling event when Revit starts
        application.Idling += new EventHandler<IdlingEventArgs>(myCommand.idleUpdate);
        return Result.Succeeded;
    }
    public Result OnShutdown(UIControlledApplication application)
    { return Result.Succeeded; }
}
Advertisements

11 thoughts on “Automatically Synchronize With Central every hour

  1. Hi Harry
    Great tool.
    I built the macro without errors but I don’t think it is working. I didn’t detect any auto synchronization with central.
    What am I possibly doing wrong here?

      • /*
        * Created by SharpDevelop.
        * User: vvlasov
        * Date: 7/15/2013
        * Time: 2:19 PM
        *
        * To change this template use Tools | Options | Coding | Edit Standard Headers.
        */
        using System;
        using System.Diagnostics;
        using System.IO;
        using System.Linq;
        using System.Text;
        using System.Collections.Generic;
        using Autodesk.Revit.DB;
        using Autodesk.Revit.DB.Architecture;
        using Autodesk.Revit.DB.Events;
        using Autodesk.Revit.UI.Events;
        using Autodesk.Revit.UI;
        using Autodesk.Revit.UI.Selection;
        using Autodesk.Revit.ApplicationServices;

        namespace AutoSynch2014
        {
        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
        [Autodesk.Revit.DB.Macros.AddInId(“B6DC71CA-7865-4C20-B6F0-72038004A2F4”)]
        public partial class ThisApplication
        {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
        this.Startup += new System.EventHandler(Module_Startup);
        this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion

        public static class myCommand
        {
        // store the time when the last save occurred
        static DateTime lastSaveTime;

        public static void idleUpdate(object sender, IdlingEventArgs e)
        {
        // set an initial value for the last saved time
        if (lastSaveTime == DateTime.MinValue)
        lastSaveTime = DateTime.Now;

        // check the current time
        DateTime now = DateTime.Now;

        TimeSpan elapsedTime = now.Subtract(lastSaveTime);
        double minutes = elapsedTime.Minutes;

        UIApplication uiApp = sender as UIApplication;
        // write a comment to the journal file for diagnostic purposes
        uiApp.Application.WriteJournalComment(“Idle check. Elapsed time = ” + minutes,true);

        // don’t do anything if less than 3 minutes since last auto-save
        if (minutes < 1)
        return;

        Document doc = uiApp.ActiveUIDocument.Document;
        if (!doc.IsWorkshared)
        return;

        TransactWithCentralOptions transact = new TransactWithCentralOptions();
        SynchronizeWithCentralOptions synch = new SynchronizeWithCentralOptions();
        synch.Comment = "Autosaved by the API at " + DateTime.Now;
        RelinquishOptions relinquishOptions = new RelinquishOptions(true);
        relinquishOptions.CheckedOutElements = true;
        synch.SetRelinquishOptions(relinquishOptions);

        uiApp.Application.WriteJournalComment("AutoSave To Central", true);
        doc.SynchronizeWithCentral(transact, synch);

        // update the last saved time
        lastSaveTime = DateTime.Now;
        }
        }

        class rtcApplication : IExternalApplication
        {
        public static FailureDefinitionId failureDefinitionId = new FailureDefinitionId(new Guid("E7BC1F65-781D-48E8-AF37-1136B62913F5"));
        public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
        {
        // register the idling event when Revit starts
        application.Idling += new EventHandler(myCommand.idleUpdate);
        return Result.Succeeded;
        }
        public Result OnShutdown(UIControlledApplication application)
        { return Result.Succeeded; }
        }

        public void AutoSynch2014()
        {
        }
        public void PrintSheetsFromLink()
        {
        }
        }
        }

  2. I receive a warning for the public static void idleUpdate(object sender, IdlingEventArgs e) line stating idlingeventargs is inaccessible due to its protection level

  3. How to force Revit 2013/2014 to reload linked model while SYNC to central? Is there simple way to do it or need any Macro? ANy help will be appriciated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s