添加项目文件。
This commit is contained in:
156
Sai.Toolkit.Revit/Assist/UIDocumentAssist.cs
Normal file
156
Sai.Toolkit.Revit/Assist/UIDocumentAssist.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
|
||||
using Sai.Toolkit.Revit.Helpers;
|
||||
|
||||
namespace Sai.Toolkit.Revit.Assist;
|
||||
|
||||
public static class UIDocumentAssist
|
||||
{
|
||||
/// <summary>
|
||||
/// 运行命令选择元素或选择元素执行操作
|
||||
/// </summary>
|
||||
public static List<Element> GetSelectedElements(
|
||||
this UIDocument uidoc,
|
||||
string prompt = "请选择元素",
|
||||
ISelectionFilter filter = null,
|
||||
ObjectType type = ObjectType.Element
|
||||
)
|
||||
{
|
||||
//取到当前文档。
|
||||
var doc = uidoc.Document;
|
||||
//取到当前文档的选择集。
|
||||
var collection = uidoc.Selection.GetElementIds().Select(id => doc.GetElement(id)).ToList();
|
||||
if (collection.Count != 0)
|
||||
{
|
||||
return collection;
|
||||
}
|
||||
try
|
||||
{
|
||||
var 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>
|
||||
/// <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
|
||||
{
|
||||
var 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>
|
||||
/// <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(reference => uidoc.Document.GetElement(reference)).ToList();
|
||||
}
|
||||
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
|
||||
{
|
||||
var 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user