Skip to content
On this page

Thermal insulation cotton

Below we will introduce how to use mxcad plug-in to achieve the function of drawing thermal insulation foam in CAD drawings, in which the user independently sets the width, height, size and drawing position of thermal insulation foam.

Function implementation

  1. Set the width and height of the thermal insulation foam

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

ts
// Set the width of the thermal insulation foam. The default width is 5
let width = 5;
const getWidth = new MxCADUiPrDist();
getWidth.setMessage("Enter the width of the thermal insulation foam<5>");
const widthVal = await getWidth.go();
if (widthVal) {
    width = getWidth.value()
}

// Set the height of the thermal insulation foam. The default height is 10
let height = 10;
const getHeight = new MxCADUiPrDist();
getHeight.setMessage("\nPlease enter the height of thermal insulation foam<10>");
const heightVal = await getHeight.go();
if (heightVal) {
    height = getHeight.value()
}
  1. Set the start point of the thermal insulation foam

We by calling MxCADUiPrPoint take the object to obtain the starting point for the heat preservation cotton respectively.

ts
const getFristPoint = new MxCADUiPrPoint();
getFristPoint.setMessage("Please click OK starting point");
const fristPt = await getFristPoint.go();
if (!fristPt) return
const getNextPoint = new MxCADUiPrPoint();
getNextPoint.setMessage('Please click on the next point');
// Draw the starting position dynamically
getNextPoint.setUserDraw((pt, pw) => {
    const line = new McDbLine(fristPt.x, fristPt.y, fristPt.z, pt.x, pt.y, pt.z);
    pw.drawMcDbEntity(line)
})
const nextPt = await getNextPoint.go();
if (!nextPt) return
  1. Draw the thermal insulation foam object

We call after getting the starting line segment according to the starting point of the thermal insulation cotton obtained in the above steps McDbLine.offsetCurves() The offset curve of the position of the starting line segment is obtained. Then call McDbLine getPointAtDist () Methods According to the width and height of the insulation cotton, the center of the circle corresponding to the end point and the arc part of the insulation cotton are intercepted on the offset curve. The last section of the line for McDbPolyline all endpoints, and connected to the object To draw arc McDbArc, managed to draw heat preservation cotton object.

ts
const line = new McDbLine(fristPt.x, fristPt.y, fristPt.z, nextPt.x, nextPt.y, nextPt.z);
if (fristPt.y < nextPt.y) {
    line.startPoint = new McGePoint3d(nextPt.x, nextPt.y, nextPt.z);
    line.endPoint = new McGePoint3d(fristPt.x, fristPt.y, fristPt.z);;
}
const midPt = line.getPointAtDist(line.getLength().val / 2).val;
const line_clone = line.clone() as McDbLine;
line_clone.rotate(midPt, Math.PI / 2);
const pt1 = line_clone.getPointAtDist(line.getLength().val / 2 + height / 2).val;
const pt2 = line_clone.getPointAtDist(line.getLength().val / 2 - height / 2).val;
// Migration curve
let line1: any;
let line2: any;
line.offsetCurves(height / 2, pt1).forEach(e => {
    line1 = e;
})
line.offsetCurves(height / 2, pt2).forEach(e => {
    line2 = e;
});
// Take a point on the offset curve according to the width and height of the thermal insulation foam
let num = line.getLength().val / width * 2;
let ptArr: McGePoint3d[] = [];
let center1: McGePoint3d[] = [];
let center2: McGePoint3d[] = [];
for (let i = 0; i < num; i++) {
    if (i % 2 == 0) {
        let pt = line2.getPointAtDist(i * (width / 2)).val;
        let center = line1.getPointAtDist(i * (width / 2)).val;
        center1.push(center);
        ptArr.push(pt);
    } else {
        let pt = line1.getPointAtDist(i * (width / 2)).val;
        let center = line2.getPointAtDist(i * (width / 2)).val;
        center2.push(center);
        ptArr.push(pt);
    }
};
// Connect the end points of the thermal insulation foam using a multi-section wire
const pl = new McDbPolyline();
ptArr.forEach(pt => {
    pl.addVertexAt(pt)
})
const mxcad = MxCpp.App.getCurrentMxCAD();
mxcad.drawEntity(pl);
// Draw the circular arc in the insulation foam
center1.forEach(pt => {
    const arc = new McDbArc();
    arc.center = pt;
    arc.radius = width / 2;
    arc.startAngle = -(new McGeVector3d(line.endPoint.x - line.startPoint.x, line.endPoint.y - line.startPoint.y)).angleTo1(McGeVector3d.kXAxis);
    arc.endAngle = (new McGeVector3d(line.startPoint.x - line.endPoint.x, line.startPoint.y - line.endPoint.y)).angleTo1(McGeVector3d.kXAxis);
    mxcad.drawEntity(arc);
})
center2.forEach(pt => {
    const arc = new McDbArc();
    arc.center = pt;
    arc.radius = width / 2;
    arc.startAngle = (new McGeVector3d(line.startPoint.x - line.endPoint.x, line.startPoint.y - line.endPoint.y)).angleTo1(McGeVector3d.kXAxis);
    arc.endAngle = -(new McGeVector3d(line.endPoint.x - line.startPoint.x, line.endPoint.y - line.startPoint.y)).angleTo1(McGeVector3d.kXAxis);
    mxcad.drawEntity(arc)
})

