Skip to content
On this page

Text with box

Next, we will introduce how to use mxcad plug-in to achieve text framing in CAD drawings. In this function, the user chooses the target text entity independently and adds an outer border to the text entity according to the location of the text. The text frame function can help users highlight the text, or modify the display form.

Function implementation

  1. Select the target text entity

We can call MxCADResbuf set filters, Through MxCADUtility.userSelect() Method filters out single-line text objects and multi-line text objects in the selected entity.

ts
// Set filter
const filter = new MxCADResbuf();
filter.AddMcDbEntityTypes("TEXT, MTEXT");
// Select text object
let aryIds = await MxCADUtility.userSelect("Please select a text object", filter);
if (aryIds.length == 0) {
    return;
};
  1. Draw a text border

We according to McDbEntity.getBoundingBox() The minimum bounding box where the text entity is located can be obtained, and the maximum point and minimum point in the minimum bounding box are the two corner points of the text border. Finally, McDbPolyline section of the line to draw the text frame.

In addition, since directly drawing the text border composed of the text minimum surrounding box will be more crowded, we can set the inner margin for the text border to do an optimization.

ts
// Adds a border to a text entity
aryIds.forEach(id => {
    const event = id.getMcDbEntity();
    // Get the two corners of the text
    let bound = event.getBoundingBox();
    if (!bound.ret) return;
    const maxPt: McGePoint3d = bound.maxPt;
    const minPt: McGePoint3d = bound.minPt;
    // Get the other two corner points
    const pt1 = new McGePoint3d(minPt.x, maxPt.y);
    const pt2 = new McGePoint3d(maxPt.x, minPt.y);
    // Set the margins for the text box
    const vec1 = minPt.clone().sub(maxPt).normalize().mult(2);
    const vec2 = pt1.clone().sub(pt2).normalize().mult(2);
    // Draw text box
    const pl = new McDbPolyline();
    pl.addVertexAt(minPt.addvec(vec1));
    pl.addVertexAt(pt1.addvec(vec2));
    pl.addVertexAt(maxPt.subvec(vec1));
    pl.addVertexAt(pt2.subvec(vec2));
    pl.isClosed = true;
    MxCpp.getCurrentMxCAD().drawEntity(pl);
})

Functional practice

Practical effects are as follows:

  • Click the text box button to execute the text box method
  • Left click to select the target text entity, right click to end the selection
  • Successfully drew the text box