sketchucation logo sketchucation
    • Login
    ⌛ Sale Ending | 30% Off Profile Builder 4 ends 30th September

    [Code] Tracking Models during a Ruby session

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 5.1k Views 5 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.
    • thomthomT Offline
      thomthom
      last edited by

      @dan rathbun said:

      On PC.. Sketchup.active_model.object_id stays the same, even if the model is modified, saved, modified more, saved as a different filename.
      But model.object_id reflects the Model object, right? if I'm getting a different Model object, for the same model, then surely model.object_id will be different as well?

      @driven said:

      I found that Sketchup.active_model.definitions.object_id() was the most robust identifier on mac. see apple_pudding.

      That's interesting.

      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
        driven
        last edited by

        @thomthom said:

        @driven said:

        I found that Sketchup.active_model.definitions.object_id() was the most robust identifier on mac. see apple_pudding.

        That's interesting.

        It works with multiple unsaved windows, where everything else appears to fail. It also copes if I run a second instance of SU, even if it's the same version, which access's the same folders.

        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

          @thomthom said:

          @dan rathbun said:

          On PC.. Sketchup.active_model.object_id stays the same, even if the model is modified, saved, modified more, saved as a different filename.
          But model.object_id reflects the Model object, right?

          YES

          @thomthom said:

          if I'm getting a different Model object, for the same model, then surely model.object_id will be different as well?

          YES

          OK ThomThom, I see what you mean. The model object (incl. object_id,) will change, before it's saved, after it's modified, BUT not immediately !! Seems like, after modifying, it stays the same, until you change the active tool. Kind of like the C-side is doing things in a batch way.

          This is annoying.

          I'm not here much anymore.

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

            OK there is two approaches to try. Presbyterian (top down,) and Congregational (bottom up.)

            The Presbyterian, ie, keeping track of models, and assigning a tool instance to each model, seems to not work. (At least as simply as we wish it to.)
            I've noticed some behavior, that John eludes to. That is that behind the scenes, when the app thinks it needed, it creates a new model object, but transfers, or copies, things over to the new object. The entities object is one I noticed. On PC, when you open a new empty model, calling methods against the old model object, returns child objects of the new model object.

            Anyway.. I am going to think about doing it the opposite way around, always creating one tool instance, and having that instance check what the active_model is before doing anything.. like whenever a click state changes, etc.

            John... have you tested what happens in tools when you switch model document windows ?
            Does deactivate() or suspend() get called in the "from" tool, and activate() or resume(), in the model switched to?

            I'm not here much anymore.

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

              I suppose the "easy" way is to just reset the tool and all it's variables, if the active model changes at all.

              I can't really see keeping a database of all the tool's instance variables, for each model. (ie an Array of Hashes) That's just too much.

              I am leaning back towards my original thinking... the Tool class is best implemented as a singleton class, AND when the active model changes, just reset the tool and all it's variables. Then notify users they can't switch horses in midstream.

              I'm not here much anymore.

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

                the post notifications are bad today... just came back by chance and there 4 new posts, I've had no notifications...

                when I was looking at how you might track tool usage, across models in the same session, the only reliable method was to assign a unique id and save it to file. I thought I'd posted the WIP script, but maybe I just Pm'd a couple of mac user's for testing.

                #####################################################################
                ##
                #  Apple Pudding      Wed Jan 11 00;12;31 +0000 2012
                #
                ##      
                #####################################################################
                # load("/Library/Application Support/Google SketchUp 8/SketchUp/plugins/apple_pudding_proof.rb")
                require('sketchup.rb')  # d/s restricts access to SU only calls
                #Sketchup.send_action("openRubyConsole;")
                #module JcB    # later dan...
                def proof      # just type proof after loading
                #---------------------------------------------------------------------
                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
                #--------------------------------------------------------------------
                begin
                path = "/tmp/" 
                # our Directory current_path? If not make one.
                cacheDir = "/tmp/macSUcache"  # is CLEARED after a system Shut Down not Restart which is all I normally do.
                if not File.exists? cacheDir 
                then   Dir.mkdir(path + 'macSUcache')
                end 
                #check
                    puts "current path is; " + cacheDir.to_s 
                    puts "cacheDir " + "is a " + defined? cacheDir 
                rescue
                    puts "what now???"
                else
                    puts "Congratulations  no errors!"
                end
                #--------------------------------------------------------------------
                ##
                begin
                unique_Id =(cacheDir + "/" + current_title.to_s + "_su.txt")
                #check
                    puts "this models cache ID; " + unique_Id.to_s    
                    puts "unique_Id " + "is a " + defined? unique_Id 
                rescue
                    puts "again?...what now???"
                else
                    puts "Congratulations  no errors!"
                end
                #--------------------------------------------------------------------
                ## Proof
                #######################################################################
                begin
                # Create/open a current model file in read-write [adds new to end of existing] mode, use "w" for over-writing 
                f = File.open unique_Id, "a+"  
                #
                # Write an array of Strings to the file 
                nums = [["\n"] + [Time.now] + ["", "", "", "", "", ""].sort_by { rand }]
                f.puts  nums
                #check
                  #  puts "the File; " + unique_Id.to_s + " is now open? " + File.exists?(unique_Id).to_s
                    puts "f " + "is a " + defined? f 
                rescue
                    puts "again?...what now???"
                else
                    puts "Congratulations  no errors!"
                #should return true   is now writen
                #
                ##
                # Rewind the file, read its lines, and print the third line
                # #
                # 
                f.rewind 
                f.readlines.join  
                ensure
                  f.close unless f.nil?
                #check
                 puts "Congratulations  no errors!"
                 puts "this File; " + unique_Id.to_s + " now closed? " + f.closed?.to_s 
                 end #begin
                 end #def
                ##############################################################################
                 ##--------------------------------------------------------------------------
                # 
                # # TODO; add a 'Clear This' method
                # def clr_proof #
                # # Delete the current model file 
                # f = File.open unique_Id
                # deleteCur = File.delete unique_Id
                # puts "does tmp file for " + unique_Id.to_s + " still Exists? " + File.exists?(unique_Id).to_s 
                # end #clr_proof
                #
                ##
                ###############################################################
                =begin
                ##
                # purge all .su.txt files
                #eraseSUtxt=(system "e- cd / \n rm -rf /tmp/macSUcache/*su.txt")
                #eraseDir =(system "e- cd / \n rm -rf /tmp/macSUcache")
                #eraseSUtxt
                #eraseDir
                ##
                ################################################################
                =end 
                
                #end #def proof
                #end #module
                #--------------------------------------------------------------------
                
                # file_loaded(File.basename(__FILE__))
                
                #--------------------------------------------------------------------
                #####################################################################
                
                

                aside, when testing this code block, I had to double comment my system calls, as I couldn't copy/paste from this, without seeing errors in SU?

                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

                  Post concerning tool icons, moved back to parent thread.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • dkendigD Offline
                    dkendig
                    last edited by

                    from Paul on the SketchUp Extensibility Team:

                    @unknownuser said:

                    Yes, I believe that would work fine. However, I recommend using the Entity member entityID instead, like this: Sketchup.active_model.definitions.entityID. This one also doesn't persist between sessions, but it is a unique index across all entities Sketchup has loaded (even with multiple models), and active_model.definitions is a SketchUp entity.

                    Devin Kendig
                    Developer

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      That's interesting!

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

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        @dkendig said:

                        from Paul on the SketchUp Extensibility Team:

                        @unknownuser said:

                        Yes, I believe that would work fine. However, I recommend using the Entity member entityID instead, like this: Sketchup.active_model.definitions.entityID. This one also doesn't persist between sessions, but it is a unique index across all entities Sketchup has loaded (even with multiple models), and active_model.definitions is a SketchUp entity.

                        So under OSX this returns a new integer for each model opened.
                        But under Windows it returns the same integer even if you open or create a new model. It appear to reuse the DefinitionList entity.

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

                        1 Reply Last reply Reply Quote 0
                        • eneroth3E Offline
                          eneroth3
                          last edited by

                          Any news on this topic? Can model specific states be safely indexed by the model object now or do we still need to use hacks such as using the DefinitionsList object id as index?

                          My website: http://julia-christina-eneroth.se/

                          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