Edit multiple lines attribute in dynamic block

Edit multiple lines attribute in dynamic block

fieldguy
Advisor Advisor
2,850 Views
4 Replies
Message 1 of 5

Edit multiple lines attribute in dynamic block

fieldguy
Advisor
Advisor

We use a block definition that includes custom properties and 2 attribute definitions.  1 of the attribute defs has "Multiple lines" = "Yes".  When the attribute is added, the textstring is set to "first line \n second line".  I test when the att def is created and IsMTextAttribute is true.

 

This all works until the attribute has to be edited.  The attref textstring does not contain "\n" or "\p".  Am I missing something?  

 

I tried attref.TextString and MText mt = attref.MTextAttribute.  Neither the textstring or the mt.Contents contain any newline character.

 

<edit> the second line is the area of a closed pline.  i need to edit the area if it changes.  I am using Civil 3D 2016

0 Likes
Accepted solutions (2)
2,851 Views
4 Replies
Replies (4)
Message 2 of 5

ActivistInvestor
Mentor
Mentor

@fieldguy wrote:

We use a block definition that includes custom properties and 2 attribute definitions.  1 of the attribute defs has "Multiple lines" = "Yes".  When the attribute is added, the textstring is set to "first line \n second line".  I test when the att def is created and IsMTextAttribute is true.

 

This all works until the attribute has to be edited.  The attref textstring does not contain "\n" or "\p".  Am I missing something?  

 

I tried attref.TextString and MText mt = attref.MTextAttribute.  Neither the textstring or the mt.Contents contain any newline character.

 

<edit> the second line is the area of a closed pline.  i need to edit the area if it changes.  I am using Civil 3D 2016


You need to get the value of the AttributeReference's MTextAttribute (which is an MText object), and use that to edit the text.

 

After you've edited the returned MText, you have to assign it back to the MTextAttribute property of the AttributeReference.

0 Likes
Message 3 of 5

fieldguy
Advisor
Advisor
Accepted solution

 

Thanks - I see what happened.  When I created the attref I used attref.TextString instead of attref.MTextAttribute.

 

To create the attref value:

string attribtext = "line 1\nline 2";

MText attval = attref.MtextAtttribute;

attval.Contents = attribtext;

attref.MTextAttribute = attval;

attref.UpdateMTextAttribute();

 

To edit the attref value:

MText old = attref.MTextAttribute;

string[] parts = old.Split('\n');

string newpart = "new line 2";

string after = parts[0] + "\n" + newpart;

MText new = attref.MTextAttribute;

new.Contents = after;

attref.MTextAttribute = new;

attref.UpgradeMTextAttribute();

 

0 Likes
Message 4 of 5

ActivistInvestor
Mentor
Mentor

LOL

0 Likes
Message 5 of 5

fieldguy
Advisor
Advisor
Accepted solution

The edit portion of this solution is not right.  Autocad uses \\P instead of \\n.  \\P in C# is not easy to deal with.  I felt I should update my solution so it doesn't keep me awake at night - just in case someone (like me who forgets) finds this post in the future.

 

In my case I need to edit the last line of attribute mtext but I don't know how many text lines are above it.

 

I changed create to:  

string attribtext = "line 1\\Pline 2";

 

The "edit":

MText oldtext = attref.MTextAttribute;
DBObjectCollection objs = new DBObjectCollection();
oldtext.Explode(objs);
int i = 0;
string after = string.Empty;
foreach (DBObject obj in objs)
{
    DBText dt = (DBText)obj;
    if (i < objs.Count - 1)
        after += dt.TextString + "\\P";
    i++;
    dt.Dispose();
}// foreach exploded text obj
string newpart = "new last line";
after += newpart;
MText mt = attref.MTextAttribute;
mt.Contents = after;
attref.MTextAttribute = mt;
attref.UpgradeMTextAttribute;
0 Likes