Skip to content

[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:

  1. Directly call methods such as selectEnt/findEntAtPoint/getCurrentSelect through MxCADUtility;
  2. Pass in the rule container MxCADResbuf to limit the query scope;
  3. After obtaining the McObject ID, you can continue to call getMcDbEntity () to read the entity object;
  4. Use methods such as highlightEntity () and eraseObject () to perform object operations.

Example

ts
   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 ​

Constructors ​

constructor ​

• new MxCADUtilityClass()

Methods ​

builderHatchFromPoint ​

▸ builderHatchFromPoint(pt): null | McDbHatch

Fill the corresponding position entity with a point coordinate

Parameters ​

NameTypeDescription
PtMcGePoint3d (2d. McGePoint3d. md)Point Object

Returns ​

null | McDbHatch

Return a filled object

Example

ts
  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 ​

NameTypeDescription
Pt1McGePoint3d (2d. McGePoint3d. md)Start Point
Pt2[McGePoint3d] (2d. McGePoint3d. md)Midpoint
Pt3McGePoint3d (2d. McGePoint3d. md)End Point

Returns ​

Object

Calculate the convexity result

NameType
retboolean
valnumber

Example

ts
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 ​

NameTypeDefault valueDescription
IdnumberundefinedID of the graphic object that needs to be deleted
IsErasebooleantrueWhether to delete, default to true

Returns ​

boolean

Return a Boolean value indicating whether the deletion was successful

Example

ts
 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 ​

NameTypeDefault valueDescription
DXnumberundefinedX coordinate value of the coordinate point
DYnumberundefinedY coordinate value of the coordinate point
DZnumberundefinedZ coordinate value of the coordinate point
DSearhRangenumber-1Set the selection range, default to -1 for adaptation, or enter the corresponding CAD coordinate length
filternull \[MxCADResbuf] (2d. MxCADResbuf. md)null

Returns ​

McObjectId

Return the ID of the found graphic object

Example

ts
  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 ​

NameTypeDefault valueDescription
strPrompt?` StringundefinedString prompt
pt1?[McGePoint3d] (2d. McGePoint3d. md)undefinedFirst point (when a corner point is known)
detailedResult?(retcode: DetailedResult)=>anyundefinedDetailedResult Interaction Result Platform callback
DrawSelectCronerbooleanfalseDraw a checkbox for the selected object (true with background, false without background)
IsDisabAllTracebooleanfalseDo you want to disable all traces
init?(getPoint: MxCADUiPrPoint) => anyundefined-

Returns ​

Promise<null | { pt1: McGePoint3d ; pt2: McGePoint3d ; pt3: McGePoint3d ; pt4: McGePoint3d }>

Return a Promise containing two corner objects

Example

ts
import { MxCADUtility } from "mxcad";

Const ret=await MxCADUtility. getCorner ("test");
if (!ret) return;
console.log(ret.pt1, ret.pt2)
pt4----pt2
|       |
pt1----pt3

getCurrentSelect ​

▸ getCurrentSelect(filter?, returnMxCADObject?, returnMxDrawObject?, whenEmptyReturnPrvSelect?, isFilterLockLayer?): McObjectId[]

Get the currently selected object.

Parameters ​

NameTypeDefault valueDescription
filternull | [Object]( https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object )\[MxCADResbuf] (2d. MxCADResbuf. md)null
returnMxCADObjectbooleantrue-
returnMxDrawObjectbooleantrue-
whenEmptyReturnPrvSelectbooleantrue-
isFilterLockLayerbooleanfalse-

Returns ​

McObjectId[]


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

NameType
isvalidboolean
point1McGePoint3d
point2McGePoint3d

getMcDbEntitysBoundingBox ​

▸ getMcDbEntitysBoundingBox(aryId): undefined | { maxPt: McGePoint3d ; minPt: McGePoint3d }

Obtain a bounding box formed by multiple entities

Parameters ​

NameTypeDescription
AryId[McObject Id] (2d. McObject Id. md) []Entity Object ID Array

Returns ​

undefined | { maxPt: McGePoint3d ; minPt: McGePoint3d }

MinPt minimum point | maxPt maximum point

Example

ts
  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 ​

NameType
strstring
dTextHeightnumber
dWidthFactornumber
idTextStyleRecordMcObjectId

Returns ​

Object

NameType
maxPtMcGePoint3d
minPtMcGePoint3d
retboolean

Example

ts
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 ​

NameTypeDefault value
textMcDbEntityundefined
isTrimLastSpacebooleantrue

Returns ​

Object

NameType
maxPtMcGePoint3d
minPtMcGePoint3d
retboolean

Example

ts
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 ​

NameTypeDefault valueDescription
IdnumberundefinedID of the graphic object that needs to be highlighted
IsHighlightbooleantrueWhether 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 ​

NameType
aryPointMcGePoint3dArray
xnumber
yNumber

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 ​

NameTypeDefault valueDescription
strPrompt?` StringundefinedThe prompt message when selecting, default to null (indicating no prompt message displayed)
filternull \[MxCADResbuf] (2d. MxCADResbuf. md)null
IsSelectMxDrawbooleantrueWhether to select annotation graphics default to true
init?(getPoint: MrxDBgUiPrPoint)=>anyundefinedInitialize 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))=>voidundefinedThe callback function retrieves the coordinate points of the selected shape

Returns ​

Promise<McObjectId[]>

Return a Promise containing an array of IDs for the selected graphic object

Example

ts
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 ​

NameTypeDefault valueDescription
strPrompt? string | [Object]( https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object )undefinedString prompt
Filternull[MxCADResbuf] (2d. MxCADResbuf. md)null
init?(ss: MxCADSelectionSet, getPoint: MrxDbgUiPrPoint) => anyundefined-
isRetCurrentSelectbooleantrue-
isFilterLockLayerbooleanfalse-

Returns ​

Promise<McObjectId[]>

Return a Promise containing the selected object ID

Example

ts
   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);