Hi,
You can either use a selection filter and Editor.SelectAll() or iterate through the model space enities.
Using a selection filter
C#
public class Commands
{
[CommandMethod("Test1")]
public void Test1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue[] tvs = new[]
{
new TypedValue(0, "TEXT,MTEXT"), // only DBText or MText
new TypedValue(410, "Model") // in model space
};
SelectionFilter filter = new SelectionFilter(tvs);
PromptSelectionResult selection = ed.SelectAll(filter);
if (selection.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject obj in selection.Value)
{
ObjectId id = obj.ObjectId;
if (id.ObjectClass.DxfName == "TEXT")
{
DBText text = (DBText)tr.GetObject(id, OpenMode.ForRead);
ed.WriteMessage("\nText = {0} ({1})", text.TextString, text.Position);
}
else
{
MText mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
ed.WriteMessage("\nText = {0} ({1})", mtext.Text, mtext.Location);
}
}
tr.Commit();
}
}
VB (with the help of Telerik code converter)
<CommandMethod("Test1")>
Public Sub Test1()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim tvs As TypedValue() = New TypedValue() _
{
New TypedValue(0, "TEXT,MTEXT"), ' only DBText or MText
New TypedValue(410, "Model") ' in model space
}
Dim filter As New SelectionFilter(tvs)
Dim selection As PromptSelectionResult = ed.SelectAll(filter)
If selection.Status <> PromptStatus.OK Then
Return
End If
Using tr As Transaction = db.TransactionManager.StartTransaction()
For Each obj As SelectedObject In selection.Value
Dim id As ObjectId = obj.ObjectId
If id.ObjectClass.DxfName = "TEXT" Then
Dim text As DBText = DirectCast(tr.GetObject(id, OpenMode.ForRead), DBText)
ed.WriteMessage(vbLf & "Text = {0} ({1})", text.TextString, text.Position)
Else
Dim mtext As MText = DirectCast(tr.GetObject(id, OpenMode.ForWrite), MText)
ed.WriteMessage(vbLf & "Text = {0} ({1})", mtext.Text, mtext.Location)
End If
Next
tr.Commit()
End Using
End Sub
Iterating through all model space entities (this may be a little faster than using a selection set)
C#
[CommandMethod("Test2")]
public void Test2()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (var tr = db.TransactionManager.StartTransaction())
{
var model = (BlockTableRecord)tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in model)
{
switch (id.ObjectClass.DxfName)
{
case "TEXT":
var text = (DBText)tr.GetObject(id, OpenMode.ForRead);
ed.WriteMessage($"\nText = {text.TextString} ({text.Position})");
break;
case "MTEXT":
var mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
ed.WriteMessage($"\nText = {mtext.Text} ({mtext.Location})");
break;
default:
break;
}
}
tr.Commit();
}
}
VB
<CommandMethod("Test2")>
Public Sub Test2()
Dim doc = Application.DocumentManager.MdiActiveDocument
Dim db = doc.Database
Dim ed = doc.Editor
Using tr = db.TransactionManager.StartTransaction()
Dim model = DirectCast(tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead), BlockTableRecord)
For Each id As ObjectId In model
Select Case id.ObjectClass.DxfName
Case "TEXT"
Dim text = DirectCast(tr.GetObject(id, OpenMode.ForRead), DBText)
ed.WriteMessage(vbLf & "Text = {0} ({1})", text.TextString, text.Position)
Exit Select
Case "MTEXT"
Dim mtext = DirectCast(tr.GetObject(id, OpenMode.ForWrite), MText)
ed.WriteMessage(vbLf & "Text = {0} ({1})", mtext.Text, mtext.Location)
Exit Select
Case Else
Exit Select
End Select
Next
tr.Commit()
End Using
End Sub