Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dead-End: Very slow exception handling #11

Open
wants to merge 1 commit into
base: reope/performance-base
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/Libraries/RevitNodes/Elements/Category.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Autodesk.DesignScript.Runtime;
using Autodesk.Revit.DB;
using RevitServices.Persistence;
using System.Collections.Generic;

namespace Revit.Elements
{
Expand Down Expand Up @@ -35,7 +34,7 @@ public string Name
get
{
var parent = internalCategory.Parent;
if(parent == null)
if (parent == null)
{
return internalCategory.Name;
}
Expand Down Expand Up @@ -103,14 +102,16 @@ public static Category ById(long id)
var document = DocumentManager.Instance.CurrentDBDocument;
BuiltInCategory categoryId = (BuiltInCategory)id;
Autodesk.Revit.DB.Category category = Autodesk.Revit.DB.Category.GetCategory(document, categoryId);
if(null == category)
throw new ArgumentException(Properties.Resources.InvalidCategory);
if (null == category)
{
return null;
}

return new Category(category);
}
catch
{
throw new ArgumentException(Properties.Resources.InvalidCategory);
return null;
}
}

Expand Down Expand Up @@ -143,40 +144,40 @@ public override int GetHashCode()
private static Autodesk.Revit.DB.Category GetCategory(String name)
{
Autodesk.Revit.DB.Category category = null;

var splits = name.Split('-');

// search by ui name first, that can be "{parent name} - {name}" or only the "{name}"
category = FindCategory((Autodesk.Revit.DB.Category cat) => { return name.Equals(new Revit.Elements.Category(cat).Name); });
if(category != null)
if (category != null)
{
return category;
}
else if (splits.Count() > 1)
{
var indexs = FindAllChars(name, '-');
foreach(var index in indexs)
foreach (var index in indexs)
{
var parentName = name.Substring(0, index).TrimEnd(' ');
var subName = name.Substring(index + 1).TrimStart(' ');
Autodesk.Revit.DB.Category parentCategory = FindCategory((Autodesk.Revit.DB.Category cat) => { return parentName.Equals(cat.Name); });
if(parentCategory != null)
if (parentCategory != null)
{
if(parentCategory.SubCategories.Contains(subName))
if (parentCategory.SubCategories.Contains(subName))
{
category = parentCategory.SubCategories.get_Item(subName);
break;
}
}
}
}
}
else
{
// Fall back
// Use category enum name with or without OST_ prefix
var fullName = name.Length > 3 && name.Substring(0, 4) == "OST_" ? name : "OST_" + name;
var names = Enum.GetNames(typeof(BuiltInCategory));
if(System.Array.Exists(names, entry => entry == fullName))
if (System.Array.Exists(names, entry => entry == fullName))
{
var builtInCat = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), fullName);
category = Autodesk.Revit.DB.Category.GetCategory(DocumentManager.Instance.CurrentDBDocument, builtInCat);
Expand All @@ -194,8 +195,8 @@ private static List<int> FindAllChars(String source, char specifiedChar)
int index = -1;
for (int i = 0; i < splits.Count() - 1; i++)
{
index = index + splits[i].Length + 1;
CharIndex.Add(index);
index = index + splits[i].Length + 1;
CharIndex.Add(index);
}

return CharIndex;
Expand All @@ -219,7 +220,7 @@ private static Autodesk.Revit.DB.Category FindCategory(Predicate<Autodesk.Revit.
continue;
}

if(tempCategory != null && pred(tempCategory))
if (tempCategory != null && pred(tempCategory))
{
return tempCategory;
}
Expand Down