Optimize performance while creating 3DViews and Filters.

Optimize performance while creating 3DViews and Filters.

mohd.fadel
Enthusiast Enthusiast
352 Views
3 Replies
Message 1 of 4

Optimize performance while creating 3DViews and Filters.

mohd.fadel
Enthusiast
Enthusiast

Hello,

 

I'm creating an add-in that creates views and add filters (which i also create via the api). On average, there are around 1000 views and 1000 filters added to each view. I found that CreateIsometric takes longer than duplicating the view using ActiveView.Duplicate(ViewDuplicateOption.Duplicate) so i decided to use Duplicate to create my views. However, adding the filters using AddFilter takes a lot of time. Is there any way to optimize my code? Here is a snippet of my code:

 

 

foreach (string tempName in allTempNames)
{
Element CreatedView = doc.GetElement(ActiveView.Duplicate(ViewDuplicateOption.Duplicate));
Autodesk.Revit.DB.View newView = doc.GetElement(CreatedView.Id) as Autodesk.Revit.DB.View;
newView.Name = tempName.Remove(phase.LastIndexOf('*'));
}

FilteredElementCollector viewCollector = new FilteredElementCollector(doc).OfClass(typeof(View3D));
            List<View3D> views3d = viewCollector.ToElements().Cast<View3D>().Where(x => x.IsTemplate == false).ToList();

            foreach (View3D view in views3d)
            {
                if (allList.Any(a => a.name== view.Name))
                {
                    foreach (LOE lo2 in allFilters)
                    {
                        view.AddFilter(lo2.filter_id);
                    }
                }
            }

 

 

Thank you in advance,

 

Mohamad.

0 Likes
353 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Yes, I see some (very minor) optimisation possibilities:

 

  • No need to call GetElement twice; use a cast instead of the second call
  • Eliminate the call to ToElements(); it serves no useful purpose and wastes both time and memory
  • Eliminate the call to ToList(); the same applies
  • Add benchmarking code to identify the bottleneck

 

I see no transactions; does all this code get executed within one single transaction?

   

I would suggest adding some benchmarking code:

  

https://www.google.com/search?q=benchmark&as_sitesearch=thebuildingcoder.typepad.com

  

With some reliable benchmarking results in hand, we can discuss further; if the problem makes sense and persists, please submit a complete minimal reproducible case for analysis.

  

 

 

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

mohd.fadel
Enthusiast
Enthusiast

Hello Jeremy,

 

Thank you for your reply. I tested my code multiple times. The bottleneck is in this code:

 

foreach (string test in allTest)
                    {
                        View3D view = viewCollector.Cast<View3D>().Where(x => x.Name == test.Remove(test.LastIndexOf('*'))).FirstOrDefault() as View3D;
                        
                        foreach (ElementId id in allFiltersIds)
                        {
                            view.AddFilter(id);
                        }
                    }

 

 

 

Like i said, I have over a 1000 view and over a 1000 filter. Each view should add the 1000 filter. It is taking a lot of time over 2 hours. So you think there's anything that can be done?

 

Regards,

 

Mohamad.

Message 4 of 4

jeremy_tammik
Alumni
Alumni

Well, basically Revit was designed as an end-user product, so it is not optimised for being driven programmatically. 

  

How long does this operation take manually in the UI? (Sorry, that was a joke.)

  

Have you discussed your approach with an application expert who has confirmed that you are indeed following best practices in performing this step?

  

If you like, you can submit a complete minimal reproducible test case for analysis by the development team:

  

https://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

  

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