.NET

Reply
Mentor
BrentBurgess1980
Posts: 165
Registered: ‎06-16-2008
Message 1 of 5 (377 Views)

Annotation Scale Sorting

377 Views, 4 Replies
12-20-2011 05:42 PM

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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 5 (361 Views)

Re: Annotation Scale Sorting

12-21-2011 04:18 AM in reply to: BrentBurgess1980

I'm afraid you're out off luck

because of AutoCAD has its own sorting rule...

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
BrentBurgess1980
Posts: 165
Registered: ‎06-16-2008
Message 3 of 5 (346 Views)

Re: Annotation Scale Sorting

12-21-2011 04:14 PM in reply to: Hallex

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!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 5 (331 Views)

Re: Annotation Scale Sorting

12-22-2011 03:32 AM in reply to: BrentBurgess1980

Holy Cow!

Ya da man

Accepted for 2010 :smileyhappy:

Regards,

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
rick.hberg
Posts: 78
Registered: ‎06-27-2011
Message 5 of 5 (55 Views)

Re: Annotation Scale Sorting

10-17-2014 12:25 PM in reply to: BrentBurgess1980

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.

Civil 3D 2014
Windows 7 x64
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.