.NET

.NET

Reply
Contributor
20 Posts
0 Kudos
Registered: ‎12-29-2005
Post 1 of 5

Get the string value of MText.Content

176 Views, 4 Replies
04-09-2006 07:31 AM
Hi,
when I got the string value of a MText,it contains TextStyle information such as font、bold、italic and so.Then how can I get the pure value of the MText.Content except the style information.I use C#.
Thanks in advance!
*Tony Tanzillo
Post 2 of 5

Re: Get the string value of MText.Content

04-09-2006 01:25 PM in reply to: simonkx
Have a look here:

http://www.caddzone.com/MTextFragmentSample.cs

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5136968@discussion.autodesk.com...
Hi,
when I got the string value of a MText,it contains TextStyle information such as font、bold、italic and so.Then how can I get the pure value of the MText.Content except the style information.I use C#.
Thanks in advance!
Distinguished Contributor
303 Posts
1 Kudo
Registered: ‎09-26-2004
Post 3 of 5

Re: Get the string value of MText.Content

04-09-2006 02:17 PM in reply to: simonkx
It is a sample without any error checking:
[code]
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using App = Autodesk.AutoCAD.ApplicationServices.Application;
using EI = Autodesk.AutoCAD.EditorInput;
using ED = Autodesk.AutoCAD.EditorInput.Editor;

[assembly: CommandClass(typeof(ClassLibrary.Class))]

namespace ClassLibrary {
public class Class {
// Define Command "SplitMtext"
[CommandMethod("SplitMtext")]
static public void test() { // This method can have any name
ED ed = App.DocumentManager.MdiActiveDocument.Editor;
EI.PromptEntityResult rs = ed.GetEntity("\nSelect Mtext: ");
if (rs.Status == EI.PromptStatus.OK) {
ObjectId id = rs.ObjectId;
Database db = App.DocumentManager.MdiActiveDocument.Database;
Transaction trans = db.TransactionManager.StartTransaction();
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead);
MText mt = ent as MText;
if (mt != null) {
DBObjectCollection dbs = new DBObjectCollection();
mt.Explode(dbs);
string s = "";
foreach (Entity en in dbs) {
DBText txt = en as DBText;
if (txt != null) {
s += txt.TextString;
}
}
ed.WriteMessage("\n" + s); // Print pure Mtext
} else {
ed.WriteMessage("\nIt is not MText entity!");
}
trans.Commit();
}
}
}
}
[/code]
Contributor
20 Posts
0 Kudos
Registered: ‎12-29-2005
Post 4 of 5

Re: Get the string value of MText.Content

04-10-2006 07:23 AM in reply to: simonkx
Thank you,Alexander,it really works very well.
Distinguished Contributor
303 Posts
1 Kudo
Registered: ‎09-26-2004
Post 5 of 5

Re: Get the string value of MText.Content

04-10-2006 07:36 AM in reply to: simonkx
I think that solution by Tony is more universal and elegant, because in next versions of AutoCAD method explode may get text's in other order then now.
It is not documented trick!

Best Regard,
Alexander Rivilis.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!