We're working on a huge porting from 100+ MA applications from V8i to Connect Edition.One of the problems we're encountering regards the old mdlText_extract API.In order to determine the size of an existing text element, we so far use the following code:
MSElement* pElement = ...;
char text[256];TextSize tileSize;TextSize textSize;mdlText_extract(NULL, NULL, NULL, NULL, text, NULL, NULL, NULL, &tileSize, &textSize, pElement);printf("Text '%s' has tileSize (%.1lf, %.1lf) and textSize (%.1lf, %.1lf)\n", text, tileSize.width, tileSize.height, textSize.width, textSize.height);
It has the following output:
Text 'TTF3' has tileSize (525.0, 1200.0) and textSize (1901.5, 1200.0)Text 'ODF10' has tileSize (420.0, 1200.0) and textSize (1785.7, 1200.0)
I tried the code as suggested by the link:
https://communities.bentley.com/products/programming/microstation_programming/f/microstation-programming---forum/112563/connect-c-textblock-code-to-replace-mdltext_extract
ElementHandle eh(pElement, ACTIVEMODEL);TextBlockPtr textBlock = TextHandlerBase::GetFirstTextPartValue(eh);DPoint2d fontSize = textBlock->GetRunPropertiesForAdd().GetFontSize();DRange3d range = textBlock->GetNominalRange();printf("FontSize of %ls: (%.1lf, %.1lf)\n", textBlock->ToString().GetWCharCP(), fontSize.x, fontSize.y);printf("low/high: (%.1lf, %.1lf) / (%.1lf, %.1lf)\n", range.low.x, range.low.y, range.high.x, range.high.y);printf("doc.width: %.1lf\n", textBlock->GetProperties().GetDocumentWidth());
But this gives me the output:
FontSize of TTF3: (0.0, 12.0)low/high: (0.0, -12.0) / (0.0, 0.0)doc.width: 0.0FontSize of ODF10: (0.0, 12.0)low/high: (0.0, -12.0) / (0.0, 0.0)doc.width: 0.0
In other words, I get the height of the text (expressed as the fontsize) but not the width.
Any idea how to get the actual width and height of an existing text element?I didn't find anything useful within the TextBlock class.
Thanks,
Robert Kock
Robert Kock said:Determine the size of an existing text element
While snuggled up amid autumn blankets, I was browsing the API help — it's a great way to combat insomnia — when I came across the TextString class: Used to draw a single-line collection of like-formatted characters.
TextString
It has some methods that may be useful to you...
GetExtents
GetWidth
GetHeight
And several more. It's not clear how one constructs a TextString. It looks like you have to use a static Create() method, but I can find no examples.
Create()
Regards, Jon Summers LA Solutions
Jon Summers said: It's not clear how one constructs a TextString.
TextString is only used for view decoration (for example, you want to draw a text at a fixed point, which doesn't move when you change the view), not for normal text element.
I today faced a Japanese user's query for how to extract text element's information and wrote below test code in MSCE-U16. It seems work well.
void extractTextInfo(ElementId myId) { ElementHandle eh(myId, ACTIVEMODEL); TextBlockPtr txtBlk = TextElemHandler::GetFirstTextPartValue(eh); WString myStr = txtBlk->ToString(); mdlDialog_dmsgsPrint(myStr.GetWCharCP()); TextBlockPropertiesCR blkProp = txtBlk->GetProperties(); bool isVertical = blkProp.IsVertical(); myStr.Sprintf(L"isVertical = %d", isVertical); mdlDialog_dmsgsPrint(myStr.GetWCharCP()); ParagraphPropertiesR paraProp = txtBlk->GetParagraphPropertiesForAddR(); TextElementJustification just = paraProp.GetJustification(); double lineSpacing = paraProp.GetLineSpacingValue(); myStr.Sprintf(L"just = %d, lineSpacing = %.2f", just, lineSpacing); mdlDialog_dmsgsPrint(myStr.GetWCharCP()); RunPropertiesR runProp = txtBlk->GetRunPropertiesForAddR(); DPoint2d fontSize = runProp.GetFontSize(); myStr.Sprintf(L"Width = %.2f, Height = %.2f", fontSize.x, fontSize.y); //in UOR unit mdlDialog_dmsgsPrint(myStr.GetWCharCP()); }
Is there a way to use TextBlockPtr to get the actual height and width of the whole string "Abc"?
zwindcn said:Is there a way
Please respect the best practices! Do not steal this over a year old discussion with own new question (especially when your question is specifically about TextBlockPtr).
Ask in a new post and share all mandatory information like product and its exact version.
Regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Sorry. I just saw Fu replied the post a month ago. I will ask in a new post.