70001812 How to sort entities of layers so to be displayed above entities of other layers

Article 70001812
Type HowTo
Product WebJS
Version 8
Date Added 8/24/2020
Fixed 8.8006.0.1 (8/24/2020)
Submitted by Brendan Fry

Summary

How to sort entities of layers in order to be displayed above entities of other layers

Solution

In the 1st example we will show how to sort the model entities by its layer name in VectorDraw WebControl through JavaScript and in the 2nd example we will show the same procedure in VectorDraw Framework through C#.

Example 1:

  
               for (var i = 0; i < vddoc.Model.Entities.Items.length; i++) { //before sort

                 var ea = vdcanvas.GetDictItem(vddoc.Model.Entities, vddoc.Model.Entities.Items[i]);
                 var la = vdcanvas.GetDictItem(vddoc.Layers, ea.Layer);
                 var laName = la.Name.toUpperCase();
                 console.log(laName);
               }

                 vddoc.Model.Entities.Items.sort(function (a, b) {
                 var ea = vdcanvas.GetDictItem(vddoc.Model.Entities, a); // entity a
                 var eb = vdcanvas.GetDictItem(vddoc.Model.Entities, b); // entity b
                 var la = vdcanvas.GetDictItem(vddoc.Layers, ea.Layer); // layer of entitty a
                 var lb = vdcanvas.GetDictItem(vddoc.Layers, eb.Layer); // layer of entitty b
                 var laName = la.Name.toUpperCase(); // layer name to lower case
                 var lbName = lb.Name.toUpperCase();
                
                 if (laName > lbName) // compare the layer names
                  return 1;
                 else if (laName < lbName) 
                 return -1;
                 else if (laName == lbName) 
                 return 0;
                 else 
                 return 0;
             }
             );
             setTimeout(vdcanvas.redraw); // post a redraw to change the displayed entities order on the screen
             for (var i = 0; i < vddoc.Model.Entities.Items.length; i++) {// after sort

                 var ea = vdcanvas.GetDictItem(vddoc.Model.Entities, vddoc.Model.Entities.Items[i]);
                 var la = vdcanvas.GetDictItem(vddoc.Layers, ea.Layer);
                 var laName = la.Name.toUpperCase();
                 console.log(laName);
             }
Example 2:
 class EntitiesLayerSort : System.Collections.Generic.IComparer
        {
            public int Compare(vdFigure x, vdFigure y)
            {
                //uses the default string comparer from A.. to .. Z order (Ignores case sensitive)
                return System.StringComparer.InvariantCultureIgnoreCase.Compare(x.Layer.Name, y.Layer.Name);
                
                //returns > 0 if x layer Name is bigger than y Layer Name
                //returns < 0 if x layer Name is smaller than y Layer Name
                //returns 0 if x Layer Name is equal with y Layer name
            }
        }
void test()
{
               System.Diagnostics.Debug.WriteLine("=====Before Sort=============");
                foreach (vdFigure item in doc.Model.Entities)
                {
                    System.Diagnostics.Debug.WriteLine(item.Layer.Name);
                }
                System.Diagnostics.Debug.WriteLine("=====After Sort=============");
                doc.Model.Entities.Sort(new EntitiesLayerSort());
                foreach (vdFigure item in doc.Model.Entities)
                {
                    System.Diagnostics.Debug.WriteLine(item.Layer.Name);
                }
}

Send comments on this topic.