Cadonline Forum :: Xem chủ đề - Reactors - Dịch từ www.afralisp.net (Kenny Ramage)
 Trợ giúpTrợ giúp   Tìm kiếmTìm kiếm   Nhóm làm việcNhóm làm việc   Thông tin cá nhânThông tin cá nhân   Tin nhắn riêngTin nhắn riêng   Đăng nhậpĐăng nhập 

Reactors - Dịch từ www.afralisp.net (Kenny Ramage)

 
gửi bài mới   Trả lời chủ đề này    Cadonline Forum -> Lisp nâng cao
Xem chủ đề trước :: Xem chủ đề kế  
Tác giả Thông điệp
hochoaivandot
Quản trị
Quản trị


Ngày tham gia: 04/12/2011
Bài gửi: 107
Đến từ: Quảng Nam

Bài gửigửi: 06.12.2011    Tiêu đề: Reactors - Dịch từ www.afralisp.net (Kenny Ramage) Trả lời với trích dẫn nội dung bài viết này

What are Reactors?
"What the heck is a reactor?" I asked myself exactly the same question when I first came across the phrase. "It must be another name for an Event", I said to myself. I was wrong, again!
" 1 Reactor là cái chết tiệc gì? Tôi đã tự hỏi như vậy khi lần đầu tiên tôi đọc tình cờ đọc nó. "Nó là cách gọi khác của Event (Sự kiện)", Một lân nữa tôi đã sai!
A reactor is an Object you attach to AutoCAD Objects to have AutoCAD notify your application when a particular event, or events occur. (Remember, that an AutoCAD Object can be an Object within your drawing, the drawing itself, or even AutoCAD, the application - just thought I'd tell you). This is the general procedure:
Reactor là một đối tượng bạn gắn với các đối tượng AutoCAD để yêu cầu Acad thông báo tới ứng dụng của bạn khi 1 hoặc những Event nào đó xuất hiện, (Hãy nhớ rằng, một đối tượng AutoCAD có thể là một đối tượng trong bản vẽ của bạn, chính bản vẽ, hoặc thậm chí ứng dụng AutoCAD) . Dưới đây là quá trình tổng quát :
* An Event occurs in your drawing.
Event xuất hiện trong bản vẽ của bạn.
* AutoCAD notifies the Reaction associated with that Event.
AutoCAD thông báo Reaction có liên quan với Event đó.
* The reaction then notifies your application, known as a callback function, passing to it certain information applicable to the Event in the form of arguments.
Reaction sau đó thông báo cho ứng dụng của bạn, giống như là một hàm gọi lại, đi qua một số thông tin nhất định áp dụng cho các Event dưới hình thức các đối số.
* Your application then does it's thing using the information passed to it frrom the Reactor.
Sau đó ứng dụng của bạn sẽ sử dụng thông tin được truyền qua nó từ Reactor để thực hiện côngviệc của nó.
So, in simple terms, a Reactor is the link between the Event and the callback function.
Vì vậy, nói đơn giản , 1 Reactor là "liên kết" giữa Event và hàm gọi lại.

Before we get into looking at the different types of AutoCAD Reactors and Events, let's have a look at a fairly straightforward Reactor to give you an idea of how simple they can be. Before you start though, you must ask yourself two questions:
Trước khi chúng ta đi vào tìm hiểu các loại Reactors and Events khác nhau của Acad, chúng ta hãy, chúng ta hãy nhìn một Reactor khá đơn giản để biết được Reactor và event đơn giản như thế nào. Trước khi bạn bắt đầu, bạn phải tự hỏi mình hai câu hỏi :
1. Before, after or during what Event do you want your function to occur?
Trước-sau-hoặc trong Event nào, bạn muốn hàm của bạn xảy ra xuất hiện?
2. What do you want your function to do?
Những gì bạn muốn hàm của bạn thực hiện?
For this example, I will answer both these questions on your behalf.
Trong ví dụ này, tôi sẽ trả lời cả cả 2câu hỏi này thay cho bạn .
1. "I want my function to run every time a drawing Save is completed".
Tôi muốn hàm của tôi chạy mỗi khi bản vẽ hoàn thành lệnh Save
2. "I want the function to tell me what the name of the drawing is, and how big the drawing is".
Tôi muốn hàm cho tôi biết tên & dung lượng của bản vẽ
O.K. Looking at the Reactor listings, (Refer to Visual Lisp Reference), under the Editor Reactor Types, I find a reactor called vl-dwg-reactor. This reactor, I am led to believe, responds to the "Save Complete" event of a drawing, and returns callback data of a string containing the actual file name used for the save. Hey, this is just what we are looking for. Let's order two to take-away…
O.K. Nhìn vào danh sách Reactor , (Tham khảo Visual Lisp tham khảo ) , Trong các loại Reactor tôi tìm thấy một Reactor được gọi là vl-dwg-Reactor . Reactor này trả lời sự kiện "Save Complete" của bản vẽ , và trả về dữ liệu callback một string chứa tên file thực tế được sử dụng khi lưu. Hey, đây chỉ là những gì chúng tôi đang tìm kiếm.
vlr-dwg-reactor - Constructs an editor reactor object that notifies of a drawing event.
Xây dựng 1 đối tượng Reactor mà sẽ thông báo 1 sự kiến của bản vẽ
(vlr-dwg-reactor data callbacks)
Arguments : Đối số
data : AutoLisp data to be associated with the reactor object, or nil, if no data
data: Dữ liệu Alisp liên quan đến đối tượng Reator, hoặc là nil nếu khôg có dữ liệu
callbacks : A list of pairs of the following form : List dot pair dạng
Code:
(event-name . callback_function)
(vlr-dwg-reactor data (event-name . callback_function))
wherre event-name is one of the symbols listed in the "DWG reactor events" table, and callback_function is a symbol representing the function to be called when the event fires.
Each callback function will accept two arguments :
Tên sự kiện là một trong những ký tự được liệt kê trong bảng "các sự kiện Reactor DWG " , và callback_function là một ký tự đại diện cho các function được gọi là khi các sự kiện xuất hiện. Mỗi hàm gọi lại sẽ chấp nhận hai đối số:
(defun callback_function (reactor_object list))
reactor_object - the VLR object that called the callback function,
Đối tượng Vlr mà được gọi trong hàm gọi lại
list - a list of extra data elements associated with the particular event.
1 list các thành phần dữ liệu liên quan đến sự kiện đặc biệt
How does it work?
Cách nó làm việc như thế nào?
I don't know about you? But, whew…! Right, let's try and put this into simple English. Let's look at the syntax again : Chúng ta hãy nhìn vào cú pháp một lần nữa : [/color]
(vlr-dwg-reactor data callbacks)
The first part vlr-dwg-reactor is easy. This is the name of the reactor type. This name will be sent to your call back function.
Phần đầu tiên vlr-dwg-reactor thì dễ. Đây chính là tên của loại reactor. Tên này sẽ được gởi đến hàm gọi lại
Tên sự kiện là một trong những ký tự được liệt kê trong bảng "các sự kiện Reactor DWG " , và callback_function là một ký tự đại diện cho các function được gọi là khi các sự kiện xuất hiện. Mỗi hàm gọi lại sẽ chấp nhận hai đối số:
(defun callback_function (reactor_object list))
reactor_object - the VLR object that called the callback function,
Đối tượng Vlr mà được gọi trong hàm gọi lại
list - a list of extra data elements associated with the particular event.
1 list các thành phần dữ liệu liên quan đến sự kiện đặc biệt
How does it work?
Cách nó làm việc như thế nào?
I don't know about you? But, whew…! Right, let's try and put this into simple English. Let's look at the syntax again : Chúng ta hãy nhìn vào cú pháp một lần nữa : [/color]
(vlr-dwg-reactor data callbacks)
The first part vlr-dwg-reactor is easy. This is the name of the reactor type. This name will be sent to your call back function.
Phần đầu tiên vlr-dwg-reactor thì dễ. Đây chính là tên của loại reactor. Tên này sẽ được gởi đến hàm gọi lại
The first argument data, is User Application Data. We usually set this to a reactor name of our choosing. This way we can distinguish between reactors if an Objects has multiple reactors attached.
Đối số đầu tiên data, là Data ứng dụng người dùng. Chúng ta thường đặt cái này chính là tên của 1 reactor. Bằng cách nầy, chúng ta có thể phân biệt giữa các reactor nếu 1 đối tượng có nhiều reactor gắn liền với nó.

The second argument callbacks is a straightforward list of dotted pairs.
Đối số thứ 2 callbạcks là 1 list đơn giản dạng dotted pairs
* The first element of the list is the name of the reactor "event" that will trigger the reactor and then call your callback function.
Phần tử thứ nhất của list là tên của sự kiện reactor mà sẽ kích hoạt reactor và sau đó gọi hàm gọi lại.
* The second element, is the name of your Callback function.
Element thứ 2, là tên của hàm gọi lại của bạn
This is what our reactor function will look like :
(vlr-dwg-reactor "Save Complete" '((:vlr-savecomplete . savedrawinginfo)))
Or, graphically :
Reactor Diagram
Let's have a look at our Reactor Function in action. Copy and Paste this coding into the Visual LISP Editor and save it as SaveDrawingInfo.lsp. Next Load the application, but do not run it.
Chúng ta hãy có một cái nhìn hàm Reactor của chúng ta khi thực hiện. Copy và Paste code vào trình soạn thảo LISP và save as là SaveDrawingInfo.lsp . Tiếp theo load ứng dụng nhưng khoan hãy chạy nó.
Code:
(vl-load-com)   
;**************************************************************   
;setup and intilise the reactor
(vlr-dwg-reactor "Save Complete" '((:vlr-savecomplete . savedrawinginfo)))
 
;**************************************************************
(defun saveDrawingInfo (calling-reactor commandInfo / dwgname filesize
                                         reactType reactData reactCall
                                              reactEvent reactCallback)   
;get the reactor Object
(setq reactInfo calling-reactor
 
      ;get the reactor Type
      reactType (vl-symbol-name (vlr-type reactInfo))
 
      ;get the Application Data
      reactData (vlr-data reactInfo)
 
      ;get the Callback list
      reactCall (car (vlr-reactions reactInfo))
 
      ;extract the Event Reactor
      reactEvent (vl-symbol-name (car reactCall))
 
      ;extract the Callback Function
      reactCallback (vl-symbol-name (cdr reactCall))
 
);setq
 
;get the Drawing Name
(setq dwgname (cadr commandInfo)
 
     ;extract the filesize
     filesize (vl-file-size dwgname)
     
);setq
 
;display the Drawing Name and Size
(alert (strcat "The file size of " dwgname " is "
           
(itoa filesize) " bytes."))
 
;Display the Reactor Information
(alert
 
  (strcat
    "A " "\"" reactType "\"" " named " "\"" reactData "\"" "\n"
    "was triggered by a " "\"" reactEvent "\"" " event call." "\n"
    "Callback Data was passed to the" "\n"
    "\"" reactCallback "\"" " call back function."))
 
(princ)
 
);defun
 
;********************************************************
 
(princ)
 
;********************************************************

Once the application is loaded, return to AutoCAD and save the drawing. This dialog will appear :
Code:

_________________
Học hoài vẫn dốt


Được sửa chữa bởi hochoaivandot ngày 08.12.2011, sửa lần 11
Chuyển lên trên
Xem thông tin cá nhân của thành viên gửi tin nhắn gửi email Website của thành viên này
hochoaivandot
Quản trị
Quản trị


Ngày tham gia: 04/12/2011
Bài gửi: 107
Đến từ: Quảng Nam

Bài gửigửi: 08.12.2011    Tiêu đề: Danh sách sự kiện, loại Reactor. Trả lời với trích dẫn nội dung bài viết này

:VLR-Linker-Reactor
:VLR-rxAppLoaded
:VLR-rxAppUnLoaded
:VLR-Editor-Reactor
:VLR-unknownCommand
:VLR-commandWillStart
:VLR-commandEnded
:VLR-commandCancelled
:VLR-commandFailed
:VLR-lispWillStart
:VLR-lispEnded
:VLR-lispCancelled
:VLR-beginClose
:VLR-beginDxfIn
:VLR-abortDxfIn
:VLR-dxfInComplete
:VLR-beginDxfOut
:VLR-abortDxfOut
:VLR-dxfOutComplete
:VLR-beginDwgOpen
:VLR-endDwgOpen
:VLR-dwgFileOpened
:VLR-databaseConstructed
:VLR-databaseToBeDestroyed
:VLR-beginSave
:VLR-saveComplete
:VLR-sysVarWillChange
:VLR-sysVarChanged
:VLR-AcDb-Reactor
:VLR-objectAppended
:VLR-objectUnAppended
:VLR-objectReAppended
:VLR-objectOpenedForModify
:VLR-objectModified
:VLR-objectErased
:VLR-objectUnErased
:VLR-DocManager-Reactor
:VLR-documentCreated
:VLR-documentToBeDestroyed
:VLR-documentLockModeWillChange
:VLR-documentLockModeChangeVetoed
:VLR-documentLockModeChanged
:VLR-documentBecameCurrent
:VLR-documentToBeActivated
:VLR-documentToBeDeactivated
:VLR-Command-Reactor
:VLR-unknownCommand
:VLR-commandWillStart
:VLR-commandEnded
:VLR-commandCancelled
:VLR-commandFailed
:VLR-Lisp-Reactor
:VLR-lispWillStart
:VLR-lispEnded
:VLR-lispCancelled
:VLR-DXF-Reactor
:VLR-beginDxfIn
:VLR-abortDxfIn
:VLR-dxfInComplete
:VLR-beginDxfOut
:VLR-abortDxfOut
:VLR-dxfOutComplete

:VLR-DWG-Reactor
:VLR-beginDwgOpen
:VLR-endDwgOpen
:VLR-dwgFileOpened
:VLR-databaseConstructed
:VLR-databaseToBeDestroyed
:VLR-beginSave
:VLR-saveComplete
:VLR-beginClose
:VLR-Wblock-Reactor
:VLR-wblockNotice
:VLR-beginWblockPt
:VLR-beginWblockId
:VLR-beginWblock
:VLR-otherWblock
:VLR-abortWblock
:VLR-endWblock
:VLR-beginWblockObjects
:VLR-SysVar-Reactor
:VLR-sysVarWillChange
:VLR-sysVarChanged
:VLR-DeepClone-Reactor
:VLR-beginDeepClone
:VLR-beginDeepCloneXlation
:VLR-abortDeepClone
:VLR-endDeepClone
:VLR-XREF-Reactor
:VLR-beginAttach
:VLR-otherAttach
:VLR-abortAttach
:VLR-endAttach
:VLR-redirected
:VLR-comandeered
:VLR-beginRestore
:VLR-abortRestore
:VLR-endRestore
:VLR-xrefSubcommandBindItem
:VLR-xrefSubcommandAttachItem
:VLR-xrefSubcommandOverlayItem
:VLR-xrefSubcommandDetachItem
:VLR-xrefSubcommandPathItem
:VLR-xrefSubcommandReloadItem
:VLR-xrefSubcommandUnloadItem
:VLR-Undo-Reactor
:VLR-undoSubcommandAuto
:VLR-undoSubcommandControl
:VLR-undoSubcommandBegin
:VLR-undoSubcommandEnd
:VLR-undoSubcommandMark
:VLR-undoSubcommandBack
:VLR-undoSubcommandNumber
:VLR-Window-Reactor
:VLR-docFrameMovedOrResized
:VLR-mainFrameMovedOrResized
:VLR-Toolbar-Reactor

:VLR-toolbarBitmapSizeWillChange
:VLR-toolbarBitmapSizeChanged
:VLR-Mouse-Reactor
:VLR-beginDoubleClick
:VLR-beginRightClick
:VLR-Miscellaneous-Reactor
:VLR-pickfirstModified
:VLR-layoutSwitched
:VLR-Object-Reactor
:VLR-cancelled
:VLR-copied
:VLR-erased
:VLR-unerased
:VLR-goodbye
:VLR-openedForModify
:VLR-modified
:VLR-subObjModified
:VLR-modifyUndone
:VLR-modifiedXData
:VLR-unappended
:VLR-reappended
:VLR-objectClosed
:VLR-Inserrt-Reactor
:VLR-beginInserrt
:VLR-beginInserrtM
:VLR-otherInserrt
:VLR-abortInserrt
:VLR-endInserrt
_________________
Học hoài vẫn dốt


Được sửa chữa bởi hochoaivandot ngày 08.12.2011, sửa lần 1
Chuyển lên trên
Xem thông tin cá nhân của thành viên gửi tin nhắn gửi email Website của thành viên này
hochoaivandot
Quản trị
Quản trị


Ngày tham gia: 04/12/2011
Bài gửi: 107
Đến từ: Quảng Nam

Bài gửigửi: 08.12.2011    Tiêu đề: Phần 2-To be continued,,, Trả lời với trích dẫn nội dung bài viết này

Another Editor Type of reactor is the VLR-Command-Reactor. This reactor notifies us of a Command Event. In this instance we will make use of the vlr-commandEnded reactor event which returns a event parameter list containing a string identifying the command that has been cancelled. Every time a drawing is plotted, our call back function will first, save the drawing, and secondly save the drawing to a backup directory, namely C:/Backup. Let's have a look at the coding :
Một loại Reactor # là VLR-Command-Reactor. Reactor này thông báo chúng ta sự kiện về Command. Trong ví dụ này, chúng tôi sẽ sử dụng sự kiện reactor vlr-commandEnded, nó trả lại 1 list các thông số sự kiện chứa 1 string xác định command đã cancel. Mỗi lần bản vẽ in, đầu tiên hàm gọi lại của chúng ta sẽ lưu bản vẽ và sau đó sẽ lưu bản vẽ đến thư mục backup có tên là C:/Backup. Xem code dưới đây
Code:

(prompt " \nLoad Only....Do NOT Run...")
(vl-load-com)
;*******************************************************
(vlr-command-reactor 
   "Backup After Plot" '((:vlr-commandEnded . endPlot)))
;*******************************************************
(defun endPlot (calling-reactor endcommandInfo /
         thecommandend drgName newname)
   (setq thecommandend (nth 0 endcommandInfo))
   (if (= thecommandend "PLOT")
   (progn
   (setq acadDocument (vla-get-activedocument
             (vlax-get-acad-object)))
   (setq drgName (vla-get-name acadDocument))
   (setq newname (strcat "c:\\backup\\" drgName))
   (vla-save acadDocument)
   (vla-saveas acadDocument newname)
   );progn
   );if 
(princ) 
);defun
;*********************************************************
(princ)

A word of warning! Did you notice how I used ActiveX statements and functions to "Save" and "SaveAs". You cannot use interactive functions frrom within a reactor as AutoCAD may still be processing a command at the time the event is triggered. Therefore, avoid the use of input-acquisition methods such as getPoint, ensel, and getkword, as well as "sellection set" operations and the command function.
Bạn có để ý tôi đã sử dụng các hàm ActiveX để Save và Save as không? Bạn không thể sử dụng các hàm tương tác với nhau trong 1 reactor bởi vì Autocad có thể vẫn còn đang thực hiện 1 command tại thời điểm sự kiện xuất hiện. Vì thế, bạn hãy tránh sử dụng các method nhập như (getPoint) (entsel) (getkword) (ssget) và (command).
Here's another interesting command event reactor :
Dưới đây là 1 reactor sự kiện command thú vị khác
When a user adds Text or Hatch to the drawing, the layer will automatically change to Layer "4" or Layer "6" respectively. When the command is completed, or cancelled, the user is returned to the original Layer he was on before he started the command. Save the file as LayMan.lsp, BUT please remember, that as this routine contains reactors, you must only Load it and NOT Run it. If you want to use this routine on a permanent basis, you'll have to ensure that it is loaded at startup. There is also no checking to ensure that the layers exist, are frozen, switched off, etc. and no other form of error checking. I've got to leave something for you to do!
Khi user thêm text và hatch vào bản vẽ, layer sẽ tự động thay đổi lần lượt thành layer 4 hay layer 6. Khi command hoàn thành hoặc cancel, user sẽ được trả lại layer gốc mà user sử dụng trước khi dùng lệnh command. Lưu file tên LayMan.lsp, nhưng hãy nhớ lisp này chứa các reactor, bạn chỉ được load và khoan hãy run. Nếu bạn muốn sử dụng lisp này mãi mãi, bạn phải chắc chắn rằng nó đã được load vào startup. Không thể kiểm tra chắc chắn layer có tồn tại không, co frozen, có off….
Code:
(prompt " \nLoad Only....Do NOT Run...")
(vl-load-com)
;****************************************
(vlr-command-reactor
   nil '((:vlr-commandWillStart . startCommand)))
(vlr-command-reactor
   nil '((:vlr-commandEnded . endCommand)))
(vlr-command-reactor
   nil '((:vlr-commandCancelled . cancelCommand)))
;******************************************************
(defun startCommand (calling-reactor startcommandInfo /
           thecommandstart)
(setq OldLayer (getvar "CLAYER"))
(setq thecommandstart (nth 0 startcommandInfo))
(cond
  ((= thecommandstart "TEXT") (setvar "CLAYER" "4"))
  ((= thecommandstart "MTEXT") (setvar "CLAYER" "4"))
  ((= thecommandstart "DTEXT") (setvar "CLAYER" "4"))
  ((= thecommandstart "HATCH") (setvar "CLAYER" "6"))
  ((= thecommandstart "BHATCH") (setvar "CLAYER" "6"))
);cond
(princ)
);defun
;****************************************************
(defun endCommand (calling-reactor endcommandInfo /
         thecommandend)
(setq thecommandend (nth 0 endcommandInfo))
(cond
  ((= thecommandend "TEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandend "MTEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandend "DTEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandend "HATCH") (setvar "CLAYER" OldLayer))
  ((= thecommandend "BHATCH") (setvar "CLAYER" OldLayer))
);cond
 (princ)
);defun
;********************************************************
(defun cancelCommand (calling-reactor cancelcommandInfo /
            thecommandcancel)
(setq thecommandcancel (nth 0 cancelcommandInfo))
(cond
  ((= thecommandcancel "TEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandcancel "MTEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandcancel "DTEXT") (setvar "CLAYER" OldLayer))
  ((= thecommandcancel "HATCH") (setvar "CLAYER" OldLayer))
  ((= thecommandcancel "BHATCH") (setvar "CLAYER" OldLayer))
);cond
(princ)
);defun
;*********************************************************
(princ)

Did you notice that this application used three command reactors with three different command events. We could have incorporated all three reactor events and call back functions under one command reactor type, but I prefer to leave them separate for clarity and ease of debugging.
Bạn có để ý ứng dụng này sử dụng những 3 reactor với 3 sự kiện command khác nhau không? Chúng ta hoàn toàn có thể gộp cả 3 sự kiện reactor và các hàm gọi lại vào 1 loại reactor command, nhưng tôi thích để chúng riêng lẻ hơn cho rõ ràng và dễ sửa lỗi
_________________
Học hoài vẫn dốt


Được sửa chữa bởi hochoaivandot ngày 08.12.2011, sửa lần 2
Chuyển lên trên
Xem thông tin cá nhân của thành viên gửi tin nhắn gửi email Website của thành viên này
hochoaivandot
Quản trị
Quản trị


Ngày tham gia: 04/12/2011
Bài gửi: 107
Đến từ: Quảng Nam

Bài gửigửi: 08.12.2011    Tiêu đề: Phần 3 (the end) Trả lời với trích dẫn nội dung bài viết này

Object Reactors, or VLR-Object-Reactor, fall under general reactor types. They are almost identical in functionality to Drawing and Command reactors except for a couple of things! They need to include a reference to the Object that will be reacted upon, (Crikey, that sounds terrible!) and the reference to the Object needs to be created before the reactor is called. Let's have a look at the syntax of an Object reactor :
Object Reactors, hay VLR-Object-Reactor, được liệt là những loại reactor chính. Chúng hầu như giống hệt về chức năng đối với Acad và các rectors command ngoại trừ 2 điều! Chúng cần phải bao gồm 1 tham chiếu đến đối tượng mà sẽ được tương tác. Và tham chiếu đến đối tượng cần được tạo trước khi reactor được gọi. CHúng ta hãy nhìn cấu trúc 1 reactor Object
Code:
(vlr-object-reactor owners data callback)

The data and callback arguments, we are familiar with. But what is the owners argument? This is an AutoLISP list of Visual Lisp Objects identifying the drawing Objects to be watched. In other words, a reference to the Object that contains the reactor.
Data vf đối số callback thì tương tự. Nhưng đối số owners là gì? Đây là 1 list Alisp các đối tượng Vlisp mà nhận ra các đối tượng Acad. Hay nói cách khác. 1 tham chiếu đến các Object chứa các reactor
The reactor event we are going to use is the :vlr-modified event, and our Callback function will be named print-length. Have a look at the coding for our reactor :
Một sự kiện reactor chúng ta sẽ sử dụng là :vlr-modified event, và hàm callback Sẽ có tên là print-length. Hãy nhìn bên dưới
Code:
(vlr-object-reactor (list myLine) "Line Reactor"
      '((:vlr-modified . print-length)))

As I mentioned earlier though, we need to have a reference to the Object before we can call this statement. Consider the following :
Code:
(vl-load-com)
;*************
(defun line-draw ()
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq mspace (vla-get-modelspace acadDocument))
(setq apt (getpoint "Specify First Point: "))
(setq pt (getpoint "Specify next point: " apt))
(setq myLine (vla-addline mspace (vlax-3d-point apt)(vlax-3d-point pt)))
(setq lineReactor (vlr-object-reactor (list myLine)
"Line Reactor" '((:vlr-modified . print-length))))
(princ)
);defun

We started off by drawing a line. As the line was created frrom scratch, and created using Visual LISP functions, we already have a reference to the line Object (myLine). We can now safely run our reactor function and attach it to our Line. "But wherre is the Callback function?"
Chúng ta khởi động cad, vẽ 1 line. Khi 1 line được tạo từ scratch, và được tạo bằng cách sử dụng hàm Vlisp, chúng ta phải tham chiếu 1 đối tượng Line (myline). CHúng ta có thể chạy những hàm reactor và gắn chúng với Line của chúng ta. Nhưng hàm call back ở đâu?
Hah, I was waiting for that. We've made the Callback function a separate function for one main reason. If we didn't, every time we ran the application it would prompt us to draw a new line. So, what we have to do now, is link the reactor function to our Callback function so that when our line is modified, only the Callback function is put into motion. The reactor sends three arguments to the Callback function, the notifier-object (our line), the reactor-object (:vlr-modified), and the event parameter-list which in this case is nil.
Tôi đang đợi điều đó. Chúng ta sẽ tạo 1 hàm callback, 1 hàm tách vì 1 nguyên nhân chính. Nếu chúng ta không tách, mỗi lần chúng ta chạy ứng dụng nó sẽ thông báo cho chúng ta vẽ 1 line mới. Vì thế, những gì chúng ta phải làm bây giờ,là liên kết hàm reactor với hàm callback của chúng ta để khi line của chúng ta modify, chỉ hàm callbạck được đặt vào hoạt động này. Reactor gởi 3 đối số đến hàm callback, đối tượng thông báo (line của chúng ta), đối tượng reactor (:vlr-modified), và list đối số sự kiện mà trong trường hợp này là nil
Here's the coding for the Callback function :
Code:
(defun print-length (notifier-object reactor-object parameter-list)
(cond
   ((vlax-property-available-p notifier-object "Length")
    (alert (strcat "The length is now "
                        (rtos (vla-get-length notifier-object)))))
);cond
(princ)
);defun
(princ)

Copy all of this coding into one file and save it as Line-Draw.lsp. Now load Line-Draw.lsp and then run the line-draw function. Draw a single line when prompted. Now stretch the line so that it's length changes. A dialog will appear displaying the new length of the line :
Copy tấc cả những code này vào trong 1 file và save nó với tên Line-Draw.lsp. Bây giờ chúng ta load file trên và sau đó chạy hàm line-draw. Vẽ 1 đường line khi được yêu cầu. bây giờ stretch line đó để thay đổi chiều dài của nó. 1 hộp thoại sẽ xuất hiện thể hiện chiều dài mới của line

In essence, this is what happened : Về bản chất, những việc này đã diẽn ra
1. We loaded Line-Draw.lsp and all functions contained within were placed into memory.
Chúng ta load lsp Line-Draw và tấc cả những hàm trong đó sẽ được ghi vào bộ nhớ
2. We ran the line-draw function which prompted us to draw a line. The reactor was then loaded and linked to both the line Object and the Callback function.
Chúng ta chạy hàm line-draw sẽ báo chúng ta sẽ 1 line. Sau đó 1 reactor được load và được liên kết với cả đối tượng line và hàm gọi lại
3. As the Callback function print-length was also loaded into memory, every time we modify the line Object, the Callback function is processed and the length of the line is displayed.
Khi hàm gọi lại print-length cũng được load vào trong bộ nhớ, mỗi lần chúng ta modify đối tượng line, hàm gọi lại sẽ hoạt động vf chiều dài line được thể hiện
Did you notice how we checked that our Object had a Length Property before continuing? Good idea, as this validation can save lot's of problems.
Bạn có chú ý chúng ta đã kiểm tra đối tượng của chúng ta có property chiều dài trước hay không khi tiếp tục? Ý kiến hay, vì sự kiểm tra này có thể giải quyết được nhiều vấn đề[color]
"But what happens when I close my drawing? Will I lose all my reactors?" Good questions. Reactors can be transient or persistent. Transient reactors are lost when the drawing closes and this is the default reactor mode. Persistent reactors are saved with the drawing and exist when the drawing is next open.
[color=blue]Nhưng chuyện j sẽ xảy ra khi tôi đóng bản vẽ. Tôi sẽ mất tấc cả các reactor? Câu hỏi hay.Reactor có thể ngắn hạn hay liên tục (dài hạn). Reactor ngắn hạn sẽ mất khi bản vẽ đóng và đây là mode reactor mặc định. Reactor dài hạn được lưu với bản vẽ và tồn tại khi bản vẽ mở ra lại

You can use the vlr-pers function to make a reaction persistent. To remove a persistence frrom a reactor and make it transient, use the vlr-pers-release function. To determine whether a reactor is persistent or transient, use the vlr-pers-p function. Each function takes the reactor Object as it's only argument :
Bạn có thể sử dụng hàm vlr-pers để tạo 1 reactor dài hạn. Để loại bỏ reactor này khỏi bản vẽ và biến nó thành ngắn hạn, sử dụng hàm vlr-pers-release. Để xác định liệu 1 reactor là ngắn hạn hay dài hạn, bạn dùng hàm vlr-pers. Mỗi hàm yêu cầu đối tượng reactor như đối số duy nhất.
_
Code:
$(vlr-pers lineReactor)
#<VLR-Object-Reactor>

If successful vlr-pers returns the specified reactor Object.
Note : A reactor is only a link between an event and a Callback function. The Callback function is not part of the reactor, and is normally not part of the drawing. The reactors saved in the drawing are only usable if their associated Callback functions are loaded in AutoCAD. In other words, if we made our reactor lineReactor persistent, we would have to ensure that the Callback function print-length was loaded every time the drawing containing our lines with reactors was opened.
Ghi chú: Reactor chỉ là 1 liên kết giữa 1 sự kiện và 1 hàm gọi lại. Hàm gọi lại không phải là 1 phần của reactor mà thông thường là 1 phần của bản vẽ. Reactor được lưu trong bản vẽ chỉ sử dụng được khi hàm gọi lại của chúng được load vào Acad. Nói cách khác, nếu chúng ta tạo ra 1 reactor lineReactor dài hạn, chúng ta sẽ chắc chắn rằng hàm gọi lại print-length đã được load mỗi khi bản vẽ chứa lines với reactors được mở
_________________
Học hoài vẫn dốt
Chuyển lên trên
Xem thông tin cá nhân của thành viên gửi tin nhắn gửi email Website của thành viên này
Trình bày bài viết theo thời gian:   
gửi bài mới   Trả lời chủ đề này    Cadonline Forum -> Lisp nâng cao Thời gian được tính theo giờ [GMT+7giờ]
Trang 1 trong tổng số 1 trang

 
Chuyển đến 
Bạn không có quyền gửi bài viết
Bạn không có quyền trả lời bài viết
Bạn không có quyền sửa chữa bài viết của bạn
Bạn không có quyền xóa bài viết của bạn
Bạn không có quyền tham gia bầu chọn

Trang chủ    ¤   Diễn đàn    ¤   Liên hệ    ¤   Sơ đồ site    ¤   Tìm kiếm    ¤   Thăm dò dư luận    ¤   Thư viện Download
© Copyright 2011-2012 AutoCAD Online
Người phát triễn : Dương Bá Diệp Email : badiep@duyxuyen.vn
Website : http://cadonline.duyxuyen.vn
Diễn đàn BBtoNuke 2.0.21 © 2006 Nuke Cops
Được phát triển từ phpBB 2.0.21 © 2001-2006 phpBB Group