.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Annotation Scale Sorting

6 REPLIES 6
Reply
Message 1 of 7
BrentBurgess1980
1262 Views, 6 Replies

Annotation Scale Sorting

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

6 REPLIES 6
Message 2 of 7
Hallex
in reply to: BrentBurgess1980

I'm afraid you're out off luck

because of AutoCAD has its own sorting rule...

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 7
BrentBurgess1980
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!

Message 4 of 7
Hallex
in reply to: BrentBurgess1980

Holy Cow!

Ya da man

Accepted for 2010 🙂

Regards,

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 7

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
Message 6 of 7
agaalen
in reply to: rick.hberg

Thanks!    Just the post I need!

Message 7 of 7

Did work very well. tks

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost