想请教一下,我使用 ReadDwgFile 获取 Database ;
使用 Transaction 获取 Database 中的 DBText 与 AttributeReference;
对其属性 TextString 进行修改;
但只修改了文本值,对齐方式与宽度因子未随之改变;
尝试使用 AdjustAlignment,但并未解决问题。
using (var docLock = document.LockDocument())
{
using(var ndb = new Database(false, true))
{
ndb.ReadDwgFile("D:\\Projects\\Document3.dwg", FileShare.ReadWrite, true, "");
using (Transaction tr = ndb.TransactionManager.StartTransaction())
{
var bt = tr.GetObject(ndb.BlockTableId, OpenMode.ForRead) as BlockTable;
var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in btr)
{
var dbo = tr.GetObject(id, OpenMode.ForWrite);
if (dbo.GetType() == typeof(DBText))
{
txt = dbo as DBText;
txt.TextString = "填充文本";
var originalDatabase = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = ndb;
txt.AdjustAlignment(ndb);
HostApplicationServices.WorkingDatabase = originalDatabase;
}
}
tr.Commit();
}
ndb.SaveAs("D:\\Projects\\Document3.dwg", DwgVersion.Current);
ndb.Dispose();
}
}

I borrowed from previous posts:
https://forums.autodesk.com/t5/net/attrubute-text-alignment-problem-after-update/m-p/2624675#M17532
https://forums.autodesk.com/t5/net/wrong-justification-of-dbtext-in-block/m-p/9401872/highlight/fals...
https://forums.autodesk.com/t5/net/c-dbtext-alignment-error-when-openning-readdwgfile-method-have/m-...
use code:
var originalDatabase = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = ndb;
txt.AdjustAlignment(ndb);
HostApplicationServices.WorkingDatabase = originalDatabase;
no success
Must the DWG file be opened in a viewport to take effect?