[mxcad_2d API documentation] (../README. md)/[2d] (../modules/2d. md)/MxCADUtilityClass
Class: MxCADUtilityClass
2d.MxCADUtilityClass
MxCADUtilityClass is a universal utility class for MxCAD, providing common operations such as object selection, graphic queries, deletion, and highlighting.
Description
This class is used to encapsulate and simplify common interaction and query capabilities in CAD views, such as searching for entities by point, selecting objects, obtaining the current selection, deleting objects, setting object highlighting, etc. It is one of the most commonly used tool entrances in CAD secondary development, suitable for implementing interactive editing, object recognition, and graphic operations. Usage:
- Directly call methods such as selectEnt/findEntAtPoint/getCurrentSelect through MxCADUtility;
- Pass in the rule container MxCADResbuf to limit the query scope;
- After obtaining the McObject ID, you can continue to call getMcDbEntity () to read the entity object;
- Use methods such as highlightEntity () and eraseObject () to perform object operations.
Example
import { MxCADUtility, MxCADUiPrPoint } from "mxcad";
async function test() {
const getPoint = new MxCADUiPrPoint();
GetPoint.setMessage ("Please select object on graph");
const point = await getPoint.go();
if (!point) return;
//Find graphic objects near a specified point
const objId = MxCADUtility.findEntAtPoint(point.x, point.y, point.z, -1, null);
if (objId.isValid()) {
//Highlight the hit object
MxCADUtility.highlightEntity(objId.id, true);
Console.log ("hit object:", objId);
}
}Table of contents
Constructors
Methods
- builderHatchFromPoint
- calcBulge
- eraseObject
- findEntAtPoint
- getCorner
- getCurrentSelect
- getCurrentSelectPoints
- getMcDbEntitysBoundingBox
- getTextBox
- getTextEntityBox
- highlightEntity
- init
- pointInPolygon
- selectEnt
- userSelect
Constructors
constructor
• new MxCADUtilityClass()
Methods
builderHatchFromPoint
▸ builderHatchFromPoint(pt): null | McDbHatch
Fill the corresponding position entity with a point coordinate
Parameters
| Name | Type | Description |
|---|---|---|
| Pt | McGePoint3d (2d. McGePoint3d. md) | Point Object |
Returns
null | McDbHatch
Return a filled object
Example
import { MxCADUiPrPoint, MxCADUtility } from 'mxcad'
async function test(){
const getPoint = new MxCADUiPrPoint();
GetPoint.setMessage ("\ nSpecify a point inside the filled area:");
getPoint.disableAllTrace(true);
getPoint.setDisableOsnap(true);
let pt = (await getPoint.go()) as McGePoint3d;
if (!pt) return;
let hatch = MxCADUtility.builderHatchFromPoint(pt);
MxCpp.mxcad.drawEntity(hatch);
}calcBulge
▸ calcBulge(pt1, pt2, pt3): Object
Calculate convexity
Parameters
| Name | Type | Description |
|---|---|---|
| Pt1 | McGePoint3d (2d. McGePoint3d. md) | Start Point |
| Pt2 | [McGePoint3d] (2d. McGePoint3d. md) | Midpoint |
| Pt3 | McGePoint3d (2d. McGePoint3d. md) | End Point |
Returns
Object
Calculate the convexity result
| Name | Type |
|---|---|
ret | boolean |
val | number |
Example
import { McGePoint3d, MxCADUtility } from "mxcad"
const pt1 = new McGePoint3d(0,0,0);
const pt2 = new McGePoint3d(20,10,0);
const pt3 = new McGePoint3d(40,0,0);
const Bulge = MxCADUtility.calcBulge(pt1, pt2, pt3);
console.log(Bulge)eraseObject
▸ eraseObject(lId, isErase?): boolean
The eraseObject method is used to delete a specified graphic object
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| Id | number | undefined | ID of the graphic object that needs to be deleted |
| IsErase | boolean | true | Whether to delete, default to true |
Returns
boolean
Return a Boolean value indicating whether the deletion was successful
Example
import { MxCpp, McGePoint3d, McDbLine, MxCADUtility } from "mxcad";
const line = new McDbLine(new McGePoint3d(0,0,0), new McGePoint3d(20,1,0));
const lineId = MxCpp.getCurrentMxCAD().drawEntity(line);
const res = MxCADUtility.eraseObject(lineId.id)
Console.log ("Delete result:", res);findEntAtPoint
▸ findEntAtPoint(dX, dY, dZ, dSearhRange?, filter?): McObjectId
The findEntAtPoint method is used to search for graphic objects near a specified coordinate point
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| DX | number | undefined | X coordinate value of the coordinate point |
| DY | number | undefined | Y coordinate value of the coordinate point |
| DZ | number | undefined | Z coordinate value of the coordinate point |
| DSearhRange | number | -1 | Set the selection range, default to -1 for adaptation, or enter the corresponding CAD coordinate length |
filter | null \ | [MxCADResbuf] (2d. MxCADResbuf. md) | null |
Returns
Return the ID of the found graphic object
Example
import { MxCADUiPrPoint, MxCADUtility, MxCpp } from "mxcad"
async function test(){
const mxcad = MxCpp.getCurrentCAD();
const getPoint = new MxCADUiPrPoint();
GetPoint.setMessage ('Please select object ');
const point = await getPoint.go();
if (!point) break;
const filter = new MxCADResbuf([DxfCode.kEntityType, "LINE,ARC,CIRCLE,LWPOLYLINE"]));
let objId = MxCADUtility.findEntAtPoint(point.x, point.y, point.z, -1, filter);
mxcad.addCurrentSelect(objId);
}getCorner
▸ getCorner(strPrompt?, pt1?, detailedResult?, drawSelectCroner?, isDisableAllTrace?, init?): Promise<null | { pt1: McGePoint3d ; pt2: McGePoint3d ; pt3: McGePoint3d ; pt4: McGePoint3d }>
Get corner points (two points clicked by the mouse)
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
strPrompt? | ` String | undefined | String prompt |
pt1? | [McGePoint3d] (2d. McGePoint3d. md) | undefined | First point (when a corner point is known) |
detailedResult? | (retcode: DetailedResult)=>any | undefined | DetailedResult Interaction Result Platform callback |
| DrawSelectCroner | boolean | false | Draw a checkbox for the selected object (true with background, false without background) |
| IsDisabAllTrace | boolean | false | Do you want to disable all traces |
init? | (getPoint: MxCADUiPrPoint) => any | undefined | - |
Returns
Promise<null | { pt1: McGePoint3d ; pt2: McGePoint3d ; pt3: McGePoint3d ; pt4: McGePoint3d }>
Return a Promise containing two corner objects
Example
import { MxCADUtility } from "mxcad";
Const ret=await MxCADUtility. getCorner ("test");
if (!ret) return;
console.log(ret.pt1, ret.pt2)
pt4----pt2
| |
pt1----pt3getCurrentSelect
▸ getCurrentSelect(filter?, returnMxCADObject?, returnMxDrawObject?, whenEmptyReturnPrvSelect?, isFilterLockLayer?): McObjectId[]
Get the currently selected object.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
filter | null | [Object]( https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object )\ | [MxCADResbuf] (2d. MxCADResbuf. md) | null |
returnMxCADObject | boolean | true | - |
returnMxDrawObject | boolean | true | - |
whenEmptyReturnPrvSelect | boolean | true | - |
isFilterLockLayer | boolean | false | - |
Returns
getCurrentSelectPoints
▸ getCurrentSelectPoints(): Object
When obtaining the current selected object on the graph, select the range point.
Returns
Object
Point1 Corner 1 | Point2 Corner 2 | Is invalid
| Name | Type |
|---|---|
isvalid | boolean |
point1 | McGePoint3d |
point2 | McGePoint3d |
getMcDbEntitysBoundingBox
▸ getMcDbEntitysBoundingBox(aryId): undefined | { maxPt: McGePoint3d ; minPt: McGePoint3d }
Obtain a bounding box formed by multiple entities
Parameters
| Name | Type | Description |
|---|---|---|
| AryId | [McObject Id] (2d. McObject Id. md) [] | Entity Object ID Array |
Returns
undefined | { maxPt: McGePoint3d ; minPt: McGePoint3d }
MinPt minimum point | maxPt maximum point
Example
import { MxCADUtility } from "mxcad";
async function test(){
Let aryId=await MxCADUtility. userSelect ("Select Target Object");
let ext = MxCADUtility.getMcDbEntitysBoundingBox(aryId);
if (!ext) return;
Console.log ("minimum point", ext.minPt)
Console.log ("maximum point", ext.maxPt)
}getTextBox
▸ getTextBox(str, dTextHeight, dWidthFactor, idTextStyleRecord): Object
Outsourcing box for calculating text
Parameters
| Name | Type |
|---|---|
str | string |
dTextHeight | number |
dWidthFactor | number |
idTextStyleRecord | McObjectId |
Returns
Object
| Name | Type |
|---|---|
maxPt | McGePoint3d |
minPt | McGePoint3d |
ret | boolean |
Example
import { MxCADUtility, MxCpp } from "mxcad"
//Get the size of the text space symbol in the current font style
const { minPt.maxPt, ret} = MxCADUtility.getTextBox(" ", 100, 1.0, MxCpp.getCurrentDatabase().getCurrentlyTextStyleId());
if(ret){
Console.log ("minimum point", minPt)
Console.log ("maximum point", maxPt)
}getTextEntityBox
▸ getTextEntityBox(text, isTrimLastSpace?): Object
Calculate the bounding box of text objects
Parameters
| Name | Type | Default value |
|---|---|---|
text | McDbEntity | undefined |
isTrimLastSpace | boolean | true |
Returns
Object
| Name | Type |
|---|---|
maxPt | McGePoint3d |
minPt | McGePoint3d |
ret | boolean |
Example
import { MxCpp, MxCADUtility, } from "mxcad";
async function test(){
let mxcad = MxCpp.getCurrentMxCAD();
Let retIds=await MxCADUtility. selectEnt ("select");
if (retIds.length == 0) {
return;
}
let id = retIds[0];
let ent = id.getMcDbEntity();
if (ent) {
let box = MxCADUtility.getTextEntityBox(ent, false);
if (box.ret) {
mxcad.drawLine(box.maxPt.x, box.maxPt.y, box.minPt.x, box.minPt.y);
}
}
}highlightEntity
▸ highlightEntity(lId, isHighlight?): any
The highlightEntity method is used to highlight a specified graphic object
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| Id | number | undefined | ID of the graphic object that needs to be highlighted |
| IsHighlight | boolean | true | Whether to highlight, default to true |
Returns
any
init
▸ init(): void
initialization
Returns
void
pointInPolygon
▸ pointInPolygon(aryPoint, x, y): boolean
Determine whether a point is within a closed area
Parameters
| Name | Type |
|---|---|
aryPoint | McGePoint3dArray |
x | number |
y | Number |
Returns
boolean
selectEnt
▸ selectEnt(strPrompt?, filter?, isSelectMxDraw?, init?, callbackGetPoint?): Promise<McObjectId[]>
The selectEnt method is used to select graphic objects at a specified location
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
strPrompt? | ` String | undefined | The prompt message when selecting, default to null (indicating no prompt message displayed) |
filter | null \ | [MxCADResbuf] (2d. MxCADResbuf. md) | null |
| IsSelectMxDraw | boolean | true | Whether to select annotation graphics default to true |
init? | (getPoint: MrxDBgUiPrPoint)=>any | undefined | Initialize the function before selecting the shape (will be instantiated with the [mxdraw point class])( https://mxcadx.gitee.io/mxdraw_api_docs/classes/MrxDbgUiPrPoint.html )Triggered at the moment, obtain a point instance) |
callbackGetPoint? | (point: [McGePoint3d] (2d. McGePoint3d. md))=>void | undefined | The callback function retrieves the coordinate points of the selected shape |
Returns
Return a Promise containing an array of IDs for the selected graphic object
Example
import { MxCADUtility, MxCADResbuf } from "mxcad";
async function selectTargetEntity() {
//Limit selection to only lines, circles, arcs, and polylines
const filter = new MxCADResbuf();
filter.AddMcDbEntityTypes("LINE,CIRCLE,ARC,LWPOLYLINE");
//Enable users to select objects on the drawing through interactive means
const ids = await MxCADUtility.selectEnt(
Please select the target object,
filter,
true,
(getPoint) => {
GetPoint.setMessage ("Please click on the target object on the image");
getPoint.setDisableOsnap(true);
},
(pt) => {
Console.log (click location: pt. x, pt. y, pt. z);
}
);
if (ids.length === 0) {
Console.log ("No object selected");
return;
}
Console.log ("Selected Object ID:", ids);
for (const id of ids) {
const ent = id.getMcDbEntity();
if (ent) {
Console.log ("Object Type:", ent. objectName);
}
}
}userSelect
▸ userSelect(strPrompt?, filter?, init?, isRetCurrentSelect?, isFilterLockLayer?): Promise<McObjectId[]>
User selection
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
strPrompt? | string | [Object]( https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object ) | undefined | String prompt |
| Filter | null | [MxCADResbuf] (2d. MxCADResbuf. md) | null |
init? | (ss: MxCADSelectionSet, getPoint: MrxDbgUiPrPoint) => any | undefined | - |
isRetCurrentSelect | boolean | true | - |
isFilterLockLayer | boolean | false | - |
Returns
Return a Promise containing the selected object ID
Example
import { MxCADResbuf, MxCADUtility } from 'mxcad';
let filter = new MxCADResbuf();
filter.AddMcDbEntityTypes("CIRCLE,ARC,LINE,LWPOLYLINE,ELLIPSE");
Let aryId=await MxCADUtility. userSelect ("Select target curve", filter);
console.log(aryId);