Skip to content
On this page

Extract text

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([DxfCode.kEntityType, "MTEXT,TEXT"]);
// Select text object
const ss = new MxCADSelectionSet();
if (!await ss.userSelect("Please set the text extraction range", filter)) return;
if (ss.count() == 0) return;
  1. Extract text content

We according to McObjectId.getMcDbEntity() Get the target text entity, Through McDbEntity.objectName Attribute to get the entity name, because in the above steps we filter out are text entities, so we only need to determine whether it is a multi-line text class, or a single-line text class, and call the corresponding text attribute according to different text classes to get text information.

ts
let content: string = '';
// Iterate through all text entities to get text information
ss.forEach(id => {
    const ent = id.getMcDbEntity();
    const objName = ent.objectName;
    if (objName === 'McDbText') {
        const _clone = ent.clone() as McDbText;
        content += `${_clone.textString}\n`;
    } else if (objName === 'McDbMText') {
        const _clone = ent.clone() as McDbMText;
        content += _clone.contents.replace('\\P', `\n`);
    }
});
// Integrate text information
console.log(content)

Functional practice

Practical effects are as follows:

  • Click the Extract text button to execute the Extract text method
  • Left click to select the target text entity, right click to end the selection
  • The target text information was successfully extracted into the text box