.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to handle MTEXTED environment variable cases in AutoCAD

2 REPLIES 2
Reply
Message 1 of 3
diendev16
174 Views, 2 Replies

How to handle MTEXTED environment variable cases in AutoCAD

Hi everyone,

I’m working with the MTEXTED system variable in AutoCAD, and I need some guidance on how to handle different cases. Specifically, I want to know how to handle the following scenarios:

  1. Internal editor (MTEXTED = "internal")
    • How to invoke AutoCAD's internal text editor directly.
  2. Old editor (MTEXTED = "oldeditor")
    • How to use the old text editor in AutoCAD programmatically.
  3. External editor (MTEXTED = "notepad", or other external apps)
    • I want to use external text editors like Notepad, VSCode, or Notepad++ for editing text. After the user edits the text and closes the editor, the updated text should be reflected back in AutoCAD.

Here’s the code I’m trying:

 

 

 

 

 private void EditMText(ObjectId objectId, Transaction tr)
 {
     var mText = tr.GetObject<MText>(objectId, OpenMode.ForWrite);
     if (mText is null) return;

     undoStack.Push((objectId, mText.Contents));

     var app = SysVar.GetString("MTEXTED");
     if (app.ToLower() == "internal" || app.ToLower() == "" || app.ToLower() == "oldeditor")
     {
         InplaceTextEditor.Invoke(mText, new InplaceTextEditorSettings());
     }
     else
     {
         var tempFileName = "acm" + Guid.NewGuid().ToString("N").Substring(0, 5) + ".tmp";
         var tempFilePath = Path.Combine(Path.GetTempPath(), tempFileName);

         File.WriteAllText(tempFilePath, mText.Contents);

         var process = System.Diagnostics.Process.Start(app + ".exe", tempFilePath);
         process.WaitForExit();
         if (File.Exists(tempFilePath))
         {
             var newText = File.ReadAllText(tempFilePath);
             mText.Contents = newText;
         }
         File.Delete(tempFilePath);
     }
     tr.Commit();
 }

 

 

 

 

 

diendev16_1-1734596145243.pngdiendev16_2-1734596175668.pngdiendev16_3-1734596212285.png

 

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: diendev16

As I can see, you are almost there for using external editor. What you missed is to handle Process.Exited event. Inspired by your topic, I published a snippet of code that fully runnable here:

 

https://drive-cad-with-code.blogspot.com/2024/12/using-external-text-editor-to-modify.html 

 

instead of post code here, in case AutoCAD still runs mostly as it does now in 10 years, thus the code would be still relevant and helpful and not risk the code posted being deleted by this forum:angry_face:

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
diendev16
in reply to: norman.yuan

Thank you so much

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report