Files

274 lines
9.3 KiB
C#
Raw Permalink Normal View History

2026-02-28 21:01:57 +08:00
using System;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using static Bentley.DgnPlatformNET.DgnElementSetTool;
namespace Mstn.Toolkit.External
{
public class UserPickTool : DgnElementSetTool
{
private Func<Element, bool> allowElement;
private string prompt;
private Action<ElementAgenda> operations;
private UserPickTool(int toolId, int promptId) : base(toolId, promptId)
{
}
public static void Setup(Func<Element, bool> allowElement, Action<ElementAgenda> operations, string prompt = "请选择元素")
{
UserPickTool tool = new(0, 0)
{
prompt = prompt,
allowElement = allowElement,
operations = operations,
};
tool.InstallTool();
}
/// <summary>
/// 判断是否要启用框选或者划选
/// </summary>
/// <returns></returns>
protected override UsesDragSelect AllowDragSelect()
{
return UsesDragSelect.Box;
}
///// <summary>
///// 筛选拾取的元素
///// </summary>
///// <returns></returns>
//protected override bool FilterAgendaEntries()
//{
// for (uint i = 0; i < ElementAgenda.GetCount(); i++)
// {
// var elem = ElementAgenda.GetEntry(i);
// return allowElement(elem);
// }
// return false;
// //return base.FilterAgendaEntries();
//}
///// <summary>
///// 通过点选、围栅还是选择集来填充ElementAgenda
///// </summary>
///// <returns></returns>
//protected override ElementSource GetElementSource()
//{
// return base.GetElementSource();
//}
/// <summary>
/// 获取偏好选择方式
/// </summary>
/// <returns></returns>
protected override ElementSource GetPreferredElementSource()
{
switch (AllowFence())
{
case UsesFence.Check:
if (UseActiveFence() && FenceManager.IsFenceActive)
{
return ElementSource.Fence;
}
break;
case UsesFence.Required:
return ElementSource.Fence;
case UsesFence.None:
break;
default:
break;
}
switch (AllowSelection())
{
case UsesSelection.Check:
if (SelectionSetManager.IsActive())
{
return ElementSource.SelectionSet;
}
break;
case UsesSelection.Required:
return ElementSource.SelectionSet;
case UsesSelection.None:
break;
default:
break;
}
return ElementSource.Pick;
}
/// <summary>
/// 判断是否需要用户再点击左键才开始处理元素
/// </summary>
/// <returns></returns>
protected override bool NeedAcceptPoint()
{
return false;
}
///// <summary>
/////
///// </summary>
///// <param name="ev"></param>
///// <param name="newSearch">是否重新搜索</param>
///// <remarks>newSearch=false:表明用户最后拾取的元素不是想要的调用RemoveAgendaElement移除并继续往后定位其他元素</remarks>
//protected override void LocateOneElement(DgnButtonEvent ev, bool newSearch)
//{
// base.LocateOneElement(ev, newSearch);
//}
///// <summary>
///// 对ElementAgenda中的元素进行修改筛选
///// </summary>
//protected override void ModifyAgendaEntries()
//{
// base.ModifyAgendaEntries();
//}
///// <summary>
///// 点击确认键(默认为左键)后触发,判断是否需要拾取元素
///// </summary>
///// <param name="ev"></param>
///// <returns></returns>
//protected override bool OnDataButton(DgnButtonEvent ev)//
//{
// //var promptId = "OnDataButton";
// NotificationManager.OutputPrompt(prompt);//将提示语输出到提示框中
// if (ElementSource.Pick == GetElementSource())
// {
// //光标变为点选状态
// BeginPickElements();
// }
// else if (ElementSource.SelectionSet == GetElementSource())
// {
// //是否需要用户输入额外的确认点才开始处理选择集元素/判断是否需要用户再点击左键才开始处理元素
// if (NeedPointForSelection())
// {
// }
// else
// {
// BuildAgenda(ev);
// ProcessAgenda(ev);
// OnModifyComplete(ev);
// }
// if (!NeedPointForDynamics())
// {
// if (ElementSource.Pick != GetElementSource())
// {
// BuildAgenda(ev);
// }
// }
// }
// return true;
//}
/// <summary>
/// 修改完毕,对选择集中的元素进行处理
/// </summary>
/// <param name="ev"></param>
/// <returns></returns>
protected override bool OnModifyComplete(DgnButtonEvent ev)
{
operations(ElementAgenda);
//for (uint i = 0; i < ElementAgenda.GetCount(); i++)
//{
// var elem = ElementAgenda.GetEntry(i);
// operation(elem);
//}
NotificationManager.OutputPrompt("完成");//将提示语输出到提示框中
return base.OnModifyComplete(ev);
}
/// <summary>
/// 工具激活后执行
/// </summary>
protected override void OnPostInstall()
{
//var promptId = "OnPostInstall";
//NotificationManager.OutputPrompt(promptId);//将提示语输出到提示框中
NotificationManager.OutputPrompt(prompt);
//var dgnModel = Session.Instance.GetActiveDgnModel();//获得当前的模型空间
base.OnPostInstall();
}
/// <summary>
/// 光标定位到某个元素时调用,判断是否支持该元素,能否被选中
/// </summary>
/// <param name="path"></param>
/// <param name="cantAcceptReason">光标在上方时,进行提醒,为什么不能被选中</param>
/// <returns></returns>
protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
{
var elem = path.GetCursorElement();
var b = allowElement(elem);
cantAcceptReason = b ? path.GetCursorElement().Description : "元素不适用此功能";
return b;
}
/// <summary>
/// 点击重置键(默认为右键)触发
/// </summary>
/// <param name="ev"></param>
/// <returns></returns>
protected override bool OnResetButton(DgnButtonEvent ev)//
{
ExitTool();//退出工具
return true;//返回
}
///// <summary>
///// 重启工具时触发
///// </summary>
protected override void OnRestartTool()
{
Setup(allowElement, operations, prompt);
}
///// <summary>
///// 设置选择方式
///// </summary>
///// <param name="source"></param>
//protected override void SetElementSource(ElementSource source)
//{
// switch (source)
// {
// case ElementSource.Pick:
// break;
// case ElementSource.Fence:
// break;
// case ElementSource.SelectionSet:
// break;
// default:
// break;
// }
//}
/// <summary>
/// 是否需要继续进行选择
/// </summary>
/// <param name="ev"></param>
/// <returns></returns>
protected override bool WantAdditionalLocate(DgnButtonEvent ev)
{
if (ev == null)//若没有选择到元素
{
return true;
}
if (ev.IsControlKey)//若检测到按动Ctrl行为
{
return true;
}
if (ElementAgenda.GetCount() == 0)//若选择集中没有元素
{
return true;
}
return false;
}
/*
* SUCCESS的话_DoOperationForModify
* _IsModifyOriginal返回true
* Dgn文件中
*/
public override StatusInt OnElementModify(Element element)
{
return StatusInt.Error;
}
}
}