Function extension

  1. Customize thermal insulation foam

In order to facilitate subsequent management and modification of the entity, We can through inheritance McDbCustomEntity custom entity class to extend heat preservation cotton class.

ts
// Custom thermal insulation foam
class McDbTestCottonInsulation extends McDbCustomEntity {
    // Define a point object within McDbTestCottonInsulation 
    // Thermal insulation foam start point
    private startPt: McGePoint3d = new McGePoint3d();
    // End point of thermal insulation foam
    private endPt: McGePoint3d = new McGePoint3d();
    // Width of insulation cotton
    private width: number = 10;
    // Thermal insulation cotton height
    private height: number = 20;

    // constructor
    constructor(imp?: any) {
        super(imp);
    }
    // Create function
    public create(imp: any) {
        return new McDbTestCottonInsulation(imp)
    }
    // Get class name
    public getTypeName(): string {
        return "McDbTestCottonInsulation";
    }
    //Set or obtain the width of thermal insulation foam
    public set cWidth(val: number) {
        this.width = val;
    }
    public get cWidth(): number {
        return this.width;
    }
    // Set the thermal insulation foam height
    public set cHeight(val: number) {
        this.height = val;
    }
    public get cHeight(): number {
        return this.height;
    }
    // Read from defined entity data startPt、endPt、 width、height
    public dwgInFields(filter: IMcDbDwgFiler): boolean {
        this.startPt = filter.readPoint("startPt").val;
        this.endPt = filter.readPoint("endPt").val;
        this.width = filter.readDouble("width").val;
        this.height = filter.readDouble("height").val;
        return true;
    }
    // Writes custom entity data startPt、endPt、 width、height
    public dwgOutFields(filter: IMcDbDwgFiler): boolean {
        filter.writePoint("startPt", this.startPt);
        filter.writePoint("endPt", this.endPt);
        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();
        if (iIndex === 0) {
            this.startPt.x += dXOffset;
            this.startPt.y += dYOffset;
            this.startPt.z += dZOffset;
        } else {
            this.endPt.x += dXOffset;
            this.endPt.y += dYOffset;
            this.endPt.z += dZOffset;
        }

    };
    // Gets the pinch point of a custom object
    public getGripPoints(): McGePoint3dArray {
        let ret = new McGePoint3dArray()
        ret.append(this.startPt);
        ret.append(this.endPt);
        return ret;
    };
    // Draw entity
    public worldDraw(draw: MxCADWorldDraw): void {
        const line = new McDbLine();
        line.startPoint = this.startPt;
        line.endPoint = this.endPt;
        if (this.startPt.y < this.endPt.y) {
            line.startPoint = new McGePoint3d(this.endPt.x, this.endPt.y, this.endPt.z);
            line.endPoint = new McGePoint3d(this.startPt.x, this.startPt.y, this.startPt.z);;
        }
        const midPt = line.getPointAtDist(line.getLength().val / 2).val;
        const line_clone = line.clone() as McDbLine;
        line_clone.rotate(midPt, Math.PI / 2);
        const pt1 = line_clone.getPointAtDist(line.getLength().val / 2 + this.height / 2).val;
        const pt2 = line_clone.getPointAtDist(line.getLength().val / 2 - this.height / 2).val;
        let line1: any;
        let line2: any;
        line.offsetCurves(this.height / 2, pt1).forEach(e => {
            line1 = e;
        })
        line.offsetCurves(this.height / 2, pt2).forEach(e => {
            line2 = e;
        })
        let num = line.getLength().val / this.width * 2;
        let ptArr: McGePoint3d[] = [];
        let center1: McGePoint3d[] = [];
        let center2: McGePoint3d[] = [];
        for (let i = 0; i < num; i++) {
            if (i % 2 == 0) {
                let pt = line2.getPointAtDist(i * (this.width / 2)).val;
                let center = line1.getPointAtDist(i * (this.width / 2)).val;
                center1.push(center);
                ptArr.push(pt);
            } else {
                let pt = line1.getPointAtDist(i * (this.width / 2)).val;
                let center = line2.getPointAtDist(i * (this.width / 2)).val;
                center2.push(center);
                ptArr.push(pt);
            }
        };
        const pl = new McDbPolyline();
        ptArr.forEach(pt => {
            pl.addVertexAt(pt)
        })
        draw.drawEntity(pl);
        center1.forEach(pt => {
            const arc = new McDbArc();
            arc.center = pt;
            arc.radius = this.width / 2;
            arc.startAngle = -(new McGeVector3d(line.endPoint.x - line.startPoint.x, line.endPoint.y - line.startPoint.y)).angleTo1(McGeVector3d.kXAxis);
            arc.endAngle = (new McGeVector3d(line.startPoint.x - line.endPoint.x, line.startPoint.y - line.endPoint.y)).angleTo1(McGeVector3d.kXAxis);
            draw.drawEntity(arc);
        })
        center2.forEach(pt => {
            const arc = new McDbArc();
            arc.center = pt;
            arc.radius = this.width / 2;
            arc.startAngle = (new McGeVector3d(line.startPoint.x - line.endPoint.x, line.startPoint.y - line.endPoint.y)).angleTo1(McGeVector3d.kXAxis);
            arc.endAngle = -(new McGeVector3d(line.endPoint.x - line.startPoint.x, line.endPoint.y - line.startPoint.y)).angleTo1(McGeVector3d.kXAxis);
            draw.drawEntity(arc)
        })
    }
    // Set the starting point of the thermal insulation foam
    public setStartPoint(pt: McGePoint3d) {
        this.assertWrite();
        this.startPt = pt.clone();
    }
    // Obtain the starting point for obtaining thermal insulation foam
    public getStartPoint() {
        return this.startPt;
    }
    // Set the end point of the thermal insulation foam
    public setEndPoint(pt: McGePoint3d) {
        this.assertWrite();
        this.endPt = pt.clone();
    }
    // Obtain the end point of the thermal insulation foam
    public getEndPoint() {
        return this.startPt;
    }
}

Functional practice

Practical effects are as follows:

  • Click the thermal insulation foam button to perform the Draw thermal insulation foam method
  • Set the width and height of the thermal insulation foam as prompted
  • Click on the canvas to set the starting point of the thermal insulation foam
  • Paint insulation foam successfully