DC / Dictionary Interaction
-
Split off from: http://forums.sketchucation.com/viewtopic.php?f=180&t=25386
@pauline01 said:
up
up??
I don't think you have solved your problem.
[REVISED - 28 JAN 2010]You can use:
%(#BF0000)[*object*.get_attribute('*MyDico*', *numfriend*)] %(#BF0000)[*object*.set_attribute('*MyDico*', *numfriend*, *namefriend*)]
OR also:
%(#BF0000)[*mod* = Sketchup.active_model *obj* = *mod*.selection[0] *dict* = *obj*.attribute_dictionary('*MyDico*')] %(#008000)[# to get attribute values] %(#BF0000)[*namefriend* = *dict*[*numfriend*]] %(#008000)[# to set attribute values] %(#BF0000)[*dict*[*numfriend*]=*namefriend*]
To deletedictionary key/value pairs, DO NOT USE:
%(#BF0000)[*object*.set_attribute('*MyDico*',*numfriend*,nil)]
instead USE:
%(#BF0000)[*object*.delete_attribute('*MyDico*',*numfriend*)]
OR:
%(#BF0000)[*dict*.delete_key(*numfriend*)]
To load your menu from a dictionary, DO NOT write your own loop.
This can result in a 'fencepost error', and other errors
_(where iterator vars are off by 1 ..etc.)
USE the built-in AttributeDictionary iterator methods .each, .each_key and .each_pair, like this:%(#BF0000)[*dict*.each_pair do |*numfriend*, *namefriend*|] %(#F0F0F0)[_]%(#BF0000)[*menu*.add_item("Delete a Friend(#{*namefriend*.to_s})"){ *dict*.delete_key(*numfriend*) } end]%(#008000)[# dict iterate]
__ -
@dan rathbun said:
DO NOT USE:
object.get_attribute
object.set_attribute
THEY ARE BUGGED AND DO NOT WORKDon't see where you got that from. I use them often and have no issues with them.
-
@thomthom said:
@dan rathbun said:
DO NOT USE:
object.get_attribute
object.set_attribute
THEY ARE BUGGED AND DO NOT WORKDon't see where you got that from. I use them often and have no issues with them.
They 'seem' to work. You can set a value, and then get it back. Which means it's saved 'someplace' in memory. But if you at the same time try to get a value, that was set thru the object.set_attribute, using the dict['key'] it's not in the dictionary. And dict.size returns 0. So there's some kind of 'break in the link' there.
I'll do more testing tomarrow, perhaps if I save the file, and reopen it, things will get 'linked' back up.
-
` model = Sketchup.active_model
#Sketchup::Model:0x84e3900
model.set_attribute('test', 'foo', 'bar')
bar
model.get_attribute('test', 'foo')
bar
d = model.attribute_dictionaries['test']
#Sketchup::AttributeDictionary:0xb66c360
d.each { |k,v| puts "#{k} - #{v}" }
foo - bar
nil
d.length
1
d['foo']
bar`
I'm not seeing that behaviour.Mind you, what is the object type of the value you try to set. The dictionaries doesn't handle all types. Strings, Numerics and Booleans etc. Not custom classes or Sketchup Classes.
-
@dan rathbun said:
turned off the DC extension (which WILL make all the dictionary methods no longer work except .name)
?
but the attribute methods existed in SU6 as well, right? then there was no DC... -
@dan rathbun said:
@thomthom said:
@dan rathbun said:
DO NOT USE:
object.get_attribute
object.set_attribute
THEY ARE BUGGED AND DO NOT WORKDon't see where you got that from. I use them often and have no issues with them.
They 'seem' to work. You can set a value, and then get it back. Which means it's saved 'someplace' in memory. But if you at the same time try to get a value, that was set thru the object.set_attribute, using the dict['key'] it's not in the dictionary. And dict.size returns 0. So there's some kind of 'break in the link' there.
I'll do more testing tomarrow, perhaps if I save the file, and reopen it, things will get 'linked' back up.
OK the test you wrote is the same one I had done at the console. I did it again and everything is fine.
So I'm happy to say it was just my own confusion. Either I corrupted SU memory testing a plugin or had turned off the DC extension (which WILL make all the dictionary methods no longer work except .name)
.. my Bad ! (OK, I revised my previous post.)
_ -
@thomthom said:
@dan rathbun said:
turned off the DC extension (which WILL make all the dictionary methods no longer work except .name)
?
but the attribute methods existed in SU6 as well, right? then there was no DC...I dunno, Thom, but that's what happens.
Perhaps the way they wrote the DC it's so integrated now that, it's really part of the program, not really an extension. (I have a mind to comment out all the Extension code in the DC registration script, and just make it load always. That way I won't ever accidentely ever turn it off again.)
It happened because, I was tracking down the source of JJ's eval warnings the day before I posted here. See:
(eval): warnings are caused by Dynamic Components extension!
and I guess I forgot to turn the DC back on.I'm happy to be wrong, and not have more bugs in the API to deal with!
_ -
hm... I need to look into this. Check what happens with my scripts under SU6...
Pauline: sorry for going a bit off topic. Maybe it's best if I split these last couple of posts into a new thread? Ok, Pauline & Dan?
Edit: Now split of - as you can see.
Advertisement