Skip to content
On this page

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()McObject.mxdraw.on()McObject.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.

ts
import { McObject } from "mxcad"

const mxcad = new McObject;
mxcad.on("init", () => {
   console.log('Project initialization')
});

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.

ts
import { McObject } from "mxcad"

const mxcad = new McObject;
mxcad.on("init_mxcad", () => {
   console.log('mxcad initialize')
});

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[]).

ts
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.

ts
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()) {
    // Double-click multiple lines of text
    console.log(ent.contents)
  }
  if (ent && ent instanceof McDbText && oldEnt?.getHandle() === ent.getHandle()) {
    // Double-click a single line of text
    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.

ts
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.

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

const mxcad:McObject = MxCpp.getCurrentMxCAD();
// Listening event
const eventListener = (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)
    })
}
// Register event listening
mxcad.mxdraw.on("objectGripEdit",eventListener)

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().

ts
import { MxCpp } from "mxcad"

import { MxCpp } from "mxcad"

const mxcad:McObject = MxCpp.getCurrentMxCAD();
// An event that has been registered to listen to
const event1 = ()=>{
    console.log('Select event listening is set')
}
mxcad.off("selectChange", event1);

// An event that has been registered to listen to
const event2 = ()=>{
    console.log('Set up pinch edit event listening')
}
mxcad.mxdraw.off("objectGripEdit",event2)

Because the pinch close event is executed immediately, other operations that need to be performed when the listening target event is turned off can be performed directly after the listening event is turned off. The following example is to disable listening after only one listening event is executed:

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

const mxcad:McObject = MxCpp.getCurrentMxCAD();
const e = (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 event
         mxcad.mxdraw.off("objectGripEdit", e);
         console.log('Pinch edit event closed') ;
}) }

// Register event listening
mxcad.mxdraw.on("objectGripEdit", e)