Skip to content
On this page

Measuring coordinates

Below we will introduce how to use mxcad plug-in to achieve the function of measuring coordinates in CAD drawings, in which the user clicks the target point object will automatically mark the coordinate value of the point object, and the user can customize the location of the marked coordinates. The measuring coordinate function can help users quickly grasp the data information of the target point object and facilitate the statistics of the engineering quantity.

Function implementation

  1. Realize the custom coordinate annotation class

In order to facilitate later management and annotation modification, We can through inheritance McDbCustomEntity custom entity class to extend the coordinate with class. Then, we can use McDbMText measurement information text object, will coordinate the label information on the page.

ts
// Custom measurement coordinate annotation class
class McDbTestCoordinateLabeling extends McDbCustomEntity {
    // Define McDbTestCoordinateLabeling points within the object
    // Point object
    private point: McGePoint3d = new McGePoint3d();
    // Label point
    private position: McGePoint3d = new McGePoint3d();
    // Word height
    private height: number = 30;
    // constructor
    constructor(imp?: any) {
        super(imp);
    }
    // Create function
    public create(imp: any) {
        return new McDbTestCoordinateLabeling(imp)
    }
    // Get class name
    public getTypeName(): string {
        return "McDbTestCoordinateLabeling";
    }
    //Set or get the measuring point coordinates
    public set coordinatePoint(val: McGePoint3d) {
        this.point = val.clone();
    }
    public get coordinatePoint(): McGePoint3d {
        return this.point;
    }
    //Sets or gets the word height
    public set coordinateHeight(val: number) {
        this.height = val;
    }
    public get coordinateHeight(): number {
        return this.height;
    }
    // Read from the defined entity data 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;
    }
    // Writes the custom entity data 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;
    }

    // Moves the pinch point of a custom object
    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;
        }
    };
    // Gets the pinch point of a custom object
    public getGripPoints(): McGePoint3dArray {
        let ret = new McGePoint3dArray()
        ret.append(this.point);
        ret.append(this.position);
        return ret;
    };
    // Draw entity
    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);
    }
    // Get position
    public setPosition(pt: McGePoint3d) {
        this.assertWrite();
        this.position = pt.clone();
    }
    // Get position
    public getPosition() {
        return this.position;
    }
}
  1. Register custom class information
ts
new McDbTestCoordinateLabeling().rxInit();
  1. Write a method, called McDbTestCoordinateLabeling custom coordinate annotation class implements measuring coordinate functions
  • Get the target circle object and get the relevant data information

We can choose using entity object MxCADUiPrEntity() According to the coordinates of the user's mouse click to get the corresponding entity, where we need to select only the circle object, so, We'll call MxCADResbuf() to select entity object set filter to filter out the target entity.

ts
// Coordinate annotation
// Select the marked coordinate points
const getPoint1 = new MxCADUiPrPoint();
getPoint1.setMessage('Please select the target coordinate point');
const pt1 = await getPoint1.go();
if (!pt1) return;

// Set annotation location
const getPoint2 = new MxCADUiPrPoint();
getPoint2.setMessage('Please specify the location');
// Dynamic rendering
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);

Functional practice

Practical effects are as follows:

  • Click the Measure coordinate button to perform the measure coordinate method
  • Select the target point object
  • Set the point location
  • The contents of the measurement annotations were successfully drawn