Question

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.

Was it helpful?

Solution

Here's some code that Trae Moore and I tapped out yesterday afternoon while discussing the concept:

[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());
    }
}

The question was good enough for me to want the concept code posted, but typically you're going to find you'll get a much better reception if you come to Stack with existing code showing the effort you put in. The sorting used is based solely on the attribute tag, so if additional tweaks are needed you may have to come up with something more specific.

OTHER TIPS

You can change the order of attributes in a block by swapping their handles. Be aware that this can have side effects as the handle may be used as a unique ID by an external app. This would also be the case with cloning the block and creating new attributes.

Also, if you ATTSYNC a block you will lose any xdata attached to any inserts.

See this post for more details: http://through-the-interface.typepad.com/through_the_interface/2010/07/swapping-autocad-block-attribute-order-using-net.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top