- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am looking at a method that would grab existing modeled content and attempt to place this content on a workset after running this method. On a button click, the button finds the levels in the project and then gets the level hosted of the item and, again, attempts to place this on a workset with the level name. So, you would have a family on "Level 1" with a workset named "Level 1" and then moves it to the correct workset instead of Workset 1.
I am working in the Sample Architecture project that comes with Revit. When running this action, it will run through about 800-ish of the elements of the 835, then runs up against a Legend Component Element in the sample file, which then throws an InvalidOperationException.
My question is, should a Legend Component be caught in the FilteredElementCollector that I am collecting and how should I attempt to rewrite the FEC to just catch "real" modeled content.
NOTE: I was running the first FEC with IEnumerable and the ForEach loop below. I got caught with the InvalidOperationException of MoveNext on the Enumerator, so I tried to fallback to a for loop and run through List to find the issue.
Thanks in advance!
public static Autodesk.Revit.UI.Result PlaceElementsOnWorksets(Document doc)
{
Options opt = new Options();
IList<Element> modeledElements = new FilteredElementCollector(doc).WhereElementIsNotElementType().
WhereElementIsViewIndependent().Where(e => null != e.Category && null != e.get_Geometry(opt)).ToList();
//Let's start a transaction group, cause we are getting ready to do a lot of things.
using (TransactionGroup elemWS = new TransactionGroup(doc))
{
try
{
elemWS.Start("Assign Elements to Worksets");
FilteredWorksetCollector worksets = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset);
for (int i=0; i < modeledElements.Count(); i++)
{
System.Diagnostics.Debug.WriteLine(modeledElements[i].Id.ToString());
Parameter param = modeledElements[i].GetParameters("Workset").FirstOrDefault();
string workset = param.AsValueString();
if (param != null && !param.IsReadOnly)
{
try
{
Element level = doc.GetElement(modeledElements[i].LevelId);
if (level != null)
{
string levelname = level.Name;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Change Workset ID for Element: " + modeledElements[i].Id.ToString());
foreach (Autodesk.Revit.DB.Workset ws in worksets)
{
if (ws.Name.Contains(levelname))
{
param.Set(ws.Id.IntegerValue);
break;
}
}
tx.Commit();
}
}
}
catch (NullReferenceException)
{
}
}
}
//foreach (Element e in modeledElements)
//{
// System.Diagnostics.Debug.WriteLine(e.Id.ToString());
// Parameter param = e.GetParameters("Workset").FirstOrDefault();
// string workset = param.AsValueString();
// if (param != null && !param.IsReadOnly)
// {
// try
// {
// Element level = doc.GetElement(e.LevelId);
// if(level != null)
// {
// string levelname = level.Name;
// using (Transaction tx = new Transaction(doc))
// {
// tx.Start("Change Workset ID for Element: " + e.Id.ToString());
// foreach (Autodesk.Revit.DB.Workset ws in worksets)
// {
// if (ws.Name.Contains(levelname))
// {
// param.Set(ws.Id.IntegerValue);
// break;
// }
// }
// tx.Commit();
// }
// }
// }
// catch(NullReferenceException)
// {
// }
// }
//}
elemWS.Assimilate();
}
catch(InvalidOperationException)
{
return Autodesk.Revit.UI.Result.Failed;
}
}
return Autodesk.Revit.UI.Result.Succeeded;
}I've also included a Snoop of the ElementID, but more importantly the GUID, if you were looking for the element.
Solved! Go to Solution.