|
@@ -19,31 +19,54 @@ namespace RevitExport
|
|
|
/// <summary>
|
|
|
/// 解析实例
|
|
|
/// </summary>
|
|
|
- public class ParseInstance<C> where C:ParseContext
|
|
|
+ public class ParseInstance<C, Result> where C:ParseContext
|
|
|
{
|
|
|
- public ParseInstance(C context,List<IParseElement<C>> parsers)
|
|
|
+ public ParseInstance(C context,List<IParseElement<C, Result>> parsers)
|
|
|
{
|
|
|
Context = context;
|
|
|
Parsers = parsers;
|
|
|
+ InitFastFilter();
|
|
|
}
|
|
|
- protected List<IParseElement<C>> Parsers { get; set; }
|
|
|
-
|
|
|
- protected virtual IParseElement<C> GetMatchParser(ElementWrapper wrapper)
|
|
|
+ protected Dictionary<string, IParseElement<C, Result>> FastFilter { get; set; }
|
|
|
+ protected List<IParseElement<C, Result>> Parsers { get; set; }
|
|
|
+ protected void InitFastFilter()
|
|
|
{
|
|
|
+ FastFilter = new Dictionary<string, IParseElement<C, Result>>();
|
|
|
+ foreach (var parseElement in Parsers)
|
|
|
+ {
|
|
|
+ var key = parseElement.FastIndex();
|
|
|
+ if (!string.IsNullOrWhiteSpace(key))
|
|
|
+ {
|
|
|
+ FastFilter[key] = parseElement;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ protected virtual IParseElement<C, Result> GetMatchParser(ElementWrapper wrapper)
|
|
|
+ {
|
|
|
+ if (FastFilter.TryGetValue(wrapper.Category, out IParseElement<C, Result> parser))
|
|
|
+ {
|
|
|
+ return parser;
|
|
|
+ }
|
|
|
var currentParse = Parsers.FirstOrDefault(p => p.Match(wrapper));
|
|
|
return currentParse;
|
|
|
}
|
|
|
+
|
|
|
+ public virtual List<Result> ParseElement(ElementWrapper elementWrapper, C context)
|
|
|
+ {
|
|
|
+ var currentParser = GetMatchParser(elementWrapper);
|
|
|
+ if (currentParser != null)
|
|
|
+ {
|
|
|
+ return currentParser.Parse(elementWrapper, Context);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
#region 解析元素相关
|
|
|
public virtual void ParseElement(object sender, ExportArgs e)
|
|
|
{
|
|
|
if (!Parsers.Any())
|
|
|
return;
|
|
|
ElementWrapper element = e.ElementWrapper;
|
|
|
- var currentParse = GetMatchParser(element);
|
|
|
- if (currentParse != null)
|
|
|
- {
|
|
|
- currentParse.Parse(element, Context);
|
|
|
- }
|
|
|
+ ParseElement(element, Context);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -58,12 +81,19 @@ namespace RevitExport
|
|
|
/// <returns></returns>
|
|
|
public virtual bool Parse(ExportInstance export)
|
|
|
{
|
|
|
- BindingExport(this,export);
|
|
|
- Context.Export = export;
|
|
|
- return export.Export();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ BindingExport(this, export);
|
|
|
+ Context.Export = export;
|
|
|
+ return export.Export();
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ export.ParseElement -= ParseElement;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static void BindingExport(ParseInstance<C> instance, ExportInstance export)
|
|
|
+ public static void BindingExport(ParseInstance<C, Result> instance, ExportInstance export)
|
|
|
{
|
|
|
export.ParseElement -= instance.ParseElement;
|
|
|
export.ParseElement += instance.ParseElement;
|