• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Posts: 67
    Registered: ‎06-22-2007
    Accepted Solution

    Attribute will sometimes move when changing it value

    163 Views, 3 Replies
    10-22-2012 06:22 AM

    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

    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: Attribute will sometimes move when changing it value

    10-22-2012 07:15 AM in reply to: wowens63

    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

    Please use plain text.
    Valued Contributor
    Posts: 67
    Registered: ‎06-22-2007

    Re: Attribute will sometimes move when changing it value

    10-22-2012 07:52 AM in reply to: khoa.ho

    thank you

    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: Attribute will sometimes move when changing it value

    10-22-2012 08:29 AM in reply to: wowens63

    You are welcome.

    -Khoa

    Please use plain text.