Author Topic: Best way to set a layer filter current?  (Read 1581 times)

0 Members and 1 Guest are viewing this topic.

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Best way to set a layer filter current?
« on: May 19, 2017, 12:46:00 AM »
I'm creating a layer filter, and want to set it current the first time I create it. Is there a good way to do this?
Reading Kean's blog, I put together some code to create the layer filter. The code works as expected, but I can't find a way to set it current. Any ideas?

Code - C#: [Select]
  1. // create prefix filter
  2. try
  3. {
  4.   LayerFilterTree lft = Active.Database.LayerFilters;
  5.   LayerFilterCollection lfc = lft.Root.NestedFilters;
  6.   string pref = "prefix-";
  7.   // check to see if we've created the layerfilter already
  8.   if (!lfc.Cast<LayerFilter>().Any(lf => lf.Name.Equals(pref)))
  9.   {
  10.     // Create it
  11.     LayerFilter prefixFilter = new LayerFilter();
  12.     prefixFilter.Name = pref;
  13.     prefixFilter.FilterExpression = $"NAME==\"{pref}*\"";
  14.     lfc.Add(prefixFilter);
  15.     Active.Database.LayerFilters = lft;
  16.     // TODO set it current?
  17.   }
  18. }
  19. catch (Exception)
  20. {
  21.   Debug.Print("Problem creating Prefix LayerFilter");
  22. }
« Last Edit: May 25, 2017, 11:17:16 AM by Atook »

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Best way to set a layer filter current?
« Reply #1 on: May 24, 2017, 08:54:33 PM »
   I borrowed steeled made up this.
It doesn't really work until you manually open the layer manager, so no help from me

  //http://adndevblog.typepad.com/autocad/2012/06/use-layerfiltertree-to-add-a-new-layer-filter.html
     [CommandMethod("NoXrefLayers")]
     static public void NoXrefLayers()
     {
         Document doc = acadApp.DocumentManager.MdiActiveDocument;
         Database db = doc.Database;
         Editor ed = doc.Editor;
         bool bXref = false;
         LayerFilterTree lft = db.LayerFilters;
         LayerFilterCollection lfc = db.LayerFilters.Root.NestedFilters;


         for (int i = 0; i < lfc.Count; ++i)
         {
             LayerFilter lf = lfc;
             //ed.WriteMessage(Environment.NewLine + lf.FilterExpression);
             if (lf.Name == "Xref")  //only shows up if there is an xref           
                 bXref = true;
             else if (lf.Name == "no_xref_layers")
             {
                 // make it current
                 LayerFilterTree lyrTree2 = new LayerFilterTree(lft.Root, lf);
                 lyrTree2.Root.GenerateNested();
                 db.LayerFilters = lyrTree2;
                 return;
             }
         }
         if (!bXref) return;

         LayerFilter lfXref = new LayerFilter();
         lfXref.Name = "no_xref_layers";
         lfXref.FilterExpression = "NAME==\"~*|*\"";

         lft.Root.NestedFilters.Add(lfXref);
         lft.Root.GenerateNested();
         db.LayerFilters = new LayerFilterTree(lft.Root, lfXref);

     }

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Best way to set a layer filter current?
« Reply #2 on: May 25, 2017, 01:26:42 AM »
Thanks Bryco, that actually worked pretty well! If the layermanager is open, it does it right away. I've decided against setting the layerfilter current though, I can already hear the users complaining that all the rest of their layers are 'gone'!  :evillaugh:

For future user's reference (and maybe my own) here's the solution I came up with after looking at your code:
Code - C#: [Select]
  1. // create prefix filter
  2. try
  3. {
  4.   LayerFilterTree lft = Active.Database.LayerFilters;
  5.   LayerFilterCollection lfc = lft.Root.NestedFilters;
  6.   string pref = "prefix-";
  7.   // check to see if we've created the layerfilter already
  8.   if (!lfc.Cast<LayerFilter>().Any(lf => lf.Name.Equals(pref)))
  9.   {
  10.     // Create it
  11.     LayerFilter prefixFilter = new LayerFilter();
  12.     prefixFilter.Name = pref;
  13.     prefixFilter.FilterExpression = $"NAME==\"{pref}*\"";
  14.     lfc.Add(prefixFilter);
  15.     // set the new filter current (create a new layer filter tree with our filter as current)
  16.     LayerFilterTree nlft = new LayerFilterTree(lft.Root,prefixFilter);
  17.     // assign the new tree to the Active.Database
  18.     Active.Database.LayerFilters = nlft;
  19.   }
  20. }
  21. catch (Exception)
  22. {
  23.   Debug.Print("Problem creating Prefix LayerFilter");
  24. }
« Last Edit: May 25, 2017, 11:17:44 AM by Atook »

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Best way to set a layer filter current?
« Reply #3 on: May 26, 2017, 12:04:17 AM »
Glad it helped a little. I only use 2 filters: one as shown above and one called asis on receiving a client xref so I can thaw everything and delete all the unnecessary stuff.
I did toy with the idea of adding all the layer names as text in modelspace and asking the user to create a filter by selecting the layers.  Never got around to it though.