Hi, I am not sure how to do this. I need to prompt for entity selection. Also, I want the procedure to use a default value, but to give the user a chance to change the initial or default value. So, I thought to use Keywords. I want to prompt the user with something like this "Please select a block or [Set value] <30>:" The problem I have is that if I set a default value it has to be included on the keywords collection. I prefer not to have default value shown as a keyword. I tried to use the visible property of the keyword item, but it will crash AutoCAD. Can someone help me to make this work. Thank you in advance.
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Let's see what user will think and do after seeing the prompt "Please select block or [Set value] <30>":
Firstly, the prompt is from PromptEntityOption used in Editor.GetEntity() call, right? User will
1. go ahead with mouse to pick block/entity;
2. or, user enter "s" for keywork "Set", which will be followed a prompt to ask user to enter a number (call Editor.GetInterger()/GetDouble()
3. or, user simply hit Enter/Space for default keywork, so that a value of 30 is assumed.
Since you do not want to present "30" as a keywork option to user, you can build the keyword as needed (i.e. add "30" as keyword and set it as default) and set PromptEntityOption.AppendKeywordToMessage to false (default value is true). This way you use the "Message" to build the fake keyword list you want user to see, and the invisible keyword list and its default still works as it should.
Here is the code:
[CommandMethod("MyPick")] public static void RunMyCommand() { Document dwg = Application.DocumentManager.MdiActiveDocument; Editor ed = dwg.Editor; int val = 0; ObjectId pickedId = ObjectId.Null; while (true) { PromptEntityOptions opt = new PromptEntityOptions( "\nSelect an entity or [Set value] <30>:"); opt.AllowNone = true; opt.Keywords.Add("Set value"); opt.Keywords.Add("30"); opt.Keywords.Default = "30"; opt.AppendKeywordsToMessage = false; PromptEntityResult res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) { ed.WriteMessage("\nPicked entity: {0}", res.ObjectId.ToString()); pickedId = res.ObjectId; break; } else if (res.Status == PromptStatus.Keyword) { ed.WriteMessage("keyword: {0}", res.StringResult); if (res.StringResult == "Set") { //use Editor.GetInteger() tp get new value //val=.... } else { val = 30; } } else { ed.WriteMessage("\nInvalid pick or cancelled"); break; } } if (pickedId != ObjectId.Null) { //Do something } Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt(); } }
Norman Yuan
Norman, thank you for your suggestion. It is a good idea. Do you know what the visible property of the keyword class is for? I thought that by setting the visible property of a keywork to false, it would not be shown to the user.
I have to admit that I have not looked into/used Keyword.Visible property before. Now that you asked, I just tried it out: yes, it is a read/write property of Keyword class and it can be used to for your requirement with the same result as the approach I posted previously. Well, maybe, use the Keyword.Visible property would be more natural.
I do not know how your code used Keyword.Visible proerty that did not work for you, but it works for me with following simple code:
public class MyCommands { [CommandMethod("MyCmd", CommandFlags.Session)] public static void RunMyCommand() { Document dwg = Application.DocumentManager.MdiActiveDocument; Editor ed = dwg.Editor; PromptEntityOptions opt = new PromptEntityOptions("\nSelect an entity:"); opt.AllowNone = true; opt.Keywords.Add("Aaaa"); opt.Keywords.Add("Bbbb"); opt.Keywords.Add("Cccc"); opt.Keywords.Default = "Cccc"; opt.Keywords[2].Visible = false; PromptEntityResult res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) { ed.WriteMessage("\nSelected entity: {0}", res.ObjectId.ToString()); } else if (res.Status == PromptStatus.Keyword) { ed.WriteMessage("Keyword entered: {0}", res.StringResult); } else { ed.WriteMessage("\n*Cancell*"); } ed.WriteMessage("\nMyCmd executed"); Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt(); } }
When the command is invoked, the command line text looks like
As you can see, the keyword "Cccc" is not listed in the keyword list, but it still can be the defalt keyword.
Norman Yuan
Thaks again Norman. I found why it was not working for me, I was using "Keywords.Item("Cccc")" instead of "Keywords.Item(2)".