using System; using Bentley.DgnPlatformNET; using Bentley.DgnPlatformNET.Elements; using static Bentley.DgnPlatformNET.DgnElementSetTool; namespace Mstn.Toolkit.External { public class UserPickTool : DgnElementSetTool { private Func allowElement; private string prompt; private Action operations; private UserPickTool(int toolId, int promptId) : base(toolId, promptId) { } public static void Setup(Func allowElement, Action operations, string prompt = "请选择元素") { UserPickTool tool = new(0, 0) { prompt = prompt, allowElement = allowElement, operations = operations, }; tool.InstallTool(); } /// /// 判断是否要启用框选或者划选 /// /// protected override UsesDragSelect AllowDragSelect() { return UsesDragSelect.Box; } ///// ///// 筛选拾取的元素 ///// ///// //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(); //} ///// ///// 通过点选、围栅还是选择集来填充ElementAgenda ///// ///// //protected override ElementSource GetElementSource() //{ // return base.GetElementSource(); //} /// /// 获取偏好选择方式 /// /// 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; } /// /// 判断是否需要用户再点击左键才开始处理元素 /// /// protected override bool NeedAcceptPoint() { return false; } ///// ///// ///// ///// ///// 是否重新搜索 ///// newSearch=false:表明用户最后拾取的元素不是想要的,调用RemoveAgendaElement移除,并继续往后定位其他元素 //protected override void LocateOneElement(DgnButtonEvent ev, bool newSearch) //{ // base.LocateOneElement(ev, newSearch); //} ///// ///// 对ElementAgenda中的元素进行修改筛选 ///// //protected override void ModifyAgendaEntries() //{ // base.ModifyAgendaEntries(); //} ///// ///// 点击确认键(默认为左键)后触发,判断是否需要拾取元素 ///// ///// ///// //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; //} /// /// 修改完毕,对选择集中的元素进行处理 /// /// /// 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); } /// /// 工具激活后执行 /// protected override void OnPostInstall() { //var promptId = "OnPostInstall"; //NotificationManager.OutputPrompt(promptId);//将提示语输出到提示框中 NotificationManager.OutputPrompt(prompt); //var dgnModel = Session.Instance.GetActiveDgnModel();//获得当前的模型空间 base.OnPostInstall(); } /// /// 光标定位到某个元素时调用,判断是否支持该元素,能否被选中 /// /// /// 光标在上方时,进行提醒,为什么不能被选中 /// protected override bool OnPostLocate(HitPath path, out string cantAcceptReason) { var elem = path.GetCursorElement(); var b = allowElement(elem); cantAcceptReason = b ? path.GetCursorElement().Description : "元素不适用此功能"; return b; } /// /// 点击重置键(默认为右键)触发 /// /// /// protected override bool OnResetButton(DgnButtonEvent ev)// { ExitTool();//退出工具 return true;//返回 } ///// ///// 重启工具时触发 ///// protected override void OnRestartTool() { Setup(allowElement, operations, prompt); } ///// ///// 设置选择方式 ///// ///// //protected override void SetElementSource(ElementSource source) //{ // switch (source) // { // case ElementSource.Pick: // break; // case ElementSource.Fence: // break; // case ElementSource.SelectionSet: // break; // default: // break; // } //} /// /// 是否需要继续进行选择 /// /// /// 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; } } }