Mac Unique 'per model' ID: question
-
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 -
@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.
seeModelObserver#onPreSaveModel()
andModelObserver#onPostSaveModel()
. -
@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::Listeruse the local pointer to call methods:
mylist = lister.new("List of Models")`
-
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 ?
-
@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
-
@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
Advertisement