There are several Document methods for use with a workshared project file.
If a document is not already workshared, which can be determined from the Document.IsWorkshared property, worksharing can be enabled via the Revit API using the Document.EnableWorksharing() method. The document's Undo history will be cleared by this command, therefore this command and others executed before it cannot be undone. Additionally, all transaction phases (e.g. transactions, transaction groups and sub-transactions) that were explicitly started must be finished prior to calling EnableWorksharing().
The method Document.ReloadLatest() retrieves changes from the central model (due to one or more synchronizations with central) and merges them into the current session.
The following examples uses ReloadLatest() to update the current session, and then calls Document.HasAllChangesFromCentral() to confirm that there were no synchronizations with central performed during execution of ReloadLatest.
Code Region: Reload from Central |
void ReloadDocument(Document doc) { ReloadLatestOptions reloadOpts = new ReloadLatestOptions(); try { doc.ReloadLatest(reloadOpts); // Check to make sure no new changes were synced with Central during reload if (doc.HasAllChangesFromCentral() == false) { // If there are changes from central, reload again doc.ReloadLatest(reloadOpts); } } catch (Exception e) { TaskDialog.Show("Reload Failed", e.Message); } } |
The method Document.SynchronizeWithCentral() reloads any changes from the central model so that the current session is up to date and then saves local changes back to central. A save to central is performed even if no changes were made.
When using SynchronizeWithCentral(), options can be specified for accessing the central model as well as synchronizing with it. The main option for accessing the central is to determine how the call should behave if the central model is locked. Since the synchronization requires a temporary lock on the central model, it cannot be performed if the model is already locked. The default behavior is to wait and repeatedly try to lock the central model in order to proceed with the synchronization. This behavior can be overridden using the TransactWithCentralOptions parameter of the SynchronizeWithCentral() method.
The SynchronizeWithCentralOptions parameter of the method is used to set options for the actual synchronization, such as whether elements or worksets owned by the current user should be relinquished during synchronization.
In the following example, an attempt is made to synchronize with a central model. If the central model is locked, it will immediately give up.
Code Region: Synchronize with Central |
public void SyncWithoutRelinquishing(Document doc) { // Set options for accessing central model TransactWithCentralOptions transOpts = new TransactWithCentralOptions(); SynchLockCallback transCallBack = new SynchLockCallback(); // Override default behavior of waiting to try again if the central model is locked transOpts.SetLockCallback(transCallBack); // Set options for synchronizing with central SynchronizeWithCentralOptions syncOpts = new SynchronizeWithCentralOptions(); // Sync without relinquishing any checked out elements or worksets RelinquishOptions relinquishOpts = new RelinquishOptions(false); syncOpts.SetRelinquishOptions(relinquishOpts); // Do not automatically save local model after sync syncOpts.SaveLocalAfter = false; syncOpts.Comment = "Changes to Workset1"; try { doc.SynchronizeWithCentral(transOpts, syncOpts); } catch (Exception e) { TaskDialog.Show("Synchronize Failed", e.Message); } } class SynchLockCallback : ICentralLockedCallback { // If unable to lock central, give up rather than waiting public bool ShouldWaitForLockAvailability() { return false; } } |
The WorksharingUtils.CreateNewLocal() method copies a central model to a new local file. This method does not open the new file.