How do I change to the default style? (Via Ruby)


#1

I’m looking at the Ruby docs, but I don’t get it.
I think it says do this, but it doesn’t work.

styles = Sketchup.active_model.styles
style1 = styles[0]
style2 = styles[“00Default Style.style”]


#2

That code snippet assign variables to the styles in the collections of model styles.

Are you trying to change the active style?
Or modify it’s properties?


#3

I am trying to finish writing a batch script, that changes all the files to the default style. I have written the rendering options before;
Sketchup.active_model.rendering_options[“BackgroundColor”] = 210,208,185
Sketchup.active_model.rendering_options[“BandColor”] = 0,0,0
Sketchup.active_model.rendering_options[“ConstructionColor”] = 0,0,0
Sketchup.active_model.rendering_options[“DepthQueWidth”] = 4
Sketchup.active_model.rendering_options[“DisplayColorByLayer”] = false
ETC, but the script doesn’t always change the style correctly. But everything else works ok. I’m just not sure, how to tell it to change the style to the default. Instead of all the rendering options code, I would just like to point to the default style.
Thanks.


Set line color using ruby api
#4

Batch convert to SU7.rb (6.2 KB)


#5

Do you have specific example?

The default? There is no absolute default. Each template has their own style. The style you try to switch to - is it loaded into the model?


#6

These are files I download from the warehouse, and with this batch script, I change the settings to the settings I like, 1/64 inch accuracy, etc. They are all saved in different styles, measurements, etc. The script does everything I want, but it won’t always change the style correctly. I just want to change the style, on all the files to
00Default Style.style.
It is located here…C:\Program Files (x86)\Google\Google SketchUp 7\Styles\Default Styles\00Default Style.style


#7

this works on a mac, but only if I use Test.FindFile

default_styles = Sketchup.find_support_files("","Styles").grep(/Default Styles/)
change_style = Test.FindFile(default_styles[0],"00Default Style.style")
styles = Sketchup.active_model.styles
status = styles.add_style(change_style, true)
styles.purge_unused

you still need to set your Units/Shadows etc, separately…
john


#8

Thanks John.
Didn’t work. Maybe, it’s just not an option.


#9

does this fail with a hardcoded path?
it should work from v6 on…

styles = Sketchup.active_model.styles
status = styles.add_style "C:\Program Files (x86)\Google\Google SketchUp 7\Styles\Default Styles\00Default Style.style ", true
UI.messagebox ('A style was added: ’ + status.to_s)
john


#10

It doesn’t work. It says: false. It is the right path though.
When I just use…
styles = Sketchup.active_model.styles
status = styles.add_style "C:\Program Files (x86)\Google\Google SketchUp 7\Styles\Default Styles\00Default Style.style "

I get Run aborted. Error: (eval):2:in ‘add_style’: wrong number of arguments(1 for 2)


#11

the second arg is the ,true at the end of line…
also I just noticed the escaping in the path isn’t showing correctly in the forum…
I believe you need \\ to escape the separator, and wrapping in args in () won’t hurt…
styles.add_style( “C:\\Program Files (x86)\Google\\Google SketchUp 7\\Styles\Default Styles\\00Default Style.style”, true )
john


#12

Still not working. I give up. Shouldn’t be this hard to add a default style.
Thanks for the help !!


#13

It is easier to just use Ruby forward slashes, (or single-quoted strings if you insist on backslashes.)


#14

As Dan said…
A file-path should usually use forward-slashes / as separators - "C:/P..." or 'C:/P...'
Inside "" or '' [double/single quotes]
You can use a back-slash \
BUT then inside "" you’ll then need to escape ALL of them as "C:\\P...", note that 'C:\P...' will work too…
It’s just easier to use a / :smiley:


#15

@DanRathbun or @TIG, why would my first example fail on windows?
do you not have TTest.FindFile or is the problem SU v7 related?
john


#16

You have NOT escaped all of the \ as \\ !
There are several lone \ !!
Read your text more carefully…


#17

@TIG
I was referring to the first example, that has no path separators…
that’s all left up to ruby/SU…

 default_styles = Sketchup.find_support_files("","Styles").grep(/Default Styles/)
change_style = Test.FindFile(default_styles[0],"00Default Style.style")
styles = Sketchup.active_model.styles
status = styles.add_style(change_style, true)
styles.purge_unused

john


#18

@John: Test::FindFile is not documented.

http://ruby-doc.org/stdlib-2.0.0/libdoc/test/unit/rdoc/Test.html

The Trimble team does add some methods to the Test module for internal use only.

Can your example be rewritten using one of the Enumerable methods mixed into the Array class ?


#19

[quote=“john_drivenupthewall, post:17, topic:10020”]
I was referring to the first example, that has no path separators…that’s all left up to ruby/SU…
default_styles = Sketchup.find_support_files("",“Styles”).grep(/Default Styles/)
change_style = Test.FindFile(default_styles[0],“00Default Style.style”)
styles = Sketchup.active_model.styles
status = styles.add_style(change_style, true)
styles.purge_unused
[/quote] So try…

default_style = Sketchup.find_support_file("00Default Style.style", "Styles/Default Styles")
styles = Sketchup.active_model.styles
status = styles.add_style(default_style, true)
styles.purge_unused

Assuming that the subfolder ‘Styles/Default Styles’ and the style ‘00Default Style.style’ exist it WILL work.

You seem to be making this more complex than it needs to be … :wink:


#20

no doubt …
it was a quick cobble and find_support_file is very fickle on a mac with sub-directories…
your version does work and avoids the need for two bites at the cherry…

@DanRathbun I knew it was undocumented but I couldn’t find the sub-dir that TIG’s version does …

cheers to both

john