Skip to content
On this page

测量坐标

下面我们将介绍如何利用 mxcad 插件实现在CAD图纸中测量坐标的功能,该功能中用户点击目标点对象将自动标记出这个点对象的坐标值,同时用户可以自定义选择标注坐标的位置。测量坐标功能能够帮助用户快速掌握目标点对象的数据信息,方便统计工程量。

功能实现

  1. 实现自定义坐标标注类

为了方便后期管理与修改标注,我们可以通过继承 McDbCustomEntity 自定义实体类来扩展实现坐标标注类。然后,我们可以利用 McDbMText 构造测量信息多文本对象,将坐标的标注信息绘制在页面中。

ts
// 自定义测量坐标标注类
class McDbTestCoordinateLabeling extends McDbCustomEntity {
    // 定义McDbTestCoordinateLabeling内部的点对象 
    // 点对象
    private point: McGePoint3d = new McGePoint3d();
    // 标注点
    private position: McGePoint3d = new McGePoint3d();
    // 字高
    private height: number = 30;
    // 构造函数
    constructor(imp?: any) {
        super(imp);
    }
    // 创建函数
    public create(imp: any) {
        return new McDbTestCoordinateLabeling(imp)
    }
    // 获取类名
    public getTypeName(): string {
        return "McDbTestCoordinateLabeling";
    }
    //设置或获取测量点坐标
    public set coordinatePoint(val: McGePoint3d) {
        this.point = val.clone();
    }
    public get coordinatePoint(): McGePoint3d {
        return this.point;
    }
    //设置或获取字高
    public set coordinateHeight(val: number) {
        this.height = val;
    }
    public get coordinateHeight(): number {
        return this.height;
    }
    // 读取自定义实体数据point、position、height
    public dwgInFields(filter: IMcDbDwgFiler): boolean {
        this.point = filter.readPoint("point").val;
        this.position = filter.readPoint("position").val;
        this.height = filter.readDouble("height").val;
        return true;
    }
    // 写入自定义实体数据point、position、height
    public dwgOutFields(filter: IMcDbDwgFiler): boolean {
        filter.writePoint("point", this.point);
        filter.writePoint("position", this.position);
        filter.writeDouble("height", this.height);
        return true;
    }

    // 移动自定义对象的夹点
    public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {
        this.assertWrite();
        if (iIndex === 0) {
            this.point.x += dXOffset;
            this.point.y += dYOffset;
            this.point.z += dZOffset;
        } else {
            this.position.x += dXOffset;
            this.position.y += dYOffset;
            this.position.z += dZOffset;
        }
    };
    // 获取自定义对象的夹点
    public getGripPoints(): McGePoint3dArray {
        let ret = new McGePoint3dArray()
        ret.append(this.point);
        ret.append(this.position);
        return ret;
    };
    // 绘制实体
    public worldDraw(draw: MxCADWorldDraw): void {
        const text = new McDbMText();
        text.textHeight = MxFun.screenCoordLong2Doc(this.height);
        text.attachment = McDb.AttachmentPoint.kMiddleLeft;
        text.contents = `X=${(this.point.x).toFixed(3)}\\PY=${(this.point.y).toFixed(3)}`;
        text.location = this.position;
        draw.drawEntity(text);

        const mxcad = MxCpp.getCurrentMxCAD();
        const id = mxcad.drawEntity(text);
        const { maxPt, minPt } = id.getMcDbEntity().getBoundingBox();
        id.erase();

        const length = Math.abs(maxPt.x - minPt.x);
        const pt3 = new McGePoint3d(this.position.x + length * 1.2, this.position.y);
        const pl = new McDbPolyline();
        pl.addVertexAt(this.point);
        pl.addVertexAt(this.position);
        pl.addVertexAt(pt3);
        draw.drawEntity(pl);
    }
    // 获取position
    public setPosition(pt: McGePoint3d) {
        this.assertWrite();
        this.position = pt.clone();
    }
    // 获取position
    public getPosition() {
        return this.position;
    }
}
  1. 注册自定义类信息
ts
new McDbTestCoordinateLabeling().rxInit();
  1. 编写方法,调用 McDbTestCoordinateLabeling 自定义坐标标注类实现测量坐标功能
  • 获取目标圆对象,得到相关数据信息

我们可以利用选择实体对象 MxCADUiPrEntity() 根据用户鼠标点击的坐标得到对应的实体,其中我们需要只选择圆对象,因此,我们再调用 MxCADResbuf() 为选择实体对象设置过滤器来过滤出目标实体。

ts
// 坐标标注
// 选择标注坐标点
const getPoint1 = new MxCADUiPrPoint();
getPoint1.setMessage('请选择目标坐标点');
const pt1 = await getPoint1.go();
if (!pt1) return;

// 设置标注位置
const getPoint2 = new MxCADUiPrPoint();
getPoint2.setMessage('请指定标注位置');
// 动态绘制
const coord = new McDbTestCoordinateLabeling();
coord.coordinatePoint = pt1;
getPoint2.setUserDraw((pt, pw) => {
    coord.setPosition(pt);
    pw.drawMcDbEntity(coord);
});
const pt2 = await getPoint2.go();
if (!pt2) return;
coord.setPosition(pt2);
MxCpp.getCurrentMxCAD().drawEntity(coord);

功能实践

实践效果如下:

  • 点击测量坐标按钮,执行测量坐标方法
  • 选中目标点对象
  • 设置标注点位置
  • 成功绘制测量标注内容