Jump to content

What is a handle ?


FranAJA

Recommended Posts

  • 2 weeks later...

Thanks a lot guys! Another question about the handle: is there a way to convert string to handle? I somehow need to temporarily store the handles in json and reload them. However I found that if I pass the handle as string to function like


handle='A77637D8'
vs.DelObject(handle)

it won't work.

Edited by ge25yak
Link to comment

Word of advice.  Don't try and store and reuse handles outside of VW.

 

If you really are not doing anything else in VW between uses you MIGHT be ok. But if you do any other operations inside VW after saving the handle it is very likely that whatever handle you have stored will no longer be correct. And if you close and reopen the file between uses you are almost guaranteed to have a handle stored that points to nothing.

 

The correct data structure to use for storing a way to retrieve an object is to use a UUID.  Take a look at the GetObjectUUID and GetObjectByUUID functions.

 

If you truly want to store handles, can you store them a LongInts instead of Strings? That is what the handle actually is, a Long Integer.

 

 

Link to comment
1 hour ago, ge25yak said:

Thank a lot guys! Another questions about the handle: is there a way to convert string to handle? I somehow need to temporarily store the handles in json and reload them. However I found that if I pass the handle as string to function like


handle='A77637D8'
vs.DelObject(handle)

it won't work.

 

As said before storing handles in variables as string permanentely is not really a good move as they change every single execution of the script. 
That handle that you have should be a direct return of a method which returns a handle.

like hobj = vs.LNewObj (method that return the handle of last new object

def vs.LNewObj(): return HANDLE

you can use this handle in the method which asks for, example vs.DelObject(hobj)

another example below:

 

    p1 = (0, 0)
    p2 = (25, 25)
    vs.Rect(p1, p2) # method that does not return handle
    hrect = vs.LNewObj() # get the handle of the last new object created
    vs.DelObject(hrect) 

 

Edited by FranAJA
Link to comment

Just to clarify @FranAJA‘s remarks, storing a handle into a variable is fine, but you should not store a handle into a parameter or any other kind of persistent storage. These were the warnings about handles at the top of the thread.

 

A handle is nothing more than the location of a drawing object in memory. You can only reliably depend on that block of memory referring to the object you want for the duration of the script, and as long as the script doesn’t delete or otherwise invalidate the object. 
 

If you need to store a list of objects for later use, use their UUID or assign name, which can then be used to retrieve the handle. 

Link to comment
38 minutes ago, JBenghiat said:

Just to clarify @FranAJA‘s remarks, storing a handle into a variable is fine, but you should not store a handle into a parameter or any other kind of persistent storage. These were the warnings about handles at the top of the thread.

 

A handle is nothing more than the location of a drawing object in memory. You can only reliably depend on that block of memory referring to the object you want for the duration of the script, and as long as the script doesn’t delete or otherwise invalidate the object. 
 

If you need to store a list of objects for later use, use their UUID or assign name, which can then be used to retrieve the handle. 

 

I spent two months to understand this concept 😄

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...