It is important to remember that events simply provide information on the state or activities taking place within AutoCAD. Although event handlers can be written to respond to those events, AutoCAD might be in the middle of an operation when the event handler is triggered. Event handlers, therefore, have some restrictions on what they can do if they are to provide safe operations in conjunction with AutoCAD and its database.
需要记住的是,事件只是提供了 AutoCAD 内部发生的状态或动作信息。虽然事件处理程序可以响应这些事件,但触发事件处理程序时,AutoCAD 通常都是正处在处理命令的执行过程中。因此,如果要使事件处理程序在涉及 AutoCAD 及其数据库时提供安全的操作,则需要对事件处理程序进行一些限制。
When writing event handlers, do not rely on the sequence of events to happen in the exact order you think they occur. For example, if you issue an OPEN command, the events CommandWillStart, DocumentCreateStarted, DocumentCreated, and CommandEnded will all be triggered. However, they may not occur in that exact order each and everytime. The only thing you can rely on is that a most events occur in pairs, a beginning and ending event.
编写事件处理程序时,请不要依赖用户所认为的事件发生顺序。例如,如果用户发出 OPEN 命令,则事件 CommandWillStart, DocumentCreateStarted, DocumentCreated, 和 CommandEnded 将全部被触发。但它们的发生顺序可能并不是这样。唯一可以确定大多数事件都是成对出现的,你 beginning 和 Ending 事件。
If you delete object1 and then object2, do not rely on the fact that you will receive the ObjectErased event for object1 and then for object2. You may receive the ObjectErased event for object2 first.
如果用户删除 object1,然后再删除 object2,这时请不要依赖这样的事实,即先收到 object1 的 ObjectErased 事件,然后收到 object2 的事件。实际上用户可能会先收到 object2 的 ObjectErased 事件。
Attempting to execute interactive functions from within an event handler can cause serious problems, as AutoCAD may still be processing a command at the time the event is triggered. Therefore, you should always avoid requesting for input at the Command prompt, as well as object selection requests, and using the SendStringToExecute method from within event handlers.
试图从事件处理程序中执行交互式函数可能会造成严重问题,因为触发事件时 AutoCAD 可能仍在处理命令。因此,应始终在事件处理程序中避免在命令提示符下请求用户输入,也不要请求选择对象和 SendStringToExecute 方法。
Dialog boxes are considered interactive functions and can interfere with the current operation of AutoCAD. Message boxes and alert boxes are not considered interactive and can be issued safely; however issuing a message box within some event handlers such as EnterModal, LeaveModal, DocumentActivated, and DocumentToBeDeactivated can result in unexpected sequencing.
对话框和交互式函数一样,也会干涉 AutoCAD 的当前操作。消息框和警告框不被认为是交互的,因而可以安全地发出;但在事件处理程序中像 EnterModal, LeaveModal, DocumentActivated 和 DocumentToBeDeactivated 事件中发出消息框则会导致顺序混乱。
Obviously, any object causing an event to be triggered could still be open and the operation currently in progress. Therefore, avoid modifying an object from an event handler for the same object. However, you can safely read information from the object triggering an event.
显然,导致触发事件的任何对象可能仍在 处于打开状态,并用于当前正在执行的操作。因此,要避免从对象的事件处理程序将任何信息再写入到该对象。然而,用户可以安全地从触发事件的对象读取信息。
If you perform the same action in an event handler that triggers that same event, you will create an infinite loop. For example, you should never attempt to open an object from within the ObjectOpenedForModify event, or AutoCAD will simply continue to open objects.
如果在事件处理程序中执行触发该相同事件的相同操作,会导致无限循环。例如,用户不该尝试从 ObjectOpenedForModify 事件中打开对象;否则,AutoCAD 将不断打开对象。