Jump to content

Using VB.Net and Autocad Electrical


bigr1822

Recommended Posts

Team,

 

I am trying to write a command method in VB.Net for AutoCAD Electrical.  I want to open a series of drawing and change the text font to a ttf.  The code below works as follows:

 

  • user enters CHANGEPROJECTTEXTSTYLEFONT in the command manager
  • a userform pops up
  • user selects a button on userform
  • a folderdialog comes up, they select a folder
  • all DWG files are put into an array
  • Autocad opens file, one at a time, changes font

 

Here is where my problem is.  I can't figure out how to save and close the file, before the next one gets open.  Right after I use the Commit, I try to execute CloseAndSave(acDoc, acDoc.Name) and this gives me an error.  Any ideas what I am doing wrong?

 

image.thumb.png.669edfff4a99815e08190d3a68b52821.png

 

 <CommandMethod("ChangeProjectTextStyleFont")> Public Sub ChangeProjectTextStyleFont()

        ' This stack stores the directories to process.
        Dim stackElec As New Stack(Of String)
        Dim resultElec As New List(Of String)


        Dim frmDialog As New frmFolderBrowserDialog
        frmDialog.ShowDialog()

        ' Add the initial directory
        stackElec.Push(frmFolderBrowserDialog.locationOfProj)

        Dim i As Integer = 0
        ' Continue processing for each stacked directory
        Do While (stackElec.Count > 0)
            ' Get top directory string
            Dim dirElec As String = stackElec.Pop
            Try
                ' Add all immediate file paths
                resultElec.AddRange(Directory.GetFiles(dirElec, "*.dwg"))

                ' Loop through all subdirectories and add them to the stack.
                Dim directoryName As String
                For Each directoryName In Directory.GetDirectories(dirElec)
                    stackElec.Push(directoryName)
                Next
            Catch ex As Exception

            End Try
        Loop

        For Each drawingFile As String In resultElec

            Dim strFileName As String = drawingFile
            Dim acDocMgr As DocumentCollection = Application.DocumentManager


            Dim acDoc As Document = DocumentCollectionExtension.Open(acDocMgr, strFileName, False)
            Dim acCurDb As Database = acDoc.Database

            '' Get the current document and database, and start a transaction
            'Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Application.DocumentManager.MdiActiveDocument = acDoc
            System.Windows.Forms.Application.DoEvents()

            '' Lock the new document
            Using acLckDoc As DocumentLock = acDoc.LockDocument()

                Using tr As Transaction = acCurDb.TransactionManager.StartTransaction()

                    Dim tsTbl As TextStyleTable = TryCast(tr.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead), TextStyleTable)

                    For Each objId As ObjectId In tsTbl
                        Dim tsTblRec As TextStyleTableRecord = TryCast(tr.GetObject(objId, OpenMode.ForWrite), TextStyleTableRecord)

                        Dim textStyleName As String = tsTblRec.Name

                        '' Get the current font settings
                        Dim acFont As Autodesk.AutoCAD.GraphicsInterface.FontDescriptor
                        acFont = tsTblRec.Font

                        '' Update the text style's typeface with "PlayBill"
                        Dim acNewFont As Autodesk.AutoCAD.GraphicsInterface.FontDescriptor
                        acNewFont = New Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("Times New Roman", False, False, acFont.CharacterSet, acFont.PitchAndFamily)

                        tsTblRec.Font = acNewFont
                        acDoc.Editor.Regen()
                        System.Windows.Forms.Application.DoEvents()
                    Next

                    '' Saves the changes to the database and closes the transaction
                    tr.Commit()

                    CloseAndSave(acDoc, acDoc.Name)
                End Using
            End Using
        Next
    End Sub

image.png

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...