setting multi text heights in an mtext object

setting multi text heights in an mtext object

a.kouchakzadeh
Advocate Advocate
1,216 Views
4 Replies
Message 1 of 5

setting multi text heights in an mtext object

a.kouchakzadeh
Advocate
Advocate

Hello every one

Norman, Ed big thumbs up to you guys for yer previous help 

 

I was wondering how is it possible to set multi text heights in a single MTEXT object?

This is the code I got:

 

Dim gn As AcadMText
Dim gx As String
Dim sx As String

Dim fx as string
Dim width As Integer
width = 23812
Dim Insp(2) As Double
Insp(0) = 38271
Insp(1) = 4460

sx = "\B\LThis is\b \l an example"
gx = vbNewLine & vbNewLine & "Hello \Bworld"
fx = sx & gx
Set gn = ThisDrawing.ModelSpace.AddMText(Insp, width, fx)
gn.Layer = "details"
gn.StyleName = "nsd-details txt"
gn.Height = 317

 

Problem is, I want sx text height to be 100

and gx to be 317

any ideas Norman and Ed?

PS: \B and \L makes the text Bold and Underlined. really cool

@norman.yuan 

@Ed__Jobe 

0 Likes
Accepted solutions (1)
1,217 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

AFAIK, there is no direct way (i.e. no API) to change the size (text height) of a portion of the text in a MTEXT. You would need to directly manipulate the markups of the MText content, which are not officially documented. 

 

A way to try it is to manually edit a MTEXT, say, selecting portion of the MText, change it size/height, and then examine the content property in Properties window.

 

The picture below shows what the MText content would be when a portion of text in a MText is set to different height (much bigger, in this case):

Set_MTEXT_HEIGHT.png

 

As you can see, the MText's TextHeight property is 2.5, so text in the MText should be 2.5 high. The portion of text circled/selected is set to be 12 in height. That results in the Content property being updated with markup

 

{\H4.8x;[The Selected Text]}

 

As I said, the MText markups are not documented officially, but we can very well guess here that "\H4.8x;" here means the selected text in the curly bracket should change its height 4.8 times of the MText's TextHeight property (2.5 x 4.8 = 12).

 

So, in your case, you could try to manipulate the MText's Content property in similar way to achieve what you want.

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

a.kouchakzadeh
Advocate
Advocate

thanks for the reply sir.

my intention is creating a long MText (general notes for a plan), which is a gonna be alot of trouble if have to make this change manually. 

another thing which came across my head is to create separated mtexts and at the end, make them a single m text object.
is this possible to merge different mtexts using VBA?

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

I do not see there is way to "merge" 2 MText entities into one to achieve what you want to do. 

 

However, if your goal is to set the text height of portion of the MText's text string, it is not difficult to do it in code as my previous reply suggested. I thought my previous reply has enough hint to write some code. Now that you asked again, here is the code sample of doing what I suggested:

 

Let's say, there is a MText entity, its text string contains "CONTENT", and I want to emphasize it by making its higher much higher than other. The MText's Height property is 2.5 (i.e. the default text height of the text string in the MText is 2.5). Now I want only the word "CONTENT" in the MText's text string to be 13.0 high. See the quite simple code below:

 

Option Explicit

Public Sub SetMTextHeight()

    Dim ent As AcadEntity
    Dim pt As Variant
    Dim mtxt As AcadMText
    
    ''On Error Resume Next
    
    ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Select an MText entity:"
    If Not ent Is Nothing And TypeOf ent Is AcadMText Then
        Set mtxt = ent
        UpdateMText mtxt, "CONTENT", 13#
    End If
    
End Sub

Private Sub UpdateMText(mtext As AcadMText, TextToChange As String, textHeight As Double)

    Dim content As String
    Dim i As Integer
    
    Dim hFactor As Double
    hFactor = textHeight / mtext.Height
    
    content = mtext.TextString
    i = InStr(1, content, "{\H", vbTextCompare)
    
    If i = 0 Then
        content = Replace(content, "CONTENT", "{\H" & Format(hFactor, "##0.0") & "x;CONTENT}")
    End If
    
    mtext.TextString = content
    mtext.Update

End Sub

 

See the result of running the code in the video clip below:

 

HTH
 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

a.kouchakzadeh
Advocate
Advocate

that was awesome

thank you very much sir

0 Likes