Assigning Element to Workset FilteredElementCollector issue

Assigning Element to Workset FilteredElementCollector issue

kraftwerk15
Advocate Advocate
1,167 Views
1 Reply
Message 1 of 2

Assigning Element to Workset FilteredElementCollector issue

kraftwerk15
Advocate
Advocate

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.

legendComponent.png

 

0 Likes
Accepted solutions (1)
1,168 Views
1 Reply
Reply (1)
Message 2 of 2

kraftwerk15
Advocate
Advocate
Accepted solution

I noticed that included in the FEC was a list of objects that if expanded in Visual Studio, Revit could not get any data back from those elements. I'm not a wizard, so I don't know why that's is the case. In VS everything returns as an InvalidOperation except for one value, IsValidObject.

 

I added a check to make sure what was coming through is a ValidObject and then it works.

 

Here is a snippet from the post above with the modification.

 

            Options opt = new Options();
            opt.IncludeNonVisibleObjects = false;
            IList<Element> modeledElements = new FilteredElementCollector(doc).WhereElementIsNotElementType().WhereElementIsViewIndependent()
                .Where(e => null != e.Category && null != e.get_Geometry(opt) && e.Category.Id.IntegerValue != BuiltInCategory.OST_PreviewLegendComponents.GetHashCode()).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++)
                    {
                        if(modeledElements[i].IsValidObject)
                        {
                            System.Diagnostics.Debug.WriteLine(modeledElements[i].Id.ToString());
                            System.Diagnostics.Debug.WriteLine(i.ToString());
                            Parameter param = modeledElements[i].GetParameters("Workset").FirstOrDefault();
                            string workset = param.AsValueString();
0 Likes