How to handle MTEXTED environment variable cases in AutoCAD

How to handle MTEXTED environment variable cases in AutoCAD

diendev16
Participant Participant
386 Views
2 Replies
Message 1 of 3

How to handle MTEXTED environment variable cases in AutoCAD

diendev16
Participant
Participant

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

 

0 Likes
387 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

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😠

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

diendev16
Participant
Participant

Thank you so much

0 Likes