GetKeywords and '-' in the keyword

GetKeywords and '-' in the keyword

antti.kujala
Contributor Contributor
1,431 Views
10 Replies
Message 1 of 11

GetKeywords and '-' in the keyword

antti.kujala
Contributor
Contributor

Hi, 

 

I am using GetKeyWords() like this:

Snippet

var pko = new PromptKeywordOptions("Define projection:") {
                    AllowArbitraryInput = true
                };

                foreach (var k in new[] { "X""Y""Z""-X""-Y""-Z" })
                    pko.Keywords.Add(k);

                var projection = Utils.Editor.GetKeywords(pko);

 

When I select any of key words starting with '-' the prompt result status is PromptStatus.OK, but the result itself is an empty string. So what is going on? I have understood that there is some special handling for certain characters, but is the '-' one of the special characters.

 

How I use GetKeyWords() with strings starting with '-' and where to get the documentation explaining how the get keywords really works ?

 

 

br,

Antti

 

 

0 Likes
1,432 Views
10 Replies
Replies (10)
Message 2 of 11

SENL1362
Advisor
Advisor

What AutoCAD version.

This is the result for AutoCAD 2016:

Screenshot_1.png

 

 

       [CommandMethod("GetKW")]
        public static void GetKeyword()
        {
            Document doc = null;
            Database db = null;
            Editor ed = null;


            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc == null)
                    throw new System.Exception("No MdiActiveDocument");
                db = doc.Database;
                ed = doc.Editor;

                var pko = new PromptKeywordOptions("Define projection:");
                pko.AllowArbitraryInput = true;
      

                foreach (var k in new[] { "X", "Y", "Z", "-X", "-Y", "-Z" })
                    pko.Keywords.Add(k);

                var pr = ed.GetKeywords(pko);
                if (pr.Status != PromptStatus.OK)
                    return;

                ed.WriteMessage("\n Keyword=" + pr.StringResult);


            }
            catch (System.Exception ex)
            {
                if (ed != null)
                    ed.WriteMessage("\n Error in GetKeyword: {0}", ex.Message);
            }
        }
0 Likes
Message 3 of 11

antti.kujala
Contributor
Contributor

Hi, 

 

Autocad version is 2016. I double checked the prompt result and still the result is the same, I get empty string for all keywords starting with '-'.

If I change keywords for example:

new[] { "X", "Y", "Z", "MINUSX", "MINUSY", "MINUSZ" }

then I get the correct values from the promp result.

 

 

 

 

 

0 Likes
Message 4 of 11

SENL1362
Advisor
Advisor
also when you use:
ed.GetKeywords(pko);
instead of
Utils.Editor.GetKeywords(pko);
0 Likes
Message 5 of 11

oneMSN
Advocate
Advocate

Did you ever find a resolution to this issue, I have a similar issue in that the stringResult is truncated after the '-'?

 

 

Message 6 of 11

oneMSN
Advocate
Advocate

Sadly no,  I can't remember why I was doing this and I have searched my repos for the code and cant find it.  Either I did something else to work round it or abandoned what I was doing.

0 Likes
Message 7 of 11

Ed__Jobe
Mentor
Mentor

Have you tried preceding the dash with the escape char "\"?

foreach (var k in new[] { "X", "Y", "Z", "\-X", "\-Y", "\-Z" })

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 8 of 11

kerry_w_brown
Advisor
Advisor

For reference:

in ac2025 

using an escape char "\"  generates a compiler error :: Error CS1009 Unrecognized escape sequence

 

2024-03-28_09-38-36.jpg

Using the code as posted by @SENL1362 in post 2  works as expected.

 

    foreach (var k in new[] { "X", "Y", "Z", "-X", "-Y", "-Z" })
        pko.Keywords.Add(k);
    var pr = ed.GetKeywords(pko);
    if (pr.Status != PromptStatus.OK)
        return;
    ed.WriteMessage("\n Keyword= " + pr.StringResult);

 

2024-03-28_09-34-36.jpg

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 9 of 11

Ed__Jobe
Mentor
Mentor

@kerry_w_brown wrote:

For reference:

in ac2025 

using an escape char "\"  generates a compiler error :: Error CS1009 Unrecognized escape sequence


Thanks for testing Kerry. I didn't have time. Is the backslash being replaced then?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 10 of 11

Ed__Jobe
Mentor
Mentor

@oneMSN wrote:

Did you ever find a resolution to this issue, I have a similar issue in that the stringResult is truncated after the '-'?


Since @kerry_w_brown  tested @SENL1362 code and it worked, you should show your code that is giving you a problem. What version of acad? VS?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 11 of 11

kerry_w_brown
Advisor
Advisor

@Ed__Jobe wrote:

@kerry_w_brown wrote:

For reference:

in ac2025 

using an escape char "\"  generates a compiler error :: Error CS1009 Unrecognized escape sequence


Thanks for testing Kerry. I didn't have time. Is the backslash being replaced then?


 

using a backslash  "\-X" won't compile

using a doublebackslash  "\\-X" displays '\-X' and returns '-X'

I have no inkling about the inner workings of the Editor.GetKeywords()  method

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes