Elements in Worksets

Elements in Worksets

Each element in the document must belong to one and only one workset. Each element has a WorksetId which identifies the unique workset to which it belongs. Additionally, given a WorksetId, it is possible to get all of the elements in the document belonging to that Workset using the ElementWorksetFilter as shown below.

Code Region: ElementWorksetFilter

public void WorksetElements(Document doc, Workset workset)
{
    // filter all elements that belong to the given workset
    FilteredElementCollector elementCollector = new FilteredElementCollector(doc);
    ElementWorksetFilter elementWorksetFilter = new ElementWorksetFilter(workset.Id, false);
    ICollection<Element> worksetElemsfounds = elementCollector.WherePasses(elementWorksetFilter).ToElements();

    // how many elements were found?
    int elementsCount = worksetElemsfounds.Count;
    String message = "Element count : " + elementsCount;

    // Get name and/or Id of the elements that pass the given filter and show a few of them
    int count = 5;  // show info for 5 elements only
    foreach (Element ele in worksetElemsfounds)
    {
        if (null != ele)
        {
             message += "\nElementId : " + ele.Id;
             message += ", Element Name : " + ele.Name;

             if (0 == --count)
                  break;
         }
    }

    Autodesk.Revit.UI.TaskDialog.Show("ElementsOfWorkset", message);
}

New elements are automatically placed in the active workset in the user's local copy of the model. Since the WorksetId for an element is a read only property, use the parameter ELEM_PARTITION_PARAM. The following example demonstrates the creation of an element that is changed to belong to a different workset.

Code Region: Changing a new element's workset

Document doc = commandData.View.Document;
String targetWorksetName = "Target workset";

//Find target workset
FilteredWorksetCollector worksetCollector = new FilteredWorksetCollector(doc);
worksetCollector.OfKind(WorksetKind.UserWorkset);
Workset workset = worksetCollector.FirstOrDefault<Workset>(ws => ws.Name == targetWorksetName);

// Workset not found, abort
if (workset == null)
{
    TaskDialog dialog = new TaskDialog("Error");
    dialog.MainInstruction = String.Format("There is no workset named {0} in the document. Aborting this operation.", targetWorksetName);
    dialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
    dialog.Show();
    return Result.Cancelled;
}

// Find "Level 1" for the new wall
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(Level));
Level level = collector.Cast<Level>().First<Level>(lvl => lvl.Name == "Level 1");

using (Transaction t = new Transaction(doc, "Add elements by API"))
{
    t.Start();

    // Create the wall
    Wall wall = Wall.Create(doc, Line.CreateBound(new XYZ(25, 0, 0), new XYZ(25, 15, 0)), level.Id, false);

    // Get the parameter that stores the workset id
    Parameter p = wall.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);

    // This parameter storage type is Integer
    p.Set(workset.Id.IntegerValue);

    t.Commit();
}

Worksharing information such as the current owner and checkout status of an element can be obtained using the WorksharingUtils class. It is a static class that contains utility functions related to worksharing.

Code Region: WorksharingUtils

public void GetElementWorksharingInfo(Document doc, ElementId elemId)
{
    String message = String.Empty;
    message += "Element Id: " + elemId;

    Element elem = doc.GetElement(elemId);
    if(null == elem)
    {
       message += "Element does not exist";
       return;
    }

    // The workset the element belongs to
    WorksetId worksetId = elem.WorksetId;
    message += ("\nWorkset Id : " + worksetId.ToString());

    // Model Updates Status of the element
    ModelUpdatesStatus updateStatus = WorksharingUtils.GetModelUpdatesStatus(doc, elemId);
    message += ("\nUpdate status : " + updateStatus.ToString());

    // Checkout Status of the element
    CheckoutStatus checkoutStatus = WorksharingUtils.GetCheckoutStatus(doc, elemId);
    message += ("\nCheckout status : " + checkoutStatus.ToString());

    // Getting WorksharingTooltipInfo of a given element Id
    WorksharingTooltipInfo tooltipInfo = WorksharingUtils.GetWorksharingTooltipInfo(doc, elemId);
    message += ("\nCreator : " + tooltipInfo.Creator);
    message += ("\nCurrent Owner : " + tooltipInfo.Owner);
    message += ("\nLast Changed by : " + tooltipInfo.LastChangedBy);

    Autodesk.Revit.UI.TaskDialog.Show("GetElementWorksharingInfo", message);
}