Text Search
Below we will introduce how to use mxcad plug-in to implement the text lookup function in CAD drawings. In this function, the user selects the target text entity independently and searches the same text in the drawing according to the text content. The text search function can help users find words, numbers, and letters in drawings, and can be used to quickly locate the contents of drawings or count the number of text primitives.
Function implementation
- Set search text
We can call MxCADUiPrString Set the text content we want to find. In addition, you can also obtain the text content you need to find through the input box or other means.
// Set find text content
const getStr = new MxCADUiPrString();
getStr.setMessage('Please enter the search text\n');
const string = await getStr.go();
if (!string) return;
- Set the search mode
We can call MxCADUiPrKeyWord Set the type of text search based on the keywords entered by the user, such as full word matching, text containing, text prefix matching, and text suffix matching.
// Set search mode
const getKey = new MxCADUiPrKeyWord();
getKey.setMessage('Please select Find mode\n');
getKey.setKeyWords("[Full word matching(A)/ Word containing(B)/ word prefix matching(C)/ word suffix matching(D)]");
const key = await getKey.go();
if (!key) return;
- Set the search range and find the target text
We can call MxCADResbuf set filters, Through MxCADUtility.userSelect() Method filters out single-line text objects in the selected entity.
Since we have obtained the search method selected by the user in the above steps, we will filter out the filtered text entities to filter out the objects that meet the requirements according to different search methods. And through it McObject.addCurrentSelect() Method is added to the currently selected entity for our observation.
const filter = new MxCADResbuf();
// Add object types, select sets Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT")
let ss = new MxCADSelectionSet();
if (!await ss.userSelect("Select the range you want to find:", filter)) return;
if (ss.count() == 0) return;
// Selected object
const mxcad = MxCpp.getCurrentMxCAD();
let objArr: McObjectId[] = [];
// Iterate over the text object to find the text that matches the criteria
ss.forEach((id: McObjectId) => {
const obj = id.getMcDbEntity() as McDbText;
const str = obj.textString;
if (key === 'A') {
if (str === string) {
objArr.push(id);
}
} else if (key === 'B') {
if (str.includes(string)) {
objArr.push(id);
}
} else if (key === 'C') {
if (str.startsWith(string)) {
objArr.push(id);
}
} else if (key === 'D') {
if (str.endsWith(string)) {
objArr.push(id);
}
}
});
if (objArr.length > 0) {
objArr.forEach(id => {
mxcad.addCurrentSelect(id)
})
}
Functional practice
Practical effects are as follows:
- Click the Text Search button to perform the text search method
- enter the text content you want to find in the input box according to the command line prompt, and press Enter to determine the search content
- Select the text search method according to the command line prompt again, enter the corresponding letter in the input box, and press Enter to determine the search method
- Click the left mouse button to select the search area
- Click the left mouse button again to end the box selection
- The target text was successfully found and selected