Event listening
In the process of CAD secondary development through mxcad, we often involve monitoring some events, such as entity selection event on the drawing, clip editing event, mxcad instance object creation completion time and so on. Therefore, we provide McObject.on()
, McObject.mxdraw.on()
methods to set the associated series of listening events, and McObject.off()
, McObject.mxdraw.off()
methods to turn off listening events. Understanding and skillfully using these events can help users complete the development of the project more smoothly. Below we will introduce several common event monitoring.
Click [McObject.on()]https://www.mxdraw3d.com/mxcad_docs/api/classes/2d.McObject.html#on)、McObject.mxdraw.on()、[McObject.off()]https://www.mxdraw3d.com/mxcad_docs/api/classes/2d.McObject.html#off)、McObject.mxdraw.off() to check the properties and methods in detail.
Listen to project initialization
We set the event name init
in the McObject.on()
method to listen for the initialization phase of the project, when the MxCAD instance object has not yet been created.
import { McObject } from "mxcad"
const mxcad = new McObject;
mxcad.on("init", () => {
console.log('项目初始化')
});
Listen for mxcad initialization
We set the event name init_mxcad
in the McObject.on()
method to listen to the initialization period of the MxCAD instance object, in this stage we can complete the setting of adding mouse and keyboard response events, setting the listening file fully open events, registering commands, etc.
import { McObject } from "mxcad"
const mxcad = new McObject;
mxcad.on("init_mxcad", () => {
console.log('mxcad 初始化')
});
Listen object selection event
We set the event name selectChange
in the McObject.on()
method to listen for whether the entity on the drawing is selected, and the callback argument of the event will return an array of the ids of the selected entity (McObjectId[]
).
import { MxCpp, McObject, McObjectId } from "mxcad"
const mxcad:McObject = MxCpp.getCurrentMxCAD();
mxcad.on("selectChange", (ids: McObjectId[])=> {
if (ids.length == 0) return;
ids.forEach( id =>{
let ent = id.getMcDbEntity()
if(!ent) return
console.log(ent.objectName)
})
})
In addition, we can also monitor the double-click object event by monitoring whether the user clicks the same entity several times in a short period of time on the basis of listening to the object selection event.
import { MxCpp, McDbMText, McDbText, McDbEntity } from "mxcad";
let oldEnt: McDbEntity | null
let time: NodeJS.Timeout
MxCpp.getCurrentMxCAD().on("selectChange", (selectEnt) => {
if (selectEnt.length === 0) return
const ent = selectEnt[0]?.getMcDbEntity()
if (ent && ent instanceof McDbMText && oldEnt?.getHandle() === ent.getHandle()) {
// 双击多行文本
console.log(ent.contents)
}
if (ent && ent instanceof McDbText && oldEnt?.getHandle() === ent.getHandle()) {
// 双击单行文本
console.log(ent.textString)
}
oldEnt = ent
if (time) clearTimeout(time)
time = setTimeout(() => {
oldEnt = null
}, 300)
})
Listen for file fully open events
We set the event name openFileComplete
in the McObject.on()
method to listen for the MxCAD instance object to successfully open the drawing.
import { MxCpp } from "mxcad"
const mxcad:McObject = MxCpp.getCurrentMxCAD();
mxcad.on("openFileComplete", () => {
console.log("MxTip:openFileComplete ")
});
Listen for pinch edit events
We set the event name objectGripEdit
in the McObject.mxdraw.on()
method to listen for whether there is a physical pinch point in the edit drawing, and the moved pinch point information will be returned in the callback parameter of the event.
import { MxCpp, McObjectIdType, McObjectId } from "mxcad"
const mxcad:McObject = MxCpp.getCurrentMxCAD();
mxcad.mxdraw.on("objectGripEdit",(grips)=>{
grips.forEach((grip)=>{
const id = new McObjectId(grip.id, grip.type === "mxcad" ? McObjectIdType.kMxCAD: McObjectIdType.kMxDraw);
const ent = id.getMcDbEntity();
console.log('Edit entity information',ent)
})
})
Turn off listening events
If we want to turn off a listening event that we set earlier, we can call the event's corresponding turn off method off()
.
import { MxCpp } from "mxcad"
const mxcad:McObject = MxCpp.getCurrentMxCAD();
mxcad.off("selectChange", () => {
console.log("Successfully turn off the listening selection event!")
});
mxcad.mxdraw.off("objectGripEdit",()=>{
console.log("Successfully closed pinch editing event!")
})