Author Topic: UpgradeOpen<T> version 2.0  (Read 7611 times)

0 Members and 1 Guest are viewing this topic.

TheMaster

  • Guest
UpgradeOpen<T> version 2.0
« on: April 22, 2013, 07:53:00 PM »
Regarding the code from this post, here is a new version that I recently refactored to support OpenCloseTransaction, which is also much faster.

Code - C#: [Select]
  1.  
  2.    /// <summary>
  3.    ///
  4.    /// Extension method: UpgradeOpen<T>()  Version 2
  5.    ///
  6.    /// This is a refactoring of the original UpgradeOpen<T>()
  7.    /// extension method that can be useful for upgrading the
  8.    /// OpenMode of objects to OpenMode.ForWrite, and skipping
  9.    /// objects on locked layers.
  10.    ///
  11.    /// The original version was abysmally-slow due to its
  12.    /// reliance on exceptions that are thrown each time an
  13.    /// an attempt is made to upgrade an entity on a locked
  14.    /// layer to OpenMode.ForWrite.
  15.    ///
  16.    /// This version eliminates that problem using a caching
  17.    /// scheme that caches the ObjectIds of locked layers,
  18.    /// and uses it to avoid the potentially-large number of
  19.    /// execptions that plagued the original version.
  20.    ///
  21.    /// In this refactored version, rather than an exception
  22.    /// being throw and caught for each entity on a locked
  23.    /// layer, only one exception is thrown for each locked
  24.    /// layer referenced by entities in the sequence.
  25.    ///
  26.    /// This refactored version runs about 5x faster than
  27.    /// the original, depending on the number of entities
  28.    /// on locked layers, and the number of locked layers
  29.    /// referenced by entities in the sequence.
  30.    ///
  31.    /// In a test involving 100,000k DBPoint entities, about
  32.    /// 1/3 of which were on 4 locked layers, the new version
  33.    /// performed ~4x faster than the original (both tested
  34.    /// with the same dataset).
  35.    ///
  36.    /// -----------------------------------------------------
  37.    /// Upgrades the OpenMode of a sequence DBObjects to
  38.    /// OpenMode.ForWrite.
  39.    ///
  40.    /// If includeObjectsOnLockedLayers is true, entities on
  41.    /// locked layers are upgraded and included. Otherwise,
  42.    /// entities on locked layers are not upgraded, and are
  43.    /// omitted from the result, and no exception is raised,
  44.    ///
  45.    /// If includeObjectsOnLockedLayers is true, this method
  46.    /// requires an active transaction, or must be passed a
  47.    /// a transaction, which can be an OpenCloseTransaction,
  48.    /// if the objects were initially opened with same.
  49.    ///
  50.    /// </summary>
  51.  
  52.    public static IEnumerable<T> UpgradeOpen<T>( this IEnumerable<T> source,
  53.       bool includeObjectsOnLockedLayers = false,
  54.       Transaction tr = null ) where T : DBObject
  55.    {
  56.       if( source.Any() )
  57.       {
  58.          HashSet<ObjectId> lockedLayers = new HashSet<ObjectId>();
  59.  
  60.          /// If includeObjectsOnLockedLayers is true, the entities
  61.          /// must be open in the Top transaction, or a Transaction
  62.          /// must be passed in (which can be an OpenCloseTransaction).
  63.  
  64.          if( includeObjectsOnLockedLayers && tr == null )
  65.          {
  66.             DBObject first = source.First();
  67.             if( first == null )
  68.                throw new ArgumentNullException( "element" );
  69.             if( first.IsTransactionResident && first.Database != null )
  70.                tr = first.Database.TransactionManager.TopTransaction;
  71.             if( tr == null )
  72.                throw new Autodesk.AutoCAD.Runtime.Exception(
  73.                   ErrorStatus.NoActiveTransactions );
  74.          }
  75.  
  76.          foreach( T obj in source )
  77.          {
  78.             if( obj == null )
  79.                throw new ArgumentException( "element" );
  80.             if( !obj.IsWriteEnabled )
  81.             {
  82.                Entity entity = obj as Entity;
  83.                if( entity == null )
  84.                {
  85.                   obj.UpgradeOpen(); // not an Entity
  86.                }
  87.                else
  88.                {
  89.                   ObjectId layerId = ObjectId.Null;
  90.                   try
  91.                   {
  92.                      layerId = entity.LayerId;
  93.  
  94.                      // caller wants to open objects for write on
  95.                      // locked layers, so use GetObject() on the
  96.                      // transaction to upgrade the open mode:
  97.  
  98.                      if( includeObjectsOnLockedLayers )
  99.                      {
  100.                         tr.GetObject( obj.ObjectId,
  101.                               OpenMode.ForWrite, obj.IsErased, true );
  102.                      }
  103.                      else  // omit entities on locked layers
  104.                      {
  105.                         // if the entity's layer id is in the hashset,  
  106.                         // the layer is locked and we skip the entity,
  107.                         // avoiding a much more-expensive exception:
  108.  
  109.                         if( lockedLayers.Contains( layerId ) )
  110.                            continue;
  111.  
  112.                         // the entity may still be on a locked layer
  113.                         // that hasn't been added to the HashSet yet
  114.                         // so we deal with that in the catch block:
  115.  
  116.                         obj.UpgradeOpen();
  117.                      }
  118.                   }
  119.                   catch( Autodesk.AutoCAD.Runtime.Exception ex )
  120.                   {
  121.                      if( ex.ErrorStatus != ErrorStatus.OnLockedLayer )
  122.                         throw; // something else is wrong
  123.  
  124.                      // add the locked layer's id to the HashSet so
  125.                      // that we can avoid having to use exceptions
  126.                      // to deal with additional entities that are
  127.                      // on this same locked layer:
  128.  
  129.                      lockedLayers.Add( layerId );
  130.  
  131.                      continue; /// omit this entity from the result
  132.                   }
  133.                }
  134.             }
  135.             yield return obj;
  136.          }
  137.       }
  138.    }
  139.  
  140.  

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: UpgradeOpen<T> version 2.0
« Reply #1 on: April 23, 2013, 12:32:36 PM »
I am continually amazed that someone with your knowledge is freely sharing it.  Thank you Tony!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: UpgradeOpen<T> version 2.0
« Reply #2 on: April 23, 2013, 01:22:20 PM »
Will,
Tony has been doing exactly that for more than 25 years, and yes, he doesn't get thanked enough.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BlackBox

  • King Gator
  • Posts: 3770
