Deleting unused annotation scales in AutoCAD using .NET

Back in this previous post we looked at some code to add new annotation scales and attach them to textual entities inside AutoCAD. As a comment on that post, someone requested information on how to safely delete annotation scales from a drawing. I (very) speculatively responded, at the time, that it was probably appropriate to use Database.Purge() to make sure there were no dependencies on the scales before erasing them. I've just seen an internal email confirming this, so I thought I'd write it up in a quick post. [As a side not, this is presumably the technique used in the scale list cleanup utility.]

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

namespace AnnotationScaling

{

  public class Commands

  {

    [CommandMethod("ASD")]

    public void DeleteAll()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

      ObjectContextManager ocm =

        db.ObjectContextManager;

      if (ocm != null)

      {

        // Now get the Annotation Scaling context collection

        // (named ACDB_ANNOTATIONSCALES_COLLECTION)

        ObjectContextCollection occ =

          ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

        if (occ != null)

        {

          // Create a collection to collect the IDs

          ObjectIdCollection oic =

            new ObjectIdCollection();

          foreach (ObjectContext oc in occ)

          {

            if (oc is AnnotationScale)

              oic.Add(

                new ObjectId(oc.UniqueIdentifier)

              );

          }

          // Check the object references using Purge

          // (this does NOT purge the objects, it only

          // filters the objects that are not purgable)

          db.Purge(oic);

          // Now let's erase each of the objects left

          Transaction tr =

            db.TransactionManager.StartTransaction();

          using (tr)

          {

            // Maintain a count which we decrement for

            // each error we receive on open/erase

            int count = oic.Count;

            foreach (ObjectId id in oic)

            {

              try

              {

                DBObject obj =

                  tr.GetObject(id, OpenMode.ForWrite);

                obj.Erase();

              }

              catch

              {

                count--;

              }

            }

            tr.Commit();

            // Inform the user of the results

            ed.WriteMessage(

              "\n{0} annotation scale{1} removed.",

              count,

              count == 1 ? "" : "s"

            );

          }

        }

      }

    }

  }

}

To test this, take a look at the number of annotation scales by running the SCALELISTEDIT command in a new drawing (based on acad.dwt):

Scale list edit 1

Here's what happens when we run the ASD command:

Command: ASD

32 annotation scales removed.

And here's the greatly-reduced list of annotation scale objects when we re-run SCALELISTEDIT:

Scale list edit 2

6 responses to “Deleting unused annotation scales in AutoCAD using .NET”

  1. Roland Feletic Avatar
    Roland Feletic

    Thank you.
    Now I can change my codes that they really work perfect.
    Regards
    Roland

  2. Kean:

    do i call this command through NETLOAD?
    and should it be added to the startup?

  3. steve,

    Yes, you'd need to build it into a .NET Class Library and NETLOAD it into AutoCAD. You could also use demand-loading to load it on start-up.

    You could run the command on startup, but obviously you'd need to watch out for unused scales that are actually important to keep around.

    Regards,

    Kean

  4. I need instead default(puched button 'reset') my own custom scales.
    Do U know how to add new buttom after 'reset'.
    Or how to create class like yours t operform it?

    Thanks

  5. I need in every dwrg, my custome annotation scales.
    I saw something similar here sites.google.com/site/bushmansnetlaboratory/sendbox/stati/props-and-scales
    But if U will be soo kind please give me your advice..

  6. Kean Walmsley Avatar

    It's not really clear whether you need help creating a button for your command (I suggest searching on "CUIX") or how to implement the functionality. It should be easy to maintain your own list of scales somewhere in the DWG (or perhaps tag them with XData - that could/should work) and then provide the capability to delete them.

    If this doesn't help, please post a more detailed question to the ADN team or the AutoCAD .NET Discussion Group.

    Kean

Leave a Reply to steve Cancel reply

Your email address will not be published. Required fields are marked *