Skip to content

Commit

Permalink
Merge pull request #32 from shichuyibushishiwu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
shichuyibushishiwu authored Oct 16, 2023
2 parents 510a338 + 05b4847 commit c11a75b
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 125 deletions.
4 changes: 3 additions & 1 deletion sample/Tuna.Sample/Commands/ElementFilterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System.Linq;
using Tuna.Revit.Extension;


namespace Tuna.Sample.Commands;

[Transaction(TransactionMode.Manual)]
Expand Down Expand Up @@ -78,6 +77,9 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
uIDocument.SelectElement(BuiltInCategory.OST_Walls, "选择墙体");








Expand Down
8 changes: 5 additions & 3 deletions sample/Tuna.Sample/Commands/SelectionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions sample/Tuna.Sample/Tuna.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup Condition="'$(Configuration.StartsWith(Rvt_16))'">
<!--<ItemGroup Condition="'$(Configuration.StartsWith(Rvt_16))'">
<PackageReference Include="Tuna.Revit.Extension" Version="2016.2.$(TunaVersion)" />
</ItemGroup>
Expand Down Expand Up @@ -50,9 +50,15 @@
<ItemGroup Condition="'$(Configuration.StartsWith(Rvt_24))'">
<PackageReference Include="Tuna.Revit.Extension" Version="2024.0.$(TunaVersion)" />
</ItemGroup>-->

<ItemGroup>
<Content Remove="C:\Users\Shiwu\.nuget\packages\tuna.revit.extension\2021.0.9\contentFiles\any\any\TunaRibbon.xsd" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Tuna.Revit.Extension" Version="2021.0.9" />
<PackageReference Include="Tuna.Revit.Extension" Version="2016.2.10" />
</ItemGroup>


