Is there a way to get the width of a label of a viewport without the underline?

Is there a way to get the width of a label of a viewport without the underline?

jfranzR9VWK
Contributor Contributor
313 Views
3 Replies
Message 1 of 4

Is there a way to get the width of a label of a viewport without the underline?

jfranzR9VWK
Contributor
Contributor

I have some code that will center a title on a viewport in a sheet. It works just fine, but it requires that the user size the underline to the title before running the command. Is there a way I can get the width, so that I can automatically size the underline before centering it. I saw that geometry is part of element, but I haven't figured out how it works. This is the working code that I have.

//vp is the viewport I am editing
Outline boxOutline = vp.GetBoxOutline();
Outline labelOutline = vp.GetLabelOutline();

//The label position is equal to the centerpoint minus half the width of the title
double labelOffest = Math.Abs((labelOutline.MaximumPoint.X - labelOutline.MinimumPoint.X) / 2);
vp.LabelOffset = new XYZ(vp.GetBoxCenter().X - boxOutline.MinimumPoint.X - labelOffest, -0.040, 0);

 

0 Likes
Accepted solutions (1)
314 Views
3 Replies
Replies (3)
Message 2 of 4

ctm_mka
Collaborator
Collaborator

To be clear, you want the width of the text displayed in a viewport title, which could be either the "view name" or "Title on sheet" parameter correct?

A couple of thoughts come to mind:

  • Option 1, create a view title without a line in the project, temporarily change the viewport to use this, then proceed as above.
  • Option 2, grab Title on sheet parameter, if "string.isnullorempty()" then grab view name. Proceed to create a text element with the same font size, then use its bounding box instead.
0 Likes
Message 3 of 4

jfranzR9VWK
Contributor
Contributor

Using the view name would work for my case.

0 Likes
Message 4 of 4

jfranzR9VWK
Contributor
Contributor
Accepted solution

The solution was easier than I thought. You simply change the length of the label line to 0, get the outline from the labelOutline, then use MinimumPoint and Maximum point to measure the width of the label.

vp.LabelLineLength = 0;
//Get the outlines for the viewport and the label
//Label outline depends on the length of the underlining bar or text, whichever is bigger
Outline boxOutline = vp.GetBoxOutline();
Outline labelOutline = vp.GetLabelOutline();

//calculate length of title then change label line length to match it
XYZ max = labelOutline.MaximumPoint;
XYZ min = labelOutline.MinimumPoint;
double labelLength = max.X - min.X;
0 Likes