Skip to content

mxcad_2d API 文档 / 2d / MxCADUtilityClass

Class: MxCADUtilityClass ​

2d.MxCADUtilityClass

MxCADUtilityClass 是 MxCAD 的通用工具类,提供对象选择、图形查询、删除和高亮等常用操作。

Description

该类用于封装和简化 CAD 视图中的常用交互与查询能力,例如:按点查找实体、选择对象、获取当前选中项、删除对象、设置对象高亮等。 它是 CAD 二次开发中最常用的工具入口之一,适合实现交互式编辑、对象识别和图形操作。 使用方法:

  1. 通过 MxCADUtility 直接调用 selectEnt / findEntAtPoint / getCurrentSelect 等方法;
  2. 传入规则容器 MxCADResbuf 来限制查询范围;
  3. 获取到 McObjectId 后,可以继续调用 getMcDbEntity() 读取实体对象;
  4. 通过 highlightEntity()、eraseObject() 等方法完成对象操作。

Example

ts
   import { MxCADUtility, MxCADUiPrPoint } from "mxcad";

   async function test() {
   const getPoint = new MxCADUiPrPoint();
   getPoint.setMessage("请选择图上对象");
   const point = await getPoint.go();
   if (!point) return;

   // 查找指定点附近的图形对象
   const objId = MxCADUtility.findEntAtPoint(point.x, point.y, point.z, -1, null);
   if (objId.isValid()) {
       // 高亮显示命中的对象
       MxCADUtility.highlightEntity(objId.id, true);
       console.log("命中对象:", objId);
   }
   }

Table of contents ​

Constructors ​

Methods ​

Constructors ​

constructor ​

• new MxCADUtilityClass()

Methods ​

builderHatchFromPoint ​

▸ builderHatchFromPoint(pt): null | McDbHatch

通过一个点坐标填充对应位置实体的填充

Parameters ​

NameTypeDescription
ptMcGePoint3d点对象

Returns ​

null | McDbHatch

返回一个填充对象

Example

