<?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: Replacing Text of MText and DBText Entities with some string value in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/9565679#M55026</link>
    <description>&lt;P&gt;I've followed by your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;                                BlockTableRecord btrr = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;

                                foreach (ObjectId id in btrr)
                                {
                                    Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
                                    //Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, true) as Entity;
                                    if (currentEntity == null)
                                    {
                                        continue;
                                    }
                                    if (currentEntity.GetType() == typeof(MText))
                                    {
                                        if (((MText)currentEntity).Contents == "H e l l o")
                                        {
 
                                            //currentEntity.UpgradeOpen();             
                                            ((MText)currentEntity).Contents = "123";
                                            //currentEntity.DowngradeOpen();
                                       
                                            //TextEditor textEditor = TextEditor.CreateTextEditor((MText)currentEntity);
                                            //    textEditor.SelectAll();
                                            //    TextEditorSelection selection = textEditor.Selection;
                                            //    selection.InsertString("123");
                                            //    textEditor.Close(TextEditor.ExitStatus.ExitSave);
                                         
                                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(((MText)currentEntity).Contents + "--MText");
                                        }
                                    }
                                }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;but result is text overlapping:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="cad.png" style="width: 363px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/780947iF2DE43B15FE6ABEB/image-dimensions/363x166?v=v2" width="363" height="166" role="button" title="cad.png" alt="cad.png" /&gt;&lt;/span&gt;I've tried commented part individually but same result.&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 07 Jun 2020 18:55:23 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-06-07T18:55:23Z</dc:date>
    <item>
      <title>Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3502552#M55021</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; I was writing code for my custom .net extension dll and one of the procedure involved therein was to replace the Text of MTEXT and DBTEXT entities with some predetermined Text String. I am using C#, AutoCAD 2012, and Visual Studio 2010. Here is the part of code wherein I am trying to replace the Text String of the MTEXT or DBTEXT entities.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    foreach (SelectedObject selectedObject in selectionSet) {
        Entity currentEntity = transaction.GetObject(selectedObject.ObjectId, OpenMode.ForWrite, false) as Entity;
        if (currentEntity == null) {
            continue;
        }
        if (currentEntity.GetType() == typeof(MText)) {
            ((MText)currentEntity).Contents = textToCopy;
        }
        else {
            ((DBText)currentEntity).TextString = textToCopy;
        }
    }
    transaction.Commit();&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The issue is that the text of the MTEXT or DBTEXT is not getting replaced with the string I am trying to. I debugged the code and it correctly hits the setting of MText.Contents == "Some String" and also transaction.commit(). But the text of the MTEXT still remains to the old value. Is this the correct way of changing the text of MTEXT / DBTEXT &amp;nbsp;through the .Net API. If not, can someone guide me in any way please ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;Nirvan.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2012 16:24:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3502552#M55021</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-06-15T16:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3504440#M55022</link>
      <description>&lt;P&gt;Hi Nirvan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried your code and had no problem with it. Could there be something wrong with how your SelectionSet is being populated?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the test command that I created based on your code. It changes the text for all DBText and MText entities in model space. And it worked perfectly for me in AutoCAD 2012 using VS 2010.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[CommandMethod("TestTextChange")]
