How can change the position of FormatText in LinearDimension by RubyAPI

The following codes do not take effect

doc = Layout::Document.new
dim = Layout::LinearDimension.new(Geom::Point2d.new(3,3), Geom::Point2d.new(5,3), 2)
dim.text.transform!(Geom::Transformation2d.translation(Geom::Vector2d.new(0,5)))
doc.add_entity(dim, doc.layers.first, doc.pages.first)

image

I do not know if you can produce this style of dimension.

However, as we have found other topics, It is best (and often required) to add entity objects to the Layout document before modifying them. Otherwise the hard-coded style defaults for the entity class will override any changes you attempt to make before the entity is “owned” by the document.

FormattedText is one of these quirky entity classes.

It’s ok by Layout. move the FormatText.

Yes, I know. You told me something like this before
And I’ve tried. Same.

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)
dim.text.transform!(Geom::Transformation2d.translation(Geom::Vector2d.new(0,5)))

Thinking a loud… (I’m just a beginner about Layout API too)

According to API doc:

. A LinearDimension is composed of the following sub-entities:

  • two ‘extension lines’ that connect to the Entity being dimensioned.
  • a ‘dimension line’ connecting the ends of the leaders. This may be represented by one or two Paths depending on appearance.
  • an optional ‘leader line’ that is used for small LinearDimensions.
  • a ‘dimension text’ that displays the LinearDimension’s text.

There are six points that may be modified for a LinearDimension:

  • two ‘connection points’ that define the start and end of the LinearDimension.
  • two ‘extent points’ that define the start and end of the dimension line and are the ends of the two extension lines.
  • two ‘offset points’ that define the starting points of the extension lines.

So, if I understand right the “six points that may be modified” does not include the text position.

However perhaps looking this:
The #text= method sets the Layout::LinearDimension’s FormattedText.

makes me think that modifying FormattedText. anchor_point, anchor_type or grow_mode can do something that helps… ??

The anchor type is the location within the text bounds for the position of the anchor, or more specifically, the text is justified around the anchor point according to it’s anchor type. (There are figures showing text anchors for text drawn to SketchUp’s viewport in Sketchup::View#draw_text documentation. The anchor types for Layout::FormattedText is similar.)

So the type needs to likely be bottom center in this scenario. Ie: Layout::FormattedText::ANCHOR_TYPE_BOTTOM_CENTER

The anchor point is what (I think) would be moved in this scenario.

I think from previous testing that the default for grow mode is: Layout::FormattedText::GROW_MODE_UNBOUNDED
So no need of change for this.

1 Like

But,I can change the position of FormattedText in LinearDimension by Layout.
So, I think I think there should be a way to change the position of the text by RubyAPI.
And, the key point I want to explain is not the ( anchor_point , anchor_type or grow_mode), but the text position can be assigned arbitrarily.
Maybe, official developers may know more details that are not on the API

There are many things that not exposed to API… :blush:

…however, "change the position of FormatText in LinearDimension by RubyAPI " seems to be possible: :innocent:
(Tested in SU2021)

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)
path = File.join(ENV['Home'], 'Downloads', 'mydoc.layout')
status = doc.save(path)

I got this (the animation shows, it is still “follow” to the dim size:
laydimtext

“Play” with it… :wink:

2 Likes

nice, It’s ok. But, can not drop the leader line of FormattedText by RubyAPI.

1 Like

Sure, because a leader is “part” of LinearDimension :wink:

The #leader_line_type= method sets the type of leader line the Layout::LinearDimension is using.

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)

dim.leader_line_type = Layout::LinearDimension::LEADER_LINE_TYPE_HIDDEN

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

2 Likes

Sorry, I only followed the Style API.

I find a new question about anchor point of FormattedText.
The anchor position does not seem to match exactly.

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
text_p = Geom::Point2d.new((5 + 3) / 2.0, 4)
dim.text = Layout::FormattedText.new(text, text_p, anchor_type)
dim.leader_line_type = Layout::LinearDimension::LEADER_LINE_TYPE_HIDDEN

cp = Geom::Point2d.new((5 + 3) / 2.0, 3)
path = Layout::Path.new(cp, text_p)
doc.add_entity(path, doc.layers.first, doc.pages.first)

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

It looks like LayOut is correcting your leader because the leader is going down from the dimension. (Ie, it “knows” that the leader should not go across the dimension’s text.)

In the situation shown the text anchor type should be ANCHOR_TYPE_TOP_CENTER.

I think the text should be placed on the anchor point I set. And It has nothing to do with alignment.

doc = Layout::Document.new
s_p, e_p = Geom::Point2d.new(3, 5), Geom::Point2d.new(3, 3)
dim = Layout::LinearDimension.new(s_p, e_p, 1)
doc.add_entity(dim, doc.layers.first, doc.pages.first)

text = dim.text.plain_text
anchor_type = Layout::FormattedText::ANCHOR_TYPE_CENTER_CENTER
text_p = Geom::Point2d.new((s_p.x + e_p.x) / 2.0 + 1, (s_p.y + e_p.y) / 2.0)
dim.text = Layout::FormattedText.new(text, text_p, anchor_type)

cp = Geom::Point2d.new((s_p.x + e_p.x) / 2.0, (s_p.y + e_p.y) / 2.0)
path = Layout::Path.new(cp, text_p)
doc.add_entity(path, doc.layers.first, doc.pages.first)

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

There is no rule in the following situations.
image

When I add a FormattedText, it’s ok.

doc = Layout::Document.new
s_p, e_p = Geom::Point2d.new(3, 5), Geom::Point2d.new(3, 3)
anchor_type = Layout::FormattedText::ANCHOR_TYPE_CENTER_CENTER
text = Layout::FormattedText.new("test_text", e_p, anchor_type)
doc.add_entity(text, doc.layers.first, doc.pages.first)

path = Layout::Path.new(s_p, e_p)
doc.add_entity(path, doc.layers.first, doc.pages.first)

file = File.join(ENV['Home'], 'Downloads', 'mydoc.layout')
status = doc.save(file)
UI.openURL file

image