Setting the Workset based on the Element Category
This macro sets the workset of the elements in the model according to the category of the element. Structural Framing elements are placed on the workset containing "S_Framing", and structurla column elements are placed on the S-Columns workset. Obviously, for this to work, the model must be workshared with worksets that contain S-Framing and S-Columns, and columns or framing elements must be in the model. (again, code may not be original, but I'm not sure who to give credit to.
public void test_workset()
{
int WorksetId_framing = 0;
int WorksetId_cols = 0;
UIDocument uidoc = this.ActiveUIDocument; //dont need this
Document doc = this.ActiveUIDocument.Document;
//Get worksets
IList<Workset> worksetList = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets();
foreach (Workset workset in worksetList)
{
TaskDialog.Show("Name", workset.Name.ToString());
if (workset.Name.Contains("S-Framing"))
{
WorksetId_framing = workset.Id.IntegerValue;
}
if (workset.Name.Contains("S-Columns"))
{
WorksetId_cols = workset.Id.IntegerValue;
}
}
FilteredElementCollector collector = new FilteredElementCollector(doc);
try
{
using (Transaction trans = new Transaction(doc, "Set Worksets"))
{
trans.Start();
//Set workset for framing
ElementCategoryFilter filter1 = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
IList<Element> framing = collector.WherePasses(filter1).WhereElementIsNotElementType().ToElements();
foreach (Element elem in framing)
{
Parameter wsparam = elem.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
wsparam.Set(WorksetId_framing);
}
//Set workset for columns
ElementCategoryFilter filter2 = new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns);
IList<Element> cols = collector.WherePasses(filter2).WhereElementIsNotElementType().ToElements();
foreach (Element elem in cols)
{
Parameter wsparam = elem.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
wsparam.Set(WorksetId_cols);
}
trans.Commit();
}
return;
}
catch (Exception err)
{
TaskDialog.Show("Error", err.Message.ToString());
return;
}
}