Author Topic: Setting a Text Style Current and Making Updates  (Read 6436 times)

0 Members and 1 Guest are viewing this topic.

MGorecki

  • Guest
Setting a Text Style Current and Making Updates
« on: January 06, 2011, 01:19:53 PM »
Hello,
I have code that will bring in a text style from another drawing, but it is still raw in that a text hight needs to be set.  How can I find the text style in the style table and make it current, then change the text height?
I know I can change the text height using "myTextSyle = dwgTextHeight", but how do I get the style name (ROMANS) to be current?

Thanks,
Mark

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Setting a Text Style Current and Making Updates
« Reply #1 on: January 06, 2011, 01:25:12 PM »
Hi

Application.SetSystemVariable("TEXTSTYLE", yourTextStyle);
Speaking English as a French Frog

MGorecki

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #2 on: January 06, 2011, 02:54:47 PM »
Hello Gile,
Thanks, I'll give that a try.

Mark

MGorecki

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #3 on: January 06, 2011, 04:10:40 PM »
I tried what you said, but maybe I'm doing it wrong (I'm pretty new to VB.Net, so I wouldn't be surprised).
Here is my code:
Code: [Select]
    Public Sub UpdateTextStyle(ByVal dwgTextHeight As Double)
        Dim myDWG As ApplicationServices.Document
        Dim myDB As DatabaseServices.Database

        myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        myDB = myDWG.Database

        'Locate the text style "ROMANS" in the text style table
        Using styleTrans As Transaction = myDB.TransactionManager.StartTransaction
            Dim txtStyleTbl As TextStyleTable = myDB.TextStyleTableId.GetObject(OpenMode.ForRead)
            For Each objId As ObjectId In txtStyleTbl
                Dim txtStyleTableRec As TextStyleTableRecord = objId.GetObject(OpenMode.ForRead)
                If txtStyleTableRec.Name = "ROMANS" Then
                    Application.SetSystemVariable("TEXTSTYLE", "ROMANS")
                End If
            Next
            styleTrans.Commit()
            styleTrans.Dispose()
        End Using

        'Start a transaction
        Using myTrans As Transaction = myDB.TransactionManager.StartTransaction()
            'Open the current text style for write
            Dim myTSTR As TextStyleTableRecord
            myTSTR = myTrans.GetObject(myDB.Textstyle, OpenMode.ForWrite)
            myTSTR.TextSize = dwgTextHeight
            'Save the changes and dispose of the transaction
            myTrans.Commit()
            myTrans.Dispose()
        End Using
    End Sub

Can you see what I'm doing wrong?
Thanks

MGorecki

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #4 on: January 06, 2011, 04:30:45 PM »
Never mind, that code works.  I just made a dumb mistake.

kaefer

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #5 on: January 06, 2011, 05:12:40 PM »
Here is my code:
You aren't wrong, and I don't speak VB.  So just a couple of hints:

Quote
Code: [Select]
           For Each objId As ObjectId In txtStyleTbl
                Dim txtStyleTableRec As TextStyleTableRecord = objId.GetObject(OpenMode.ForRead)
                If txtStyleTableRec.Name = "ROMANS" Then
                    Application.SetSystemVariable("TEXTSTYLE", "ROMANS")
                End If
            Next
You could use the Has property instead. Database.Textstyle should be settable, too.
Code: [Select]
               If txtStyleTbl.Has("ROMANS") Then
                    myDB.Textstyle = txtStyleTbl("ROMANS")
                End If

Quote
Code: [Select]
           styleTrans.Dispose()
        End Using
The Using statement takes care of disposing, so you don't need to call Dispose(). It wouldn't even be called if there were an exception before it was reached; not so with the implicit Try-Finally of Using.

Cheers
« Last Edit: January 06, 2011, 05:21:32 PM by kaefer »

dan.glassman

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #6 on: January 06, 2011, 06:47:16 PM »
Since you are starting a transaction, you should use the Transaction.GetObject() method instead of the ObjectId.GetObject() method to open the table (which I see you are doing in your second transaction to change the text height).  Mixing ObjectId.GetObject() with transactions is considered harmful [might work okay in this case, but it's good practice to not mix the two.]

You can perform both operations in a single transaction.

Updated [with apologies if I'm getting VB.Net syntax incorrect]:

Code: [Select]
Using styleTrans As Transaction = myDB.TransactionManager.StartTransaction
    Dim txtStyleTbl As TextStyleTable = [color=red]styleTrans[/color].GetObject([color=red]myDb.TextStyleTableId[/color], OpenMode.ForRead)
    If txtStyleTbl.Has("ROMANS")
        Dim romansId as ObjectId = txtStyleTbl("ROMANS")
        Dim romans as TextStyleTableRecord = styleTrans.GetObject(romansId, OpenMode.ForWrite)
        romans.TextSize = dwgTextHeight
        myDb.Textstyle = romansId 'sets current text style -- an alternative to Application.SetSystemVariable
    End If
    styleTrans.Commit
End Using

Quote
Code: [Select]
Using styleTrans As Transaction = myDB.TransactionManager.StartTransaction
            Dim txtStyleTbl As TextStyleTable = myDB.TextStyleTableId.GetObject(OpenMode.ForRead)
            For Each objId As ObjectId In txtStyleTbl
                Dim txtStyleTableRec As TextStyleTableRecord = objId.GetObject(OpenMode.ForRead)
                If txtStyleTableRec.Name = "ROMANS" Then
                    Application.SetSystemVariable("TEXTSTYLE", "ROMANS")
                End If
            Next
End Using

fixo

  • Guest
Re: Setting a Text Style Current and Making Updates
« Reply #7 on: January 07, 2011, 02:47:38 AM »
Hi Dan
You're absolutely right
Welcome on board!
Cheers :)