How to write text on a diagram?

Started by spud, September 22, 2011, 08:26:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

spud

Hello,

I'm new here.  Been coding up some stuff to translate diagrams from company XML documents (1000s of them) to Visio.  I think I have most of what I need - shapes, connectors, a few rectangles, layers, etc.  I've been able to read the xml and drop those items on to a diagram in Visio with good results.   What I can't seem to figure out is how to draw free floating text on the diagram.  It must be so obvious that I can't see it.  Some of the company diagrams have text in different sizes and fonts just placed in random locations around the page. 

I have searched for what to do on that but can't find an example.   It's difficult to search on 'text' and find anything meaningful since the word is so generic. 

Would you mind pointing me in the right direction on how to 'drop' text on a diagram?  Any help on this would be appreciated.

Thanks,
spud

Paul Herber

A text shape is nothing special, draw a plain rectangle, set the line size to 0 and turn off any fill.
You might want to resize the text area depending upon the size of the text to stop the text from wrapping.
http://www.visguy.com/2009/05/06/top-twelve-text-tips/
will help.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

spud

So, I guess there is no specific 'text' shape.  Ok, that was too obvious for me to see that.  That should work.

Is there an obvious way to figure out the width an height of the rectangle using the text?  What I have is the text and a point size of the text and a font.    I'm assuming I'll have to know the font size, the characters I'm using and do some type of text metric to find that out. 

Thanks very much,
spud

Paul Herber

Quote from: spud on September 22, 2011, 09:27:59 PM
Is there an obvious way to figure out the width an height of the rectangle using the text?  What I have is the text and a point size of the text and a font.    I'm assuming I'll have to know the font size, the characters I'm using and do some type of text metric to find that out. 
Item 6 in the above article.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

spud

Ok, excellent.  I'll get that going and post my results.


Thanks much for the quick responses,
spud


spud

Well, ok, here's what I was able to come up with and it worked.  Don't know if I did any of the assignments into the cells properly but the code will create a line of text on the diagram without the rectangular line and filler.  I also colored the text, changed the size, the horizontal alignment and the style.   No fonts are specified in the original application, so I'll probably add a font parameter at a later time.


void VisioShapeAgent::DrawText( const _bstr_t & p_textLabel, double p_coords[], const _bstr_t * p_fontColor,
int p_fontSize, const _bstr_t & p_fontType, const _bstr_t & p_textAlign)
{
        //creating a zero area rectangle but at intended location, using the same location for x2, y2
IVShapePtr shape = m_VisioApp->ActivePage->DrawRectangle(
p_coords[0],p_coords[1],p_coords[0],p_coords[1]);

        //set the text
shape->Text = p_textLabel;

//font size
shape->GetCells("Char.Size")->FormulaU = "\"" + _bstr_t(p_fontSize) + " pt.\"";

//wrap the box around the text
IVCellPtr aCell = shape->GetCellsSRC(visSectionObject,visRowXFormOut,visXFormWidth);
aCell->FormulaU = "\"=GUARD(MAX(TEXTWIDTH(TheText),8*Char.Size))\"";
aCell = shape->GetCellsSRC(visSectionObject,visRowXFormOut,visXFormHeight);
aCell->FormulaU = "\"=GUARD(MAX(TEXTHEIGHT(TheText,Width)))\"";

//Remove rectangular filler
aCell = shape->GetCellsSRC(visSectionFirstComponent,visRowComponent,visCompNoFill);
aCell->FormulaU = "=TRUE";

//Remove the rectangular line
aCell = shape->GetCellsSRC(visSectionFirstComponent,visRowComponent,visCompNoLine);
aCell->FormulaU = "=TRUE";

//Font color
aCell = shape->GetCellsSRC(visSectionCharacter, visRowCharacter, Visio::visCharacterColor);
aCell->FormulaU = "=RGB(" + p_fontColor[0] + "," + p_fontColor[1] + "," + p_fontColor[2] + ")";

//Font type or style: Normal, Italic, Bold, Underline
aCell = shape->GetCellsSRC(visSectionCharacter, visRowCharacter, visCharacterStyle);
VisCellVals fontType = Visio::VisCellVals(0);  //Normal; default
if( p_fontType == _bstr_t("Italic") )
{
fontType = VisCellVals::visItalic;
}
else if( p_fontType == _bstr_t("Bold") )
{
fontType = VisCellVals::visBold;
}
else if( p_fontType == _bstr_t("Underline") )
{
fontType = VisCellVals::visUnderLine;
}
aCell->ResultIU = fontType;

//horizontal alignment: Left, Center, Right.  Visio has more.
aCell = shape->GetCellsSRC(visSectionParagraph,visRowParagraph,visHorzAlign);
VisCellVals horizontalAlign = Visio::VisCellVals(0); //Left; default
if( p_textAlign == _bstr_t("Center") )
{
horizontalAlign = VisCellVals::visHorzCenter;
}
else if( p_textAlign == _bstr_t("Right") )
{
horizontalAlign = VisCellVals::visHorzRight;
}
aCell->ResultIU = horizontalAlign;
}



Any suggestions for improved value assignment would be appreciated.  It is in C++ since the legacy application was done in C++ as well. 

Thanks very much for the help!

spud