.NET

Reply
Distinguished Contributor
HJohn1
Posts: 169
Registered: ‎09-26-2008
Message 1 of 5 (546 Views)
Accepted Solution

PromptEntityOptions Help

546 Views, 4 Replies
05-24-2013 10:06 AM

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.

*Expert Elite*
norman.yuan
Posts: 968
Registered: ‎04-27-2009
Message 2 of 5 (524 Views)

Re: PromptEntityOptions Help

05-24-2013 01:04 PM in reply to: HJohn1

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();
    }
}

 

Distinguished Contributor
HJohn1
Posts: 169
Registered: ‎09-26-2008
Message 3 of 5 (482 Views)

Re: PromptEntityOptions Help

05-27-2013 10:36 AM in reply to: 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.

*Expert Elite*
norman.yuan
Posts: 968
Registered: ‎04-27-2009
Message 4 of 5 (471 Views)

Re: PromptEntityOptions Help

05-27-2013 12:22 PM in reply to: HJohn1

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

 

Untitled.png

 

As you can see, the keyword "Cccc" is not listed in the keyword list, but it still can be the defalt keyword.

 

Distinguished Contributor
HJohn1
Posts: 169
Registered: ‎09-26-2008
Message 5 of 5 (463 Views)

Re: PromptEntityOptions Help

05-27-2013 02:27 PM in reply to: 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)". 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community