Author Topic: Select Entity By Handle  (Read 2226 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
Select Entity By Handle
« on: January 06, 2012, 05:35:54 AM »
Hi,

How do I select an entity if I know the handle of that entity as string?

Thanks

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Select Entity By Handle
« Reply #1 on: January 06, 2012, 06:27:03 AM »
Hi,

If your handle is a Handle object, you can use:
Code: [Select]
ObjectId id = Database.GetObjectId(false, handle, 0);If your handle is a string convert the string into a long to create a new Handle object, then get the ObjectId:
Code: [Select]
long lg = Convert.ToInt64(handle, 16);
ObjectId id = Database.GetObjectId(false, new Handle(lg), 0)

EDIT: two safer methods

Code: [Select]
public ObjectId HandleToObjectId(string str, Database db)
{
long id;
if (Int64.TryParse(str, System.Globalization.NumberStyles.HexNumber, null, out id))
return HandleToObjectId(new Handle(id), db);
else
return ObjectId.Null;
}

public ObjectId HandleToObjectId(Handle handle, Database db)
{
try { return db.GetObjectId(false, handle, 0); }
catch { return ObjectId.Null; }
}
« Last Edit: January 06, 2012, 06:47:59 AM by gile »
Speaking English as a French Frog

kaefer

  • Guest
Re: Select Entity By Handle
« Reply #2 on: January 06, 2012, 06:57:07 AM »
Code: [Select]
...
public ObjectId HandleToObjectId(Handle handle, Database db)
{
try { return db.GetObjectId(false, handle, 0); }
catch { return ObjectId.Null; }
}

Hi gile,

much better, but still... there's TryGetObjectId too. I didn't know that you could pass null instead of InvariantCulture. And if you were so inclined, you could use Nullables to signal the error condition. Something like:

Code: [Select]
    static class TryGetObjectIdDemo
    {
        public static ObjectId? StringToObjectId(this Database db, string str)
        {
            long hnd;
            ObjectId objId;
            if(!Int64.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hnd))
                return null;
            if(!db.TryGetObjectId(new Handle(hnd), out objId))
                return null;
            return objId;
        }
    }




gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Select Entity By Handle
« Reply #3 on: January 06, 2012, 07:06:07 AM »
Hi Thorsten,

I didn't know (and can't find in the docs) any Database.TryGetObjectId() method.
Isn't it from your own library ?
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Select Entity By Handle
« Reply #4 on: January 06, 2012, 07:12:34 AM »

public bool TryGetObjectId(Handle objHandle, out ObjectId id);
 
Declaring Type: Autodesk.AutoCAD.DatabaseServices.Database
Assembly: Acdbmgd, Version=18.2.0.0
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.

kaefer

  • Guest
Re: Select Entity By Handle
« Reply #5 on: January 06, 2012, 07:16:50 AM »
I didn't know (and can't find in the docs) any Database.TryGetObjectId() method.

Amazing, those docs. It wasn't in 16.2, but I saw Andrey using it; it must have been added in any release between 2007 and 2009.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Select Entity By Handle
« Reply #6 on: January 06, 2012, 07:23:57 AM »
Thanks Kerry,

It seems it have been added to the 2012 (18.2) version I don't see it in the 2011 Object Browser.
Speaking English as a French Frog

cadpro

  • Guest
Re: Select Entity By Handle
« Reply #7 on: January 06, 2012, 09:16:05 AM »
Thanks all for your help. I basically wanted to highlight the selected entity. So this is what I did with gile's code as reference.

Code: [Select]

           Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim ln As Long = Convert.ToInt64(handle, 16)
                Dim oId As ObjectId = db.GetObjectId(False, New Handle(ln), 0)
                Dim ent As Entity = CType(tr.GetObject(oId, OpenMode.ForRead), Entity)
                If Not ent Is Nothing Then
                    ent.Highlight()
                End If
            End Using


Thanks