Surprise! Native styles distributed with SketchUp have square brackets around their names and descriptions. When loading style files via Sketchup::Styles#add_style it comes as a surprise that strings properties are "bookended" with square brackets.
Ie, the "Simple Style" actually has this name: "[Simple Style]".
If an existing style is loaded into the model again the differencing number is added to the end outside of the brackets.
ie: "[Simple Style]1"
The documentation does not mention either the brackets nor the differencing.
So coders trying to get a reference via the Sketchup::Styles#[] method are clueless as to why the style is not found.
Would I be correct in assuming that the bracketing is used as marks for SketchUp to localize distributed style names and descriptions for the interface ?
In addition the result of the #add_style method is boolean when the average coder would expect a reference to the newly loaded style. Likely this cannot be changed at this late date.
So we are forced to do the snapshot and array subtract routine ...
before = styles.to_a
success = styles.add_style(path, false)
if success
style = (styles.to_a - before).first
# ... etc ...
A pattern like the above would be a benefit to the code example in the docs. IE:
folder = Sketchup.find_support_file('Styles')
filename = 'MyStyle.style'
filepath = File.join(folder,filename)
styles = Sketchup.active_model.styles
before = styles.to_a # snapshot
status = styles.add_style(filepath, false)
if !status
# ... bailout ...
else
style = (styles.to_a - before).first
# ... use style, etc ...
end
SketchUp Ruby API Documentation Issue
Surprise! Native styles distributed with SketchUp have square brackets around their names and descriptions. When loading style files via
Sketchup::Styles#add_style
it comes as a surprise that strings properties are "bookended" with square brackets. Ie, the "Simple Style" actually has this name:"[Simple Style]"
.If an existing style is loaded into the model again the differencing number is added to the end outside of the brackets. ie:
"[Simple Style]1"
The documentation does not mention either the brackets nor the differencing.
So coders trying to get a reference via the
Sketchup::Styles#[]
method are clueless as to why the style is not found.Would I be correct in assuming that the bracketing is used as marks for SketchUp to localize distributed style names and descriptions for the interface ?
In addition the result of the
#add_style
method is boolean when the average coder would expect a reference to the newly loaded style. Likely this cannot be changed at this late date.So we are forced to do the snapshot and array subtract routine ...
A pattern like the above would be a benefit to the code example in the docs. IE: