Layout api style set_dimension_units

Hello fellow developers. I’m currently exploring the Layout api and I’m having some troubles which I hope some of you might have encountered already.

I’m having troubles changing the dimension style to fractional inches. I looks like they just keep showing up as decimals.

# p1, p2, p1_3d, p2_3d and layout_su_model are given...
new_dimension = Layout::LinearDimension.new(p1, p2, 5.mm)
new_dimension.style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 16) # attempt 1
document.add_entity(new_dimension, layout_su_model.layer_instance.definition, layout_su_model.page)
new_dimension.style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 16) # attempt 2
p1_connection =  Layout::ConnectionPoint.new(layout_su_model, p1_3d)
p2_connection =  Layout::ConnectionPoint.new(layout_su_model, p2_3d)
new_dimension.connect(p1_connection, p2_connection)
new_dimension.style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 16)  # attempt 3

Anyone tackled this already? I’m confident I must be overlooking something…

Hi @kengey,

Sorry I didn’t see your post earlier!

The #style method actually returns a copy of the entity’s style attributes, rather than a direct reference. What you need to do is save the style to a variable, set the dimension units, then apply that style back to the original entity. It’s unintuitive and non-ideal, but was necessary due to the internal architecture.

Example:

new_dimension = Layout::LinearDimension.new(p1, p2, 5.mm)
style = new_dimension.style
style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 16)
new_dimension.style = style

Hope that helps,
Adam

1 Like

Thanks Adam,

Kind regards,

Kenny

Hi @adam,

Although this works for set_dimension_units it doesn’t seem to work for other styling options like start_arrow_type. The following code only applies the dimension units to the styling of the entity and none of the others:

# get current style object
current_style = new_dimension.style
# overwrite things
current_style.start_arrow_type = Layout::Style::ARROW_FILLED_TRIANGLE
current_style.start_arrow_size = 2
current_style.end_arrow_type = Layout::Style::ARROW_FILLED_TRIANGLE
current_style.end_arrow_size= 2
current_style.stroke_width = 0.5
current_style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 0.065)
# and apply to our freshly created dimension
new_dimension.style = current_style

Only the dimension units will be correctly applied in this case it seems.

Hi @kengey,

This is another area of complexity with the style attributes. To properly set the arrow type and size, you need to apply it using a sub-entity style. For example:

style = new_dimension.style

# Set the dimension units on the root style
style.set_dimension_units(Layout::Style::FRACTIONAL_INCHES, 0.065)

# Get the dimension text style to make text changes
dim_text_style = style.get_sub_style(Layout::Style::DIMENSION_TEXT)
dim_text_style.text_color = Sketchup::Color.new(255, 0, 0, 255)

# Get the dimension line style to make arrow head changes
dim_line_style = style.get_sub_style(Layout::Style::DIMENSION_LINE)
dim_line_style.start_arrow_type = Layout::Style::ARROW_FILLED_TRIANGLE
dim_line_style.start_arrow_size = 2
dim_line_style.end_arrow_type = Layout::Style::ARROW_FILLED_TRIANGLE
dim_line_style.end_arrow_size= 2
dim_line_style.stroke_width = 0.5

# Apply the dimension text style changes to the dimension style
style.set_sub_style(Layout::Style::DIMENSION_TEXT, dim_text_style)
# Apply dimension line style changes to the dimension style
style.set_sub_style(Layout::Style::DIMENSION_LINE, dim_line_style)
# Apply the dimension style changes to the dimension
new_dimension.style = style

You may also want to get the DIMENSION_START_EXTENSION_LINE or DIMENSION_END_EXTENSION_LINE sub style objects if you want to modify the extent lines.

Hope this helps,
Adam

Thanks again Adam