ts
  import { MxCADUiPrPoint, MxCADUtility } from 'mxcad'

  async function test(){
   const getPoint = new MxCADUiPrPoint();
   getPoint.setMessage("\n指定填充区域内部一点:");
   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

计算凸度

Parameters ​

NameTypeDescription
pt1McGePoint3d开始点
pt2McGePoint3d中点
pt3McGePoint3d结束点

Returns ​

Object

计算凸度结果

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

eraseObject 方法用于删除指定的图形对象

Parameters ​

NameTypeDefault valueDescription
lIdnumberundefined需要删除的图形对象的 ID
isErasebooleantrue是否需要删除,默认为 true

Returns ​

boolean

返回一个布尔值,表示是否删除成功

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("删除结果:", res);

findEntAtPoint ​

▸ findEntAtPoint(dX, dY, dZ, dSearhRange?, filter?): McObjectId

findEntAtPoint 方法用于在指定坐标点附近查找图形对象

Parameters ​

NameTypeDefault valueDescription
dXnumberundefined坐标点的 X 坐标值
dYnumberundefined坐标点的 Y 坐标值
dZnumberundefined坐标点的 Z 坐标值
dSearhRangenumber-1设置选取范围,默认为 -1 表示自适应,或者输入对应的CAD坐标长度
filternull | MxCADResbufnull筛选条件对象,用于限定查找到的图形对象范围,默认为 null(表示不做限制)

Returns ​

McObjectId

返回查找到的图形对象的 ID

Example

ts
  import { MxCADUiPrPoint, MxCADUtility, MxCpp } from "mxcad"
  
  async function test(){
   const mxcad = MxCpp.getCurrentCAD();
   const getPoint = new MxCADUiPrPoint();
   getPoint.setMessage('请选择对象\n');
   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 }>

获取角点(鼠标点击的两个点)

Parameters ​

NameTypeDefault valueDescription
strPrompt?stringundefined字符串提示
pt1?McGePoint3dundefined第一个点 (已知一个角点的情况)
detailedResult?(retcode: DetailedResult) => anyundefinedDetailedResult 交互结果讲台的回调
drawSelectCronerbooleanfalse绘制选择对象的选框(true有背景false无背景)
isDisableAllTracebooleanfalse是否禁用所有跟踪
init?(getPoint: MxCADUiPrPoint) => anyundefined-

Returns ​

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

返回一个 Promise,其中包含两个角点对象

Example

ts
import { MxCADUtility } from "mxcad";

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

getCurrentSelect ​

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

得到当前选中的对象。

Parameters ​

NameTypeDefault valueDescription
filternull | Object | MxCADResbufnull筛选条件对象
returnMxCADObjectbooleantrue-
returnMxDrawObjectbooleantrue-
whenEmptyReturnPrvSelectbooleantrue-
isFilterLockLayerbooleanfalse-

Returns ​

McObjectId[]


getCurrentSelectPoints ​

▸ getCurrentSelectPoints(): Object

得到图上当前选择对象时,选择范围点。

Returns ​

Object

point1 角点1 | point2 角点2 | isvalid 是否有效

NameType
isvalidboolean
point1McGePoint3d
point2McGePoint3d

getMcDbEntitysBoundingBox ​

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

获取多个实体形成的包围盒

Parameters ​

NameTypeDescription
aryIdMcObjectId[]实体对象ID数组

Returns ​

undefined | { maxPt: McGePoint3d ; minPt: McGePoint3d }

minPt 最小点 | maxPt 最大点

Example

ts
  import { MxCADUtility } from "mxcad";

  async function test(){
   let aryId = await MxCADUtility.userSelect("选择目标对象");
   let ext = MxCADUtility.getMcDbEntitysBoundingBox(aryId);
   if (!ext) return;
   console.log("最小点",ext.minPt)
   console.log("最大点", ext.maxPt)
  }

getTextBox ​

▸ getTextBox(str, dTextHeight, dWidthFactor, idTextStyleRecord): Object

计算文字的外包框

Parameters ​

NameType
strstring
dTextHeightnumber
dWidthFactornumber
idTextStyleRecordMcObjectId

Returns ​

Object

NameType
maxPtMcGePoint3d
minPtMcGePoint3d
retboolean

Example

ts
import { MxCADUtility, MxCpp } from "mxcad"
// 获取当前字体样式下文字空格符号的大小
const { minPt.maxPt, ret} = MxCADUtility.getTextBox(" ", 100, 1.0, MxCpp.getCurrentDatabase().getCurrentlyTextStyleId());
if(ret){
console.log("最小点", minPt)
console.log("最大点", maxPt)
}

getTextEntityBox ​

▸ getTextEntityBox(text, isTrimLastSpace?): Object

计算文字对象的外包框

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("选择");
   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

highlightEntity 方法用于高亮指定的图形对象

Parameters ​

NameTypeDefault valueDescription
lIdnumberundefined需要高亮的图形对象的 ID
isHighlightbooleantrue是否需要高亮,默认为 true

Returns ​

any


init ​

▸ init(): void

初始化

Returns ​

void


pointInPolygon ​

▸ pointInPolygon(aryPoint, x, y): boolean

判断一个点是否在闭合区域内

Parameters ​

NameType
aryPointMcGePoint3dArray
xnumber
yNumber

Returns ​

boolean


selectEnt ​

▸ selectEnt(strPrompt?, filter?, isSelectMxDraw?, init?, callbackGetPoint?): Promise<McObjectId[]>

selectEnt 方法用于在指定位置选择图形对象

Parameters ​

NameTypeDefault valueDescription
strPrompt?stringundefined选择时的提示信息,默认为 null(表示不显示提示信息)
filternull | MxCADResbufnull筛选条件对象,用于限定选择到的图形对象范围,默认为 null(表示不做限制)
isSelectMxDrawbooleantrue是否选择批注图形 默认true
init?(getPoint: MrxDbgUiPrPoint) => anyundefined选择图形前初始化函数( 会在实例化mxdraw取点类时触发,得到取点实例)
callbackGetPoint?(point: McGePoint3d) => voidundefined回调函数 获取选中图形时的坐标点

Returns ​

Promise<McObjectId[]>

返回一个 Promise,其中包含选择到的图形对象的 ID 数组

Example

ts
import { MxCADUtility, MxCADResbuf } from "mxcad";

async function selectTargetEntity() {
  // 限定只允许选择直线、圆、圆弧和多段线
  const filter = new MxCADResbuf();
  filter.AddMcDbEntityTypes("LINE,CIRCLE,ARC,LWPOLYLINE");

  // 通过交互式方式让用户在图纸上选择对象
  const ids = await MxCADUtility.selectEnt(
    "请选择目标对象",
    filter,
    true,
    (getPoint) => {
      getPoint.setMessage("请点击图上的目标对象");
      getPoint.setDisableOsnap(true);
    },
    (pt) => {
      console.log("点击位置:", pt.x, pt.y, pt.z);
    }
  );

  if (ids.length === 0) {
    console.log("未选中任何对象");
    return;
  }

  console.log("已选择对象 ID:", ids);
  for (const id of ids) {
    const ent = id.getMcDbEntity();
    if (ent) {
      console.log("对象类型:", ent.objectName);
    }
  }
}

userSelect ​

▸ userSelect(strPrompt?, filter?, init?, isRetCurrentSelect?, isFilterLockLayer?): Promise<McObjectId[]>

用户选择

Parameters ​

NameTypeDefault valueDescription
strPrompt?string | Objectundefined字符串提示
filternull | MxCADResbufnull筛选条件对象
init?(ss: MxCADSelectionSet, getPoint: MrxDbgUiPrPoint) => anyundefined-
isRetCurrentSelectbooleantrue-
isFilterLockLayerbooleanfalse-

Returns ​

Promise<McObjectId[]>

返回一个 Promise,其中包含得到选择的对象ID

Example

ts
   import { MxCADResbuf, MxCADUtility } from 'mxcad';
   let filter = new MxCADResbuf();
   filter.AddMcDbEntityTypes("CIRCLE,ARC,LINE,LWPOLYLINE,ELLIPSE");
   let aryId = await MxCADUtility.userSelect("选择目标曲线", filter);
   console.log(aryId);