Sketchup.active_model.set_attribute
-
Thanks Todd
Actualy, I need this to keep track of some add_text objects I create - so that I can run through the 'test' array and delete the objects that I have created, even if the file has been saved and Sketchup restarted. Will this be possible ?
Is this right ?
"rc = Sketchup.active_model.set_attribute("TM_area", "test", "false") ; "
do you save the test array ?As you (and surely many others) can se, I'm not a programmer, just an architect, that have a need for a special function, and tries to create this by looking in others scripts and in this forum
Maybe there should be two groups under Ruby, one for programming ruby/sketchup and one for using plugins ?
Thanks
JorgensenThanks
-
No, that doesn't save a test array at all. that saves a key of "test", and it's value" of "false".
If you want to save an array worth of data, and then get it later, here is the code to do that:
def save_array(data) model = Sketchup.active_model dictionary_name = "TM_area" ; i = 1 ; data.each {|value| model.set_attribute(dictionary_name, "var"+i.to_s , value ) ; i+= 1 ; } end ; def get_array() model = Sketchup.active_model any_array = [] ; dictionary_name = "TM_area" ; i = 1 ; while true value = model.get_attribute(dictionary_name, "var"+i.to_s ) ; break if !value any_array.push(value) i += 1 ; end return any_array ; end ; myarray = [] ; myarray.push("This is the first entry") ; myarray.push("Entry #2...") ; myarray.push("3rd and final entry.") ; save_array(myarray) ; myarray = [] ; # reset array to empty myarray = get_array() ; i = 1 ; myarray.each {|value| puts "Value #{i} is #{value}." ; i+=1 ; }
-
Hi Todd
Thanks for your input - it works well
I have one problem though:
I use the get_array to recive an array (logic)
I then run through this array - witch contains some entityIDs and if an object, with this ID exist, I delete it from the model and from the array. I then save the array again.The problem is, that even if I delete some keys from the Array - so lets say the original array has an length of 10 and I then delete five objects, the length of the array is 5. I then save the array - from i=1 to i=6 - but the old ones from 6-11 still exits in the dictionaryname - how do I delete the var1 - var10 before updating the dictionaryname ?
Thanks
Jorgensen -
I guess it could be solved via delete_key - but how ?
-
I have just tried to save
model.set_attribute("TM_area", "objects", data)where data is an array - I can close sketchup and open gain, reload the file and the array can be read again as model.get_attribute("TM_area", "objects")
Works fine - BUT
why the #Β€"#Β€%Β€%) have the entityID for the objects I created change when I reload the file ? now I can't keep track of them anymore
a guite tired
Jorgensen -
I guess this has something to do with it
"The entityID is not persistent between sessions."But how can I then recognise objects when a file is reloaded ?
Thanks
Jorgensen -
I'm certain there is a delete method to get rid of a dictionary key. Before saving the array, delete all existing keys. It's a brainless (simple) way to do it.
How? Figure it out! Use the Ruby console to experiment. http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-AttributeDictionary.html#delete_key
You can't use entityID across SketchUp session. Assign them your own ID as a key/value. "Jorg1", "Jorg2", "Jorg_Cabinet", "Jorg_Oil_Platform", etc. Whatever you want to call them. Use some Dictionary name that you expect will be unique in the bog scheme of things. Perhaps a dictionary names like "Jorgensen" or something unique to you / about you. I use "Smustard".
Todd
-
YES YES YES YES
I found the solutionI add a entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)
This is kept and can be used to recognise the objects - finally the script can be finshedI won't disturb anymore - I did that quite alot - sorry
Thanks Todd (thanks all)
Jorgensen
Here it is in action. This will be very useful for me when sketching projects with focus on areas
-
@jorgensen said:
YES YES YES YES
I found the solutionI add a entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)
This is kept and can be used to recognise the objects - finally the script can be finshedI won't disturb anymore - I did that quite alot - sorry
Thanks Todd (thanks all)
Jorgensen
Here it is in action. This will be very useful for me when sketching projects with focus on areas
Be advised that entityID can change when a file is saved and loaded. I hope that isnt a problem for your application.
-
That should not be a problem, because I'm adding my own attribute:
entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)that I use to keep track of objects.
Thanks
Jorgensen
Advertisement