在AutoCAD

时间:2015-09-02 14:26:11

标签: autocad

我有一个简单的例程来更新attributereference的文本值。例程运行后,值将在图形中更新,但文本左对齐且不居中。我无法找到任何会导致AutoCAD更新文本位置的命令。所以任何帮助都会受到赞赏。

我的代码

                using (Transaction acTrans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)acTrans.GetObject(db.BlockTableId, OpenMode.ForRead);

                    foreach (ObjectId oid in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(oid, OpenMode.ForRead);

                        foreach (ObjectId o in btr)
                        {
                            if (o.ObjectClass.Name == "AcDbBlockReference")
                            {
                                BlockReference br = (BlockReference)acTrans.GetObject(o, OpenMode.ForRead);
                                BlockTableRecord b2 = (BlockTableRecord)acTrans.GetObject(br.BlockTableRecord, OpenMode.ForRead);

                                if (b2.Name == blockName)
                                {
                                    AttributeCollection ac = br.AttributeCollection;

                                    foreach (ObjectId i in ac)
                                    {
                                        AttributeReference ar = (AttributeReference)acTrans.GetObject(i, OpenMode.ForWrite);

                                        string tagName = ar.Tag;

                                        foreach (TestAutoCADCntrl.CBAttributeTag t in tags)
                                        {
                                            if (t.TagName == tagName)
                                            {
                                                ar.Justify = AttachmentPoint.MiddleCenter;
                                                ar.AdjustAlignment(db);
                                                ar.TextString = t.TagValue;
                                                ar.DowngradeOpen();
                                            }
                                        }
                                    }

                                    br.RecordGraphicsModified(true);
                                }
                            }
                        }
                    }

                    acTrans.Commit();

1 个答案:

答案 0 :(得分:3)

抱歉,我一直在搜索此问题3天,并在发布此问题后立即找到答案。对于其他任何人,您只需在更新属性文本值之前更改工作数据库。

foreach (TestAutoCADCntrl.CBAttributeTag t in tags)
{
    if (t.TagName == tagName)
    {
        Database wdb = HostApplicationServices.WorkingDatabase;                                                 HostApplicationServices.WorkingDatabase = db;

        ar.TextString = t.TagValue;
        ar.AdjustAlignment(db);

        HostApplicationServices.WorkingDatabase = wdb;
    }
}