Counting views on sheets

Counting views on sheets

jackchaskell
Contributor Contributor
1,014 Views
7 Replies
Message 1 of 8

Counting views on sheets

jackchaskell
Contributor
Contributor

Hi - I'm trying to increment an integer variable depending on whether or not each view in a list of views is on a sheet or not. I don't think my "onSheet" test (looking for the "Sheet Number" parameter) is working correctly here since it  just keeps incrementing viewsNotOnSheets. What am I doing wrong? According to RevitLookUp, views not on sheets don't have the built in "Sheet Number" parameter assigned/available...

if (!view.IsTemplate)
                    {
                        allViews++;
                        //TODO: fix this to split viewsNotOnSheets and viewsOnSheets
                        bool onSheet = false;
                        ParameterSet pSet = view.Parameters;
                        foreach (Parameter p in pSet)
                        {
                            if (p.Definition.Name == "Sheet Number") { onSheet = true; break; }
                        }
                        if (!onSheet) { viewsNotOnSheets++; } //increase viewsNotOnSheets
                        else { viewsOnSheets++; }
                        //Sorted and unsorted
                        (bool hasSortPars, string viewUse, string viewSheetGroup) = supplementalFunctions.ViewHasSortPars(view, fileName);
                        if (hasSortPars == true)
                        {
                            if (viewUse == null || viewSheetGroup == null) { unsortedViews++; } //increase unsortedViews
                            else { sortedViews++; } //otherwise increase sortedViews
                        }

                        //With templates and without templates
                        int viewTemplateId = view.ViewTemplateId.IntegerValue;
                        if (viewTemplateId == -1) { viewsNoTemplate++; } //increase viewsNoTemplate
                        else { viewsWithTemplate++; } //increase viewsWithTemplate
                    }

 

0 Likes
Accepted solutions (1)
1,015 Views
7 Replies
Replies (7)
Message 2 of 8

jeremy_tammik
Alumni
Alumni
Accepted solution

There are a lot of optimisation opportunities in your code.

 

Here are the two that first come to mind: 

 

 

However, a much more efficient approach is probably possible, maybe even several.

 

Here is one to try:

 

  

Do you think that would work?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 8

Sean_Page
Collaborator
Collaborator

If you happen to be using Revit 2023 or newer you can use the ViewPlacementOnSheetStatus method on the view.

 

https://www.revitapidocs.com/2023/bc769050-18b4-e147-b1ac-753c11b62c70.htm

 

If not, the fastest way I have found to do this is to get all Viewports from a FilteredElementCollector and then use the ViewId property to get the view. This way you are iterating the smallest collection I think you can. May be some work to check Legends or schedules if needed.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
Message 4 of 8

jackchaskell
Contributor
Contributor

Yeah, I know my code is messy at the moment - plan on re-writing soon...

I think iterating through sheets is probably the way I'll go - I had thought of it, but was hoping there was a way to do it right from my list of views (which I didn't gather from sheets, just based on their ViewType and a FilteredElementCollector). Thanks for the suggestions!

0 Likes
Message 5 of 8

jackchaskell
Contributor
Contributor

I wish I was writing this for 2023 - but my company stays a version or 2 behind the most current release.

0 Likes
Message 6 of 8

jeremy_tammik
Alumni
Alumni

So what version are you on? 2021 or 2022? Looking forward to hearing how you end up solving this, and happy to continue discussing it. What is your goal with this?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 7 of 8

jackchaskell
Contributor
Contributor

I'm in 22. I'll get back into it after the weekend, but this is part of a larger add-in that gathers a variety of data from our Revit models and shoots it over to an SQL Database. I'm building a dashboard in PowerBI to track model statistics and "health" over time. Fun little R&D project for my company 🙂 I usually work in Dynamo/Python, so I'm learning more about c# as I go.

0 Likes
Message 8 of 8

rhanzlick
Advocate
Advocate

as a follow-up to @spage's comment, you can skip the ViewPlacementOnSheetStatus property and go straight for ViewPorts:

var viewports = new FilteredElementCollector(doc).OfClass(typeof(Viewport)).OfType<Viewport>().ToList();
var viewSheetMap = viewports.GroupBy(vp => vp.SheetId).ToDictionary(g=>g.Key,g=>g.Select(vp=>vp.ViewId));

in this code:
the 'viewports' line will select all viewports in your Document, cast them as a ViewPort, and put them in a list.
Then, the viewSheetmap looks at the SheetId property of all the viewports, puts them into in IGrouping object where any SheetId result is the same. Finally, it creates a dictionary where your keys are the SheetId result from the grouping, and the value is the list of respective ViewIds for each SheetId.

You could further filter your initial views list, if you wanted specific types of views, only independent views, views of a particular phase, etc.

P.S. I have tested this on 2022 and it seems to work, but it does not show sheets containing 0 views. If you want that, you will have to run a separate FilteredElementCollector and pre-construct the dictionary with all sheets, then you can use the results above to update the values for the respective sheet.Id key.

P.P.S. As you explore C# more, you will certainly come to enjoy the benefits of LINQ and lambda expressions.

good luck!