Get all Views that accept View Template

Get all Views that accept View Template

Anonymous
Not applicable
4,781 Views
7 Replies
Message 1 of 8

Get all Views that accept View Template

Anonymous
Not applicable

Hi, i'm trying to get all Views that has View Template parameter. However, my code gives me extra data that i don't need (encircled in red). can someone point me to the right direction?. i can't find solution in by Googling or my googling skills really hates me 😞

 

i try this two below code but return the same:

Test Filter.PNG

test file 2.PNG

 

 

 

this eliminate those data that i don't want. however, looks like it's not efficient way

 

 

 

 

test 3.PNG

0 Likes
Accepted solutions (2)
4,782 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

I cannot guarantee that what you are doing achieves what you want at all.

 

However, just looking at what you are doing with your code, here are a couple of suggestions:

 

You can use RevitLookup and other Revit database exploration tools to explore why this sometimes works and sometimes does not:

 

 

You can definitely make your code more concise, readable and effective in various ways.

 

Here is a suggestion for some improvement:

 

    /// <summary>
    /// Return all views with a 
    /// "View Template" parameter
    /// </summary>
    static IEnumerable<View> GetViewsWithTemplate(
      Document doc )
    {
      BuiltInParameter bip_t
        = BuiltInParameter.VIEW_TEMPLATE;

      FilteredElementCollector views
        = new FilteredElementCollector( doc )
          .WhereElementIsNotElementType()
          .OfClass( typeof( View ) );

      IEnumerable<View> views_w_t = views
        .Where( v
          => null != v.get_Parameter( bip_t ) )
        .Cast<View>();

      return views_w_t;
    }

  

One possible reason for the differences may be that several different built-in parameters are used for the "View Template" property, e.g., BuiltInParameter.VIEW_TEMPLATE as well as BuiltInParameter.VIEW_TEMPLATE_FOR_SCHEDULE.

 

Maybe it is not sufficient to check only whether the parameter exists on the view, but also whether it has a valid non-zero value.

 

In any case, the parameter checking can be further speeded up by using a parameter filter to avoid the costly .NET or LINQ post-processing of results.

 

I hope this helps.

 

Cheers,

 

Jeremy

 



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

Message 3 of 8

FAIR59
Advisor
Advisor
Accepted solution

I don't think there is a direct solution. But .. if you can apply a ViewTemplate to a view, you can also enable Temporary View Properties, and vice versa.  So .. all the views that CanUseTemporaryVisibilityModes, will accept a View Template.

IEnumerable<View> views = new FilteredElementCollector(doc)
				.WhereElementIsNotElementType()
				.OfClass(typeof(View))
				.Cast<View>()
				.Where(v1=> !v1.IsTemplate)
				.Where(v1=> v1.CanUseTemporaryVisibilityModes());
Message 4 of 8

Anonymous
Not applicable

Hi, Jeremy thanks for always being helpful i'll try this and get back to you what's my result. 

i know my code is messy 🙂 i'm still searching some ways how to improve in writing a code. 

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi. this works like what i want! however, this eliminates the view schedule which has View Template parameter so i just add view schedule in the list and cast it to it's base class to get the .IsTitleblockRevisionSchedule properties to gets rid of Revision Schedule that i'am getting on my first code.

Here is my result. comment in my code is very much appreciated for improvement

 

Result.PNG

 

 

thanks to both of you  @jeremytammik @FAIR59  for helping 🙂

0 Likes
Message 6 of 8

jeremytammik
Autodesk
Autodesk

Thank you very much for your appreciation and sharing the solution.

 

Please do not post screen snapshots of code, because that makes it much harder to copy and paste the code, and also for Internet search engines and human beings to discover it at all.

 



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

Message 7 of 8

jeremytammik
Autodesk
Autodesk
Accepted solution

You can avoid a performance hit caused by instantiating a new List, copying part of your IEnumerable collection into it, thus duplicating information, repeated checking of properties and classes, and also avoid some code duplication like this:

 

    /// <summary>
    /// Predicate for views with template for
    /// GetViewsWithTemplate
    /// </summary>
    static bool ViewHasTemplate( View v )
    {
      return !v.IsTemplate
        && (v.CanUseTemporaryVisibilityModes()
          || ((ViewType.Schedule == v.ViewType)
            && !((ViewSchedule) v).IsTitleblockRevisionSchedule));
    }

    /// <summary>
    /// Return all views with a "View Template" parameter 
    /// </summary>
    static IEnumerable<View> GetViewsWithTemplate(
      Document doc )
    {
      FilteredElementCollector views
        = new FilteredElementCollector( doc )
          .WhereElementIsNotElementType()
          .OfClass( typeof( View ) );

      IEnumerable<View> views_w_t = views
        .Cast<View>()
        .Where( v => ViewHasTemplate( v ) )
        .OrderBy( v => v.Name );

      return views_w_t;
    }

 



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

0 Likes
Message 8 of 8

Anonymous
Not applicable

Awesome! thanks again for the feedback on my code. it really helps me to improve

 

cheers!

0 Likes