How to change style for specific page which generating Layout

Hi there,

I am trying to generate multiple 2D drawings(on different pages) for my model and want a different style for a particular page.
Following is a code snippet of what I want to achieve

bounds = Geom::Bounds2d.new(window.flatten) # Bound for 2D view
    if(page_name.include?("material"))
      current_selected_style = Sketchup.active_model.styles.selected_style
      cabinet_style = File.join("cabinet_style.style")
      styles = Sketchup.active_model.styles
      styles.add_style(cabinet_style, true)
      layout_model = Layout::SketchUpModel.new(Sketchup.active_model.path, bounds)
      Sketchup.active_model.styles.selected_style = current_selected_style
    else
      layout_model = Layout::SketchUpModel.new(Sketchup.active_model.path, bounds)
    end

but when I generate the Layout file the page I am interested in does not have the style I want. How to achieve this?

You are using the selected_style which only points at what the user (or code) has selected (in the Styles inspector panel) for a subsequent operation.

This is different from the actual active_style that a model or page uses. The doc for the latter method states …

While #selected_style is the style being selected in the Style Browser, the #active_style is a different object also including any unsaved style changes. These changes are silently dropped once a new style is selected. To save these changes to the selected style, call #update_selected_style.


Your use of the File.join method is frivolous. You’ve given only 1 filename argument so there is nothing really to join together into a path string.

Also, you must supply absolute path strings to files for most API methods.
Meaning that you should never rely upon the current working directory to be any particular disk location. Any other extension code might have changed the working directory and not changed it back. (Which is naughty!)

On Windows a version specific user style files would be at:

ver = Sketchup.version.to_i

File.join( ENV["APPDATA"], "SketchUp/SketchUp 20#{ver}/SketchUp/Styles" )

On Mac, they’d be at something like:

File.join( ENV["HOME"], "Library/Application Support/SketchUp 20#{ver}/SketchUp/Styles" )

Simply adding a style object to the model’s styles collection does not make it the active style in use.
But the use of the true 2nd argument does.

After setting the active style, set the current scene page in your model to use the new style and update the scene page, then save the model file before creating the LayOut file:

model = Sketchup.active_model
pages = model.pages
page  = pages.selected_page
page.use_style= true
styles.add_style(cabinet_style_path, true)
page.update(PAGE_USE_RENDERING_OPTIONS)
model.save
layout_model = Layout::SketchUpModel.new(model.path, bounds)

Ruby does not require parenthesis around if, unless, until or while expressions.
Ie, Ruby is not JavaScript. It is far more readable to use a space instead, like:

    if page_name.include?("material")

Thanks @DanRathbun