Skip to content
On this page

Here is the English translation of the provided article:

Selecting Graphic Elements for Editing

Selecting and editing graphic elements, especially editing grips on graphics, is a fundamental feature in CAD.

Selecting Graphic Editing Functions

By default, mxcad enables the graphic selection and editing functionality. You can use the MxFun.setIniset provided by mxdraw to achieve various initialization configurations. Refer to the iniConfig for configuration parameters.

ts
import { MxFun } from "mxdraw";
MxFun.setIniset({
    // Enable grip editing, enable single selection of graphics (enabled by default in mxcad)
    "EnableGripEidt": true,
    // Enable multiple selection
    "multipleSelect": true
});

With the above settings, you can edit and select graphics using the mouse.

To get the currently selected graphics:

ts
import { MxCpp, MxCADUtility, McObjectId, McObjectIdType } from "mxcad";

// This should be called after createMxCad has completed creating the control
const objIds = MxCADUtility.getCurrentSelect();

objIds.forEach((objId: McObjectId)=> {
    if(objId.type === McObjectIdType.kMxCAD) {
        console.log("CAD graphic object", objId.getMcDbEntity());
    }
    if(objId.type === McObjectIdType.kMxDraw) {
        console.log("Drawing object", objId.getMxDbEntity());
    }
});

You can also use MxCADSelectionSet for selection:

ts
import { MxCADSelectionSet, MxCADResbuf, MxCADUiPrPoint } from "mxcad";

const ss = new MxCADSelectionSet();

// Select all graphic elements
ss.allSelect();

ss.forEach((id)=> {
    let ent = id.getMcDbEntity();
    if (!ent) return;
    console.log(ent);
});

const filter = new MxCADResbuf();
// Add object types, selecting only text types of objects in the selection set
filter.AddMcDbEntityTypes("TEXT,MTEXT");
ss.allSelect(filter);

// Get object IDs using the first method
ss.getIds();

// Get object IDs using the second method
ss.forEach((id)=> {
    let ent = id.getMcDbEntity();
    if (!ent) return;
    console.log(ent);
});

// Select by a single point
const getPoint = new MxCADUiPrPoint();
getPoint.go().then((point)=> {
    if(!point) return;
    // Apply filter to the selection set, selecting only text objects
    const index = ss.pointSelect(point.x, point.y, filter);
    const objId = ss.item(index);
    const ent = objId.getMcDbEntity();
    console.log(ent);
});

// Select multiple objects using the mouse
// Apply filter to the selection set, selecting only text objects
ss.userSelect("Specify objects for the selection", filter).then((is)=> {
    if(is) {
        // Get the two selection points for the selection box
        const { pt1, pt2 } =  ss.getSelectPoint();
        ss.getIds();
        ss.forEach((id)=> {
            let ent = id.getMcDbEntity();
            if (!ent) return;
            console.log(ent);
        });
    }
});

You can manually add graphic object IDs to the current selection:

ts
import { McApp } from "mxcad";

let mxcad = MxCpp.getCurrentMxCAD();
let id = mxcad.drawLine(0, 0, 1000, 1000);
mxcad.addCurrentSelect(id);

Selection edit events:

ts
import { MxCpp } from "mxcad";

// The "selectChange" event is triggered when a graphic is currently selected, and it provides a callback with the list of currently selected IDs
MxCpp.getCurrentMxCAD().on("selectChange", (ids: McObjectId[])=> {});

// The "databaseModify" event is triggered by mxdraw when a CAD drawing is edited or grip points are dragged. It indicates that the drawing has been modified.
const mxdraw = MxCpp.getCurrentMxCAD().getMxDrawObject();
mxdraw.on("databaseModify", ()=> {
    console.log("Drawing modified");
});

Feel free to adapt these code snippets to fit your specific backend and frontend architectures. If you have any specific questions or need further clarification on any part of the implementation, feel free to ask!