public static void TestTextChange()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;

	using (Transaction tr = db.TransactionManager.StartTransaction())
	{
		BlockTableRecord btr = (BlockTableRecord)tr.GetObject
			(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

		foreach (ObjectId id in btr)
		{
			Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, false) as Entity;
			if (currentEntity == null)
			{
				continue;
			}
			if (currentEntity.GetType() == typeof(MText))
			{
				((MText)currentEntity).Contents = "BlahBlah";
			}
			else
			{
				((DBText)currentEntity).TextString = "BlahBlah";
			}
		}
		tr.Commit();
	}
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you still have problems you might need to add some more code to help diagnose the source of your issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Art&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2012 13:58:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3504440#M55022</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-06-18T13:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3504476#M55023</link>
      <description>&lt;P&gt;One more thing... your else statement will be applied to any entity that is not an MText entity. Is that really what you want to do? What happens if it is a Circle? Then your cast will fail.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would write your code this way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;foreach (SelectedObject selectedObject in selectionSet)
{
	DBObject obj = transaction.GetObject(selectedObject.ObjectId, OpenMode.ForWrite);

	MText mtext = obj as MText;
	if (mtext != null)
	{
		mtext.Contents = "BlahBlah";
		continue;
	}

	DBText text = obj as DBText;
	if (text != null)
	{
		text.TextString = "BlahBlah";
	}
}
transaction.Commit();&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jun 2012 14:09:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3504476#M55023</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-06-18T14:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3508032#M55024</link>
      <description>&lt;P&gt;&lt;SPAN style="line-height: 13px;"&gt;ArtVegas,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 13px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Thanks for all the comments. Actually, I had nested transaction and the outer one was aborting. As a result, even though the inner one (as visible in my code in question) was commiting, the changes were not getting applied.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 13px;"&gt;Even though TextEntity.Context = "abc.." does replace the text string, the formatting disappears as the Contents also contain formatting data. A better way that I found out on net was to use the TextEditor. I will provide a sample below to help someone looking for replacing the MText. Here is the part of relevant code.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                        if (currentEntity.GetType() == typeof(MText)) {
                            TextEditor textEditor = TextEditor.CreateTextEditor((MText)currentEntity);
                            textEditor.SelectAll();
                            TextEditorSelection selection = textEditor.Selection;
                            selection.InsertString(replaceText);
                            textEditor.Close(TextEditor.ExitStatus.ExitSave);
                        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;Nirvan.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 13px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 13px;"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2012 12:08:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/3508032#M55024</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-06-20T12:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/9237247#M55025</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;BR /&gt;I am new in Autocad API. I am interested in this topic. I would like to ask for help.&lt;/P&gt;&lt;P&gt;My issue is I want to stack 1 or 2 numbers in an Mtext strings selection and return the Mtext string with these numbers stacked.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example I have strings like this one: 4 1/2"D Pipe B.+9' 3/4".&lt;/P&gt;&lt;P&gt;It is an Mtext object among Mtext objects that I would like to stack 1/2 and 3/4 and return the original string with numbers stacked.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 09:37:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/9237247#M55025</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-01-08T09:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Text of MText and DBText Entities with some string value</title>
      <link>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/9565679#M55026</link>
      <description>&lt;P&gt;I've followed by your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;                                BlockTableRecord btrr = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;

                                foreach (ObjectId id in btrr)
                                {
                                    Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
                                    //Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, true) as Entity;
                                    if (currentEntity == null)
                                    {
                                        continue;
                                    }
                                    if (currentEntity.GetType() == typeof(MText))
                                    {
                                        if (((MText)currentEntity).Contents == "H e l l o")
                                        {
 
                                            //currentEntity.UpgradeOpen();             
                                            ((MText)currentEntity).Contents = "123";
                                            //currentEntity.DowngradeOpen();
                                       
                                            //TextEditor textEditor = TextEditor.CreateTextEditor((MText)currentEntity);
                                            //    textEditor.SelectAll();
                                            //    TextEditorSelection selection = textEditor.Selection;
                                            //    selection.InsertString("123");
                                            //    textEditor.Close(TextEditor.ExitStatus.ExitSave);
                                         
                                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(((MText)currentEntity).Contents + "--MText");
                                        }
                                    }
                                }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;but result is text overlapping:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="cad.png" style="width: 363px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/780947iF2DE43B15FE6ABEB/image-dimensions/363x166?v=v2" width="363" height="166" role="button" title="cad.png" alt="cad.png" /&gt;&lt;/span&gt;I've tried commented part individually but same result.&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 18:55:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/replacing-text-of-mtext-and-dbtext-entities-with-some-string/m-p/9565679#M55026</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-06-07T18:55:23Z</dc:date>
    </item>
  </channel>
</rss>

