[Code] Tracking Models during a Ruby session
-
@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.
-
@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.
Butmodel.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.
-
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 ?
Doesdeactivate()
orsuspend()
get called in the "from" tool, andactivate()
orresume()
, in the model switched to? -
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. -
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?
-
Post concerning tool icons, moved back to parent thread.
-
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.
-
That's interesting!
-
@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. -
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?
Advertisement