Skip to content
On this page

Center rectangle

In the following, we will introduce how to use mxcad plug-in to achieve the function of drawing the center rectangle in CAD drawings. In this function, users can set the width and height of the rectangle and set the position of the rectangle through the center point of the rectangle.

Function implementation

  1. Set the width and height of the center rectangle

We can call MxCADUiPrDist, respectively, dynamic set center width and height of the rectangle, We can also set a default value for it during the setup process.

ts
// Set the rectangle width The default setting is 5
let width = 5;
const getWidth = new MxCADUiPrDist();
getWidth.setMessage("Please enter the rectangle width<5>");
const widthVal = await getWidth.go();
if (widthVal) {
    width = getWidth.value()
}

// 设置矩形高度默认设置为10
let height = 10;
const getHeight = new MxCADUiPrDist();
getHeight.setMessage("Please enter the rectangle height<10>");
const heightVal = await getHeight.go();
if (heightVal) {
    height = getHeight.value()
}
  1. Set the position of the rectangle

We by calling MxCADUiPrPoint take object determines the location of the center of the rectangle, And according to the above steps in access to high rectangle width by McDbPolyline rectangular section of the line to draw more center.

ts
// Sets the rectangle center point position
const getCenterPt = new MxCADUiPrPoint();
getCenterPt.setMessage("Please click OK center of the rectangle");
const centerPt = await getCenterPt.go();
if (!centerPt) return;
// Gets the positions of the four vertices of the rectangle
let pt1 = new McGePoint3d(centerPt.x + width / 2, centerPt.y + height / 2, centerPt.z)
let pt2 = new McGePoint3d(centerPt.x - width / 2, centerPt.y + height / 2, centerPt.z)
let pt3 = new McGePoint3d(centerPt.x - width / 2, centerPt.y - height / 2, centerPt.z)
let pt4 = new McGePoint3d(centerPt.x + width / 2, centerPt.y - height / 2, centerPt.z)
// Draw center rectangle
let pl = new McDbPolyline;
pl.addVertexAt(pt1)
pl.addVertexAt(pt2)
pl.addVertexAt(pt3)
pl.addVertexAt(pt4)
const mxcad = MxCpp.App.getCurrentMxCAD();
pl.isClosed = true;
mxcad.drawEntity(pl);

Function extension

  1. Implement a custom center rectangle class

In order to facilitate subsequent management and modification of the entity, We can through inheritance McDbCustomEntity custom entity class to extend the rectangle class implementation center.

ts
// Custom center rectangle class
class McDbTestCenterReact extends McDbCustomEntity {
    // Defines a point object inside McDbTestCenterReact
    // Rectangular center point
    private center: McGePoint3d = new McGePoint3d();
    // Width of rectangle
    private width: number = 10;
    // Rectangular height
    private height: number = 20;

    // constructor
    constructor(imp?: any) {
        super(imp);
    }
    // Create function
    public create(imp: any) {
        return new McDbTestCenterReact(imp)
    }
    // Get class name
    public getTypeName(): string {
        return "McDbTestCenterReact";
    }
    // Sets or gets the rectangle width
    public set cWidth(val: number) {
        this.width = val;
    }
    public get cWidth(): number {
        return this.width;
    }
    // Set rectangle height
    public set cHeight(val: number) {
        this.height = val;
    }
    public get cHeight(): number {
        return this.height;
    }
    // Read from defined entity data center、 width、height
    public dwgInFields(filter: IMcDbDwgFiler): boolean {
        this.center = filter.readPoint("center").val;
        this.width = filter.readDouble("width").val;
        this.height = filter.readDouble("height").val;
        return true;
    }
    // Write from defined entity data centert、 width、height
    public dwgOutFields(filter: IMcDbDwgFiler): boolean {
        filter.writePoint("center", this.center);
        filter.writeDouble("width", this.width);
        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();
        this.center.x += dXOffset;
        this.center.y += dYOffset;
        this.center.z += dZOffset;

    };
    // Gets the pinch point of a custom object
    public getGripPoints(): McGePoint3dArray {
        let ret = new McGePoint3dArray()
        ret.append(this.center);
        return ret;
    };
    // Draw entity
    public worldDraw(draw: MxCADWorldDraw): void {
        let pt1 = new McGePoint3d(this.center.x + this.width / 2, this.center.y + this.height / 2, this.center.z)
        let pt2 = new McGePoint3d(this.center.x - this.width / 2, this.center.y + this.height / 2, this.center.z)
        let pt3 = new McGePoint3d(this.center.x - this.width / 2, this.center.y - this.height / 2, this.center.z)
        let pt4 = new McGePoint3d(this.center.x + this.width / 2, this.center.y - this.height / 2, this.center.z)
        let pl = new McDbPolyline;
        pl.addVertexAt(pt1)
        pl.addVertexAt(pt2)
        pl.addVertexAt(pt3)
        pl.addVertexAt(pt4)
        pl.isClosed = true;
        draw.drawEntity(pl);
    }
    // Set the center point of the rectangle
    public setCenterPoint(pt: McGePoint3d) {
        this.assertWrite();
        this.center = pt.clone();
    }
    // Gets the center point of the rectangle
    public getCenterPoint() {
        return this.center;
    }
}

Functional practice

Practical effects are as follows:

  • Click the Center Rectangle button to perform the Draw Center Rectangle method
  • Follow the command line prompt steps to complete setting the width and height of the rectangle
  • Click on the canvas to set the center of the rectangle
  • The center rectangle was successfully drawn