Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

collection of elements created using ElementWorksetFilter giving incorrect count

SanjivKapila
Participant Participant
985 Views
4 Replies
Message 1 of 5

collection of elements created using ElementWorksetFilter giving incorrect count

SanjivKapila
Participant
Participant

I'm trying to retrieve empty worksets but the count method of collection of elements in a particular workset is not giving correct results. Not sure where I'm going wrong. Below is my code:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; Document doc = uiapp.ActiveUIDocument.Document; FilteredElementCollector fec = new FilteredElementCollector(doc); FilteredWorksetCollector fwc = new FilteredWorksetCollector(doc); fwc.OfKind(WorksetKind.UserWorkset); //WorksetTable wt = doc.GetWorksetTable(); try { string msg = ""; int count = 0; Transaction t = new Transaction(doc); t.Start("Check Empty Worksets"); foreach (Workset w in fwc) { ElementWorksetFilter ewf = new ElementWorksetFilter(w.Id, false); ICollection elemIds = fec.WherePasses(ewf).ToElementIds(); int foundElems = elemIds.Count; //int foundElems = TaskDialog.Show("Elements:", w.Name + ": " + foundElems.ToString()); // Workset wSet = wt.GetWorkset(w.UniqueId); if(foundElems == 0) { count++; msg += count.ToString() + ". " + w.Name + "\n"; } } if (count == 0) msg = "None"; TaskDialog.Show("Empty Worksets: ", msg); t.Commit(); t.Dispose(); } catch(Exception e) { TaskDialog.Show("Error", e.ToString()); } return Result.Succeeded; }

0 Likes
Accepted solutions (2)
986 Views
4 Replies
Replies (4)
Message 2 of 5

FAIR59
Advisor
Advisor
Accepted solution

A FilteredElementCollector isn't a static variable, but a dynamic collection. Every time you apply a filter, the elements that don't pass the filter are removed from the collection. So after the first pass of the foreach loop, the collector only contains the elements belonging to the first workset. All those elements aren't part of the second workset (2nd pass) and therefore the collector is empty after the second pass. 

Solution: initialize the collector in every pass.

 

foreach (Workset w in fwc)
{
	ElementWorksetFilter ewf = new ElementWorksetFilter(w.Id, false);
	ICollection<ElementId> elemIds = new FilteredElementCollector(doc).WherePasses(ewf).ToElementIds();
	int foundElems = elemIds.Count;
	count++;
	msg += foundElems.ToString() + ". " + w.Name + "\n";
}
0 Likes
Message 3 of 5

jeremytammik
Autodesk
Autodesk
Accepted solution

Besides Fair59's valuable solution, you might also want to simplify your transaction handling by wrapping it in a `using` statement. However, I also wonder whether you need any transaction at all for this read-only operation.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 5

SanjivKapila
Participant
Participant

Thanks Jeremy, i added transaction as i was trying to delete empty worksets but later realized that we can't delete workset using api..

0 Likes
Message 5 of 5

SanjivKapila
Participant
Participant

Thank You!

0 Likes