Skip to content
On this page

Measurement circle

Below we will introduce how to use mxcad plug-in to achieve the function of measuring the circle in the CAD drawing, the function of the user click the target circle object will automatically mark the radius, area value and circumference value of the circle, and the user can customize the location of the marked text. The measurement circle function can help users quickly grasp the data information of the target circle object, and it is convenient to count the engineering quantity.

Function implementation

  1. Implement custom circle annotation classes

In order to facilitate later management and annotation modification, We can through inheritance McDbCustomEntity custom entity class to extend round annotation classes. Among them, the round object in the mxcad corresponding entities such as McDbCircle, This class provides properties or methods to get or set information about the circle, which we can choose to call according to our functional requirements. In the circle measurement function, we need to obtain the radius, area and circumference of the circle object, Radius, so we can call McDbCircle properties for circle radius, McDbCircle.getArea() method or directly to obtain a circle area, And McDbCircle.getLength() method to get the circumference.

Then, we can use McDbMText measurement information text object, to draw round label information on the page.

ts
// Custom circle annotation class
class McDbTestMeasuringCircle extends McDbCustomEntity {
    // Defines the point object inside the McDbTestMeasuringCircle 
    // Center of a circle
    private center: McGePoint3d = new McGePoint3d();
    // Label point
    private position: McGePoint3d = new McGePoint3d();
    // Radius of the circle
    private radius: number;
    // Constructor
    constructor(imp?: any) {
        super(imp);
    }
    // Create function
    public create(imp: any) {
        return new McDbTestMeasuringCircle(imp)
    }
    // Get class name
    public getTypeName(): string {
        return "McDbTestMeasuringCircle";
    }
    //Sets or gets the radius of the circle
    public set circleRadius(val: number) {
        this.radius = val;
    }
    public get circleRadius(): number {
        return this.radius;
    }
    // Read from defined entity data center、position、radius
    public dwgInFields(filter: IMcDbDwgFiler): boolean {
        this.center = filter.readPoint("center").val;
        this.position = filter.readPoint("position").val;
        this.radius = filter.readDouble("radius").val;
        return true;
    }
    // Write from defined entity data center、position、radius
    public dwgOutFields(filter: IMcDbDwgFiler): boolean {
        filter.writePoint("center", this.center);
        filter.writePoint("position", this.position);
        filter.writeDouble("radius", this.radius);
        return true;
    }

    // Moves the pinch point of a custom object
    public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {
        this.assertWrite();
        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.position);
        return ret;
    };
    // Draw entity
    public worldDraw(draw: MxCADWorldDraw): void {
        // Draw annotated circles and annotated information
        const circle = new McDbCircle();
        circle.center = this.center;
        circle.radius = this.radius;
        const length = circle.getLength().val;
        const radius = circle.radius;
        const area = Math.PI * radius * radius;

        const mText = new McDbMText();
        mText.contents = `半径:${radius.toFixed(2)} \\P 周长:${length.toFixed(2)} \\P 面积:${area.toFixed(2)}`
        mText.textHeight = radius / 6;
        mText.attachment = McDb.AttachmentPoint.kMiddleCenter;
        mText.location = this.position;
        mText.trueColor = circle.trueColor = this.trueColor;
        draw.drawEntity(mText);
        draw.drawEntity(circle);
    }
    // Setting pt1
    public setCenter(pt: McGePoint3d) {
        this.assertWrite();
        this.center = pt.clone();
    }
    // Get pt1
    public getCenter() {
        return this.center;
    }
    // Setting 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 McDbTestMeasuringCircle().rxInit();
  1. Write a method, call McDbTestMeasuringCircle custom circle annotation class to measure the circle function
  • 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
// Select the entity object
const getEnt = new MxCADUiPrEntity();
// Set prompt information
getEnt.setMessage(" Please select a circle object ");
// Set the filter
const filter = new MxCADResbuf([DxfCode.kEntityType, "CIRCLE"]);
getEnt.setFilter(filter);
// entId Filters the ID of the selected round entity
const entId = await getEnt.go();
if (! entId.id) return;
// Obtain information about the circle
const circle = entId.getMcDbEntity() as McDbCircle;
const mCircle = new McDbTestMeasuringCircle();
mCircle.setCenter(circle.center);
mCircle.circleRadius = circle.radius;
  • Specify annotation points and draw round annotation objects

We can use MxCADUiPrPoint take the object interaction take in the page. In the process of taking points, We can go through MxCADUiPrPoint.setUserDraw() Method Dynamically drawing annotation objects, so that users can observe the position changes of annotation objects more intuitively.

ts
// Set the fetch object
const getPt = new MxCADUiPrPoint();
// Set prompt information
getPt.setMessage(' Please specify text location ');
// Dynamic rendering
getPt.setUserDraw((pt, pw) => {
    pw.setColor(0xFF0000);
    mCircle.setPosition(pt);
    pw.drawMcDbEntity(mCircle);
});
const point = await getPt.go();
if (!point) return;
// Set the annotated text position
mCircle.setPosition(point);
// Set the color of the circle annotation object
mCircle.trueColor = new McCmColor(255, 0, 0)
const mxcad = MxCpp.getCurrentMxCAD();
// Draw a circle annotation object
mxcad.drawEntity(mCircle);

Functional practice

Practical effects are as follows:

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