The example in this section shows use of the AcGiTextStyle class. It draws a rectangle around a piece of AcGi text that can be oriented and located anywhere in space.
The normal and direction vectors of the text must be perpendicular to each other. If you're unsure of the directions, consider the direction to be along the X axis and the normal along the Z axis in a right-handed coordinate system. Calculate the Y axis from these. Then the cross product of the Y axis to Z axis will give you the normal plane's interpretation of the direction. Be sure that the direction is not aligned with the normal, or you will not have a direction with respect to the normal.
The AcGiTextStyle::loadStyleRec() function loads a font if it is not already loaded. (This function does not load an ACAD STYLE.) Its return values are as follows:
Another file (not FONTALT) opened in place of -BigFont file name
Another file (not FONTALT) opened in place of file name
BigFont file name failed to be loaded
File name failed to be loaded
Files opened as called for
Text can be scaled in a number of ways. Use AcGiTextStyle::setTextSize() to scale the width and height of the text at the same time. Use setXScale() to scale the width of the text. Use setTrackingPercent() to specify how the characters of a particular font are placed next to each other. If you specify a value of 1.0, the spacing does not change; if you specify less than 1.0, the characters will squeeze together; and if it's more than 1.0, the characters will be farther apart. This example sets the tracking percent to a value of .80.
The AcGiTextStyle::extents() function returns the world coordinate size of the text's bounding box. If the penups argument is kTrue, then any undrawn pen moves made while the user was drawing the text will be included in the bounding box. The raw option tells the calculation to ignore escape code processing (so that “%%%” would not be interpreted as a single percent sign but as three percent signs). Before calling the extents() function, you must call AcGiTextStyle::loadStyleRec() to load any recent style changes into the graphics system.
The following example draws text and then draws a bounding box around a portion of the text.
Adesk::Boolean AsdkTextStyleSamp::subWorldDraw(AcGiWorldDraw* pW) { AcGePoint3d pos(4.0, 4.0, 0.0); AcGeVector3d norm(0.0, 0.0, 1.0); AcGeVector3d dir(-1.0, - 0.2, 0.0); TCHAR *pStr = _T("This is a percent, '%%%'."); int len = _tcslen(pStr); AcGiTextStyle style; AcGeVector3d vec = norm; vec = vec.crossProduct(dir); dir = vec.crossProduct(norm); style.setFileName(_T("txt.shx")); style.setBigFontFileName(_T("")); int status; if (!((status = style.loadStyleRec()) & 1)) pStr = _T("Font not found."); pW->geometry().text(pos, norm, dir, pStr, len, Adesk::kFalse, style); pos.y += 2.0; style.setTrackingPercent(0.8); style.setObliquingAngle(0.5); // You must call loadStyleRec() again after changing // the style's properties in order for the current style // settings to be loaded into the graphics system. // Otherwise, the extents calculation may be incorrect. // style.loadStyleRec(); AcGePoint2d ext = style.extents(pStr, Adesk::kFalse, _tcslen(pStr), Adesk::kFalse); pW->geometry ().text(pos, norm, dir, pStr, len, Adesk::kFalse, style); // Draw a rectangle around the last text drawn. // First you have to create a polyline the size of the // bounding box, then you have to transform it to the // correct orientation, and then to the location of the // text. // Compute the matrix that orients the box. // AcGeMatrix3d textMat; norm.normalize(); dir.normalize(); AcGeVector3d yAxis = norm; yAxis = yAxis.crossProduct(dir); yAxis.normalize(); textMat.setCoordSystem(AcGePoint3d(0.0, 0.0, 0.0), dir, yAxis, norm); // Create the bounding box and enlarge it somewhat. // double offset = ext.y / 2.0; AcGePoint3d verts[5]; verts[0] = verts[4] = AcGePoint3d(-offset, -offset, 0.0); verts[1] = AcGePoint3d(ext.x + offset, -offset, 0.0); verts[2] = AcGePoint3d(ext.x + offset, ext.y + offset, 0.0); verts[3] = AcGePoint3d(-offset, ext.y + offset, 0.0); // Orient and then translate each point in the // bounding box. // for (int i = 0; i < 5; i++) { verts[i].transformBy(textMat); verts[i].x += pos.x; verts[i].y += pos.y; verts[i].z += pos.z; } pW- >geometry().polyline(5, verts); return Adesk::kTrue;