Author Topic: Select Block by it's handle  (Read 2227 times)

0 Members and 1 Guest are viewing this topic.

Conveyor1

  • Guest
Select Block by it's handle
« on: September 24, 2018, 11:07:46 AM »
Morning,

Does anyone know how to select a block by it's handle?  I currently have some code I use to compare a dynamic blocks attributes currently in a drawing to what is in an excel file.  However, the code I have take a long time to run as it cycles thru every item in the blocktablerecord to find the block requiring the change.

Was looking for a more direct method as I have the block handle for the block needing the changes.

Please let me know when you can.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Select Block by it's handle
« Reply #1 on: September 24, 2018, 11:32:11 AM »
Hi,

You can use the Database.GetObjectId() method, or better, the undocumented one: TryGetObjectId().
Code - C#: [Select]
  1. if (db.TryGetObjectId(new Handle(Convert.ToInt64(someHandleString, 16)), out ObjectId id))
  2.     // do what you need with id
  3. else
  4.     // the handle does not exist

« Last Edit: September 24, 2018, 11:37:34 AM by gile »
Speaking English as a French Frog

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Select Block by it's handle
« Reply #2 on: September 24, 2018, 12:06:33 PM »
I use a function like this to get an ObjectId
Code - C#: [Select]
  1. /// <summary>
  2. /// Given a handle, return an associated ObjectId
  3. /// </summary>
  4. /// <param name="handle">The handle.</param>
  5. /// <returns>the required ObjectId or ObjectId.Null</returns>
  6. public static ObjectId GetObjectIDFromHandle(string handle)
  7. {
  8.         if (handle.Length < 3) return ObjectId.Null;
  9.         try
  10.         {
  11.                 // Convert hexadecimal string to 64-bit
  12.                 long ln = Convert.ToInt64(handle, 16);
  13.                 // Now create a Handle from the long integer
  14.                 Handle hn = new Handle(ln);
  15.                 // And attempt to get an ObjectId for the Handle
  16.                 ObjectId id =Active.Database.GetObjectId(false,hn,0);
  17.                 // return the objectID if it exists, ObjectId.Null if it doesn't
  18.                 if (id.IsEffectivelyErased || id.IsErased) return ObjectId.Null;
  19.                 return id;
  20.         }
  21.         catch
  22.         {
  23.                 return ObjectId.Null;
  24.         }
  25. }
  26.  

I like Gile's TryGetObjectId method, but BricsCAD doesn't support it. Too bad, seems like a better method to me.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Select Block by it's handle
« Reply #3 on: September 24, 2018, 12:29:14 PM »
@Atook, you can implement a TryGetObjectId extension method for BricsCAD, you can also overload it to directly take a string as argument

Code - C#: [Select]
  1.     static class Extension
  2.     {
  3.         public static bool TryGetObjectId(this Database db, Handle handle, out ObjectId id)
  4.         {
  5.             try
  6.             {
  7.                 id = db.GetObjectId(false, handle, 0);
  8.                 return true;
  9.             }
  10.             catch
  11.             {
  12.                 id = ObjectId.Null;
  13.                 return false;
  14.             }
  15.         }
  16.    
  17.  
  18.         public static bool TryGetObjectId(this Database db, string handle, out ObjectId id)
  19.         {
  20.             return db.TryGetObjectId(new Handle(Convert.ToInt64(handle, 16)), out id);
  21.         }
  22.     }
Speaking English as a French Frog

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Select Block by it's handle
« Reply #4 on: September 24, 2018, 12:39:02 PM »
Many thanks Gile, I love seeing how you put things together!   :idea: