Changing Style of Page (scene) programmatically, "Page.use_style=" not returning nil

StackOverflow https://stackoverflow.com/questions/15388223

  •  23-03-2022
  •  | 
  •  

Pergunta

I want to change the Style of a scene (programmatically called a Page), using the SketchUp Ruby API.

To begin with, I'm just testing this through the Ruby Console in SketchUp. I've prepended all the code I run with > for clarity.

Assuming I start off with all the Styles:

> styles = Sketchup.active_model.styles
#<Sketchup::Styles:0xad9cd08>

I add a new Style to the "pool" of Styles, and check if I can reference it

> styles.add_style("MyStyle.style", false)
true
> styles["MyStyle"]
#<Sketchup::Style:0xad6f45c>

When I try to apply the Style to a Page (in the SketchUp GUI, a Page is called a scene) using use_style=:

> pages = Sketchup.active_model.pages
#<Sketchup::Pages:0xad9cccc>
> pages[0].use_style?
true
> pages[0].style
#<Sketchup::Style:0xad7fdac>
> pages[0].style.name
[Design Style]
> styles["MyStyle"].name
MyStyle
> pages[0].use_style = styles["MyStyle"]
#<Sketchup::Style:0xad6f45c>

According to the API docs, use_style= should always return nil, so I feel like something is amiss here. In any case, here is what I get from pages[0].style after the code above is run:

> pages[0].style
#<Sketchup::Style:0xad7fdac>
> pages[0].style.name
[Design Style]

After looking around a bit more in the API, I found the Page's update method. Trying it out, however, yielded the same results. (In any case, I don't feel it's relevant here, as I think it's based on the current view on of the user.)

> pages[0].update(2)
true
> pages[0].style.name
[Design Style]

Am I missing something blatant here, or is the API just broken?

(I am running SketchUp 8.0)

Foi útil?

Solução

This should work:

styles.selected_style = styles["MyStyle"]
pages[0].update(2)

The API docs isn't always correct. Some times the info is lacking, some times it's confusing, some times it's plain wrong.

Page.use_style= sets whether or not the Page (called Scenes now in SketchUp) will store the Styles. If you select your page (scene) in the Scenes toolwindow you see there is a series of checkboxes that let you save camera, hidden geometry, layers, section planes etc. This is the property you set with Page.use_style=

For instance, select a page (scene) and make a reference to it - keep the Scenes window open and types these commands:

page.use_style = false
UI.refresh_inspectors

Notice that the "Styles and Fog" checkbox got unticked. Also note that you need to call UI.refresh_inspectors in order for the UI to update when setting this property.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top