Re: UpgradeOpen<T> version 2.0
« Reply #3 on: April 23, 2013, 02:46:24 PM »
I couldn't agree with Will more.

The wealth of knowledge, and even wisdom, that has been offered up to this community is a great kindness... Having an opportunity to interact with great minds here at TheSwamp, truly is a privilege.

Cheers
"How we think determines what we do, and what we do determines what we get."

LE3

  • Guest
Re: UpgradeOpen<T> version 2.0
« Reply #4 on: April 23, 2013, 02:52:05 PM »
Will,
Tony has been doing exactly that for more than 25 years, and yes, he doesn't get thanked enough.

+1
Indeed - I recall (if I remember right) reading (or knew about) Tony tips in the old Cadence magazine, maybe two years earlier than those 25 years, i think I might still have some of those old magazines... 

Maybe, could be a good idea to open a new of those old Mark interview topics, for Tony.

Kudos.

TheMaster

  • Guest
Re: UpgradeOpen<T> version 2.0
« Reply #5 on: April 23, 2013, 07:52:37 PM »
I am continually amazed that someone with your knowledge is freely sharing it.  Thank you Tony!

Thanks all.

I'd like to share more code, but what makes it difficult is the baggage (dependence on other library code), that has to be removed (as was the case here - my working version is more compact because it uses some other library code to do some of the work).

Jeff H

  • Needs a day job
  • Posts: 6150
Re: UpgradeOpen<T> version 2.0
« Reply #6 on: April 23, 2013, 09:41:51 PM »
When I grow up I want to be Tony.
 
Thanks again!!

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: UpgradeOpen<T> version 2.0
« Reply #7 on: April 24, 2013, 08:13:18 AM »
Maybe, could be a good idea to open a new of those old Mark interview topics, for Tony.
That would be fun!
TheSwamp.org  (serving the CAD community since 2003)

LE3

  • Guest
Re: UpgradeOpen<T> version 2.0
« Reply #8 on: April 24, 2013, 09:27:51 AM »
Maybe, could be a good idea to open a new of those old Mark interview topics, for Tony.
That would be fun!

+2 indeed.
Wonder what Tony, would think about this? Any comments Tony?

BlackBox

  • King Gator
  • Posts: 3770
Re: UpgradeOpen<T> version 2.0
« Reply #9 on: April 24, 2013, 10:07:43 AM »
Maybe, could be a good idea to open a new of those old Mark interview topics, for Tony.
That would be fun!

+2 indeed.
Wonder what Tony, would think about this? Any comments Tony?

... Is that Tony's first 'official' question?

 :-P
"How we think determines what we do, and what we do determines what we get."

Jeff H

  • Needs a day job
  • Posts: 6150
Re: UpgradeOpen<T> version 2.0
« Reply #10 on: April 24, 2013, 10:09:58 AM »
+3
I know I am not the only one that would want to ask him a 1,000 questions might need someone screening them or something similar?

TheMaster

  • Guest
Re: UpgradeOpen<T> version 2.0
« Reply #11 on: April 30, 2013, 09:27:57 PM »
+3
I know I am not the only one that would want to ask him a 1,000 questions might need someone screening them or something similar?

At the spur of the moment, I don't mind rambling on about a subject that interests me, but I'm putting together what may likely become a published work (not exactly sure what form it will take), and need to retain some of the thoughts and ideas (and code) I have related to solving common problems in reservation for that.

