Through the Interface: Deleting unused annotation scales in AutoCAD using .NET

May 2015

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


« An automatic numbering system for AutoCAD blocks using .NET - Part 4 | Main | Autodesk Design Review 2009 API documentation now available »

May 19, 2008

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



    public void DeleteAll()


      Document doc =


      Database db = doc.Database;

      Editor ed = doc.Editor;

      ObjectContextManager ocm =


      if (ocm != null)


        // Now get the Annotation Scaling context collection


        ObjectContextCollection occ =


        if (occ != null)


          // Create a collection to collect the IDs

          ObjectIdCollection oic =

            new ObjectIdCollection();

          foreach (ObjectContext oc in occ)


            if (oc is AnnotationScale)


                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)


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

          Transaction tr =


          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)




                DBObject obj =

                  tr.GetObject(id, OpenMode.ForWrite);









            // Inform the user of the results


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


              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


TrackBack URL for this entry:

Listed below are links to weblogs that reference Deleting unused annotation scales in AutoCAD using .NET:

blog comments powered by Disqus


10 Random Posts