Layout dimension style to dash

I want the dash style:

image

I tried the code:

dim = Layout::LinearDimension.new(point1, point2, 1)
# Change to dashed line
style = dim.style
style.stroke_pattern = Layout::Style::STROKE_PATTERN_DASH_SPACE
dim.style = style

doc.add_entity(dim, layer, page)

Nothing changed. The dimension was still a solid line. How can I change it to dashed style?

You need to go down to sub-entity level (sub styles).
Layout::Style#set_sub_style method.

Use the Constat in Layout::Style e.g.

Layout::Style::DIMENSION_START_EXTENSION_LINE
Layout::Style::DIMENSION_END_EXTENSION_LINE
Layout::Style::DIMENSION_LINE
Layout::Style::DIMENSION_LEADER_LINE

Then set sub style on each desired sub-entity. Eg. for the dimension line will be something like this:

style = dim.style
sub_style = style.get_sub_style(Layout::Style::DIMENSION_LINE)
sub_style.stroke_pattern = Layout::Style::STROKE_PATTERN_DASH_SPACE
style.set_sub_style(Layout::Style::DIMENSION_LINE, sub_style)
dim.style = style

Edit: tried and tested example:

doc = Layout::Document.new
dim = Layout::LinearDimension.new(Geom::Point2d.new(3,3), Geom::Point2d.new(5,3), 2)
doc.add_entity(dim, doc.layers.first, doc.pages.first)
text = dim.text.plain_text
anchor_type = Layout::FormattedText::ANCHOR_TYPE_BOTTOM_CENTER
dim.text = Layout::FormattedText.new(text, Geom::Point2d.new(4,2), anchor_type)

style = dim.style
[Layout::Style::DIMENSION_START_EXTENSION_LINE,
Layout::Style::DIMENSION_END_EXTENSION_LINE,
Layout::Style::DIMENSION_LINE,
Layout::Style::DIMENSION_LEADER_LINE].each{|sub|
  sub_style = style.get_sub_style(sub)
  sub_style.stroke_pattern = Layout::Style::STROKE_PATTERN_DASH_SPACE
  style.set_sub_style(sub, sub_style)
}
dim.style = style

path = File.join(ENV['Home'], 'Downloads', 'mydoc.layout')
status = doc.save(path)
2 Likes

@Henry12 Notice how @dezmo’s example first adds the object to the document?

This is because most LayOut entities are attached to the document using hardcoded default properties. So trying to change the style properties before attaching to the document has no effect.

The mantra is: Attach first, modify afterward.

2 Likes