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:
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();
}
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![]()
Can't find what you're looking for? Ask the community or share your knowledge.