Attribute alignment after edit in AutoCAD

混江龙づ霸主 提交于 2020-01-24 00:32:33

问题


I have a simple routine that updates the text value of an attributereference. After the routine runs the values are updated in the drawing but the text is left justified and not centered. I have not been able to find any command that will cause AutoCAD to update the text location. So any help would be appreciated.

My Code

                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:


Sorry, I have been searching for this issue for 3 days and found the answer right after i posted this question. For anyone else you just need to change the working database before you update the attribute text value.

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;
    }
}


来源:https://stackoverflow.com/questions/32355857/attribute-alignment-after-edit-in-autocad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!