ワークシェアリングされているファイルの管理

ワークシェアリングされているファイルの管理

ワークシェアリングされているプロジェクト ファイルを使用するための複数の Document メソッドがあります。

ワークシェアリングを有効化する

ドキュメントがまだワークシェアリングされていない(Document.IsWorkshared プロパティから判断する)場合は、ワークシェアリングは、Document.EnableWorksharing()メソッドを使用して Revit API から有効化することができます。ドキュメントの[元に戻す]履歴はこのコマンドでクリアされるため、このコマンドと他のコマンドは、元に戻すことができなくなる前に実行されました。また、明示的に起動されたすべてのトランザクション フェーズ(トランザクション、トランザクション グループ、サブトランザクションなど)は、EnableWorksharing()を呼び出す前に完了する必要があります。

最新版を再ロード

メソッド Document.ReloadLatest()は、中央モデルからの変更を取得し(1 回以上の中央ファイルとの同期のため)、これらを現在のセッションに合成します。

次の例は、ユーザの許可を確認してから、ReloadLatest()を使用して現在のセッションを更新します。

コード領域: 中央ファイルから再ロード

public static void ReloadLatestWithMessage(Document doc)
{
    // Tell user what we're doing
    TaskDialog td = new TaskDialog("Alert");
    td.MainInstruction = "Application 'Automatic element creator' needs to reload changes from central in order to proceed.";
    td.MainContent = "This will update your local with all changes currently in the central model.  This operation " +
                        "may take some time depending on the number of changes available on the central.";
    td.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;

    TaskDialogResult result = td.Show();

    if (result == TaskDialogResult.Ok)
    {
        // There are no currently customizable user options for ReloadLatest.
        doc.ReloadLatest(new ReloadLatestOptions());
        TaskDialog.Show("Proceeding...", "Reload operation completed, proceeding with updates.");
    }
    else
    {
        TaskDialog.Show("Canceled.", "Reload operation canceled, so changes will not be made.  Return to this command later when ready to reload.");
    }
}

中央モデルと同期する

Document.SynchronizeWithCentral()メソッドは、中央モデルからの変更を再ロードして、現在のセッションを最新の状態して、ローカルの変更を中央ファイルに戻して保存します。変更を行わない場合でも、中央ファイルへの保存は実行されます。

SynchronizeWithCentral()を使用する場合は、オプションを中央モデルとの同期とともに中央モデルへのアクセスに指定できます。中央ファイルにアクセスするための主要オプションは、中央モデルがロックされた場合に呼び出しの動作方法を決定します。同期には中央モデルを一時的にロックする必要があるため、モデルが既にロックされている場合は実行できません。既定の動作は、同期を開始するために待機して、繰り返し中央モデルのロックを試行するようになっています。この動作に対して、SynchronizeWithCentral()メソッドの TransactWithCentralOptions パラメータを使用して他の動作を優先することができます。

メソッドの SynchronizeWithCentralOptions パラメータを使用して、現在のユーザが所有している要素またはワークセットを同期中に放棄する必要があるかどうかなど、実際の同期のオプションを設定します。

次の例では、中央モデルと同期しようとしました。中央モデルがロックされている場合は、ただちに同期の実行は放棄されます。

コード領域: 中央ファイルと同期

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;
    }

}

次の例では、同期前にユーザにメッセージが表示され、ユーザは同期の際にすべての要素を放棄するか、ワークセットをチェック アウトのまま維持するかを選択できます。

コード領域: メッセージを使用した中央ファイルとの同期

public static void SynchWithCentralWithMessage(Document doc)
{
    // Checkout workset (for use with "keep checked out worksets" option later)
    FilteredWorksetCollector fwc = new FilteredWorksetCollector(doc);
    fwc.OfKind(WorksetKind.UserWorkset);
    Workset workset1 = fwc.First<Workset>(ws => ws.Name == "Workset1");

    WorksharingUtils.CheckoutWorksets(doc, new WorksetId[] { workset1.Id });

    // Make a change
    using (Transaction t = new Transaction(doc, "Add Level"))
    {
        t.Start();
        doc.Create.NewLevel(100);
        t.Commit();
    }

    // Tell user what we're doing
    TaskDialog td = new TaskDialog("Alert");
    td.MainInstruction = "Application 'Automatic element creator' has made changes and is prepared to synchronize with central.";
    td.MainContent = "This will update central with all changes currently made in the project by the application or by the user.  This operation " +
                        "may take some time depending on the number of changes made by the app and by the user.";

    td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Do not synchronize at this time.");
    td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Synchronize and relinquish all elements.");
    td.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "Synchronize but keep checked out worksets.");
    td.DefaultButton = TaskDialogResult.CommandLink1;

    TaskDialogResult result = td.Show();

    switch (result)
    {
        case TaskDialogResult.CommandLink1:
        default:
            {
                // Do not synch.  Nothing to do.
                break;
            }
        case TaskDialogResult.CommandLink2:
        case TaskDialogResult.CommandLink3:
            {
                // Prepare to synch
                // TransactWithCentralOptions has to do with the behavior related to locked or busy central models.
                // We'll use the default behavior.
                TransactWithCentralOptions twcOpts = new TransactWithCentralOptions();

                // Setup synch-with-central options (add a comment about our change)
                SynchronizeWithCentralOptions swcOpts = new SynchronizeWithCentralOptions();
                swcOpts.Comment = "Synchronized by 'Automatic element creator' with user acceptance.";

                if (result == TaskDialogResult.CommandLink3)
                {
                    // Setup relinquish options to keep user worksets checked out
                    RelinquishOptions rOptions = new RelinquishOptions(true);
                    rOptions.UserWorksets = false;
                    swcOpts.SetRelinquishOptions(rOptions);
                }

                doc.SynchronizeWithCentral(twcOpts, swcOpts);

                break;
            }
    }
}

新しいローカル モデルを作成する

WorksharingUtils.CreateNewLocal()メソッドは、中央モデルを新しいローカル ファイルにコピーします。このメソッドは、新しいファイルを開きません。新しいローカル モデルを作成して開く例については、「ワークシェアリングされているドキュメントを開く」を参照してください。