Shared Sub ChangeMTextAttributeTeste()
Dim ed As AcEd.Editor =
AcAp.Application.DocumentManager.MdiActiveDocument.Editor
Dim opt As New AcEd.PromptEntityOptions(vbLf & "Select BlockReference
to modify")
Dim res As AcEd.PromptEntityResult = ed.GetEntity(opt)
If res.Status <> AcEd.PromptStatus.OK Then Exit Sub
Dim idb As AcDb.ObjectId = res.ObjectId
Dim decimals As Integer = 2
Dim lunits As Integer =
Autodesk.AutoCAD.Runtime.DistanceUnitFormat.Current
Dim db As AcDb.Database =
Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase
Using tr As AcDb.Transaction = db.TransactionManager.StartTransaction
Dim ent As AcDb.Entity = DirectCast(tr.GetObject(idb,
AcDb.OpenMode.ForRead, False, True), AcDb.Entity)
If ent.GetType.Name <> "BlockReference" Then Exit Sub
Dim ref As AcDb.BlockReference = _
DirectCast(tr.GetObject(idb, AcDb.OpenMode.ForRead),
AcDb.BlockReference)
Dim btr As AcDb.BlockTableRecord = _
DirectCast(tr.GetObject(ref.BlockTableRecord,
AcDb.OpenMode.ForRead, False), AcDb.BlockTableRecord)
If Not btr.HasAttributeDefinitions Then Exit Sub
For Each id As AcDb.ObjectId In btr
ent = tr.GetObject(id, AcDb.OpenMode.ForRead)
If ent.GetType.Name = "AttributeDefinition" Then
Dim att As AcDb.AttributeDefinition = tr.GetObject(id,
AcDb.OpenMode.ForRead)
If att.Tag = "ElevationHeight" AndAlso ent.GetType.Name =
"MText" Then
Dim dst As String = CDbl(att.TextString)
Dim dss As String = att.TextString
Dim resd As AcEd.PromptDoubleResult = Nothing
Dim optd As New AcEd.PromptDoubleOptions("")
optd.AllowNone = False
optd.UseDefaultValue = True
optd.Message = vbLf & "New value"
optd.DefaultValue = _
AcRx.Converter.DistanceToString(dst, lunits, decimals)
resd = ed.GetDouble(optd)
If resd.Status =
Autodesk.AutoCAD.EditorInput.PromptStatus.OK Then
dss =
Autodesk.AutoCAD.Runtime.Converter.DistanceToString(resd.Value, lunits,
decimals)
att.UpgradeOpen()
att.TextString = resd.Value
Dim mtext As AcDb.MText = tr.GetObject(ent.ObjectId,
AcDb.OpenMode.ForWrite)
mtext.Contents = "{\L" & dss & "}"
RegenLayer(mtext.LayerId)
End If
End If
End If
Next
tr.Commit()
End Using
End Sub
Shared Sub RegenLayer(ByVal layerID As AcDb.ObjectId)
' Because AutoCAD 2006 VB.NET do not have regen
Dim layerIDs(0) As AcDb.ObjectId
layerIDs(0) = layerID
AcIn.LayerUtilities.RegenLayers(layerIDs,
Autodesk.AutoCAD.Internal.LayerUtilities.RegenPending)
End Sub
escreveu na mensagem news:5805654@discussion.autodesk.com...
OK, I've figured it out.
The main problem was that I couldn't access
AttributeReference.MTextAttribute.Contents directly and had to use
AttributeReference.TextString.
When I added the switch \P to AttributeReference.TextString and then added
any other text the switch was removed automatically.
So what I did was construct an entire string and then set
AttributeReference.TextString to that string and it works fine.
'Declare a string to hold the text contents
Dim textContents As String = ""
'Set the attribute text
For noteIndex As Integer = lbound To ubound
_
Step loopStep
'Add a line break if necessary
If textContents.Trim.Length > 0 Then
'Add a line break
textContents += "\P" &
noteText.GetByIndex(noteIndex)
Else
textContents =
noteText.GetByIndex(noteIndex)
End If
Next
'Set the MText
If attRef.IsMTextAttribute = True Then
'Set the text
attRef.TextString = textContents
'Update the MText attribute
attRef.UpdateMTextAttribute()