[Code] Tracking Models during a Ruby session

[Code] Tracking Models during a Ruby session

Postby thomthom » Sat Feb 11, 2012 1:16 pm

( Split from: [Code] Scarpino's SphereTool Sample ver 2.1.0 )

I've been using similar methods of keeping track of tools vs models in order to allow OSX to have the tool active for multiple models. However, I found problems with the Model class. I seemed to experience getting different Model objects during the same session. So when I tried to look up a tool for a given model it would fail. I also tried with model.guid, but that also changes. I think my current version use a combination of Model and model.guid.

It's been a while since I last looked at that issue. I'll see if I can dig up some notes on the specifics.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

Postby Dan Rathbun » Sat Feb 11, 2012 1:35 pm

Yes I do know that guid can change if the model is modified and saved.

On PC.. Sketchup.active_model.object_id stays the same, even if the model is modified, saved, modified more, saved as a different filename.
The object_id remains the same, until a new model is opened, or a modelfile is opened from disk.

The PC is the easiest of the 2, to handle.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

    Postby driven » Sat Feb 11, 2012 1:49 pm

    I found that Sketchup.active_model.definitions.object_id() was the most robust identifier on mac. see apple_pudding.
    0
    learn from the mistakes of others, you may not live long enough to make them all yourself...

    driven 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

    Postby thomthom » Sat Feb 11, 2012 1:52 pm

    Dan Rathbun wrote: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 wrote:I found that Sketchup.active_model.definitions.object_id() was the most robust identifier on mac. see apple_pudding.

    That's interesting.
    0
    Thomas Thomassen — SketchUp Monkey & Coding addict
    List of my plugins and link to the CookieWare fund
    User avatar
    thomthom 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

    Postby driven » Sat Feb 11, 2012 2:05 pm

    thomthom wrote:
    driven wrote: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.
    0
    learn from the mistakes of others, you may not live long enough to make them all yourself...

    driven 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

    Postby Dan Rathbun » Sat Feb 11, 2012 3:03 pm

    thomthom wrote:
    Dan Rathbun wrote: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 wrote: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.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

      Postby Dan Rathbun » Sat Feb 11, 2012 3:15 pm

      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?
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

        Postby Dan Rathbun » Sat Feb 11, 2012 3:27 pm

        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.
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

          Postby Dan Rathbun » Sat Feb 11, 2012 8:13 pm

          Post concerning tool icons, moved back to parent thread.
          0
          Last edited by Dan Rathbun on Sun Feb 12, 2012 11:39 am, edited 1 time in total.
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] Scarpino's SphereTool Sample ver 2.1.0

            Postby driven » Sun Feb 12, 2012 12:26 am

            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.
            Code: Select all
            #####################################################################
            ##
            #  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?
            0
            learn from the mistakes of others, you may not live long enough to make them all yourself...

            driven 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] Tracking Models during a Ruby session

            Postby dkendig » Tue Jul 09, 2013 4:40 pm

            from Paul on the SketchUp Extensibility Team:
            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.
            0
            Devin Kendig
            Developer
            User avatar
            dkendig 
             

            Re: [Code] Tracking Models during a Ruby session

            Postby thomthom » Fri Jul 12, 2013 10:23 am

            That's interesting!
            0
            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund
            User avatar
            thomthom 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] Tracking Models during a Ruby session

            Postby thomthom » Fri Jul 12, 2013 2:52 pm

            dkendig wrote:from Paul on the SketchUp Extensibility Team:
            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.
            0
            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund
            User avatar
            thomthom 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] Tracking Models during a Ruby session

            Postby eneroth3 » Sat Apr 28, 2018 6:15 pm

            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?
            0
            User avatar
            eneroth3 
            PluginStore Author
            PluginStore Author
             

            SketchUcation One-Liner Adverts

            by Ad Machine » 5 minutes ago



            Ad Machine 
            Robot
             



             

            Return to Developers' Forum

            Who is online

            Users browsing this forum: No registered users and 8 guests