Question about Handles

Question about Handles

amasutti
Explorer Explorer
627 Views
8 Replies
Message 1 of 9

Question about Handles

amasutti
Explorer
Explorer

If I erase an entity in a drawing. Is it possible that the handle can get reused or is it unique for the entire life of the drawing?

0 Likes
Accepted solutions (1)
628 Views
8 Replies
Replies (8)
Message 2 of 9

LDShaw
Collaborator
Collaborator

@_gile 
Answered this a while ago. 

The Handle is a persistent identifier which is unique per drawing.

The ObjectId is a non-persistent identifier (reassigned each time the drawing is opened) which is unique per session.

In an AutoCAD session, you can get the Handle of a DBObject from its ObjectId using the ObjectId.Handle property.

On the other hand, you can get the the ObjectId of a DBObject from its Handle (and its Database) with the Database.GetObjectId() method.

Another answer was 

From myself I will add that if you open multiple drawings, the handle may have the same value for different objects in different drawings. ObjectId is unique even if you have multiple drawings open and several different objects in the different drawings have a handle with the same value. You have to be very careful to avoid confusion by using the handle between different drawings.

If you want a store a handle of an object in a drawing (in Xrecord for example), always use handle because it is unique and constant in the drawing. Handle shall be unique in the drawing, even if you delete the object and add a new one. If you want to open object, use the ObjectId, because it is unique among drawings.



Message 3 of 9

GeeHaa
Collaborator
Collaborator

Thanks for the reply.

So to be clear, if an object gets erased. that handle is permanently retired per that drawing. And any new objects get the next handle in the database. In other words the first Handle starts at one and they just keep incrementing from there never looking back.

0 Likes
Message 4 of 9

_gile
Consultant
Consultant

Not exactly, but you can exchange a database resident entity with a non database resident  entity so that the non database resident entity gets the handle of the database resident entityy using the HandHoverTo method.

This must be done outside any standard transaction using Open/Close methods or an OpenCloseTransaction.

Here's a simple example which "erases" a circle and "reuse" the circle handle for a newly created arc.

#pragma warning disable 0618
        public static void ReplaceCircleWithArc(ObjectId circleId, double startAngle, double endAngle)
        {
            if (circleId.ObjectClass != RXObject.GetClass(typeof(Circle)))
                return;
            using (var circle = (Circle)circleId.Open(OpenMode.ForWrite))
            using (var arc = new Arc(circle.Center, circle.Radius, startAngle, endAngle))
            {
                arc.Normal = circle.Normal;
                circle.HandOverTo(arc, true, true);
            }
        }
    }
#pragma warning restore 0618

 

EDIT:

OOPS! I did not see this message was in the LISP forum (thaught it was .NET one).

I'm afraid this is not possible with LISP



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9

LDShaw
Collaborator
Collaborator

That is my understanding. If I recall the old oops command (In the very earliest version of acad was a lisp command. I've not thought of that command as an option in forever.) will bring that handle back but it's not real helpful.
Lol I see _gile answered. I will leave it to him. 

0 Likes
Message 6 of 9

cadffm
Consultant
Consultant

Hi Amasutti,

 

the answer is: The Handle can be "reuse" in this file, but not in the same edit session,

you un-delete all objects all the deleted objects of this session.

 

Exception: The handover way, but I think this is "not clean".

 

Deleted entities are purged from the drawing when the drawing is exited.

 

My Q: You asking in customize/Lisp board, but the only way to controle the handles are from outside or by .net/C++

What is the background &reason of your question?

 

Sebastian

Message 7 of 9

GeeHaa
Collaborator
Collaborator

The reason I'm asking is I have a program that can glean dwg names, attribute values and handles from a set of drawings and place them in an excel spread sheet where they can be edited then sent back to the drawings. I was just worried that if someone gleaned the values. then came back a few days later and wanted to send them back that a handle could have been reused. It's ok if it's been erased (it will be skipped) the problem would be if the handle points to another object. The program is written in .net. I just put it in the customization section because I figured a handle is a handle in any language and my shortcut to Autodesk points to the customization section first. Thanks All for the replies.

Message 8 of 9

cadffm
Consultant
Consultant

Hi,

 

initially missing informations: I have a program / written in .net

This Board here is for general customization (using the program command/settings/options, and menu macro, script AND AutoLISP-Programming),

programming in .net -> see the next door. DOOR

 

So, for usual the old Handle will not be re-used. - If we talking about drawing&editing with autodesk products.

Autodesk products using the HANDSEED and handseed is going upwards only.

https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseSer...

 

HTH

 
 

 

 

Sebastian

Message 9 of 9

vince_aman
Participant
Participant
Accepted solution

While the handle is an associated value to the entity, and unique within the Dwg. It's not necessarily guaranteed to not be reused later if the entity is deleted and a new object is created later... To guarantee, I would attach Xdata to the entity with a <Type><timestamp> code. If you copy the object to another Dwg, the Xdata would stay persistent, where the handle can be re-assigned. I've done all of this in Lisp... but, certainly should be possible in .net 

0 Likes