【问题标题】:Attribute alignment after edit in AutoCAD在 AutoCAD 中编辑后的属性对齐
【发布时间】:2015-09-02 22:55:56
【问题描述】:

我有一个简单的例程来更新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();

【问题讨论】:

    标签: autocad


    【解决方案1】:

    抱歉,我一直在寻找这个问题 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;
        }
    }
    

    【讨论】:

    • 这个建议帮助我解决了同样的问题。但我认为您在调用 AdjustAlignment 之前忘记将 WorkingDatabase 设置为“db”。此外,您只需在提交之前切换一次 WorkingDatabase 并切换回来,而不是每个属性一次。不过谢谢!这帮助很大!
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2012-09-13
    • 2023-03-15
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多