• Login
sketchucation logo sketchucation
  • Login
πŸ”Œ Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Unique identifier for model?

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 3 Posters 367 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    draftomatic
    last edited by 22 Nov 2011, 07:13

    Can anyone think of a way to get a unique id for a model?

    Model.guidsays 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...

    1 Reply Last reply Reply Quote 0
    • C Offline
      Chris Fullmer
      last edited by 22 Nov 2011, 07:24

      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.

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

      1 Reply Last reply Reply Quote 0
      • D Offline
        draftomatic
        last edited by 22 Nov 2011, 07:48

        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.

        1 Reply Last reply Reply Quote 0
        • C Offline
          Chris Fullmer
          last edited by 22 Nov 2011, 08:02

          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.

          Lately you've been tan, suspicious for the winter.
          All my Plugins I've written

          1 Reply Last reply Reply Quote 0
          • D Offline
            draftomatic
            last edited by 22 Nov 2011, 08:59

            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 with File.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.

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 22 Nov 2011, 09:00

              @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?

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • D Offline
                draftomatic
                last edited by 22 Nov 2011, 09:20

                @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.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 22 Nov 2011, 09:42

                  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&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">"webdialog_#{unique_seed}.html"&nbsp;</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. 😞

                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    draftomatic
                    last edited by 22 Nov 2011, 19:01

                    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.

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    1 / 1
                    • First post
                      1/9
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement