Skip to content
On this page

Common editing operations

Common modification and editing commands in CAD include move, copy, scale, rotate, mirror, cut, extend, array, modify and edit, which can help users modify, adjust and optimize the drawing to improve work efficiency and design quality. According to this requirement, mxcad provides the corresponding interface and method, and implements the common modification and editing commands in CAD. These operations involve the calculation of points, vectors, matrices, angles, etc., the specific rules can be referred to Guide - Mathematical library .

Delete

We can remove the lines and excess lines from the drawing process by calling the entity's erase() method.

ts
import { MxCADUtility, McObjectId } from "mxcad"

const objIds = MxCADUtility.getCurrentSelect()
objIds.forEach((objId: McObjectId) => {
    objId.erase()
})

Copy

We can copy multiple objects by calling the entity's clone() method, or we can copy one or more objects multiple times. Click on the copy entity to move to the target location after the copy is complete.

ts
import { MxCADUtility, McDbEntity, McObjectId, MxCpp } from "mxcad"

const objIds = MxCADUtility.getCurrentSelect()
objIds.forEach((objId: McObjectId) => {
    const event = objId.getMcDbEntity()
    if (!event) return;
    const event_clone = event.clone() as McDbEntity
    MxCpp.getCurrentMxCAD().drawEntity(event_clone)
})

In addition, we can call the entity's transformBy() method to copy the object. The method is to realize the editing of the graph through the transformation matrix, and the specific matrix transformation principle can be referred to Matrix in the Guide Mathematical library-mcgematrix3d.

Click on McGeMatrix3d - clone() for rectangular transform methods and properties details.

ts
import { MxCADUtility, McDbEntity, McObjectId,  MxCpp} from "mxcad"

const objIds = MxCADUtility.getCurrentSelect()
objIds.forEach((objId: McObjectId) => {
    const event = objId.getMcDbEntity()
    if (!event) return;
    const event_clone = event.clone() as McDbEntity
    const matrix = new McGeMatrix3d()
    matrix.clone()
    event_clone.transformBy(matrix)
    MxCpp.getCurrentMxCAD().drawEntity(event_clone)
})

Mirror image

We can mirror the specified object with the specified reference line by calling the entity's mirror() method.

ts
import { MxCADUtility, McObjectIdMcGeMatrix3d, MxCADUiPrPoint } from "mxcad"

async function Mx_test_mirror(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt1 = new MxCADUiPrPoint()
    getPt1.setMessage("Specifies the start point of the mirror guide")
    const pt1 = await getPt1.go()
    if(!pt1) return
    const getPt2 = new MxCADUiPrPoint()
    getPt2.setMessage("Specifies the end point of the mirror guide")
    const pt2 = await getPt2.go()
    if(!pt2) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
        event.mirror(pt1, pt2)
    })
}

In addition, we can call the entity's transformBy() method to mirror the processing object. The method is to realize the editing of the graph through the transformation matrix, and the specific matrix transformation principle can be referred to Matrix in the Guide Mathematical library -mcgematrix3d.

Click on McGeMatrix3d - setMirror() View rectangle transform details methods and properties.

ts
import { MxCADUtility, McObjectIdMcGeMatrix3d, MxCADUiPrPoint } from "mxcad"

async function Mx_test_mirror(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt1 = new MxCADUiPrPoint()
    getPt1.setMessage("Specifies the start point of the mirror guide")
    const pt1 = await getPt1.go()
    if(!pt1) return
    const getPt2 = new MxCADUiPrPoint()
    getPt2.setMessage("Specifies the end point of the mirror guide")
    const pt2 = await getPt2.go()
    if(!pt2) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
        const matrix = new McGeMatrix3d()
        matrix.setMirror(pt1,pt2)
        event.transformBy(matrix)
    })
}

Move

We can move the target object by calling the entity's move() method, which only changes the original location of the object and does not copy the object.

ts
import { MxCADUtility, McGePoint3d, McObjectId } from "mxcad"

const objIds = MxCADUtility.getCurrentSelect()
const pt1= new McGePoint3d(0,0,0)
const pt2 = new McGePoint3d(100,100,0)
objIds.forEach((objId: McObjectId) => {
    objId.getMcDbEntity()?.move(pt1, pt2)
})

In addition, we can call the entity's transformBy() method to move the object. The method is to realize the editing of the graph through the transformation matrix, and the specific matrix transformation principle can be referred to [Matrix in the Guide Mathematical library -mcgematrix3d](... /2. Guide /1. Math library. html# matrix -mcgematrix3d).

tap McGeMatrix3d-setToTranslation() View rectangle transform details methods and properties.

ts
import { MxCADUtility, McGePoint3d, McObjectId } from "mxcad"

