Select graphics
In CAD, there are many ways to select graphics, including click selection, box selection, cumulative selection, all selection and so on. mxcad also provides multiple interfaces to select graphs according to these selection methods. For example, MxCADUtilityClass provides a series of tools and methods related to drawing. MxCADSelectionSet() is used to select entities. The MxCADResbuf class is used to manipulate resbuf data in AutoCAD.
Basic Settings
mxcad in the default configuration is to open the selection of graphics editing function, we can use mxdraw provided by 'MxFun.setIniset' to achieve a variety of initial configuration, Configuration parameters reference iniConfig. Through the following Settings, we can edit and select the corresponding graphics through the mouse.
import { MxFun } from "mxdraw"
MxFun.setIniset({
// Enable pinch editing and radio graphics (mxcad is enabled by default)
"EnableGripEidt": true,
// Enable multiple selection
"multipleSelect": true
})
Click
Click to select the target object by directly clicking the graph with the mouse.
- By instantiating the MxCADUtilityClass class provided by mxcad, we can invoke the findEntAtPoint() method to find graph objects near a specified coordinate point or the selectEnt() method to select graph objects at a specified location.
Click on the MxCADUtilityClass() View detailed property and method descriptions.
import { MxCADUiPrPoint, MxCADResbuf, MxCADUtility } from "mxcad"
const getPoint = new MxCADUiPrPoint()
// Definition filter
const filter = new MxCADResbuf()
// Add object types, select sets Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT")
// Mode one:findEntAtPoint
const getPoint = new MxCADUiPrPoint()
const point = await getPoint.go()
if(!point) return
let objId = MxCADUtility.findEntAtPoint(point.x, point.y,point.z,-1,filter)
// Select the object to set highlighting
MxCADUtility.highlightEntity(objId.id, true)
console.log(objId)
// Mode two:selectEnt
let objIds = await MxCADUtility.selectEnt("Specify a location to select a graphic object", filter)
console.log(objIds)
- We can invoke the pointSelect() method by instantiating the selection set MxCADSelectionSet() provided by mxcad.
Click pointSelect() to check the properties and methods in detail.
import { MxCADSelectionSet, MxCADResbuf } from "mxcad"
// Select by a single point
const ss = new MxCADSelectionSet();
const filter = new MxCADResbuf();
// Add object type, select set Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT")
const getPoint = new MxCADUiPrPoint()
const point = await getPoint.go()
if (!point) return;
// Add filter to filter the selection set. Only text objects are selected here
const index = ss.pointSelect(point.x, point.y, filter)
const objId = ss.item(index)
const ent = objId.getMcDbEntity()
console.log(ent)
Box select
Box selection is to click a point in the blank area of the drawing to determine an intersection of the box, and then drag the mouse to form a rectangular box to select the object.
- We can call the userSelect() method by instantiating the selection set MxCADSelectionSet() provided by mxcad for frame selection of drawing contents.
Click userSelect() to check the properties and methods in detail.
import { MxCADSelectionSet, MxCADResbuf } from "mxcad"
const ss = new MxCADSelectionSet();
const filter = new MxCADResbuf();
// Add object type, select set Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT")
// Add filter to filter the selection set. Only text objects are selected here
ss.userSelect("Box selects the desired object", filter).then((is) => {
if (is) {
// Gets the two selection points selected by the box
const { pt1, pt2 } = ss.getSelectPoint()
ss.getIds()
ss.forEach((id) => {
let ent = id.getMcDbEntity();
if (!ent) return;
console.log(ent)
})
}
})
- By instantiating the MxCADUtilityClass class provided by mxcad, we can invoke the userSelect() method to select drawing contents.
Click on the MxCADUtilityClass() View detailed property and method descriptions.
import { MxCADSelectionSet, MxCADResbuf } from "mxcad"
const filter = new MxCADResbuf();
// Add object type, select set Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT")
let ids = await MxCADUtility.userSelect(' box select desired object ', filter)
console.log(ids)
Add up the choices
Cumulative selection means that as long as the selected object can be continuously clicked and box selected, all selected objects will be added to the selection set. The cumulative selection described below is a reference example, or you can implement your own cumulative selection according to the click and box selection methods described above.
- Click: We can call the addCurrentSelect() method of the mxcad instance object to manually add the graph object ID to the current selection, and then use the getCurrentSelect() method of the MxCADUtilityClass class to get the currently selected object.
tap addCurrentSelect(), getCurrentSelect() to check the properties and methods in detail.
import { McApp, MxCADUiPrPoint, MxCADUtility, McObjectId, McObjectIdType } from "mxcad"
let mxcad = MxCpp.getCurrentMxCAD();
while(true){
const getPoint = new MxCADUiPrPoint()
const point = await getPoint.go()
if(!point) break;
let objId = MxCADUtility.findEntAtPoint(point.x, point.y,point.z,-1)
mxcad.addCurrentSelect(objId)
}
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())
}
})
- Box selection: We can implement cumulative box selection by setting the isWhileSelect property of the selection set MxCADUtilityClass() to true.
import { MxCADSelectionSet, MxCADResbuf } from "mxcad"
const ss = new MxCADSelectionSet();
ss.isWhileSelect = true
ss.isSelectHighlight = true
ss.userSelect("Box selects the desired object").then(() => {})
Select all
Select All: Select all graphics objects on the current drawing. We can directly call the allselect() method in the selection set MxCADSelectionSet(), which can also be combined with the MxCADResbuf() class to implement filtering selection.
import { MxCADSelectionSet, MxCADResbuf } 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 type, select set Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT")
// Select all text objects
ss.allSelect(filter);
Listen for selection events
We can call the on() method in mxcad's instance object to listen for object selection events.
import { MxCpp } from "mxcad"
// When the graph is currently selected, the 'selectChange' event is triggered and the list of id objects that are currently selected is called back
MxCpp.getCurrentMxCAD().on("selectChange", (ids: McObjectId[])=> {})
Clear selection
We can call the clearMxCurrentSelect() method to clear all selections for the target drawing.
import { MxCpp } from "mxcad"
// Discard all selected objects on the target drawing
MxCpp.getCurrentMxCAD().mxdraw.clearMxCurrentSelect()