diff --git a/sample/Tuna.Sample/Commands/ElementFilterCommand.cs b/sample/Tuna.Sample/Commands/ElementFilterCommand.cs index 49d0343..6fb12fe 100644 --- a/sample/Tuna.Sample/Commands/ElementFilterCommand.cs +++ b/sample/Tuna.Sample/Commands/ElementFilterCommand.cs @@ -16,7 +16,6 @@ using System.Linq; using Tuna.Revit.Extension; - namespace Tuna.Sample.Commands; [Transaction(TransactionMode.Manual)] @@ -78,6 +77,9 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme uIDocument.SelectElement(BuiltInCategory.OST_Walls, "选择墙体"); + + + diff --git a/sample/Tuna.Sample/Commands/SelectionCommand.cs b/sample/Tuna.Sample/Commands/SelectionCommand.cs index ba7106e..85a87aa 100644 --- a/sample/Tuna.Sample/Commands/SelectionCommand.cs +++ b/sample/Tuna.Sample/Commands/SelectionCommand.cs @@ -26,14 +26,16 @@ internal class SelectionCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { - //UIDocument uiDocument = commandData.Application.ActiveUIDocument; - - //uiDocument.SelectObject(Autodesk.Revit.UI.Selection.ObjectType.Element); + UIDocument uiDocument = commandData.Application.ActiveUIDocument; + uiDocument.SelectObject(Autodesk.Revit.UI.Selection.ObjectType.Element); + var doors = commandData.Application.ActiveUIDocument.Document.GetElements(BuiltInCategories.Door); + + TaskDialog.Show("count", doors.Count().ToString()); return Result.Succeeded; diff --git a/sample/Tuna.Sample/Tuna.Sample.csproj b/sample/Tuna.Sample/Tuna.Sample.csproj index 5a91558..8d378e7 100644 --- a/sample/Tuna.Sample/Tuna.Sample.csproj +++ b/sample/Tuna.Sample/Tuna.Sample.csproj @@ -20,7 +20,7 @@ true - + + + + - + + + diff --git a/sample/Tuna.Sample/XMLFile1.xml b/sample/Tuna.Sample/XMLFile1.xml new file mode 100644 index 0000000..cfea5b5 --- /dev/null +++ b/sample/Tuna.Sample/XMLFile1.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/Enumerable/Enumerator.cs b/src/Enumerable/Enumerator.cs index fd6e839..3c25e5d 100644 --- a/src/Enumerable/Enumerator.cs +++ b/src/Enumerable/Enumerator.cs @@ -7,8 +7,9 @@ namespace Tuna.Revit.Extension { /// - /// + /// 考虑因素较多.暂不可使用 /// + [Obsolete] public static class Enumerator { /// diff --git a/src/Extensions/ElementExtension.cs b/src/Extensions/ElementExtension.cs index 2e339d0..40e1af8 100644 --- a/src/Extensions/ElementExtension.cs +++ b/src/Extensions/ElementExtension.cs @@ -35,38 +35,7 @@ public static class ElementExtension public static Parameter GetParameter(this Element element, ElementId parameterId) { ArgumentNullExceptionUtils.ThrowIfNullOrInvalid(element); - - if (parameterId != ElementId.InvalidElementId) - { - foreach (Parameter item in element.Parameters) - { - if (item.Id == parameterId) - { - return item; - } - } - } - return default; - } - - /// - /// 根据族名称和族类型名称获取文档中的图元 - /// Get the elements in the document by family name and family symbol name - /// - /// 要查询的文档 - /// 族名称 - /// 族类型名称 - /// - /// - public static FilteredElementCollector GetElements(this Document document, string familyName, string familySymbolName) - { - Family family = document.GetElements(f => f.Name == familyName).FirstOrDefault() - ?? throw new ArgumentNullException(nameof(familyName), $"can't find family name of {familyName} in the document"); - - FamilySymbol familySymbol = document.GetFamilySymbols(family.Id).FirstOrDefault(s => s.Name == familySymbolName) - ?? throw new ArgumentNullException(nameof(familySymbolName), $"can't find family symbol name of {familySymbolName} in the family which name is {familyName}"); - - return document.GetElements(familySymbol); + return parameterId != ElementId.InvalidElementId ? element.Parameters.ToList(p => p.Id == parameterId).First() : default; } /// diff --git a/src/Extensions/FamilyExtension.cs b/src/Extensions/FamilyExtension.cs index 990f37e..bcddd41 100644 --- a/src/Extensions/FamilyExtension.cs +++ b/src/Extensions/FamilyExtension.cs @@ -10,78 +10,85 @@ using Autodesk.Revit.DB; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Tuna.Revit.Extension +namespace Tuna.Revit.Extension; + +/// +/// Revit family extension +/// +public static class FamilyExtension { /// - /// + /// Converter to /// - public static class FamilyExtension + /// + /// + /// + /// + /// + private static List ToList(TParent set, Predicate predicate = null) where TParent : IEnumerable { - /// - /// Convert FamilyParameterSet to - /// - /// - /// - /// - public static List ToList(this FamilyParameterSet familyParameterSet, Predicate predicate = null) + static IEnumerable ToList(TParent set) { - var familyParameters = new List(); - foreach (var item in familyParameterSet) + foreach (T parameter in set) { - var familyParameter = item as FamilyParameter; - if (familyParameter == null) - { - continue; - } - if (predicate != null && predicate(familyParameter) || predicate == null) - { - familyParameters.Add(familyParameter); - } + yield return parameter; } - return familyParameters; } + return (predicate == null ? ToList(set) : ToList(set).Where(p => predicate(p))).ToList(); + } - /// - /// Convert ParameterSet to - /// - /// - /// - /// - public static List ToList(this ParameterSet parameterSet, Predicate predicate = null) - { - var parameters = new List(); - var iterator = parameterSet.ForwardIterator(); - while (iterator.MoveNext()) - { - var parameter = iterator.Current as Parameter; - if (parameter == null) - { - continue; - } - if (predicate == null || predicate(parameter)) - { - parameters.Add(parameter); - } - } - return parameters; - } + /// + /// 从 创建一个 + /// Create a from + /// + /// 族参数集 + /// 对集合进行过滤 + /// 参数集 + public static List ToList(this FamilyParameterSet familyParameterSet, Predicate predicate = null) => ToList(familyParameterSet, predicate); - /// - /// 获取族的所有类型 - /// Get all family symbols of family - /// - /// 族 - /// 从族所在的文档中查询的结果 - /// - public static IEnumerable GetFamilySymbols(this Family family) - { - ArgumentNullExceptionUtils.ThrowIfNullOrInvalid(family); - return family.Document.GetFamilySymbols(family.Id); - } + /// + /// 从 创建一个 + /// Create a from + /// + /// 参数集 + /// 对集合进行过滤 + /// 参数集 + public static List ToList(this ParameterSet parameterSet, Predicate predicate = null) => ToList(parameterSet, predicate); + + /// + /// 获取族的所有类型 + /// Get all family symbols of family + /// + /// 族 + /// 从族所在的文档中查询的结果 + /// + public static IEnumerable GetFamilySymbols(this Family family) + { + ArgumentNullExceptionUtils.ThrowIfNullOrInvalid(family); + return family.Document.GetFamilySymbols(family.Id); + } + + /// + /// 根据族名称和族类型名称获取文档中的图元 + /// Get the elements in the document by family name and family symbol name + /// + /// 要查询的文档 + /// 族名称 + /// 族类型名称 + /// + /// + public static FilteredElementCollector GetElements(this Document document, string familyName, string familySymbolName) + { + Family family = document.GetElements(f => f.Name == familyName).FirstOrDefault() + ?? throw new ArgumentNullException(nameof(familyName), $"can't find family name of {familyName} in the document"); + + FamilySymbol familySymbol = family.GetFamilySymbols().FirstOrDefault(s => s.Name == familySymbolName) + ?? throw new ArgumentNullException(nameof(familySymbolName), $"can't find family symbol name of {familySymbolName} in the family which name is {familyName}"); + + return document.GetElements(familySymbol); } } diff --git a/src/Extensions/GeometryExtension.cs b/src/Extensions/GeometryExtension.cs index 5f7c0c7..7388c2a 100644 --- a/src/Extensions/GeometryExtension.cs +++ b/src/Extensions/GeometryExtension.cs @@ -24,11 +24,11 @@ namespace Tuna.Revit.Extension; /// public static class GeometryExtension { - private static List _transientElementIds = new List(); + private static readonly List _transientElementIds = new List(); private static readonly string _displayMethod = "SetForTransientDisplay"; - private static MethodInfo _method = GetTransientDisplayMethod() ?? throw new Exception($"No target method"); + private static readonly MethodInfo _method = GetTransientDisplayMethod() ?? throw new Exception($"No target method"); /// /// 在项目中创建临时显示的图元,临时图元将不会被保存在项目中,在项目关闭后,临时图元将被删除 @@ -84,10 +84,8 @@ public static void CleanTransientElements(this Document document) { return; } - document.NewTransaction((d) => - { - d.Delete(_transientElementIds.ToArray()); - }); + + document.NewTransaction(d => d.Delete(_transientElementIds.ToArray())); _transientElementIds.Clear(); } @@ -99,16 +97,13 @@ public static void CleanTransientElements(this Document document) /// transient element id /// transient element geometries /// transient element graphics style element id - public static void ResetTransientElementGeometry(this Document document, ElementId transientElementId, IList objects, ElementId graphicsStyleId = null) + public static void ResetTransientElementGeometry(this Document document, ElementId transientElementId, IList objects, ElementId graphicsStyleId = null) => _method.Invoke(null, parameters: new object[4] { - _method.Invoke(null, parameters: new object[4] - { - document, - transientElementId, - objects, - graphicsStyleId ?? ElementId.InvalidElementId - }); - } + document, + transientElementId, + objects, + graphicsStyleId ?? ElementId.InvalidElementId + }); /// /// 重新设置临时图元的图形 diff --git a/src/Tuna.Revit.Extension.csproj b/src/Tuna.Revit.Extension.csproj index ac31d00..0aa0e9b 100644 --- a/src/Tuna.Revit.Extension.csproj +++ b/src/Tuna.Revit.Extension.csproj @@ -196,20 +196,13 @@ - - \ - - - \ - - - true - contentFiles\any\any;content - + + + - +