[CommandMethod("mysel", CommandFlags.Session)]
public void mysel()
{
var acDoc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var acEditor = acDoc.Editor;
void pso_KeywordInput(object sender, SelectionTextInputEventArgs e)
{
//user has pressed keyword, so throw Exception
acEditor.WriteMessage("\n使用了关键字");
if (e.Input == "s")
{
acEditor.WriteMessage("\ns设置");
}
else
throw new ZwSoft.ZwCAD.Runtime.Exception(ErrorStatus.OK, e.Input);
}
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "请选择对象或[设置(S)]: ";
pso.KeywordInput += new SelectionTextInputEventHandler(pso_KeywordInput);
pso.SetKeywords("[设置(S)]", "s");
var psr = acEditor.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
var ids = psr.Value.GetObjectIds();
acEditor.WriteMessage("\n选择到对象");
}
}
// ids = ed.myGetSelection("请选择需要对齐的对象或[选择集(S)]:", "[选择集(S)]", "S");
////public static ObjectId[] myGetSelection(this Editor ed, string str, string keywords, string globalkeywords)
////{
//// void pro_KeywordInput(object sender, SelectionTextInputEventArgs e)
//// {
//// ed.WriteMessage("\n使用了关键字!");
//// if (e.Input.Equals("S"))
//// {
//// ed.WriteMessage("\nS=>设置");
//// }
//// }
//// PromptSelectionOptions pso = new PromptSelectionOptions();
//// pso.AllowDuplicates = false; //重复选择
//// pso.MessageForAdding = str;
//// pso.KeywordInput += new SelectionTextInputEventHandler(pro_KeywordInput);
//// pso.SetKeywords(keywords, globalkeywords);
//// //从选择集命令中显示关键字
//// pso.MessageForAdding = pso.Keywords.GetDisplayString(true);
//// PromptSelectionResult psr = ed.GetSelection(pso);
//// if (psr.Status == PromptStatus.OK)
//// {
//// return psr.Value.GetObjectIds();
//// }
//// return new ObjectId[0];
////}
/// <summary>
/// 添加选择集关键字和回调
/// </summary>
/// <param name="keyExplain">关键字,关键字说明,关键字回调的操作</param>
/// <returns></returns>
////public static void SsgetAddKeys(this PromptSelectionOptions pso, Dictionary<string, Action> dicActions)
////{
//// // 处理后缀名的|号切割
//// foreach (var key in dicActions.Keys.ToArray())//克隆副本
//// {
//// var keySplitS = key.Split('|');
//// if (keySplitS.Length > 1)
//// {
//// foreach (var keySplit in keySplitS)
//// {
//// // 防止多个后缀通过|符越过词典约束同名
//// // 后缀(key)含有,而且Action(value)不同,就把Action(value)累加到后面.
//// if (dicActions.Keys.Contains(keySplit))
//// {
//// if (dicActions[keySplit] != dicActions[key])
//// {
//// dicActions[keySplit] += dicActions[key];
//// }
//// }
//// else
//// {
//// dicActions.Add(keySplit, dicActions[key]);
//// }
//// }
//// dicActions.Remove(key);
//// }
//// }
//// //去除关键字重复的,把重复的执行动作移动到前面
//// var keys = dicActions.Keys.ToList();
//// for (int i = 0; i < keys.Count; i++)
//// {
//// var key1 = keys[i];
//// for (int n = i + 1; n < keys.Count; n++)
//// {
//// var key2 = keys[n];
//// if (key1.Split(',')[0] == key2.Split(',')[0] && dicActions[key1] != dicActions[key2])
//// {
//// dicActions[key1] += dicActions[key2];
//// dicActions.Remove(key2);
//// keys.Remove(key2);
//// }
//// }
//// }
//// foreach (var item in dicActions)
//// {
//// var keySplitS = item.Key.Split(new string[] { ",", "|" }, StringSplitOptions.RemoveEmptyEntries);
//// for (int i = 0; i < keySplitS.Length; i += 2)
//// {
//// pso.Keywords.Add(keySplitS[i], keySplitS[i], keySplitS[i + 1] + "(" + keySplitS[i] + ")");
//// }
//// }
//// KeywordCollection keyWords = pso.Keywords;
//// //从选择集命令中显示关键字
//// pso.MessageForAdding = keyWords.GetDisplayString(true);
//// //在输入关键字时实现回调的事件 ssget关键字
//// pso.KeywordInput += (sender, e) =>
//// {
//// foreach (var item in dicActions)
//// {
//// if (item.Key.Split(',')[0] == e.Input)
//// {
//// item.Value.Invoke();
//// }
//// }
//// };
////}
调用
//定义选择集选项
var pso = new PromptSelectionOptions
{
AllowDuplicates = false, //重复选择
};
//getai遍历全图选择块有用到
var di = new Dictionary<string, Action>() {
{ "Z,全部同名", ()=> {
getai = BlockTool.EnumAttIdentical.AllBlockName;
SendEsc.Esc();
}},
{ "X,动态块显示", ()=> {
getai = BlockTool.EnumAttIdentical.Display;
}},
{ "V,属性值-默认", ()=> {
getai = BlockTool.EnumAttIdentical.DisplayAndTagText;
}},
//允许以下操作,相同的会覆盖前面的
{ "V,属性值-默认|X,啊啊啊啊", ()=> {
}},
};
pso.SsgetAddKeys(di);
//创建选择集过滤器,只选择块对象
var filList = new TypedValue[] { new TypedValue((int)DxfCode.Start, "INSERT") };
var filter = new SelectionFilter(filList);
//手选
ssPsr = ed.GetSelection(pso, filter);
if (ssPsr.Status != PromptStatus.OK)
{
return re;
}
封装
/// <summary>
/// 添加选择集关键字和回调
/// </summary>
/// <param name="keyExplain">关键字,关键字说明,关键字回调的操作</param>
/// <returns></returns>
public static void SsgetAddKeys(this PromptSelectionOptions pso,
Dictionary<string, Action> dicActions)
{
// 处理后缀名的|号切割
foreach (var key in dicActions.Keys.ToArray())//克隆副本
{
var keySplitS = key.Split('|');
if (keySplitS.Length > 1)
{
foreach (var keySplit in keySplitS)
{
// 防止多个后缀通过|符越过词典约束同名
// 后缀(key)含有,而且Action(value)不同,就把Action(value)累加到后面.
if (dicActions.Keys.Contains(keySplit))
{
if (dicActions[keySplit] != dicActions[key])
{
dicActions[keySplit] += dicActions[key];
}
}
else
{
dicActions.Add(keySplit, dicActions[key]);
}
}
dicActions.Remove(key);
}
}
//去除关键字重复的,把重复的执行动作移动到前面
var keys = dicActions.Keys.ToList();
for (int i = 0; i < keys.Count; i++)
{
var key1 = keys[i];
for (int n = i + 1; n < keys.Count; n++)
{
var key2 = keys[n];
if (key1.Split(',')[0] == key2.Split(',')[0] && dicActions[key1] != dicActions[key2])
{
dicActions[key1] += dicActions[key2];
dicActions.Remove(key2);
keys.Remove(key2);
}
}
}
foreach (var item in dicActions)
{
var keySplitS = item.Key.Split(new string[] { ",", "|" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < keySplitS.Length; i += 2)
{
pso.Keywords.Add(keySplitS[i], keySplitS[i], keySplitS[i + 1] + "(" + keySplitS[i] + ")");
}
}
KeywordCollection keyWords = pso.Keywords;
//从选择集命令中显示关键字
pso.MessageForAdding = keyWords.GetDisplayString(true);
//在输入关键字时实现回调的事件 ssget关键字
pso.KeywordInput += (sender, e) => {
foreach (var item in dicActions)
{
if (item.Key.Split(',')[0] == e.Input)
{
item.Value.Invoke();
}
}
};
}