Message 1 of 3
How to handle MTEXTED environment variable cases in AutoCAD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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:
- Internal editor (MTEXTED = "internal")
- How to invoke AutoCAD's internal text editor directly.
- Old editor (MTEXTED = "oldeditor")
- How to use the old text editor in AutoCAD programmatically.
- 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();
}