Author Topic: Layer IsUsed  (Read 3402 times)

0 Members and 1 Guest are viewing this topic.

cnicho

  • Guest
Layer IsUsed
« on: March 01, 2013, 07:28:55 AM »
Hello,
I want to set the visibility according to whether or not a layer has objects.
I am finding IsUsed to be unreliable. For example, if SHOWLAYERUSAGE=0, IsUsed seems to be of no use and if SHOWLAYERUSAGE=1, new objects don't seem to immediately cause IsUsed to be updated.
Is there an alternative to IsUsed to determine if a layer has objects?
Thanks

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #1 on: March 01, 2013, 07:40:15 AM »
Have you considered using Editor.SelectAll()?

Code - C#: [Select]
  1.         public static bool _LayerIsUsed(Editor ed, string layerName)
  2.         {
  3.             bool layerIsUsed = false;
  4.  
  5.             TypedValue[] tvs = new TypedValue[]
  6.             {
  7.                 new TypedValue((int)DxfCode.LayerName, layerName)
  8.             };
  9.  
  10.             SelectionFilter filter = new SelectionFilter(tvs);
  11.  
  12.             PromptSelectionResult psr = ed.SelectAll(filter);
  13.  
  14.             if (psr.Status == PromptStatus.OK)
  15.                 layerIsUsed = true;
  16.  
  17.             return layerIsUsed;
  18.         }
  19.  
« Last Edit: March 01, 2013, 07:58:53 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

cnicho

  • Guest
Re: Layer IsUsed
« Reply #2 on: March 01, 2013, 08:33:00 AM »
That works nicely. Thanks.
« Last Edit: March 01, 2013, 10:12:46 AM by cnicho »

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #3 on: March 01, 2013, 10:19:45 AM »
That works nicely. Thanks.

You're welcome; I'm happy to help.

Separately, I should have thought of this first, but I still think in LISP 'sub-function' terms as I'm still a student of .NET, but you might find this Extension Method preferable:

Code - C#: [Select]
  1. namespace Autodesk.AutoCAD.DatabaseServices.MyExtensions
  2. {
  3.     public static class LayerTableRecordExtensionMethods
  4.     {
  5.         public static bool HasObjects(this LayerTableRecord ltr, Editor ed)
  6.         {
  7.             bool hasObjects= false;
  8.  
  9.             TypedValue[] tvs = new TypedValue[]
  10.             {
  11.                 new TypedValue((int)DxfCode.LayerName, ltr.Name)
  12.             };
  13.  
  14.             SelectionFilter filter = new SelectionFilter(tvs);
  15.  
  16.             PromptSelectionResult psr = ed.SelectAll(filter);
  17.  
  18.             if (psr.Status == PromptStatus.OK)
  19.                 hasObjects= true;
  20.  
  21.             return hasObjects;
  22.         }
  23.     }
  24. }
  25.  

... So now, you simply add a using directive:

Code - C#: [Select]
  1. using Autodesk.AutoCAD.DatabaseServices.MyExtensions;
  2.  

... And your LayerTableRecord type can now call the HasObjects() Extension Method.
"How we think determines what we do, and what we do determines what we get."

Gasty

  • Newt
  • Posts: 90
Re: Layer IsUsed
« Reply #4 on: March 01, 2013, 10:44:45 AM »
Hi,


I think this test is not the correct one, just check with a block with entities in "LayerA" , but inserted in "LayerB", this test will wrongly show that "LayerA" "Has no Objects".

Gaston Nunez

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #5 on: March 01, 2013, 10:47:56 AM »
Thanks for pointing that out (haven't tested)... I was only trying to come up with an alternative to the Properties mentioned in the OP, and Editor.SelectAll() is the best I could come up with.

** Edit - Although, technically, that is 'normal' (not to be mistaken with correct) behavior... Same holds true in LISP using SSGET.
« Last Edit: March 01, 2013, 10:51:48 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

cnicho

  • Guest
Re: Layer IsUsed
« Reply #6 on: March 01, 2013, 11:30:08 AM »
Yes thanks for that spanner Gasty :)
The layer with the block reference in layerB is considered in use but layerA is not.

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #7 on: March 01, 2013, 11:44:26 AM »
My next thought was to change the Extension Method to filter if it were available for purge from the Database... I recently did something similar for Registry Applications, where calling the Purge() Method removed those that could not be purged... So in my hunting, I stumbled upon this blog post.

In that post, they also call IsUsed, which leads me back to the OP, as there must be something more to do for your 'new objects'... Perhaps another Transaction wrapper, etc.? Cannot be certain without more information, and perhaps posting your code.
"How we think determines what we do, and what we do determines what we get."

cnicho

  • Guest
Re: Layer IsUsed
« Reply #8 on: March 01, 2013, 12:22:54 PM »
Thanks Renderman, that blog post had the answer.

If I call GenerateUsageData() before LayerTableRecord IsUsed I can reliably set the visibility, even if SHOWLAYERUSAGE=0 and blocks exist with entities on different layers to the insertion layer.


BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #9 on: March 01, 2013, 07:20:32 PM »
Thanks Renderman, that blog post had the answer.

You're welcome; I learned something too.  ;-)
"How we think determines what we do, and what we do determines what we get."

Chumplybum

  • Newt
  • Posts: 97
Re: Layer IsUsed
« Reply #10 on: March 01, 2013, 09:45:36 PM »
You could also use database.purge(ids) and the result would be layers that can be purged... hence, layers that have no objects

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #11 on: March 01, 2013, 10:18:54 PM »
I suggested that in post #7, no?
"How we think determines what we do, and what we do determines what we get."

Chumplybum

  • Newt
  • Posts: 97
Re: Layer IsUsed
« Reply #12 on: March 01, 2013, 10:20:43 PM »
I suggested that in post #7, no?

should've read the whole thread before posting... sorry RenderMan :oops:

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer IsUsed
« Reply #13 on: March 02, 2013, 01:41:16 AM »
No worries; just wanted to make sure I didn't miscommunicate the thought somehow. I'm still very much a student of .NET development, despite my attempts to bend the universe to my will.
"How we think determines what we do, and what we do determines what we get."