viewId is not a view? FilteredElementCollector on Rooms

viewId is not a view? FilteredElementCollector on Rooms

Anonymous
Not applicable
4,150 Views
5 Replies
Message 1 of 6

viewId is not a view? FilteredElementCollector on Rooms

Anonymous
Not applicable

Hello,

 

I posted this before but deleted it because I messed up the code and couldn't edit for some reason.  So anyway, my problem is this:

I'm trying to collect all rooms that exist in a specified view, and I'm using a FilteredElementCollector to do so.  I'm calling the constructor that takes a ViewID.

 

Below is the entirety of my function.  It fails in the inner loop at the line "FilteredElementCollector collLinked = new FilteredElementCollector(linkedDoc, viewID);".  This approach works just fine when collecting family instances by view, but against rooms I get the strange error "viewId is not a view."  Any ideas?

 

Thanks!

 

public static List<Room> LoadRoomsFromRevitByView(ElementId viewID)
        {
            List<Room> ReturnRooms = new List<Room>();

            FilteredElementCollector collector = new FilteredElementCollector(ProjectInfo.ProjectDocument);

            IList<Element> elems = collector.OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(typeof(RevitLinkType)).ToElements();

            foreach (Element e in elems)
            {
                RevitLinkType linkType = e as RevitLinkType;
                String s = String.Empty;

                foreach (Document linkedDoc in ProjectInfo.ProjectApplication.Documents)
                {
                    if (linkedDoc.Title.Equals(linkType.Name))
                    {
                        try
                        {
                            FilteredElementCollector collLinked = new FilteredElementCollector(linkedDoc, viewID);

                            IList<Element> linkedRooms = collLinked.OfClass(typeof(SpatialElement)).ToElements();

                            foreach (SpatialElement eleRoom in linkedRooms)
                            {
                                Room room = eleRoom as Room;
                                if (room != null)
                                    ReturnRooms.Add(room);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex.Message);
                            continue;
                        }
                    }
                }
            }

            FilteredElementCollector collRooms = new FilteredElementCollector(ProjectInfo.ProjectDocument, viewID);

            IList<Element> mainRooms = collRooms.OfClass(typeof(SpatialElement)).ToElements();

            foreach (SpatialElement eleRoom in mainRooms)
            {
                Room room = eleRoom as Room;
                if (room != null)
                    ReturnRooms.Add(room);
            }

            return ReturnRooms;
        }

 

0 Likes
Accepted solutions (1)
4,151 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Accepted solution

It looks to me like you're trying to use the view ID from the active document on a FilteredElementCollector that's looking in a linked document.

If a view ID is specified in a FilteredElementCollector it must belong to the document that the FilteredElementCollector is searching in.

0 Likes
Message 3 of 6

Anonymous
Not applicable

Thanks!

0 Likes
Message 4 of 6

CJModis
Advocate
Advocate

So, how to get elements from RevitLinkedInstance on specified view of current document? For example - i want to get elements from section view.

In RevitLinkedInstance property ActiveView is always null.

0 Likes
Message 5 of 6

sgermanoZ2Y2F
Enthusiast
Enthusiast

Hi - What was your fix for this? Im trying to do the same thing. I have an arch model linked into mine and I can iterate and get all the rooms from the linked model, but when trying to create tags in my model views Im getting this error:

'viewId Parameter name: viewId must be associated with a plan view or section view.'

 

            Transaction trans = new Transaction(doc);
            trans.Start("ACTRAN");

            Document linkDoc = GetLinkRoomElementIds(uiapp, doc);
            TaskDialog.Show("ROOM COUNT", roomids.Count().ToString());
            View v = doc.ActiveView;

            CreateRoomTags(linkDoc, v);
            trans.Commit();
            return Result.Succeeded;
        }

        private void CreateRoomTags(Document doc, View v)
        {
            for (int i = 0; i < roomids.Count; i++)
            {
                ElementId roomid = roomids[i];
                Element e = doc.GetElement(roomid);
                Room r = e as Room;
                XYZ cen = GetRoomCenter(r);
                UV center = new UV(cen.X, cen.Y);
                doc.Create.NewRoomTag(new LinkElementId(roomid), center, v.Id);
            }
        }
Message 6 of 6

jeremytammik
Autodesk
Autodesk

The view whose id you specify lives in `doc`.

 

However, you have an open transaction on and are attempting to create a tag in `linkDoc`.

 

The error is saying: view belongs to doc, not linkDoc.

 

The view element id may refer to a totally different element in linkDoc, which is not a view.

 

Just imagine how confused you would be if the element referred to just happened to be a plan view of some kind too, in linkDoc.

 



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

0 Likes