Change Element Workset

We discussed

reading the workset of an element
,
either using the built-in parameter ELEM_PARTITION_PARAM or, more easily, the dedicated document GetWorksetId method.

This is actually another example of the possibility to choose your data access method by using either a dedicated property or a parameter, as we recently explored to achieve

changing the viewport type
.

Although the Revit API provides a dedicated property to read an element workset, it does not do so for changing it, which begs the question:

Question: The Element.WorksetId property is read-only.

I would like to change an element’s workset to other one, though.

Is this possible?

Answer: You can change the element’s workset via the built-in parameter ELEM_PARTITION_PARAM.

Here is a code snippet to show how to retrieve all workset ids and set each one to a given element in turn:


  Reference r = uidoc.Selection.PickObject( ObjectType.Element );
  Element e = doc.GetElement( r.ElementId );
 
  if( e == null )
    return;
 
  WorksetId wid = e.WorksetId;
 
  TaskDialog.Show( "Workset id", wid.ToString() );
 
  Parameter wsparam = e.get_Parameter(
    BuiltInParameter.ELEM_PARTITION_PARAM );
 
  if( wsparam == null )
    return;
 
  // Find all user worksets 
 
  FilteredWorksetCollector worksets
    = new FilteredWorksetCollector( doc )
      .OfKind( WorksetKind.UserWorkset );
 
  using( Transaction tx = new Transaction( doc ) )
  {
    tx.Start( "Change workset id" );
 
    foreach( Workset ws in worksets )
    {
      wsparam.Set( ws.Id.IntegerValue );
    }
 
    tx.Commit();
  }
  wid = e.WorksetId;
 
  TaskDialog.Show( "worksetid", wid.ToString() );

Many thanks to Phil Xia for this hint!


Comments

3 responses to “Change Element Workset”

  1. Ning Zhou Avatar
    Ning Zhou

    good to know that element’s workset can be changed afterall!

  2. Nik Mulconray Avatar
    Nik Mulconray

    Hi,
    I am new to coding in revit so realise that I wont be as articulate as I would like to be, though I know RAC2013 very well and would like to build useful addins to increase productivity at work.
    For one idea I am working on at the moment… I would like to set say the worksetID of one element say walls to another worksetID, for example a room.
    Is this possible?
    Further how do I add to a collection all the elements within a room? I know how to add to a collection all the bounding elements of a room, do I use a similar foreach loop to do this?
    I appreciate any help or pointers you could give me.

  3. Dear Nik,
    The above shows that the workset id can be read and set, so the answer appears to be yes.
    Regarding your second question, how about this?
    http://thebuildingcoder.typepad.com/blog/2013/03/filter-for-family-instances-in-a-room.html
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading