Creating railing inside a loop: Element Table in Revit's database

Creating railing inside a loop: Element Table in Revit's database

MiguelGT17
Advocate Advocate
329 Views
2 Replies
Message 1 of 3

Creating railing inside a loop: Element Table in Revit's database

MiguelGT17
Advocate
Advocate

Greetings to everyone,

I'm collecting the boundary lines for every room then Im looking to generate railings base on those lines but I'm facing the following error:

MiguelGT17_0-1669160221189.png

 

 

                foreach (Room room in rooms)
                {
                      ...getting boundary lines and group then to theBottomLinesLoop
                        if (theBottomLinesLoop.Any()) {
                        	
                        	//foreach line check if lines continue
                        	//CurveLoop cvL = CurveLoop.Create(new List<Curve>{theBottomLinesLoop[0]});
                        	//Railing rail = Railing.Create(doc,cvL,railType.Id,level.Id);
                        	
                        	foreach (Line line in theBottomLinesLoop) {
                        		CurveLoop cvL = CurveLoop.Create(new List<Curve>{line});
                        		Railing rail = Railing.Create(doc,cvL,railType.Id,level.Id);
                        	}
                        }

 

If I comment the Railing.Create(doc,...) line, no errors are thrown However I cant place the railings lol,

 

I'm confused about this matter, any possible explanation to this issue?

Best regards,

Miguel G.

0 Likes
Accepted solutions (1)
330 Views
2 Replies
Replies (2)
Message 2 of 3

mhannonQ65N2
Collaborator
Collaborator
Accepted solution

I suspect your rooms variable is a FilteredElementCollector or a lazily evaluated IEnumerable<Room> that is fed from a FilteredElementCollector. This means that with each iteration of your loop over rooms, the FilteredElementsCollector processes more elements until it finds the next matching element or is done. When you make changes to the document while the FilteredElementCollector is iterating, it causes this error. To fix it, just call ToElements() on your FilteredElementCollector or call ToArray() at the end of your LINQ method chain for rooms so it finished iterating before you make any changes to the document.

Message 3 of 3

MiguelGT17
Advocate
Advocate

What such a great community we have here! thank you @mhannonQ65N2 for your prompt reply! that was the issue, initially my collector was declared as:

 

//before
IEnumerable<Element> rooms = new FilteredElementCollector(doc).OfClass(typeof(SpatialElement)).Where<Element>(e => e is Room);


//after
List<Element> rooms = new FilteredElementCollector(doc).OfClass(typeof(SpatialElement)).Where<Element>(e => e is Room).ToList();
0 Likes