sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Mac Unique 'per model' ID: question

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 191 Views 2 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
      driven
      last edited by

      hi all,

      I'm creating unique id's for any models, including unsaved, using

      su_Id = true
      
      begin
      #
      model = Sketchup.active_model() 
      su_Id = model.definitions.object_id()
      has_title = model.title()
      add_title = "Unsaved_" + su_Id.to_s
      #
      # First check if there's an active model name         
        if has_title.to_a.length == 1 then
          current_title = has_title
        else
         current_title = add_title
        end
      rescue 
         # .. handle error
         #  UI.messagebox("You need an open .skp for this to work")
          puts "You need an open .skp for this to work"
      else
      #check
          puts "model name is; " + current_title.to_s
          ensure
          puts "current_title " + "is a " + defined? current_title 
          puts "Congratulations  no errors!"
      end
      

      It works very well, it inside in my module, but I want use it elsewhere and don't know which bit's need to be constants or def's to use in other sub-modules.

      At this stage things go wrong when I try to wrap them.
      john

      learn from the mistakes of others, you may not live long enough to make them all yourself...

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @driven said:

        I'm creating unique id's for any models, including unsaved, using

        su_Id = model.definitions.object_id()

        object_id is a Ruby integer, that is only unique within a single session. It will be reused in the next session. (The possibility exists that you may get that integer assigned to another model, in another session.)

        Sketchup already has a unique model numbering feature, that is automatically saved in the model.
        see:
        Model.guid()

        It will change if you save the model, so you'll have to track the data under the old guid, and rekey it with the new guid after saving.
        see ModelObserver#onPreSaveModel() and ModelObserver#onPostSaveModel()
        .

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @driven said:

          It works very well, .. but I want use it elsewhere and don't know which bit's need to be constants or def's to use in other sub-modules.

          You want to write a library submodule, that can be used by all your various other plugin submodules.

          module Driven
          
            module StatLib
          
              def self.libtask
                # method code here
              end # method def
          
            end # submodule Driven;;StatLib
          
          end # module Driven
          

          You would save the above file (as "statlib.rb" for example,) within your "driven" subdir, perhaps within a "lib" subsubdir.

          Then in ANOTHER file, for a plugin perhaps:

          require('driven/lib/statlib')
          
          module Driven
          
            module NiftyPlugin
          
              public
          
              def self.some_module_method
                # call your lib method
                Driven;;StatLib.libtask
                # other code here
              end # method def
          
              class << self
          
                private
          
                def some_private_method( arg1, arg2 )
                  # call your lib method
                  Driven;;StatLib.libtask      
                end
          
              end # proxy class
          
            end # submodule Driven;;NiftyPlugin
          
          end # module Driven
          

          .. and you'd save this file in it's own plugin subdir of your "driven" dir.

          You can create local vars, module vars, or constants that just point at another module, so you can give a short "nickname" to a long qualified, multi-nested module name:
          Let's say there was some lib module, that had a class you wanted to use.
          (This is a fictious example😞
          SKX::Lib::GUI::DialogHelper::Lister

          Inside your local namespace, you can create a "nickname to it:
          ` lister = SKX::Lib::GUI::DialogHelper::Lister

          use the local pointer to call methods:

          mylist = lister.new("List of Models")`

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            Your other option is to write a special mixin module and then mixin it into your other submodules. This is a bit advanced.

            Have you read the old "Pick-Axe" Ruby book ?

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              driven
              last edited by

              @dan rathbun said:

              Your other option is to write a special mixin module and then mixin it into your other submodules. This is a bit advanced.

              I agree, but will have another look at your other links,

              @unknownuser said:

              Have you read the old "Pick-Axe" Ruby book ?

              yes, and too many others, I've gone back to mainly using "Mr Neighborly's Humble Little Ruby Book" http://humblelittlerubybook.com/book/html/chapter1.html because it's 1.8.5 and most things work in mac SU.

              thanks for the comments

              john

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              1 Reply Last reply Reply Quote 0
              • D Offline
                driven
                last edited by

                @dan rathbun said:

                object_id is a Ruby integer, that is only unique within a single session. It will be reused in the next session. (The possibility exists that you may get that integer assigned to another model, in another session.

                With this 'Untitled[0..-1] Sketchup' is always reports 'Unsaved_260629430_su.txt' until one of them is has been written to, then the number sequence randomizes.

                If I 'Save' an 'Unsaved_260629430_su.txt' the full script will (eventually) update and re-name it's file.

                I also have a cleanup sequence for closed unsaved models.

                I'll try and wrap it all, and see how it goes.

                john

                learn from the mistakes of others, you may not live long enough to make them all yourself...

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

                Advertisement