Get Entity by using Entity Handle

Get Entity by using Entity Handle

vkpunique
Advocate Advocate
2,577 Views
6 Replies
Message 1 of 7

Get Entity by using Entity Handle

vkpunique
Advocate
Advocate

I am looking for code to get entity from entity handle. I am write code to highligh blockreferences using it's entity handle. User will enter entity handle manually and It will highligh block reference if match is found.

 

I am confused how to approach this. Can i get object Id from entityhandle? or Can i get access to entity directly with entity handle?

Currently i am looping through all objects in drawing to find my match. I am looking for more efficient way to solve my problem.

0 Likes
Accepted solutions (1)
2,578 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use the Database.GetObjectId method or, better, the undocumented Database.TryGetObjectId one.

        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var pr = ed.GetString("\nEnter Handle: ");
            if (pr.Status != PromptStatus.OK)
                return;
            if (long.TryParse(
                pr.StringResult,
                System.Globalization.NumberStyles.HexNumber,
                System.Globalization.CultureInfo.CurrentCulture,
                out long value))
            {
                if (db.TryGetObjectId(new Handle(value), out ObjectId id))
                {
                    if (id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Entity))))
                    {
                        using (var tr = db.TransactionManager.StartTransaction())
                        {
                            var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                            entity.Highlight();
                            tr.Commit();
                        }
                    }
                    else
                    {
                        ed.WriteMessage("\nNot an entity Handle.");
                    }
                }
                else
                {
                    ed.WriteMessage("\nInvalid Handle.");
                }
            }
            else
            {
                ed.WriteMessage("\nNot an hex number");
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

vkpunique
Advocate
Advocate

Thanks Gile, That TryGetObjectId function looks like gem, I was wondering about it's compatibility with older version of autocad. 

One more question, for highlight object  editor.SetImpliedSelection(objectsIdArrary) is simpler or Entity.Highlight?

Currently I am using code below to get object ID, it's from Kean Walmsley blog.

private static ObjectId GetObjectIdFromDataBase(string entityHandle)
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database database = doc.Database;

                //Convert Hexadecimal string to 64 bit integer
                long entityHandleLongInt = Convert.ToInt64(entityHandle, 16);

                //Create Handle from long integer
                Handle handle = new Handle(entityHandleLongInt);

                //Get Object Id for Handle
                ObjectId objectId = database.GetObjectId(false, handle, 0);

                return objectId;
            }
            catch (System.Exception)
            {
                return ObjectId.Null;
            }
        }

 

0 Likes
Message 4 of 7

_gile
Consultant
Consultant

@vkpunique  a écrit :

I was wondering about it's compatibility with older version of autocad. 


It depends on what you mean with "older versions". If I remember well, it was available before A2013.

 


@vkpunique  a écrit :

One more question, for highlight object  editor.SetImpliedSelection(objectsIdArrary) is simpler or Entity.Highlight?


Entity.Highlight only highlights the entity (display effect) and do not 'select' it.

 


@vkpunique  a écrit :

Currently I am using code below to get object ID, it's from Kean Walmsley blog.


Using TryParse and TryGetObject is safer, overall if the handle has been entered by the user.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

vkpunique
Advocate
Advocate
what's difference between highlight and selection? I just thought both are same thing. what's point of highlighting if you're not going to use it for other commands?
0 Likes
Message 6 of 7

_gile
Consultant
Consultant

@vkpunique  a écrit :
what's difference between highlight and selection? I just thought both are same thing. what's point of highlighting if you're not going to use it for other commands?

Highlighting just change the entity appearance. It can be used to visually differentiate certain entities.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 7

_gile
Consultant
Consultant

See the Entity.Highlight topic from the developper's documentation.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes