0

I am trying to re-order the attributes in the AutoCAD blocks. Everytime i edit a existing block or try to create a new block the order in which the attributes are displayed is shuffled completely.

I have tried AUTOCAD commands like 'BATTMAN' 'ATTSYNC' 'BATTORDER' but the problem is i have a bunch of blocks each with 96 -150 pins in them (Each pins has an attribute reference). So it is really pain using all the above mentioned commands to arrange all the attributes in Ascending order.

Any ideas on how to resolve this using a AUTOLISP/LISP/c# program ?! Is there a way to re-order them using the tag names ?!

For example, I need to have the block attributes re-ordered from Pin1-Pin90 in an ascending order.

4

2 回答 2

1

这是 Trae Moore 和我昨天下午在讨论这个概念时敲出的一些代码:

[CommandMethod("BlockAttributeSort")]
public void BlockAttributeSort()
{
    var acDb = HostApplicationServices.WorkingDatabase;
    var acEd = AcApplication.DocumentManager.MdiActiveDocument.Editor;

    try
    {
        using (var acTrans = acDb.TransactionManager.StartTransaction())
        {
            var acBlockTable = (BlockTable)acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead);
            foreach (ObjectId objId in acBlockTable)
            {
                var blockDef = objId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                if (!blockDef.HasAttributeDefinitions)
                    continue;

                blockDef.UpgradeOpen();
                var attCollection = new List<AttributeDefinition>();
                foreach (var attId in blockDef)
                {
                    var attDef = acTrans.GetObject(attId, OpenMode.ForWrite) as AttributeDefinition;
                    if (attDef == null)
                        continue;

                    attCollection.Add((AttributeDefinition)attDef.Clone());
                    attDef.Erase();
                }

                foreach (var att in attCollection.OrderBy(a => a.Tag))
                {
                    blockDef.AppendEntity(att);
                    acTrans.AddNewlyCreatedDBObject(att, true);
                }
            }

            acTrans.Commit();
        }
    }
    catch (System.Exception ex)
    {
        Debug.WriteLine(ex.ToString());
        acEd.WriteMessage(ex.ToString());
    }
}

这个问题对我来说已经足够好,希望发布概念代码,但通常你会发现,如果你带着现有代码来到 Stack,你会得到更好的接收,显示你付出的努力。使用的排序是基于仅在属性标签上,因此如果需要额外的调整,您可能必须想出更具体的东西。

于 2014-02-14T16:02:01.823 回答
0

您可以通过交换句柄来更改块中属性的顺序。请注意,这可能会产生副作用,因为句柄可能会被外部应用程序用作唯一 ID。克隆块和创建新属性也是如此。

此外,如果您 ATTSYNC 一个块,您将丢失任何附加到任何插入的 xdata。

有关更多详细信息,请参阅此帖子:http: //through-the-interface.typepad.com/through_the_interface/2010/07/swapping-autocad-block-attribute-order-using-net.html

于 2014-03-02T10:12:30.380 回答