<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Re : How to loop through a model to extract TEXT and MTEXT string + position in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/7992048#M32388</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code above works perfectly for MTEXT but when I am trying to extract TEXT, although I do find 38 TEXT objects in my drawing, when I try to read it using TextString, all I find is blanks. Could you please help me that? Why is this happening?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward to your reply&lt;/P&gt;</description>
    <pubDate>Thu, 10 May 2018 06:41:52 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-05-10T06:41:52Z</dc:date>
    <item>
      <title>How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6935409#M32383</link>
      <description>&lt;P&gt;I have an AutoCAD and I am in Model space, how to loop through a model to extract all TEXT and MTEXT string + position?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my first VB.NET program with AutoCAD, I tried to use a code posted on the forum but it did not return anything (no error even when I remove Try-Catch&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Dim acDocEd As Editor
        Dim acTypValAr(3) As TypedValue
        Dim acSelFtr As SelectionFilter
        Dim acSSPrompt As PromptSelectionResult
        Dim acSSet As SelectionSet
        Dim selObj As SelectedObject

        acDocEd = Application.DocumentManager.MdiActiveDocument.Editor

        acSelFtr = New SelectionFilter(acTypValAr)
        acSSPrompt = acDocEd.SelectAll(acSelFtr)

        If acSSPrompt.Status = PromptStatus.OK Then
            acSSet = acSSPrompt.Value

            Using trans As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction

                Try
                    For Each selObj In acSSet
                        Select Case selObj.ObjectId.ObjectClass.Name
                            Case "AcDbText"
                                Dim textObj As DBText = trans.GetObject(selObj.ObjectId, OpenMode.ForRead, False, True)
                                MsgBox("Text = " &amp;amp; textObj.TextString)
                                textObj.Dispose()
                            Case "AcDbMText"
                                Dim mtextObj As MText = trans.GetObject(selObj.ObjectId, OpenMode.ForRead, False, True)
                                MsgBox("Text = " &amp;amp; mtextObj.Contents)
                                mtextObj.Dispose()
                        End Select
                    Next selObj
                Catch ex As Exception
                Finally
                    trans.Commit()
                End Try
            End Using

        End If
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 20:30:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6935409#M32383</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-03-09T20:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937006#M32384</link>
      <description>&lt;P&gt;Two things that immediately come to mind:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Your selection filter: are you actually creating one? Where is the code which filters in only text and Mtext?&lt;/LI&gt;&lt;LI&gt;Second is to be sure that the name of the Objects as strings actually come out as "AcDbText" and "AcDbMtext" - &amp;nbsp;you gotta make sure it is exact.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I don't know VB.NET but if i was using c# I simply do something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;case selObj.ObjectId.ObjectClass == RXClass.GetClass(typeof(Mtext))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Dim acTypValAr(3) As TypedValue
    acTypValAr.SetValue(New TypedValue(DxfCode.Operator, "&amp;lt;or"), 0)
    acTypValAr.SetValue(New TypedValue(DxfCode.Start, "MTEXT"), 1)
    acTypValAr.SetValue(New TypedValue(DxfCode.Start, "DBTEXT"), 2)    
    acTypValAr.SetValue(New TypedValue(DxfCode.Operator, "or&amp;gt;"), 3)

    Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;untested code warning: there may be typos etc.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 13:00:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937006#M32384</guid>
      <dc:creator>BKSpurgeon</dc:creator>
      <dc:date>2017-03-10T13:00:40Z</dc:date>
    </item>
    <item>
      <title>Re : How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937299#M32385</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can either use a &lt;A href="http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-D9FB23AE-D853-4D00-A910-4F66FCC4607A" target="_blank"&gt;selection filter&lt;/A&gt; and Editor.SelectAll() or iterate through the model space enities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a selection filter&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C#&lt;/P&gt;
&lt;PRE&gt;   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();
            }
        }&lt;/PRE&gt;
&lt;P&gt;VB (with the help of &lt;A href="http://converter.telerik.com/" target="_blank"&gt;Telerik code converter&lt;/A&gt;)&lt;/P&gt;
&lt;PRE&gt;        &amp;lt;CommandMethod("Test1")&amp;gt;
        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 &amp;lt;&amp;gt; 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 &amp;amp; "Text = {0} ({1})", text.TextString, text.Position)
                    Else
                        Dim mtext As MText = DirectCast(tr.GetObject(id, OpenMode.ForWrite), MText)
                        ed.WriteMessage(vbLf &amp;amp; "Text = {0} ({1})", mtext.Text, mtext.Location)
                    End If
                Next
                tr.Commit()
            End Using
        End Sub&lt;/PRE&gt;
&lt;P&gt;Iterating through all model space entities (this may be a little faster than using a selection set)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C#&lt;/P&gt;
&lt;PRE&gt;        [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();
            }
        }&lt;/PRE&gt;
&lt;P&gt;VB&lt;/P&gt;
&lt;PRE&gt;        &amp;lt;CommandMethod("Test2")&amp;gt;
        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 &amp;amp; "Text = {0} ({1})", text.TextString, text.Position)
                            Exit Select
                        Case "MTEXT"
                            Dim mtext = DirectCast(tr.GetObject(id, OpenMode.ForWrite), MText)
                            ed.WriteMessage(vbLf &amp;amp; "Text = {0} ({1})", mtext.Text, mtext.Location)
                            Exit Select
                        Case Else
                            Exit Select
                    End Select
                Next
                tr.Commit()
            End Using
        End Sub&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 14:26:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937299#M32385</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-03-10T14:26:43Z</dc:date>
    </item>
    <item>
      <title>Re : How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937757#M32386</link>
      <description>&lt;P&gt;Thanks, the last VB code works well&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 16:52:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937757#M32386</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-03-10T16:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937764#M32387</link>
      <description>&lt;P&gt;Thanks - this is my first AutoCAD code so I am lacks of basic knowledge. The bad thing is that I could only find 1 book on AutoCAD .NET but I could not place the order.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 16:53:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/6937764#M32387</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-03-10T16:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: Re : How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/7992048#M32388</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code above works perfectly for MTEXT but when I am trying to extract TEXT, although I do find 38 TEXT objects in my drawing, when I try to read it using TextString, all I find is blanks. Could you please help me that? Why is this happening?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward to your reply&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 06:41:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/7992048#M32388</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-10T06:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: Re : How to loop through a model to extract TEXT and MTEXT string + position</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/8088208#M32389</link>
      <description>&lt;P&gt;Because someone edited the text and backed spaced all the text away instead of erasing the object.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 19:51:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-loop-through-a-model-to-extract-text-and-mtext-string/m-p/8088208#M32389</guid>
      <dc:creator>paul_bachner</dc:creator>
      <dc:date>2018-06-24T19:51:56Z</dc:date>
    </item>
  </channel>
</rss>

