.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MText as AttributeReference

9 REPLIES 9
Reply
Message 1 of 10
Guy Lowndes
2410 Views, 9 Replies

MText as AttributeReference

I've tried everything I can think of but I can't find out how to set an attribute reference to multi line text.

I'm inserting a block that is already created and the attribute defintion is multi text.

When I look at the IsMTextAttributeDefinition property of my attribute definition it is true but after I've set properties for the reference from the definition the reference still says false for IsMTextAttribute.

If I try to use UpdateMTextAttribute or set an MText object as the MTextAttribute then I get an error.

Can anyone point me in the right direction with this?
9 REPLIES 9
Message 2 of 10
Guy Lowndes
in reply to: Guy Lowndes

I finally figured it out.

I used AttributeReference.SetAttributeFromBlock, using the attribute definition and block reference that define the attribute reference in the arguments.

I'm struggling to set the MText contents property, I tried to use an MText object but I think I'm creating a memory problem, AutoCAD keeps crashing when I use this object.

I cannot access the MTextAttribute.Contents directly, I can access the TextString and that will set the MTextAttribute.Contents but only formatted for as single line regardles of whether I use /p or vbcrlf as a line break.

Does anyone have any experience on populating this property? Message was edited by: Guy Lowndes
Message 3 of 10
t.willey
in reply to: Guy Lowndes

Try using '\\P' to show a new line in mtext.
Message 4 of 10
jbooth
in reply to: Guy Lowndes

The AcDb.MText object accepts RTF (rich text file) input. In RTF format, the string "\line " denotes a new line (note the space).

When writing my own wrapper class for the MText object, I had to replace all instances of Envorinment.Newline with this string value.

It's possible you can use the same technique. It's most likely not the ideal solution, but it worked for me...
Message 5 of 10
Guy Lowndes
in reply to: Guy Lowndes

Thanks for the feed back.

I cannot change the value of AttributeReference.MTextAttribute.Contents directly, only by either using the AttributeReference.TextString property or by using AttributeReference.MTextAttribute=myMText.

I tried both options with AttributeReference.TextString, without success.

The "/line " option didn't work, the TextString property simply ripped out the '/l' and left me with 'ine' in the text.

The '\\p' option left me with '\p' in the MTextAttribute.Contents but the TextString stopped just before the switch and I didn't get the subsequent lines showing in the block in the drawing, even though MTextAttribute.Contents did show the additonal lines present when I used the Object Properties dialog box in AutoCAD.

When I use MTextAttribute=myMtext the application crashes when I try to quit it and I get a C+ virtual error message box.

I think it's something to with the way I manage the objects and I have to be honest this isn't something I'm hot at.

Here's some of my code:

'Declare an MText object
Dim attText As New MText
attText.SetPropertiesFrom(attRef.MTextAttribute)


'Set the attribute text
For noteIndex As Integer = lbound To ubound _
Step loopStep

'Add a line break if necessary
If attText.Contents.Trim.Length > 0 Then
'Add a line break
attText.Contents += "\\p"
End If

'Add the text
attText.Contents += noteText.GetByIndex(noteIndex)

Next

'Set the MText
If attRef.IsMTextAttribute = True Then

'Set the MText attribute
attRef.MTextAttribute=attText)

'Update the MText attribute
attRef.UpdateMTextAttribute()

End If

'Set the value of attRef2
attRef2 = attRef

'Dispose of the temporary MText
attText.Dispose()

I know it's a bit mixed up, it needs cleaning slightly slightly, but am I doing something fundamentally wrong?
Message 6 of 10
jbooth
in reply to: Guy Lowndes

Try a capitol P. I know for a fact that dimension text overrides will not accept a lower case P as an escape character. Mtext probably behaves the same way. Also, unless the object has a .SetContentsRTF() method, "/line" is almost guarenteed not to work.

One more thing, if you are using VB the double backslash is not required.

Example:
"Line 1\PLine 2"
Message 7 of 10
Anonymous
in reply to: Guy Lowndes

Doesn't "\n" work?

wrote in message news:5804871@discussion.autodesk.com...
Try a capitol P. I know for a fact that dimension text overrides will not
accept a lower case P as an escape character. Mtext probably behaves the
same way. Also, unless the object has a .SetContentsRTF() method, "/line" is
almost guarenteed not to work.

One more thing, if you are using VB the double backslash is not required.

Example:
"Line 1\PLine 2"
Message 8 of 10
Anonymous
in reply to: Guy Lowndes

Dim db As AcDb.Database = _
Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase()
Dim mtext As New Autodesk.AutoCAD.DatabaseServices.MText
mtext.Contents = "{First Line\PSecond Line}"
mtext.Location = New AcGe.Point3d(0, 0, 0)
mtext.TextHeight = 500
Dim id As AcDb.ObjectId
Using tr As AcDb.Transaction = db.TransactionManager.StartTransaction
Dim btr As AcDb.BlockTableRecord = _
DirectCast(tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite),
AcDb.BlockTableRecord)
id = btr.AppendEntity(mtext)
db.TransactionManager.AddNewlyCreatedDBObject(mtext, True)
tr.Commit()
End Using


"PeterG" escreveu na mensagem
news:5804921@discussion.autodesk.com...
Doesn't "\n" work?

wrote in message news:5804871@discussion.autodesk.com...
Try a capitol P. I know for a fact that dimension text overrides will not
accept a lower case P as an escape character. Mtext probably behaves the
same way. Also, unless the object has a .SetContentsRTF() method, "/line" is
almost guarenteed not to work.

One more thing, if you are using VB the double backslash is not required.

Example:
"Line 1\PLine 2"
Message 9 of 10
Guy Lowndes
in reply to: Guy Lowndes

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()
Message 10 of 10
Anonymous
in reply to: Guy Lowndes

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()

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost