tab when the text has a different size

tab when the text has a different size

Anonymous
Not applicable
593 Views
6 Replies
Message 1 of 7

tab when the text has a different size

Anonymous
Not applicable

Hello,

 

I want to insert a Text, I almost succeeded in doing so, but since sometimes my text is a different size, they don't line up.

I enclose what I have, as well as my code

 

devranakdogan_0-1617211858844.png

 [CommandMethod("TABULATION")]
        public static void Tab()
        {
            CommandTemplates.ActionCall((Document doc, Editor ed, Database db, Transaction tr) =>
            {
                Dictionary<string, int> ItemList = new Dictionary<string, int>();
                var T1 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "1",
                    ["BETA"] = "2",
                    ["OMEGA"] = "3"
                };
                var T2 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "aaaaaaaaaaaaaaaaaaaaaaaaaa",
                    ["BETA"] = "bbbbbbbb",
                    ["OMEGA"] = "ccc"
                };
                var T3 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "they are not in the same colone",
                    ["BETA"] = "they are not in the same colone",
                    ["OMEGA"] = "they are not in the same colone"
                };
                var mtext = new MText();
                mtext.Contents = string.Join("\\P", T1.Select(pair => pair.Key + "\t"+ T2[pair.Key] + "\t" + T3[pair.Key]));
                var A = mtext.Contents.ToString();
                CreateMText(A, ed); 
            });
        }
0 Likes
594 Views
6 Replies
Replies (6)
Message 2 of 7

essam-salah
Collaborator
Collaborator

Hi @Anonymous 

try use string.PadRight() / string.PadLeft to align text Left/Right

0 Likes
Message 3 of 7

essam-salah
Collaborator
Collaborator

@Anonymous 

try this:

// Columns
                var T1 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "1",
                    ["BETA"] = "2",
                    ["OMEGA"] = "3"
                };
                var T2 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "aaaaaaaaaaaaaaaaaaaaaaaaaa",
                    ["BETA"] = "bbbbbbbb",
                    ["OMEGA"] = "ccc"
                };
                var T3 = new Dictionary<string, string>()
                {
                    ["ALPHA"] = "they are not in the same colone",
                    ["BETA"] = "they are not in the same colone",
                    ["OMEGA"] = "they are not in the same colone"
                };

                var WidthArr = new int[]
                {
                    T1.Values.Select(s => s.Length).Max(),
                    T2.Values.Select(s => s.Length).Max(),
                    T3.Values.Select(s => s.Length).Max(),
                };
                var table = string.Join("\n", T1.Keys.Select(key =>
                    $"{T1[key].PadRight(WidthArr[0])}\t" +
                    $"{T2[key].PadRight(WidthArr[1])}\t" +
                    $"{T3[key].PadRight(WidthArr[2])}"));
0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

I think the idea of trying to display single text string in a table/column style is bad idea: most text fonts do not have the fixed width for all characters, so unless your entire text string only uses a SINGLE character, there is no way to guarantee text entity's width of certain count of characters, whether you use "\t", or pad the string (left or right).

 

You might want to explain why you have to use a single MTEXT to display text string in a table/column style? Why do you not use multiple text entities so that you can position/align them what ever way you want to? Or you can define a block as a table row with attributes at each column position.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

Anonymous
Not applicable

Hello @norman.yuan  & @essam-salah

 

thank you both, you helped me a lot

 

i have make this 

mtext.Contents = string.Join("\\P", ItemList.Select(pair =>"\\pxt10, 48, 63;"  + pair.Key + "\t" + Designation[pair.Key] + "\t" + pair.Value + "\t" + Certificat[pair.Key] ));

But now, i want to modify the space between two lines. (modify my "\\P" value) 

 

thanks a lot ! 

0 Likes
Message 6 of 7

essam-salah
Collaborator
Collaborator

@Anonymous wrote:

i want to modify the space between two lines. (modify my "\\P" value) 

 


I don't Know what "\\P" means, but you can add new line by "\n"  or Environment.NewLine

0 Likes
Message 7 of 7

norman.yuan
Mentor
Mentor

I am still skeptical on why you need to show data of rows/columns with a single MText entity. But for changing the space between lines of a MText, you can look into MText's properties: LineSpaceDistance, LineSpaceFactor and LineSpaceStyle.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes