Issues with set_attributes Method and Components
-
For almost two years now I have been pulling out my hair trying to figure out why my start_operation and commit_operation in the truss module was throwing an error, see this thread:
https://forums.sketchup.com/t/ruby-operation-left-open/79279
I finally had enough with this (minor) bug and pulled apart my code and systematically eliminated everything until I came to this single block of code (which is the cause of the error):
da = 'dynamic_attributes' t_def.set_attribute da, 'trussfamily', @Trussfamily t_def.set_attribute da, '_trussfamily_label', 'TrussFamily' t_def.set_attribute da, '_trussfamily_formlabel', 'Truss Family' t_def.set_attribute da, '_trussfamily_units', 'STRING' t_def.set_attribute da, '_trussfamily_access', 'VIEW'
With this block I am trying to set an attribute on the truss component using the dynamic attributes library. For some reason SketchUp does not like it and it makes the operation methods above blow up.
Alternatively I thought why not forego the dynamic library stuff and just try this:
lib = 'medeek_truss_eng' t_def.set_attribute lib, 'trussfamily', @Trussfamily
However, it does not seem that one can set attributes to a component definition, which is very frustrating.
I'm now looking for a way to set attributes to a component definition (without side effects), if this possible, but I'm coming up empty.
-
You should be able to set attributes for almost anything in SketchUp - including component-definitions...
However, if these attributes are to be reused later, then you must do it by getting/setting the definition's attributes, NOT by getting/setting the attributes of an instance of the definition.Is that perhaps where you are tripping up ?
Instances and definitions are quite separate things, so decide what you want to test/affect...
PS: In your example '@Trusstype' must be a compatible 'type' of data - I assume it's a string ?
Most 'types' are usable, but one of the few 'types' you need to tweak is a 'hash' - which can be converted to an 'array' for storage, and back again into a 'hash' when it's read back in... -
The variable "t_def" is the component definition so I am feeding it the definition and not an instance. However in my second example it does not seem to work.
Yes, @Trussfamily is a string.
-
How do you 'know' for sure that setting the definition's attribute fails ?
You give insufficient detail.
If you useget_attribute()
on the exact same definition [or instance.definition] you usedset_attribute()
on, then it should work.Make sure the attribute-dictionary-name and the key are exactly the same in both cases, and that you have a default value set up too - like
result = t_def.get_attribute(lib, 'trussfamily', 'WTF!')
.
Ifresult == @Trussfamily
it's working, but ifresult == 'WTF!'
it's not !Make sure you have presets for the 'value' set/get
@Trussfamily
, and of course the 'lib
' - which should be the string-name of the dictionary, e.g.lib = 'medeek_truss_eng'
.It a dictionary of that exact name doesn't exist it's made, if it exists then it's reused...
Personally I'd use dictionary name starting with an uppercase letter, and all keys in lowercase.
There's less confusion that way...To check what's dictionaries and keys/values are attached to a definition use something like this.
if t_def.attribute_dictionaries t_def.attribute_dictionaries.each{|d| puts "DICTIONARY == #{d.name}" d.each_pair{|k, v| puts "#{k} = #{v}" } } else puts "NO DICTS !" end
Advertisement