How can we identify whether the element we trying to modify is owned by me before commit the command, if so prompt to synchronize.
For any visible element in a Revit shared model, we can get this element’s work set work set id by Document.GetWorksetId() method. Then you can retrieve the corresponding WorkSet object by WorkSetTable.GetWorkSet() method. WorkSet.Owner returns the work set’s owner name. Finally compare the work set owner with Revit current user name.
Here is the simplest code to show the process.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
[TransactionAttribute(TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction trans = new Transaction(doc);
trans.Start("testComamnd");
Selection sel = app.ActiveUIDocument.Selection;
Reference ref1 = sel.PickObject(ObjectType.Element, "pick an element");
Element elem = doc.GetElement(ref1);
// code changing this element here.
// for simplicity, no change here.
//Get the workset information.
WorksetId idWS = doc.GetWorksetId(elem.Id);
WorksetTable table = doc.GetWorksetTable();
Workset ws = table.GetWorkset(idWS);
string owner = ws.Owner;
trans.Commit();
//Compare with the current user.
if (owner == app.Application.Username)
{
TaskDialog.Show("Synchronize reminder", "The picked element was updated, please synchronize the document");
}
return Result.Succeeded;
}
}