Skip to content
On this page

Rectangle scaling

Below we will show how to use the mxcad plug-in to implement the rectangular scaling function in CAD drawings. In this function, the user selects the target entity and sets the size of the reference rectangle. The selected entity will be scaled on the X-axis according to the width of the reference rectangle, and scaled on the Y-axis according to the height of the reference rectangle.

Function implementation

  1. Select the target entity and consolidate it into a block

We can call MxCADSelectionSet Select the entity that you want to scale. Passes the selected entity McDbBlockTableRecord.appendAcDbEntity() method is consolidated into blocks. And through the properties McDbBlockTableRecord.origin Set the center point of the consolidated block object as the base point.

ts
// Gets the entity that needs to be scaled
let ss = new MxCADSelectionSet();
if (!await ss.userSelect("Select the entity that you want the rectangle to scale:")) return;
if (ss.count() == 0) return;

// Consolidate the target entity into a block object
let mxcad = MxCpp.getCurrentMxCAD();
let blkTable = mxcad.getDatabase().getBlockTable();
let blkRecId = blkTable.add(new McDbBlockTableRecord());//Block record
let blkTableRecord: McDbBlockTableRecord = blkRecId.getMcDbBlockTableRecord() as any;
if (blkTableRecord == null) return;

ss.forEach(id => {
    const ent = id.getMcDbEntity();
    if (!ent) return;
    const ent_clone = ent.clone() as McDbEntity;
    blkTableRecord.appendAcDbEntity(ent_clone);
})

let blkRef = new McDbBlockReference();
blkRef.blockTableRecordId = blkRecId;
// Set the block base point by calculating the physical bounding box
const { ret, maxPt, minPt } = blkRef.getBoundingBox();
if (!ret) return;
const line = new McDbLine(minPt, maxPt);
const length = line.getLength().val;
blkTableRecord.origin = line.getPointAtDist(length / 2).val;
  1. Set the size of the reference rectangle

We by calling MxCADUiPrPoint take the object to determine the reference rectangular two angular point, And according to the Angle point location it is concluded that the other two vertices, through McDbPolyline section of the line to dynamically draw a rectangle.

ts
// 选择矩形的两个角点
const getPt1 = new MxCADUiPrPoint();
getPt1.setMessage("请指定矩形的角点1");
const pt1 = await getPt1.go();
if (!pt1) return;
const getPt2 = new MxCADUiPrPoint();
getPt2.setMessage("请指定矩形角点2");
getPt2.setUserDraw((pt, pw) => {
    let pl = new McDbPolyline();
    pl.addVertexAt(pt1);
    pl.addVertexAt(new McGePoint3d(pt1.x, pt.y));
    pl.addVertexAt(pt);
    pl.addVertexAt(new McGePoint3d(pt.x, pt1.y));
    pl.isClosed = true;
    pw.drawMcDbEntity(pl);
})
const pt2 = await getPt2.go();
if (!pt2) return;
  1. Set the scaling factor and draw the scaled rectangle entity

In the above step, we can obtain the width and height of the reference rectangle, the size of the bounding box where all the selected entities are located, and we can obtain the scaling factor of the entities on the X and Y axes respectively according to the ratio between the two. And finally, Set the fast object based on the resulting scaling factor [McDbBlockReference.scaleFactors](https://www.mxdraw3d.com/mxcad_docs/api/classes/2d.McDbBlockReference.html#scalefactor s) property and draw the scaled entity.

In the following example, we set the scaled entity position to the center point of the reference rectangle. In addition, We can also through MxCADUiPrPoint take object to dynamically set the position of the object.

ts
// Calculated scaling factor
const scaleX = Math.abs(pt1.x - pt2.x) / Math.abs(minPt.x - maxPt.x);
const scaleY = Math.abs(pt1.y - pt2.y) / Math.abs(minPt.y - maxPt.y);
blkRef.scaleFactors = new McGePoint3d(scaleX, scaleY, 0);
const l = new McDbLine(pt1, pt2);
blkRef.position = l.getPointAtDist(l.getLength().val / 2).val;
// Draw object
mxcad.drawEntity(blkRef);

Functional practice

Practical effects are as follows:

  • Click the Rectangle Zoom button to perform the rectangle zoom method
  • Left click to select the target entity, right click to end the selection
  • Follow the command line prompts to set reference rectangle corners
  • Successfully draws the entity after scaling the rectangle