• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    nirvanbd
    Posts: 16
    Registered: ‎01-25-2011

    Replacing Text of MText and DBText Entities with some string value

    470 Views, 3 Replies
    06-15-2012 09:24 AM

    Hi all,

          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.

     

        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();

     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  through the .Net API. If not, can someone guide me in any way please ? 

     

    regards,

    Nirvan.

     

    Please use plain text.
    Distinguished Contributor
    Artvegas
    Posts: 104
    Registered: ‎04-21-2011

    Re: Replacing Text of MText and DBText Entities with some string value

    06-18-2012 06:58 AM in reply to: nirvanbd

    Hi Nirvan,

     

    I tried your code and had no problem with it. Could there be something wrong with how your SelectionSet is being populated?

     

    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.

     

    [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();
    	}
    }

     

    If you still have problems you might need to add some more code to help diagnose the source of your issue.

     

    Art

    Please use plain text.
    Distinguished Contributor
    Artvegas
    Posts: 104
    Registered: ‎04-21-2011

    Re: Replacing Text of MText and DBText Entities with some string value

    06-18-2012 07:09 AM in reply to: Artvegas

    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.

     

    I would write your code this way:

     

    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();
    Please use plain text.
    Contributor
    nirvanbd
    Posts: 16
    Registered: ‎01-25-2011

    Re: Replacing Text of MText and DBText Entities with some string value

    06-20-2012 05:08 AM in reply to: Artvegas

    ArtVegas,

                 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. 

     

    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.

     

                            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);
                            }

     

    regards,

    Nirvan.



    Please use plain text.