本帖最后由 箭头_Row 于 2025-8-13 12:29 编辑
看動圖大致是下面步驟操作:
第一步獲取座標點,判斷操作:
option:點取點則進行接下來插入操作
option:點右鍵則進行右鍵菜單(關鍵字菜單)選取關鍵字操作
{
根據關鍵寫對應的更改參數的邏輯;
}
注意:獲取點時一定要打開可空輸入!
大致代碼如下:
 - public static class CsTest
- {
- private enum Keywords
- {
- [Description("A")]
- 轴类,
- [Description("S")]
- 设置,
- }
- [CommandMethod(nameof(Dg))]
- public static void Dg()
- {
- using var doc = Acaop.DocumentManager.MdiActiveDocument;
- var ed = doc.Editor;
- // 以下測試代碼,循環可自行擴展
- PromptPointOptions ppo = new("\n指定第一个点");
- ppo.AllowNone = true; // 打开可空才會右鍵彈關鍵字菜單
- PromptPointResult ppr = Env.Editor.GetPoint(ppo);
- if (ppr.Status == PromptStatus.None)
- {
- PromptKeywordOptions options = new(string.Empty);
- var keywordsMap = ArEnumEx.GetKeywordsMap<Keywords>();
- options.AddKeywords([.. keywordsMap.Keys]);
- options.AppendKeywordsToMessage = false; //不将关键字列表添加到提示信息中
- options.Message = "\n右鍵彈出菜單示例" + keywordsMap.ToMessage();
- PromptResult pr = ed.GetKeywords(options);
- if (pr.Status.CancelPrint())
- return;
- // 進行更改參數的操作
- }
- else if (ppr.Status != PromptStatus.OK)
- return;
- Point3d ptStart = ppr.Value;
- }
- }
 - /// <summary>
- /// 獲取關鍵字字典
- /// </summary>
- /// <typeparam name="T">關鍵字枚舉</typeparam>
- /// <returns> Dictionary 快捷鍵,(描述字_中文,Enum值)</returns>
- public static Dictionary<string, (string Key_CN, T enumValue)> GetKeywordsMap<T>()
- where T : Enum
- {
- Dictionary<string, (string, T)> keywordsDic = [];
- var type = typeof(T);
- var typeDesc = typeof(DescriptionAttribute);
- foreach (var e in Enum.GetValues(type))
- {
- var name = e.ToString();
- object objArr = type.GetField(name).GetCustomAttributes(typeDesc, true)[0];
- keywordsDic.Add(((DescriptionAttribute)objArr).Description, (name, (T)e));
- }
- return keywordsDic;
- }
感謝驚驚提示的源碼及思路:獲取關鍵字字典。
 - /// <summary>
- /// 获取关键字提示信息行。
- /// </summary>
- /// <param name="keywordsDic">keywords關鍵字字典</typeparam>
- /// <param name="defaultKeyword">可选的默认关键字,非空时添加到结果中</param>
- /// <returns> 返回格式示例:[字典(D)/板厚(T)/层高(H)/完成面(F)/图层(C)]</returns>
- public static StringBuilder ToMessage<T>(
- this Dictionary<string, (string Key_CN, T enumValue)> keywordsDic,
- string defaultKeyword = ""
- )
- where T : Enum
- {
- int number = 0;
- StringBuilder keywordNotice = new("[");
- foreach (var item in keywordsDic)
- {
- keywordNotice.Append(item.Value.Key_CN);
- keywordNotice.Append('(');
- keywordNotice.Append(item.Key);
- keywordNotice.Append(')');
- if (number < keywordsDic.Count - 1)
- keywordNotice.Append('/');
- number++;
- }
- keywordNotice.Append(']');
- if (defaultKeyword != string.Empty)
- if (keywordsDic.ContainsKey(defaultKeyword))
- keywordNotice.Append($"<{defaultKeyword}>");
- else // 無默認值時報錯提示
- throw new ArgumentException("错误的默认值!");
- return keywordNotice;
- }
 - /// <summary>
- /// 檢查提示狀態是否為取消狀態,如果是則打印指定消息並返回 true。
- /// </summary>
- /// <param name="status">提示狀態。</param>
- /// <param name="message">要打印的消息,默認為 "*取消*"。</param>
- /// <returns>如果提示狀態不是 OK,則返回 true,否則返回 false。</returns>
- public static bool CancelPrint(this PromptStatus status, string message = "*取消*")
- {
- if (status == PromptStatus.OK)
- return false;
- message.Print();
- return true;
- }
|