I have the following code to add annocation scales.
public static void AddScale(string Name, double PaperUnits, double DrawingUnits) { try { ObjectContextManager ocm = HostApplicationServices.WorkingDatabase.ObjectContextManager; if (ocm != null) { ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (occ != null) { if (!(occ.HasContext(Name))) { // new scale AnnotationScale annotationScale = new AnnotationScale(); annotationScale.Name = Name; annotationScale.PaperUnits = PaperUnits; annotationScale.DrawingUnits = DrawingUnits; Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor; //ed.WriteMessage(string.Format("\n{0}", annotationScale.Name)); //add to context collection occ.AddContext(annotationScale); } } } } catch (System.Exception ex) { AcApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString()); } } public static void ClearScaleList() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor; try { ObjectContextManager ocm = db.ObjectContextManager; if (ocm != null) { ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (occ != null) { foreach (AnnotationScale anScale in occ) { if (!(occ.CurrentContext.Name.Equals(anScale.Name))) { //ed.WriteMessage("\n" + anScale.Name + " : removed"); occ.RemoveContext(anScale.Name); } } } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }
When I run the folllowing code
Setup.ScaleList.ClearScaleList(); Setup.ScaleList.AddScale("1:1", 1,1); Setup.ScaleList.AddScale("1:2", 1, 1); Setup.ScaleList.AddScale("1:4", 1, 1); Setup.ScaleList.AddScale("1:8", 1, 1); Setup.ScaleList.AddScale("1:10", 1, 1); Setup.ScaleList.AddScale("1:16", 1, 1); Setup.ScaleList.AddScale("1:20", 1, 1); Setup.ScaleList.AddScale("1:30", 1, 1); Setup.ScaleList.AddScale("1:40", 1, 1); Setup.ScaleList.AddScale("1:50", 1, 1); Setup.ScaleList.AddScale("1:100", 1, 1); Setup.ScaleList.AddScale("1:200", 1, 1); Setup.ScaleList.AddScale("1:250", 1, 1); Setup.ScaleList.AddScale("1:500", 1, 1); Setup.ScaleList.AddScale("1:1000", 1, 1); Setup.ScaleList.AddScale("1:2000", 1, 1); Setup.ScaleList.AddScale("1:5000", 1, 1); Setup.ScaleList.AddScale("1:10000", 1, 1); Setup.ScaleList.AddScale("1:20000", 1, 1); Setup.ScaleList.AddScale("1:50000", 1, 1); Setup.ScaleList.AddScale("1:100000", 1, 1); Setup.ScaleList.AddScale("1:200000", 1, 1); Setup.ScaleList.AddScale("1:250000", 1, 1); Setup.ScaleList.AddScale("1:500000", 1, 1);
it adds all of the scales, but they are displayed in this order
1:2
1:100
1:200
1:250
1:500
1:1000
1:2000
1:5000
1:10000
1:20000
1:50000
1:4
1:100000
1:200000
1:250000
1:500000
1:8
1:10
1:16
1:20
1:30
1:40
1:50
1:1
Is there a way to sort Annotation scales?
Thanks
I'm afraid you're out off luck
because of AutoCAD has its own sorting rule...
Halex - I managed to find a solution to this, although it did take a lot of time trawling through the AcScaleList.dll in ILSpy to find that the Autodesk.AutoCAD.Internal as a ScaleListHelpers Class and used that to develop this, and it is sorted the way I was hoping.
using System.Collections.Generic; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Internal; using AcApp = Autodesk.AutoCAD.ApplicationServices.Application; namespace Setup { public class ScaleLists { #region "Members" private static List<string> ScaleListName = new List<string>(); private static List<double> ScaleListNumerators = new List<double>(); private static List<double> ScaleListDenominator = new List<double>(); #endregion #region "Public Methods" public static void AddScaleLists() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor; ScaleListName.Clear(); ScaleListNumerators.Clear(); ScaleListDenominator.Clear(); //only runs if the GetScales returns a false value ScaleListName = GetScaleListString(); ScaleListNumerators = GetScaleNumerator(); ScaleListDenominator = GetScaleDenominator(); ScaleListHelpers.updateScaleList(ScaleListName, ScaleListName, ScaleListNumerators, ScaleListDenominator, ScaleListDenominator.Count); } #endregion #region "Private Methods" private static List<string> GetScaleListString() { List<string> Temp = new List<string>(); Temp.Add("1:1"); Temp.Add("1:2"); Temp.Add("1:4"); Temp.Add("1:8"); Temp.Add("1:10"); Temp.Add("1:16"); Temp.Add("1:20"); Temp.Add("1:30"); Temp.Add("1:40"); Temp.Add("1:50"); Temp.Add("1:100"); Temp.Add("1:200"); Temp.Add("1:250"); Temp.Add("1:500"); Temp.Add("1:1000"); Temp.Add("1:2000"); Temp.Add("1:5000"); Temp.Add("1:10000"); Temp.Add("1:20000"); Temp.Add("1:50000"); Temp.Add("1:100000"); Temp.Add("1:200000"); Temp.Add("1:250000"); Temp.Add("1:500000"); return Temp; } private static List<double> GetScaleNumerator() { List<double> Temp = new List<double>(); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); Temp.Add(1); return Temp; } private static List<double> GetScaleDenominator() { List<double> Temp = new List<double>(); Temp.Add(1); Temp.Add(2); Temp.Add(4); Temp.Add(8); Temp.Add(10); Temp.Add(16); Temp.Add(20); Temp.Add(30); Temp.Add(40); Temp.Add(50); Temp.Add(100); Temp.Add(200); Temp.Add(250); Temp.Add(500); Temp.Add(1000); Temp.Add(2000); Temp.Add(5000); Temp.Add(10000); Temp.Add(20000); Temp.Add(50000); Temp.Add(100000); Temp.Add(200000); Temp.Add(250000); Temp.Add(500000); return Temp; } #endregion } }
So far it seems to be working ok... fingers crossed!
Holy Cow!
Ya da man
Accepted for 2010 🙂
Regards,
Oleg
I know this is an old post, but it helped me a lot! Thanks!
One thing i noticed - if it helps anyone - is that if you run the first posted code twice, it will sort them in order. Only the first run seems randomly scattered. I just added another function to run the main function twice and it seems to work, sorting them properly.