123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /*-------------------------------------------------------------------------
- * 功能描述:SelectionExtension
- * 作者:xulisong
- * 创建时间: 2019/5/23 11:31:45
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.UI.Selection;
- namespace FWindSoft.Revit
- {
- /// <summary>
- /// 选择项相关扩展
- /// </summary>
- public static class SelectionExtension
- {
- static SelectionExtension()
- {
- AllSnapTypes = GetAllSnapTypes();
- }
- /// <summary>
- /// 全部可选信息
- /// </summary>
- public static ObjectSnapTypes AllSnapTypes { get; private set; }
- #region 私有方法
- /// <summary>
- /// 获取全局选择配置
- /// </summary>
- /// <returns></returns>
- private static ObjectSnapTypes GetAllSnapTypes()
- {
- ObjectSnapTypes snapSettings =
- ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints |
- ObjectSnapTypes.Intersections | ObjectSnapTypes.Midpoints |
- ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid |
- ObjectSnapTypes.Perpendicular | ObjectSnapTypes.Points |
- ObjectSnapTypes.Quadrants | ObjectSnapTypes.Tangents;
- return snapSettings;
- }
- #endregion
- #region 选取点信息
- /// <summary>
- /// 选择点信息
- /// </summary>
- /// <param name="selection"></param>
- /// <param name="snapSettings">选择项</param>
- /// <param name="strMessage">提示消息</param>
- /// <returns></returns>
- public static XYZ PickPointT(this Selection selection, ObjectSnapTypes snapSettings, string strMessage)
- {
- try
- {
- return selection.PickPoint(snapSettings, strMessage);
- }
- catch (OperationCanceledException)
- {
- return null;
- }
- }
- /// <summary>
- /// 选取点
- /// </summary>
- /// <param name="selection"></param>
- /// <param name="strMessage"></param>
- /// <returns></returns>
- public static XYZ PickPointT(this Selection selection, string strMessage)
- {
- return PickPointT(selection, AllSnapTypes, strMessage);
- }
- #endregion
- #region 选取reference
- /// <summary>
- /// 选取指定的reference
- /// </summary>
- /// <param name="selection"></param>
- /// <param name="strMessage"></param>
- /// <param name="filter"></param>
- /// <param name="objType"></param>
- /// <returns></returns>
- public static Reference PickReferenceT(this Selection selection, string strMessage,
- ISelectionFilter filter, ObjectType objType)
- {
- strMessage = strMessage ?? string.Empty;
- Reference reference = null;
- try
- {
- if (filter == null)
- {
- reference = selection.PickObject(objType, strMessage);
- }
- else
- {
- reference = selection.PickObject(objType, filter, strMessage);
- }
- }
- #region 异常处理
- catch (OperationCanceledException)
- {
- return null;
- }
- catch (InvalidOperationException)
- {
- return null;
- }
- #endregion
- return reference;
- }
- public static IList<Reference> PickReferencesT(this Selection selection, string strMessage,
- ISelectionFilter filter, ObjectType objType)
- {
- strMessage = strMessage ?? string.Empty;
- IList<Reference> references = null;
- try
- {
- if (filter == null)
- {
- references = selection.PickObjects(objType, strMessage);
- }
- else
- {
- references = selection.PickObjects(objType, filter, strMessage);
- }
- }
- #region 异常处理
- catch (OperationCanceledException)
- {
- return null;
- }
- #endregion
- return references;
- }
- /// <summary>
- /// 获取选中元素elementId
- /// </summary>
- /// <param name="selection"></param>
- /// <param name="strMessage"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static ElementId PickElementId(this Selection selection, string strMessage, ISelectionFilter filter)
- {
- ElementId selectedElementId = null;
- strMessage = strMessage ?? string.Empty;
- try
- {
- Reference reference = null;
- reference = selection.PickReferenceT(strMessage, filter, ObjectType.Element);
- if (reference != null)
- {
- selectedElementId = reference.ElementId;
- }
- }
- catch (OperationCanceledException)
- {
- return null;
- }
- return selectedElementId;
- }
- #endregion
- #region 框选
- /// <summary>
- /// 框选相关元素信息
- /// </summary>
- /// <param name="selection"></param>
- /// <param name="strMessage"></param>
- /// <param name="filter">过滤器</param>
- /// <returns></returns>
- public static List<Element> PickElementsByRectangleT(this Selection selection, string strMessage, ISelectionFilter filter)
- {
- strMessage = strMessage ?? string.Empty;
- try
- {
- var listSelected = filter != null
- ? selection.PickElementsByRectangle(filter, strMessage).ToList()
- : selection.PickElementsByRectangle(strMessage).ToList();
- if (listSelected.Count == 0)
- {
- return PickElementsByRectangleT(selection,strMessage, filter);
- }
- return listSelected;
- }
- #region 异常处理
- catch (OperationCanceledException)
- {
- return null;
- }
- catch (InvalidOperationException)
- {
- return null;
- }
- #endregion
- }
- #endregion
- }
- }
|