NP CAD Page | Articles | Russian | Download
Toolbar Dances
(from the book "AutoLISP and Visual LISP Inside AutoCAD")
Toolbars life is similar to the life of menus (e.g. popup menus) but it is rather difficult
to work with them from LISP programs because ActiveX tools are to be used.
This article shows an example of solving in AutoCAD 2006/2007 the following tasks:
Listing 1. File cr_tb.lsp
The cr_tb function has the following arguments:
First of all cr_tb checks name validity for menu group to insert toolbar. Group name is transferred
through the first argument. Program checks its type (string) and then checks menu group existence
(it should be a member of MenuGroups collection). While verifying an important vl-catch-all-apply function
is used. If error break arises (there is no group) then the function returns not group VLA object but
object with VL-CATCH-ALL-APPLY-ERROR type.
The following construction is applied for checking:
After evaluating this expression we can analyze result type without exiting program.
Similar check is done for t_name argument that is to be a string. But here vl-catch-all-apply
function is used in opposite manner: if there is no error then Toolbars collection already has
a toolbar named t_name and we must exclude this!
When group and toolbar names are suitable, program goes to toolbar creation. First of all Add method
generates VLA object of an empty toolbar:
Then with the help of AddToolbarButton method we add buttons. Number of buttons is equal to l_names list
length (it should be mentioned that to shorten program source code we missed check for length of l_helps,
l_macros and l_icons lists; if thes lists are shorted then program wil not work; moreover you should
test that all the list members are strings).
Next method SetBitmaps is used to attach pictures to buttons. In this example we use icons of the
Standard toolbar. Equal names are taken for small and large buttons (system admits this).
As a result a toolbar must appear on the screen (pic.1 shows Dancing Queen toolbar with five buttons).
Pic.1. Dancing Queen toolbar
VLA object of the built toolbar is saved in a global variable g_tb to use in other functions.
Then we delete the rest VLA objects with vlax-release-object function and free their memory.
Next move_tb function must move the toolbar from one screen point to another in required number of steps
(too quick movement is unintersting). Function source code is in listing 2.
Listing 2. File move_tb.lsp
; Appendix 5\move_tb.lsp
; N.Poleshchuk, 2006
; Based on the book: N.Poleshchuk, P.Loskutov
;"AutoLISP and Visual LISP Inside AutoCAD"
; ("BHV-Petersburg" Publishing House, 2006)
; http://poleshchuk.spb.ru/cad/eng.html
(defun move_tb (tb x1 y1 x2 y2 ntime / i x y)
(vla-Float tb x1 y1 1)
(setq i 0 x x1 y y1)
(repeat ntime
);repeat
(vlr-beep-reaction)
);defun move_tb
The move_tb function converts toolbar into one row of buttons and recalculates toolbar screen position.
The ntime argument controls the time for toolbar moving between edge points. At the end of the path
beep sound is generated by vlr-beep-reaction function. As background window does not restore in time
we see toolbar track.
Next function is called dance_tb (listing 3) pushes toolbar along the path defined by the list of points.
Listing 3. File dance_tb.lsp
; Appendix 5\dance_tb.lsp
; N.Poleshchuk, 2006
; Based on the book: N.Poleshchuk, P.Loskutov
;"AutoLISP and Visual LISP Inside AutoCAD"
; ("BHV-Petersburg" Publishing House, 2006)
; http://poleshchuk.spb.ru/cad/eng.html
(defun dance_tb (tb plist ntime / wbOK p x0 y0 x1 y1)
(if (and (listp plist)(> (vl-list-length plist) 0))
);if
);defun dance_tb
; Checking whether list has 2 elements and they both are numbers (numberp)
; Additional function
(defun test (p / wbOK)
);defun test (returns T only for valid list)
Function dance_tb checks list named plist first and deletes improper elements
(that are not 2D points). Afterwards move_tb function is called many times for movement.
To finish we will write a sample program that runs the whole dancing from begin to end (listing 4).
Listing 4. File show.lsp
; Appendix 5\show.lsp
; New toolbar parameters
(setq gn "ACAD" tn "Dancing Queen")
(setq ln (list "Dance1" "Dance2" "Dance3" "Dance4" "Dance5"))
(setq lh (list "Tooltip 1" "Tooltip 2" "Tooltip 3" "Tooltip 4" "Tooltip 5"))
(setq lm (list "(alert\"New\") " "(alert\"Open\") " "(alert\"Save\") " "(alert\"SaveAs\") " "(alert\"Print\") "))
(setq li (list "RCDATA_16_NEW" "RCDATA_16_OPEN" "RCDATA_16_SAVE" "RCDATA_16_SAVE" "RCDATA_16_PRINT"))
(setq k1 200 k2 150 kr 1)
(cr_tb gn tn ln lh lm li k1 k2 kr)
;;; Sample dancing
(dance_tb g_tb (list '(100 100) '(500 200) '(0 400)) 10000)
;;; Delete dancer
(vla-Delete g_tb)
(vlax-release-object g_tb)
(setq g_tb nil)
(princ "\nToolbar is removed. Concert is over. ")
(princ)
Pic.2. Dancing toolbar
One may leave new toolbar unremoved: it disappears itself after exiting AutoCAD session.
Toolbar exists only in computer memory (we can say, in our imagination...). But it works fine.
If user permanently needs created toolbars he is recommended not to create them dynamically but
to load them with CUI file in 2006/2007 or with menu file in previous versions.
NP CAD Page | Articles | Russian | Download