The function acedTextBox() finds the diagonal coordinates of a box that encloses a text entity. The function takes an argument, ent, that must specify a text definition or a string group in the form of a result-buffer list. The acedTextBox() function sets its p1 argument to the minimum XY coordinates of the box and its p2 argument to the maximum XY coordinates.
If the text is horizontal and is not rotated, p1 (the bottom-left corner) and p2 (the top-right corner) describe the bounding box of the text. The coordinates are expressed in the Entity Coordinate System (ECS) of ent with the origin (0,0) at the left endpoint of the baseline. (The origin is not the bottom-left corner if the text contains letters with descenders, such as g and p.) For example, the following figure shows the results of applying acedTextBox() to a text entity with a height of 1.0. The figure also shows the baseline and origin of the text.
The next figure shows the point values that acedTextBox() returns for samples of vertical and aligned text. In both samples, the height of the letters was entered as 1.0. (For the rotated text, this height is scaled to fit the alignment points.)
Note that with vertical text styles, the points are still returned in left-to-right, bottom-to-top order, so the first point list contains negative offsets from the text origin.
The acedTextBox() function can also measure strings in attdef and attrib entities. For an attdef, acedTextBox() measures the tag string (group 2); for an attrib entity, it measures the current value (group 1).
The following function, which uses some entity handling functions, prompts the user to select a text entity, and then draws a bounding box around the text from the coordinates returned by acedTextBox().
int tbox() { ads_name tname; struct resbuf *textent, *tent; ads_point origin, lowleft, upright, p1, p2, p3, p4; ads_real rotatn; char rotatstr[15]; if (acedEntSel("\nSelect text: ", tname, p1) != RTNORM) { acdbFail("No Text entity selected\n"); return BAD; } textent = acdbEntGet(tname); if (textent == NULL) { acdbFail("Couldn't retrieve Text entity\n"); return BAD; } tent = entitem(textent, 10); origin[X] = tent->resval.rpoint[X]; //ECS coordinates origin[Y] = tent->resval.rpoint[Y]; tent = entitem(textent, 50); rotatn = tent->resval.rreal; // acdbAngToS() converts from radians to degrees. if (acdbAngToS(rotatn, 0, 8, rotatstr) != RTNORM) { acdbFail("Couldn't retrieve or convert angle\n"); acutRelRb(textent); return BAD; } if (acedTextBox(textent, lowleft, upright) != RTNORM) { acdbFail("Couldn't retrieve text box coordinates\n"); acutRelRb(textent); return BAD; } acutRelRb(textent); // If not currently in the WCS, at this point add // acedTrans() calls to convert the coordinates // retrieved from acedTextBox(). p1[X] = origin[X] + lowleft[X]; // UCS coordinates p1[Y] = origin[Y] + lowleft[Y]; p2[X] = origin[X] + upright[X]; p2[Y] = origin[Y] + lowleft[Y]; p3[X] = origin[X] + upright[X]; p3[Y] = origin[Y] + upright[Y]; p4[X] = origin[X] + lowleft[X]; p4[Y] = origin[Y] + upright[Y]; if (acedCommandS(RTSTR, "pline", RTPOINT, p1, RTPOINT, p2, RTPOINT, p3,RTPOINT, p4, RTSTR, "c", 0) != RTNORM) { acdbFail("Problem creating polyline\n"); return BAD; } if (acedCommandS(RTSTR, "rotate", RTSTR, "L", RTSTR, "", RTPOINT, origin, RTSTR, rotatstr, 0) != RTNORM) { acdbFail("Problem rotating polyline\n"); return BAD; } return GOOD; }
The preceding example “cheats” by using the AutoCAD ROTATE command to cause the rotation. A more direct way to do this is to incorporate the rotation into the calculation of the box points, as follows:
ads_real srot, crot; tent = entitem(textent, 50); rotatn = tent->resval.rreal; srot = sin(rotatn); crot = cos(rotatn); . . . p1[X] = origin[X] + (lowleft[X]*crot - lowleft[Y]*srot); p1[Y] = origin[Y] + (lowleft[X]*srot + lowleft[Y]*crot); p2[X] = origin[X] + (upright[X]*crot - lowleft[Y]*srot); p2[Y] = origin[Y] + (upright[X]*srot + lowleft[Y]*crot); p3[X] = origin[X] + (upright[X]*crot - upright[Y]*srot); p3[Y] = origin[Y] + (upright[X]*srot + upright[Y]*crot); p4[X] = origin[X] + (lowleft[X]*crot - upright[Y]*srot); p4[Y] = origin[Y] + (lowleft[X]*srot + upright[Y]*crot);