Help Changing Text Styles

Help Changing Text Styles

carlosrdrzalns
Advocate Advocate
1,101 Views
2 Replies
Message 1 of 3

Help Changing Text Styles

carlosrdrzalns
Advocate
Advocate

Hi, 

 

I'm working on a Simple program to change all text in a dwg file to a diferent text style based on the current scale of model Space. The text style comes with the prefix "TfL_m1-" and then the scale number. The program does work but it only change the style name, and I need to change also its properties. 

 

The code is as following:

 

 <CommandMethod("ChangeTextStyle")>
    Public Sub SetTextStyle()
        Dim TextDic As New Dictionary(Of String, TextStyleTableRecord)
        Using tr As Transaction = m_Database.TransactionManager.StartTransaction()

            '' Get the current Annotation Scale on the drawing

            Dim scale As String = m_Database.Cannoscale.Name
            Dim subscale As String = scale.Substring(scale.IndexOf(":") + 1, scale.IndexOf("(") - scale.IndexOf(":") - 1)



            '' Create a dictionary with the text styles and names
            Dim acTextStyleTable As TextStyleTable = tr.GetObject(m_Database.TextStyleTableId, OpenMode.ForRead)
            ed.WriteMessage("la escala actual del modelSpace es:" + scale + vbCrLf)
            ed.WriteMessage("el valor que nos atañe es: " + subscale + vbCrLf)
            For Each id As ObjectId In acTextStyleTable
                Dim Symbol As TextStyleTableRecord = tr.GetObject(id, OpenMode.ForRead)
                TextDic.Add(Symbol.Name, Symbol)
                'ed.WriteMessage("estilo de texto: " + Symbol.Name + vbCrLf)
            Next

            Dim keys As Dictionary(Of String, TextStyleTableRecord).KeyCollection = TextDic.Keys


            Dim stri As String = "TfL_m1-" + subscale

            ed.WriteMessage("el valor clave del diccionario sería: " + stri + " cuya longitud es: " + Str(stri.Length) + vbCrLf)

            ed.WriteMessage("La escala a aplicar en este dibujo seria: {0} con un valor de id : {1}", stri, TextDic(stri).ToString + vbCrLf)







            'Search for all text object in the current model space

            Dim model = DirectCast(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(m_Database), OpenMode.ForWrite), BlockTableRecord)



            For Each obj As ObjectId In model
                Select Case obj.ObjectClass.DxfName
                    Case "TEXT"
                        ed.WriteMessage("Hola")
                        Dim tx As DBText = tr.GetObject(obj, OpenMode.ForWrite)
                        tx.TextStyleId = TextDic(stri).ObjectId


                    Case "MTEXT"
                        ed.WriteMessage("Hola")
                        Dim tx As MText = tr.GetObject(obj, OpenMode.ForWrite)
                        tx.TextStyleId = TextDic(stri).ObjectId

                    Case Else
                        ed.WriteMessage("Not he encontrado ninguna entidad Decimal este tipo")
                        Exit Select
                End Select

            Next

            tr.Commit()

            ed.Regen()
        End Using
    End Sub

I would be very gratefull if someone could explain me where i am wrong. 

 

regards, 

 

Carlos.

0 Likes
Accepted solutions (1)
1,102 Views
2 Replies
Replies (2)
Message 2 of 3

ddetennis
Participant
Participant
Accepted solution

I found the same results as you, so instead I change the properties individually while also changing the text style. I've also opted to use a single annotative text style where possible, and then I apply a new annotative scale to the text with this method:

 

private static void SetAnnotationScale(Database db, Entity obj, string scale)
{
    obj.Annotative = AnnotativeStates.True;
    var occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
    ObjectContexts.AddContext(obj, occ.GetContext(scale));
    ObjectContexts.RemoveContext(obj, occ.GetContext("1:1"));
}

 

(Sorry, this is C# instead of VB, but it hopefully it's fairly simple to convert.)

0 Likes
Message 3 of 3

carlosrdrzalns
Advocate
Advocate

Thank you @ddetnnis, that is a solution i also thought about but the problem is that, as i change the text style based on the model space scale, i do not know before hand the characteristics that my Text need, so it is kind of dificult to solve it that way. Maybe with a pre-established collection of object containing the data based on the scale could work.

Thanks anyways.

0 Likes