This seems to work from the some tests I did.
[CommandMethod("THWU")]
public void TextHeightWidthUpdate()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var textStyleHeights = GetFixedHeightTextStyles(db);
var textStyleWidths = GetFixedWidthTextStyles(db);
var filter = new SelectionFilter(new[] { new TypedValue(0, "MTEXT,TEXT") });
var psr = ed.SelectAll(filter);
if (psr.Status != PromptStatus.OK)
return;
var textClass = RXObject.GetClass(typeof(DBText));
using (var tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject so in psr.Value)
{
var id = so.ObjectId;
if (id.ObjectClass.IsDerivedFrom(textClass))
{
var text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
if (textStyleHeights.ContainsKey(text.TextStyleName))
text.Height = textStyleHeights[text.TextStyleName];
if (textStyleWidths.ContainsKey(text.TextStyleName))
text.WidthFactor = textStyleWidths[text.TextStyleName];
}
else
{
var mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
// set the text height equal to the textstyle height (if different from 0)
if (textStyleHeights.ContainsKey(mtext.TextStyleName))
mtext.TextHeight = textStyleHeights[mtext.TextStyleName];
// remove width and height formatting
double widthFactor = textStyleWidths.ContainsKey(mtext.TextStyleName) ?
textStyleWidths[mtext.TextStyleName] :
1.0;
TextEditor textEditor = TextEditor.CreateTextEditor(mtext);
textEditor.SelectAll();
textEditor.Selection.WidthScale = widthFactor;
textEditor.Selection.Height = textEditor.TextHeight;
textEditor.Close(TextEditor.ExitStatus.ExitSave);
}
}
tr.Commit();
}
}
Dictionary<string, double> GetFixedHeightTextStyles(Database db)
{
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
return ((TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead))
.Cast<ObjectId>()
.Select(id => (TextStyleTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Where(ts => ts.TextSize > 0.0)
.ToDictionary(ts => ts.Name, ts => ts.TextSize);
}
}
Dictionary<string, double> GetFixedWidthTextStyles(Database db)
{
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
return ((TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead))
.Cast<ObjectId>()
.Select(id => (TextStyleTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Where(ts => ts.XScale > 1.0)
.ToDictionary(ts => ts.Name, ts => ts.XScale);
}
}