const objIds = MxCADUtility.getCurrentSelect()
const pt1= new McGePoint3d(0,0,0)
const pt2 = new McGePoint3d(100,100,0)
objIds.forEach((objId: McObjectId) => {
    const event = objId.getMcDbEntity()
    if(!event) return
    const matrix = new McGeMatrix3d()
    const v = pt2.sub(pt1)
    matrix.setToTranslation(v)
    event.transformBy(matrix)
})

Spin

We can rotate the selected object at a specified Angle around a specified base point by calling the entity's rotate() method.

ts
import { MxCADUtility, MxCADUiPrPoint } from "mxcad"

async function Mx_test_rotate(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt1 = new MxCADUiPrPoint()
    getPt1.setMessage("Specified rotation base")
    const pt1 = await getPt1.go()
    if(!pt1) return
    const getPt2 = new MxCADUiPrPoint()
    getPt2.setMessage("Specified rotation Angle")
    getPt2.setUserDraw((pt,pw)=>{
        const line = new McDbLine(pt1.x,pt1.y,pt1.z,pt.x,pt.y,pt.z)
        pw.drawMcDbEntity(line)
        objIds.forEach((objId: McObjectId) => {
            const event = objId.getMcDbEntity()?.clone() as McDbEntity
            if(!event) return
            // Rotation Angle
            const dRotationAngle = pt1.sub(pt).angleTo2(McGeVector3d.kXAxis, McGeVector3d.kZAxis);
            event.rotate(pt1, dRotationAngle)
            pw.drawMcDbEntity(event)
        })
    })
    const pt2 = await getPt2.go()
    if(!pt2) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
        const dRotationAngle = pt1.sub(pt2).angleTo2(McGeVector3d.kXAxis, McGeVector3d.kZAxis);
        event.rotate(pt1, dRotationAngle)
    })
}

In addition, we can call the entity's transformBy() method to rotate the object. The method is to realize the rotation of the graph by transforming the matrix, and the specific matrix transformation principle can be referred to Matrix in the Guide Mathematical library -mcgematrix3d.

tap McGeMatrix3d-setToTranslation() View rectangle transform details methods and properties.

ts
import { MxCADUtility, MxCADUiPrPoint, McGeMatrix3d, McObjectId } from "mxcad"

async function Mx_test_rotate(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt1 = new MxCADUiPrPoint()
    getPt1.setMessage("Specify the center point of rotation")
    const pt1 = await getPt1.go()
    if(!pt1) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
        // Rotate 45 degrees around the z axis with pt1 as the center point
        const matrix = new McGeMatrix3d()
        matrix.setToRotation(Math.PI / 4, McGeVector3d.kZAxis, pt1)
        event.transformBy(matrix)
    })
}

offset

We can copy lines, circles, and segments concentrically by calling the entity's offsetCurves() method, which is equivalent to moving them parallel for a certain distance.

ts
import { MxCADResbuf, MxCADUtility, MxCpp } from "mxcad"

async function Mx_test_offset(){
    const filter = new MxCADResbuf();
    filter.AddMcDbEntityTypes("LINE") ;
    const objIds = MxCADUtility.getCurrentSelect(filter)
    const getPt1 = new MxCADUiPrPoint()
    getPt1.setMessage("Specified offset point")
    const pt1 = await getPt1.go()
    if(!pt1) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity() ;
       let objArr = (event as McDbLine).offsetCurves(100,pt1)
       if (objArr.length() === 0) return;
       objArr.forEach(obj=>{
        MxCpp.getCurrentMxCAD().drawEntity(obj as McDbEntity)
       })
    })
}

Scale

We can scale the graph freely by calling the entity's scaleEntity() method.

ts
import { MxCADUtility, MxCADUiPrPoint } from "mxcad"

async function Mx_test_scale(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt = new MxCADUiPrPoint()
    getPt.setMessage("Specified scale base")
    const pt = await getPt.go()
    if(!pt) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
         event.scaleEntity(pt,0.2)
    })
}

In addition, we can scale the object by calling the entity's transformBy() method. The method is to realize the scaling of the graph through the transformation matrix, the specific matrix transformation principle can be referred to Matrix in the Guide Mathematics library -mcgematrix3d

Click on McGeMatrix3d - setToScaling() View rectangle transform details methods and properties.

ts
import { MxCADUtility, MxCADUiPrPoint } from "mxcad"

async function Mx_test_scale(){
    const objIds = MxCADUtility.getCurrentSelect()
    const getPt = new MxCADUiPrPoint()
    getPt.setMessage("Specify the zoom center point")
    const pt = await getPt.go()
    if(!pt) return
    objIds.forEach((objId: McObjectId) => {
        const event = objId.getMcDbEntity()
        if(!event) return
        const matrix = new McGeMatrix3d()
        matrix.setToScaling(0.2, pt)
        event.transformBy(matrix)
    })
}