169 lines
5.0 KiB
C#
169 lines
5.0 KiB
C#
|
|
|
|||
|
|
using Autodesk.Revit.DB;
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.UI.Selection;
|
|||
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace Szmedi.Toolkit.RvExtensions;
|
|||
|
|
|
|||
|
|
public static class UIDocumentExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 运行命令选择元素或选择元素执行操作
|
|||
|
|
/// </summary>
|
|||
|
|
public static List<Element> GetSelectedElems(
|
|||
|
|
this UIDocument uidoc,
|
|||
|
|
string prompt = "请选择元素",
|
|||
|
|
ISelectionFilter filter = null,
|
|||
|
|
ObjectType type = ObjectType.Element
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
//取到当前文档。
|
|||
|
|
Document doc = uidoc.Document;
|
|||
|
|
//取到当前文档的选择集。
|
|||
|
|
List<Element> collection = uidoc.Selection.GetElementIds().Select(id => doc.GetElement(id)).ToList();
|
|||
|
|
if (collection.Any())
|
|||
|
|
{
|
|||
|
|
return collection;
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
IList<Reference> references =
|
|||
|
|
filter == null ? uidoc.Selection.PickObjects(type, prompt) : uidoc.Selection.PickObjects(type, filter, prompt);
|
|||
|
|
|
|||
|
|
collection = references.Select(refer => doc.GetElement(refer)).ToList();
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
|
|||
|
|
|
|||
|
|
return collection;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 重复命令
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="uidoc"></param>
|
|||
|
|
/// <param name="action"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static void RepeatCommand(this UIDocument uidoc, Action action)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
action?.Invoke();
|
|||
|
|
|
|||
|
|
uidoc.RepeatCommand(action);
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 按类型选择元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="uidoc"></param>
|
|||
|
|
/// <param name="prompt"></param>
|
|||
|
|
/// <param name="objectType"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static T SelectObject<T>(this UIDocument uidoc, string prompt = "请选择元素", ObjectType objectType = ObjectType.Element)
|
|||
|
|
where T : Element
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Reference reference = uidoc.Selection.PickObject(objectType, new GenericFilter<T>(), prompt);
|
|||
|
|
return uidoc.Document.GetElement(reference) as T;
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 按类型框选元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="uidoc"></param>
|
|||
|
|
/// <param name="prompt"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static IList<T> SelectObjectsByRectangle<T>(this UIDocument uidoc, string prompt = "请框选元素")
|
|||
|
|
where T : Element
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
return uidoc.Selection.PickElementsByRectangle(new GenericFilter<T>(), prompt).Cast<T>().ToList();
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 选择元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="uidoc"></param>
|
|||
|
|
/// <param name="prompt"></param>
|
|||
|
|
/// <param name="objectType"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static Element SelectObject(
|
|||
|
|
this UIDocument uidoc,
|
|||
|
|
FuncFilter funcFilter,
|
|||
|
|
string prompt = "请选择元素",
|
|||
|
|
ObjectType objectType = ObjectType.Element
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var reference = uidoc.Selection.PickObject(objectType, funcFilter, prompt);
|
|||
|
|
return uidoc.Document.GetElement(reference);
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static List<Element> SelectObjects(
|
|||
|
|
this UIDocument uidoc,
|
|||
|
|
FuncFilter funcFilter,
|
|||
|
|
string prompt = "请选择元素",
|
|||
|
|
ObjectType objectType = ObjectType.Element
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var references = uidoc.Selection.PickObjects(objectType, funcFilter, prompt);
|
|||
|
|
return [.. references.Select(uidoc.Document.GetElement)];
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 按类型选择多个元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="uidoc"></param>
|
|||
|
|
/// <param name="objectType"></param>
|
|||
|
|
/// <param name="prompt"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<T> SelectObjects<T>(this UIDocument uidoc, string prompt = "请选择多个元素", ObjectType objectType = ObjectType.Element)
|
|||
|
|
where T : Element
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
IList<Reference> references = uidoc.Selection.PickObjects(objectType, new GenericFilter<T>(), prompt);
|
|||
|
|
|
|||
|
|
return references.Select(refer => uidoc.Document.GetElement(refer) as T).ToList();
|
|||
|
|
}
|
|||
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|||
|
|
{
|
|||
|
|
return [];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|