When i see Kean post code on his blog that amounts to 'baby steps' compared to some of the stuff we kick around here, it irks me enough to provoke me into posting some of my code, simply because it's painful to watch what goes on there.
« Last Edit: April 30, 2013, 09:35:40 PM by TT »

CADbloke

  • Bull Frog
  • Posts: 345
  • Crash Test Dummy
Re: UpgradeOpen<T> version 2.0
« Reply #12 on: April 30, 2013, 09:30:14 PM »
... I'm putting together what may likely become a published work (not exactly sure what form it will take), and need to retain some of the thoughts and ideas (and code) I have related to solving common problems in reservation for that.

Where do I send my money?

TheMaster

  • Guest
Re: UpgradeOpen<T> version 2.0
« Reply #13 on: April 30, 2013, 09:37:03 PM »
... I'm putting together what may likely become a published work (not exactly sure what form it will take), and need to retain some of the thoughts and ideas (and code) I have related to solving common problems in reservation for that.

Where do I send my money?

I'm not sure yet (even if I'll be offering it directly or via a publisher). I've been looking into 'self-publishing' electronic works, but haven't made a decision.

CADbloke

  • Bull Frog
  • Posts: 345
  • Crash Test Dummy
Re: UpgradeOpen<T> version 2.0
« Reply #14 on: April 30, 2013, 09:47:51 PM »
I'm not sure yet (even if I'll be offering it directly or via a publisher). I've been looking into 'self-publishing' electronic works, but haven't made a decision.

Well, if you're doing an early-access program like this count me in and take my money!

Jeff H

  • Needs a day job
  • Posts: 6150
Re: UpgradeOpen<T> version 2.0
« Reply #15 on: April 30, 2013, 11:48:16 PM »
+3
I know I am not the only one that would want to ask him a 1,000 questions might need someone screening them or something similar?

At the spur of the moment, I don't mind rambling on about a subject that interests me, but I'm putting together what may likely become a published work (not exactly sure what form it will take), and need to retain some of the thoughts and ideas (and code) I have related to solving common problems in reservation for that.

When i see Kean post code on his blog that amounts to 'baby steps' compared to some of the stuff we kick around here, it irks me enough to provoke me into posting some of my code, simply because it's painful to watch what goes on there.

$1.00 x 500 for every line of code that contains "Original Author Tony Tanzillo"
$0.50 x 200 for every line of code that contains "Inspired by post from Tony Tanzillo "
That's before the book is even published and
$75.00 for a good book
Will you have a layaway option?
 
I would throw down for sure if you published anything.
I could care less if AutoCAD was even mentioned in it, and I think that is where some people completely miss out on your samples and advice.  Some of the most valuable things I have picked from you have no bearing if it is a Cad, MRI imaging, business line application or whatever type API I could be working with.
If I could pick your brain about anything it would probably have to be your methods and habits of how you approach learning and problem solving.
 
I am starting to really buy in on the idea that differences in native ability is dwarfed by methods and habits.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: UpgradeOpen<T> version 2.0
« Reply #16 on: May 01, 2013, 08:58:00 AM »
+3
I know I am not the only one that would want to ask him a 1,000 questions might need someone screening them or something similar?

At the spur of the moment, I don't mind rambling on about a subject that interests me, but I'm putting together what may likely become a published work (not exactly sure what form it will take), and need to retain some of the thoughts and ideas (and code) I have related to solving common problems in reservation for that.
Some of us were talking about an informal interview. i.e. what is your favorite color, do you play a musical instrument. Nothing to serious. :)
TheSwamp.org  (serving the CAD community since 2003)

BlackBox

  • King Gator
  • Posts: 3770
Re: UpgradeOpen<T> version 2.0
« Reply #17 on: May 01, 2013, 09:06:46 AM »
I would throw down for sure if you published anything.
I could care less if AutoCAD was even mentioned in it, and I think that is where some people completely miss out on your samples and advice.  Some of the most valuable things I have picked from you have no bearing if it is a Cad, MRI imaging, business line application or whatever type API I could be working with.
If I could pick your brain about anything it would probably have to be your methods and habits of how you approach learning and problem solving.
 
I am starting to really buy in on the idea that differences in native ability is dwarfed by methods and habits.

I have nowhere near the proficiency for development (generally) that others here have, such as Jeff, but I know enough that observing others' work, often yours Tony, despite being over my head conceptually (that's fine, I'm relatively new) I know enough that there's wisdom being shared and not just kindness.

Any work(s) that you may one day publish, I too would purchase/subscribe to... You know, just in case you needed more encouragement.  :-P
"How we think determines what we do, and what we do determines what we get."

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: UpgradeOpen<T> version 2.0
« Reply #18 on: May 01, 2013, 03:57:20 PM »
Any work(s) that you may one day publish, I too would purchase/subscribe to... You know, just in case you needed more encouragement.  :-P


+1 to that! I'd hope you made it available electronically considering the wait list that would be there for a hard copy.  If you could bundle the two as a single product that'd be perfect.  I could get my read on now and have something for the shelf after  :-D
« Last Edit: May 01, 2013, 04:09:45 PM by WILL HATCH »