.NET

Reply
Valued Contributor
wowens63
Posts: 98
Registered: ‎06-22-2007
Message 1 of 4 (261 Views)
Accepted Solution

Attribute will sometimes move when changing it value

261 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

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

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 2 of 4 (251 Views)

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

Valued Contributor
wowens63
Posts: 98
Registered: ‎06-22-2007
Message 3 of 4 (243 Views)

Re: Attribute will sometimes move when changing it value

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

thank you

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 4 of 4 (228 Views)

Re: Attribute will sometimes move when changing it value

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

You are welcome.

-Khoa

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!