</Project>
10 changes: 10 additions & 0 deletions sample/Tuna.Sample/XMLFile1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<Ribbon xmlns="http://tempuri.org/XMLSchema.xsd">
<RibbonTab Title="asd">
<RibbonPanel Title="asd">
<RibbonPushButton Title="asd" CommandClassFullName="as"/>
<RibbonPushButton Title="asd" CommandClassFullName="as"/>
<RibbonPushButton Title="asd" CommandClassFullName="as"/>
</RibbonPanel>
</RibbonTab>
</Ribbon>
3 changes: 2 additions & 1 deletion src/Enumerable/Enumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
namespace Tuna.Revit.Extension
{
/// <summary>
///
/// 考虑因素较多.暂不可使用
/// </summary>
[Obsolete]
public static class Enumerator
{
/// <summary>
Expand Down
33 changes: 1 addition & 32 deletions src/Extensions/ElementExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// 根据族名称和族类型名称获取文档中的图元
/// <para>Get the elements in the document by family name and family symbol name</para>
/// </summary>
/// <param name="document">要查询的文档</param>
/// <param name="familyName">族名称</param>
/// <param name="familySymbolName">族类型名称</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static FilteredElementCollector GetElements(this Document document, string familyName, string familySymbolName)
{
Family family = document.GetElements<Family>(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;
}

/// <summary>
Expand Down
127 changes: 67 additions & 60 deletions src/Extensions/FamilyExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Revit family extension
/// </summary>
public static class FamilyExtension
{
/// <summary>
///
/// Converter <see cref="IEnumerable"/> to <see cref="List{T}"/>
/// </summary>
public static class FamilyExtension
/// <typeparam name="T"></typeparam>
/// <typeparam name="TParent"></typeparam>
/// <param name="set"></param>
/// <param name="predicate"></param>
/// <returns></returns>
private static List<T> ToList<T, TParent>(TParent set, Predicate<T> predicate = null) where TParent : IEnumerable
{
/// <summary>
/// Convert FamilyParameterSet to
/// </summary>
/// <param name="familyParameterSet"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<FamilyParameter> ToList(this FamilyParameterSet familyParameterSet, Predicate<FamilyParameter> predicate = null)
static IEnumerable<T> ToList(TParent set)
{
var familyParameters = new List<FamilyParameter>();
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();
}

/// <summary>
/// Convert ParameterSet to
/// </summary>
/// <param name="parameterSet"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<Parameter> ToList(this ParameterSet parameterSet, Predicate<Parameter> predicate = null)
{
var parameters = new List<Parameter>();
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;
}
/// <summary>
/// 从 <see cref="FamilyParameterSet"/> 创建一个 <see cref="List{T}"/>
/// <para>Create a <see cref="List{T}"/> from <see cref="FamilyParameterSet"/> </para>
/// </summary>
/// <param name="familyParameterSet">族参数集</param>
/// <param name="predicate">对集合进行过滤</param>
/// <returns>参数集 <see cref="List{T}"/> </returns>
public static List<FamilyParameter> ToList(this FamilyParameterSet familyParameterSet, Predicate<FamilyParameter> predicate = null) => ToList<FamilyParameter, FamilyParameterSet>(familyParameterSet, predicate);

/// <summary>
/// 获取族的所有类型
/// <para>Get all family symbols of family</para>
/// </summary>
/// <param name="family">族</param>
/// <returns>从族所在的文档中查询的结果 <see cref="IEnumerable{T}"/></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static IEnumerable<FamilySymbol> GetFamilySymbols(this Family family)
{
ArgumentNullExceptionUtils.ThrowIfNullOrInvalid(family);
return family.Document.GetFamilySymbols(family.Id);
}
/// <summary>
/// 从 <see cref="ParameterSet"/> 创建一个 <see cref="List{T}"/>
/// <para>Create a <see cref="List{T}"/> from <see cref="ParameterSet"/> </para>
/// </summary>
/// <param name="parameterSet">参数集</param>
/// <param name="predicate">对集合进行过滤</param>
/// <returns>参数集 <see cref="List{T}"/> </returns>
public static List<Parameter> ToList(this ParameterSet parameterSet, Predicate<Parameter> predicate = null) => ToList<Parameter, ParameterSet>(parameterSet, predicate);

/// <summary>
/// 获取族的所有类型
/// <para>Get all family symbols of family</para>
/// </summary>
/// <param name="family">族</param>
/// <returns>从族所在的文档中查询的结果 <see cref="IEnumerable{T}"/></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static IEnumerable<FamilySymbol> GetFamilySymbols(this Family family)
{
ArgumentNullExceptionUtils.ThrowIfNullOrInvalid(family);
return family.Document.GetFamilySymbols(family.Id);
}

/// <summary>
/// 根据族名称和族类型名称获取文档中的图元
/// <para>Get the elements in the document by family name and family symbol name</para>
/// </summary>
/// <param name="document">要查询的文档</param>
/// <param name="familyName">族名称</param>
/// <param name="familySymbolName">族类型名称</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static FilteredElementCollector GetElements(this Document document, string familyName, string familySymbolName)
{
Family family = document.GetElements<Family>(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);
}
}
25 changes: 10 additions & 15 deletions src/Extensions/GeometryExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace Tuna.Revit.Extension;
/// </summary>
public static class GeometryExtension
{
private static List<ElementId> _transientElementIds = new List<ElementId>();
private static readonly List<ElementId> _transientElementIds = new List<ElementId>();

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");

/// <summary>
/// 在项目中创建临时显示的图元,临时图元将不会被保存在项目中,在项目关闭后,临时图元将被删除
Expand Down Expand Up @@ -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();
}

Expand All @@ -99,16 +97,13 @@ public static void CleanTransientElements(this Document document)
/// <param name="transientElementId">transient element id</param>
/// <param name="objects">transient element geometries</param>
/// <param name="graphicsStyleId">transient element graphics style element id </param>
public static void ResetTransientElementGeometry(this Document document, ElementId transientElementId, IList<GeometryObject> objects, ElementId graphicsStyleId = null)
public static void ResetTransientElementGeometry(this Document document, ElementId transientElementId, IList<GeometryObject> 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
});

/// <summary>
/// 重新设置临时图元的图形
Expand Down
15 changes: 4 additions & 11 deletions src/Tuna.Revit.Extension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,13 @@
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="True">
<PackagePath>\</PackagePath>
</None>
<None Include="..\Tuna.png" Pack="True">
<PackagePath>\</PackagePath>
</None>
<Content Include="..\TunaRibbon.xsd">
<Pack>true</Pack>
<PackagePath>contentFiles\any\any;content</PackagePath>
</Content>
<None Include="..\README.md" Pack="True" PackagePath="\" />
<None Include="..\Tuna.png" Pack="True" PackagePath="\" />
<None Include="..\TunaRibbon.xsd" Pack="True" PackagePath="\" Visible="False"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

</Project>

0 comments on commit c11a75b

Please sign in to comment.