Unique identifier for model?
-
Can anyone think of a way to get a unique id for a model?
Model.guid
says it changes when the model is saved. I'd like it to stay the same...EDIT: Actually I need to generate a tempfile per-model and I'm looking for a unique name that I don't need to manage myself...
-
Can you just assign it? Have an observer that write a unique number to each model as it is opened? I think that will frustrate some people as SU will then ask for the model to be saved when they close it, even when they have not done anything themselves to it. But it might work.
If you could avoid doing it on the model opening event that might be nice. I've got a script where I don't add a unique model number until after they access my plugin. Then the model gets a unique number, I add a bunch of layers and do a lot of database maintenance, but by waiting until they actually use my plugin, it saves it from messing with their models that are not related to my plugin.
-
I actually don't need the identifier to tie back to the model, only one-way so that I can delete the tempfile when I'm done using it. So I don't need to save anything on the model and therefore modify it for saving.
Anyway, sounds like you are managing the ID's yourself... I was hoping to avoid that.
-
Oh, the dreaded temp file deletion scenario. Let me know if you get a good solution.
I currently just delete my standard temp folder anytime my plugin starts. Which isn't good if the user tries to have models open at once But Windows does throw an error when I try to delete it, if SketchUp still has it open. It won't let me delete it because it is already in use, and my plugin just crashes. So I've wondered if that is a solid approach - just test to see if I can delete the temp folder, if not, assume that another copy of SU is open and using it and then make a new folder with an incremental name so that I can try to delete the next time my plugin is run.
I'm not sure how Mac handles the firectory deletion though. It might just delete it whether or not another app has it open. I'm not sure how to test the validity and soundness of that approach.
-
How about this:
Use a lock file. When the plugin starts, create a locked file with
File.flock(File::LOCK_EX)
if it doesn't exist. If it does, check for an existing lock withFile.flock(File::LOCK_NB|File::LOCK_EX)
. This will return false if it's locked, or if not, it returns 0 and obtains a lock.If there is a lock, then another instance of SU is open, so don't delete tempfiles.
I tested this in irb and it seems to work.
-
@draftomatic said:
EDIT: Actually I need to generate a tempfile per-model and I'm looking for a unique name that I don't need to manage myself...
So you just need a unique string? Don't you think the model.guid is unique enough?
dc85e69e-a682-4e47-832e-3c5c53772e42
There is always a statistical chance that GUIDs (GUID in general) produce two of the same - but that's extremely unlikely.
I don't know much about GUIDs and hashes though - but maybe make a hash of the model GUID with the current timestamp?
"#{model.guid}#{Time.now}".hash
produce something like this:148413033
Or maybe just drop the hash all together...
But if you want to make sure you're not overwriting a new file, why not just test if the file exist and increment an integer at the end of the string if it does?
-
@thomthom said:
@draftomatic said:
EDIT: Actually I need to generate a tempfile per-model and I'm looking for a unique name that I don't need to manage myself...
So you just need a unique string? Don't you think the model.guid is unique enough?
dc85e69e-a682-4e47-832e-3c5c53772e42
I will probably do this; I was just hoping there was a way to get a unique model id that sketchup keeps track of...
The problem of tempfile deletion has become more interesting. I think my solution above will work, which may be useful to others since I see other sketchucation posts about this and nobody seems to have a good solution.
-
I use the method similar to what I mentioned above in one of my plugins, generating a unique HTML file for a WebDialog subclass where each instance needs a unique temp file:
<span class="syntaxdefault">unique_seed </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"#{self.object_id}#{Time.now.to_i}"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hash</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">abs<br />filename </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"webdialog_#{unique_seed}.html" </span><span class="syntaxdefault"></span>
The main problem was cleaning up, as finalizers doesn't work under OSX, nor does
at_exit
which makes it impossible to clean up the temp files if SU where to be closed. I need to perform an extra cleanup operation when SU starts. -
Yes the problem comes when you have multiple SU processes running (I'm guessing users do this more on OSX because of the way windows are managed). So when the plugin starts and tries to delete tempfiles, there could be another SU using it. Hence the file lock.
Advertisement