AutoCad 2012...when using db.ReadDwgFile(MyDwg, IO.FileShare.ReadWrite, False, "")
to change Attribute Values the Attribute will sometimes move if i open the Drawing and run a audit on the drawing and regen then it moves back to is correct location
but if i use Dim NewDoc As Document = Application.DocumentManager.Open(DocumentName, false) and run the same code there is no problem.
so my question is why does it do that?
Sub SetAttributeValues(ByVal BlockReferenceID As ObjectId, ByVal AttributeTagsValues As Dictionary(Of String, String)) Dim DwgEditor As Editor = TryCast(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor, Editor)
Using myTrans As Transaction = BlockReferenceID.Database.TransactionManager.StartTransaction Try Dim myBRef As BlockReference = TryCast(BlockReferenceID.GetObject(OpenMode.ForRead), BlockReference) Dim myAttCollection As AttributeCollection = TryCast(myBRef.AttributeCollection, AttributeCollection) For Each myAttRefID As ObjectId In myAttCollection Dim myAttRef As AttributeReference = TryCast(myAttRefID.GetObject(OpenMode.ForWrite), AttributeReference) If AttributeTagsValues.ContainsKey(myAttRef.Tag) Then myAttRef.TextString = AttributeTagsValues(myAttRef.Tag) End If Next myTrans.Commit() Catch ex As Autodesk.AutoCAD.Runtime.Exception DwgEditor.WriteMessage(ex.Message + vbLf + ex.StackTrace) myTrans.Abort() End Try End Using End Sub
and it only seems to do that on Attributes that are set to justificated = center
Solved! Go to Solution.
Hi,
You are missing code to adjust the block attribute position after changing its value: AttributeReference.AdjustAlignment(db)
Try this code:
Public Sub SetAttributeValues(blockReferenceID As ObjectId, attributeTagsValues As Dictionary(Of String, String)) Dim dwgEditor As Editor = TryCast(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor, Editor) Dim db As Database = blockReferenceID.Database Dim workDb As Database Using myTrans As Transaction = db.TransactionManager.StartTransaction() Try Dim myBRef As BlockReference = TryCast(blockReferenceID.GetObject(OpenMode.ForRead), BlockReference) If myBRef IsNot Nothing Then Dim myAttCollection As AttributeCollection = myBRef.AttributeCollection For Each myAttRefID As ObjectId In myAttCollection Dim myAttRef As AttributeReference = DirectCast(myAttRefID.GetObject(OpenMode.ForWrite), AttributeReference) If attributeTagsValues.ContainsKey(myAttRef.Tag) Then myAttRef.TextString = attributeTagsValues(myAttRef.Tag) ' Adjust alignment of this attribute workDb = HostApplicationServices.WorkingDatabase HostApplicationServices.WorkingDatabase = db myAttRef.AdjustAlignment(db) HostApplicationServices.WorkingDatabase = workDb End If Next End If myTrans.Commit() Catch ex As Autodesk.AutoCAD.Runtime.Exception dwgEditor.WriteMessage(ex.Message + vbLf + ex.StackTrace) myTrans.Abort() End Try End Using End Sub
-Khoa
Can't find what you're looking for? Ask the community or share your knowledge.