Skip to content
On this page

Zoom

Below we will show how to use the mxcad plug-in to implement the zoom function in CAD drawings. In this function, the user can frame or click the target entity to scale it, which is used to edit the size of the entity in the drawing. In our document Common Editing operations has done the basic explanation of scaling operations, the following we will achieve the scaling function that can interact with users on this basis.

Function implementation

  1. Select the target entity

We can call MxCADSelectionSet Select the entity that you want to scale. The maximum and minimum points of the bounding box are obtained according to the selected entity, and the scaling distance ratio is set according to the obtained data.

ts
// Select the object you want to scale
let ss = new MxCADSelectionSet();
if (!await ss.userSelect("\n选择移动对象")) return;
// Gets the maximum and minimum points of the scaled bounding box
let pt1 = null, pt2 = null;
ss.forEach(id => {
    const ent = id.getMcDbEntity();
    const { minPt, maxPt } = ent.getBoundingBox();
    if (!pt1 || !pt2) {
        pt1 = minPt;
        pt2 = maxPt;
    } else {
        if (maxPt.x > pt2.x) pt2.x = maxPt.x;
        if (maxPt.y > pt2.y) pt2.y = maxPt.y;
        if (minPt.x < pt1.x) pt1.x = minPt.x;
        if (minPt.y < pt1.y) pt1.y = minPt.y;
    }
});

// Set the zoom distance ratio
let dXLen = pt2.x - pt1.x;
let dYLen = pt2.y - pt1.y;
let dScaleLen = (dXLen + dYLen) / 3.0;
if (dScaleLen < 0.00001) dScaleLen = 1;
  1. Set the scaling base point and scaling ratio

We by calling MxCADUiPrPoint take the object to determine the scaling of the object's basis points, Call MxCADUiPrDist to dynamically set or direct input zoom zoom factor of objects.

ts
// Designated base point
const getBasePt = new MxCADUiPrPoint();
getBasePt.setMessage('Please set the zoom point');
const basePoint = await getBasePt.go();
if (!basePoint) return;

// Set scale
const getScale = new MxCADUiPrDist();
getScale.setMessage('Please set the scale');
getScale.setBasePt(basePoint);
getScale.setUserDraw((pt, pw) => {
    const dist = pt.distanceTo(basePoint);
    let dScale = dist / dScaleLen;
    ss.forEach(id => {
        const ent = id.getMcDbEntity();
        const _clone = ent.clone() as McDbEntity;
        let mat = new McGeMatrix3d();
        mat.setToScaling(dScale, basePoint);
        _clone.transformBy(mat);
        pw.drawMcDbEntity(_clone);
    })
})
const val = await getScale.go();
if (!val) return;
const dist = getScale.value() || 1;
  1. Scale the entity

Since there are two ways to obtain the scale factor, one is to calculate the scale factor according to the dynamic distance between the user's moving mouse position and the base point, and the other is to input the entity's scale factor directly through the input box. We can go through MxCADUiPrDist.getDetailedResult() The ult method obtains the detailed reason for the interaction exit and sets the scaling factor of the entity according to the two methods respectively. After scaling, Call McObject.updateDisplay() method to update the drawing shows.

ts
// Get scale factor
let dScale = 0;
if(getScale.getDetailedResult() === DetailedResult.kCoordIn){
    // Enter the scale factor directly
    dScale = dist;
}else{
    // The scaling factor is calculated based on the scale of the distance
    dScale = dist / dScaleLen;
}
// Scaling entity
ss.forEach(id => {
    const ent = id.getMcDbEntity();
    let mat = new McGeMatrix3d();
    mat.setToScaling(dScale, basePoint);
    ent.transformBy(mat);
})
// Update drawing display
MxCpp.getCurrentMxCAD().updateDisplay();

Functional practice

Practical effects are as follows:

  • Click the Zoom button to perform the zoom method
  • Left click to select the target entity, right click to end the selection (or direct box selection)
  • Follow the command line prompt and left click to set the scaling point
  • Move the mouse to set the zoom factor
  • Left-click again to determine the size of the entity after scaling
  • (or enter the scale factor directly in the input field and press Enter)
  • Successfully scaled entity