Attribute alignment

Attribute alignment

WPerciful
Advocate Advocate
562 Views
2 Replies
Message 1 of 3

Attribute alignment

WPerciful
Advocate
Advocate

Despite having "att.AdjustAlignment(db)", the text alignment is off once the values have been updated.  I'm not sure why "att.AdjustAlignment(db)" isn't working.  Please help.

 

 

    Public Sub UpdateAecFileAttributes(ByVal Directory_DrawingName As String, ByVal PassedBlkName As String, ByVal dtTagValue As Data.DataTable)
        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim db As New Database '= New Database(False, True)
        Dim wdb As Database = HostApplicationServices.WorkingDatabase
        Dim blkref = RXObject.GetClass(GetType(BlockReference))
        Dim blk As BlockReference
        AecFullFileName = Directory_DrawingName
        AecFilePath = IO.Path.GetDirectoryName(AecFullFileName)
        AecFileName = IO.Path.GetFileNameWithoutExtension(AecFullFileName)
        Using db
            Try
                db.ReadDwgFile(AecFullFileName, FileOpenMode.OpenForReadAndAllShare, False, Nothing)
            Catch __unusedException1__ As System.Exception
                ed.WriteMessage(vbLf & "Unable To read drawing file.")
            End Try
            Using tran = db.TransactionManager.StartTransaction()
                Dim loDic As DBDictionary = CType(tran.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite, False), DBDictionary)
                For Each entry As DBDictionaryEntry In loDic
                    Dim layout = CType(tran.GetObject(entry.Value, OpenMode.ForWrite), Layout)
                    Dim btr As BlockTableRecord = CType(tran.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite), BlockTableRecord)
                    For Each objID As ObjectId In btr
                        If objID.ObjectClass.IsDerivedFrom(blkref) Then
                            blk = CType(tran.GetObject(objID, OpenMode.ForWrite), BlockReference)
                            If Not blk.BlockTableRecord.IsNull And blk IsNot Nothing Then
                                Dim attCol = blk.AttributeCollection
                                Dim blkName As String
                                If blk.IsDynamicBlock Then
                                    blkName = FetchEffectiveName(blk)
                                Else
                                    blkName = blk.Name
                                End If
                                If blkName = PassedBlkName Then
                                    If attCol IsNot Nothing Then
                                        For Each attobjId As ObjectId In attCol
                                            Dim att As AttributeReference = CType(tran.GetObject(attobjId, OpenMode.ForWrite), AttributeReference)
                                            Dim attTag As String = att.Tag
                                            For i As Integer = 0 To dtTagValue.Rows.Count - 1
                                                If attTag = dtTagValue.Rows(i)(0) Then
                                                    att.TextString = dtTagValue.Rows(i)(1)
                                                End If
                                            Next
                                            att.AdjustAlignment(db)
                                            HostApplicationServices.WorkingDatabase = wdb
                                        Next
                                    End If
                                End If
                            End If
                        End If
                    Next
                Next
                tran.Commit()
            End Using 'tran
            Try
                db.SaveAs(AecFullFileName, DwgVersion.Current)
                ed.WriteMessage(vbLf & AecFileName & " saved.")
            Catch __unusedException1__ As System.Exception
                ed.WriteMessage(vbLf & "Unable To save.")
            End Try
        End Using 'db
    End Sub

 



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

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

norman.yuan
Mentor
Mentor
Accepted solution

You really need to carefully go through your code as @jabowabo suggested in your other post.

 

1. You DID not set the newly opened Database as HostApplicationServices.WorkingDatabase (even you did save the current WorkingDatabase), that is the reason the Attributes are not aligned. That is you need add this line before you start a Transaction with the new database:

 

HostApplicationServices.WorkingDatabase = db

 

2. You restore the WorkingDatabase (assume you did set the new database as WorkingDatabase for your work, which you did not) in WRONG PLACE - in the nested "for..." loop - meaning, after one attribute is updated, the WorkDatabase is restored back to original one. You should only restore the WorkingDatabase when you are done with the new databaee: right before the new database is to be disposed.

 

3. The Try...Catch surrounded db.ReadDwgFile() does not make sense: if reading exception is caught, your code still continue and would crash, of course. The ...Catch clause should be at the end of the Transaction (End Using 'tran).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

jabowabo
Mentor
Mentor

FWIW, I'll add one thing to @norman.yuan 's post - If you make the switch to C# from VB.NET, you are likely to progress much faster in your coding as you'll get more help and examples.

 

I learned this myself a few years ago when I started with VB.NET. Almost everyone who is knowledgeable and provides help on this forum and others use C# over VB.NET. Generally, you'll get better and faster help for these kinds of issues. There's a lot more example code in c# as well. The learning curve is not that bad, and the sooner you do it the better off you'll be, IMO.

0 Likes