.NET

Reply
Contributor
simonkx
Posts: 20
Registered: ‎12-29-2005
Message 1 of 5 (161 Views)

Get the string value of MText.Content

161 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
Message 2 of 5 (161 Views)

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
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 3 of 5 (161 Views)

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
simonkx
Posts: 20
Registered: ‎12-29-2005
Message 4 of 5 (161 Views)

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
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 5 of 5 (161 Views)

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.
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.