Jump to content

Gọi Tool Bar trong CUI vừa Add

Dạ nó như này anh ạ.

36665_55_2.png

 

Em tham khảo 2 đoạn code này, anh copy nhặt được trên mạng, vì quá bận nên chưa nghiên cứu nhiều ^ _ ^

 

(vl-load-com)
(defun c:test()
    (setq acadObj (vlax-get-acad-object))
    (setq currMenuGroup (vla-Item (vla-get-MenuGroups acadObj) "ACAD"))
 
    ;; Create the new toolbar
    (setq newToolBar (vla-Add (vla-get-Toolbars currMenuGroup) "HA1"))
 
    ;; Assign the macro string the VB equivalent of "ESC ESC _open "
    (setq openMacro (strcat (Chr 3) (Chr 3) (Chr 95) "open" (Chr 32)))
    (setq newButton1 (vla-AddToolbarButton newToolBar "" "NewButton1" "Open a file." openMacro))
 
    ;; Display the toolbar
    (vla-put-Visible newToolBar :vlax-true)
 
    (vla-put-CommandDisplayName newButton1 "Open a File")
 
    ;; Read the current value of the Help string
    (alert (strcat "The current CommandDisplayName for the toolbar button is: " (vla-get-CommandDisplayName newButton1)))
 
    ;; Change the value of the CommandDisplayName
    (vla-put-CommandDisplayName newButton1 "Open File")
 
    ;; Read the new value of the CommandDisplayName
    (alert (strcat "The new CommandDisplayName for the toolbar button is: " (vla-get-CommandDisplayName newButton1)))
)

Và

(defun C:VBATOOLBARMENU (/ fn acadobj thisdoc menus flag currMenuGroup
newToolbar newToolbarButton openMacro
SmallBitmapName LargeBitmapName)
 
(vl-load-com)
 
;;; CreateToolbar is called if the Toolbar in question doesn't exist
  (defun createToolbar ()
    (setq newToolbar (vla-add (vla-get-toolbars currMenuGroup) "VBA Menu"))
    ;;------------------------------------------------------------------
    ;; create the first Toolbar Button, VbaLoad
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaload" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Load" "VBA Load" openMacro
           )
    )
    (setq SmallBitmapName "VbaLoad.bmp")
    (setq LargeBitmapName "VbaLoad.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Load a VBA Application")
    ;;------------------------------------------------------------------
    ;; create the second Toolbar Button, Vbaide
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaide" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Editor" "VBA Editor" openMacro
           )
    )
    (setq SmallBitmapName "Vbaide.bmp")
    (setq LargeBitmapName "Vbaide.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Switch to the VBA Editor")
    ;;------------------------------------------------------------------
    ;; create the third Toolbar Button, Vbarun
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbarun" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Macro" "VBA Macro" openMacro
           )
    )
    (setq SmallBitmapName "Vbamacro.bmp")
    (setq LargeBitmapName "Vbamacro.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Run a VBA Macro")
    ;;------------------------------------------------------------------
    ;; create the fourth Toolbar Button, Vbaman
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaman" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Manager" "VBA Manager" openMacro
           )
    )
    (setq SmallBitmapName "Vbaman.bmp")
    (setq LargeBitmapName "Vbaman.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Display the VBA Manager")
    ;;------------------------------------------------------------------
 
    ;; re-compile the VBATOOLBARMENU menu - VBATOOLBARMENU.MNC
    (vla-save currMenuGroup acMenuFileCompiled)
    ;; save it as a MNS file
    (vla-save currMenuGroup acMenuFileSource)
  )
 
  (setq flag nil)
  (if (not (findfile "VbaToolbarMenu.mns"))
    (progn
      (setq fn (open "VbaToolbarMenu.mns" "w"))
      (close fn)
    )
  )
  ;; get hold of the application object
  ;; we'll use it to reference the menuGroups collection
  (setq acadobj (vlax-get-acad-object))
  ;; .. and get the active document
  (setq thisdoc (vla-get-activeDocument acadobj))
  ;; get all menu groups loaded into AutoCAD
  (setq menus (vla-get-menuGroups acadobj))
  (princ "\nLoaded menus: ")
  (vlax-for n menus
    (if (= (vla-get-name n) "VbaToolbarMenu")
      (setq flag T)
    )
    (terpri)
    (princ (vla-get-name n))
  )
  ;; if VbaToolbarMenu wasn't among the loaded menus then load it
  (if (null flag)
    (vla-load menus "VbaToolbarMenu.mns")
  )
  (setq currMenuGroup (vla-item menus "VbaToolbarMenu"))
  ;; if no Toolbars exist in VbaToolbarMenu then go create one
  ;; otherwise exit with grace
  (if (<= (vla-get-count (vla-get-menus currMenuGroup)) 0)
    (createToolbar)
    (princ "\nThe Vba Toolbar Menu is already loaded")
  )
  (princ)
)
 
(princ)
(vl-load-com)
(defun c:test()
    (setq acadObj (vlax-get-acad-object))
    (setq currMenuGroup (vla-Item (vla-get-MenuGroups acadObj) "ACAD"))
 
    ;; Create the new toolbar
    (setq newToolBar (vla-Add (vla-get-Toolbars currMenuGroup) "HA1"))
 
    ;; Assign the macro string the VB equivalent of "ESC ESC _open "
    (setq openMacro (strcat (Chr 3) (Chr 3) (Chr 95) "open" (Chr 32)))
    (setq newButton1 (vla-AddToolbarButton newToolBar "" "NewButton1" "Open a file." openMacro))
 
    ;; Display the toolbar
    (vla-put-Visible newToolBar :vlax-true)
 
    (vla-put-CommandDisplayName newButton1 "Open a File")
 
    ;; Read the current value of the Help string
    (alert (strcat "The current CommandDisplayName for the toolbar button is: " (vla-get-CommandDisplayName newButton1)))
 
    ;; Change the value of the CommandDisplayName
    (vla-put-CommandDisplayName newButton1 "Open File")
 
    ;; Read the new value of the CommandDisplayName
    (alert (strcat "The new CommandDisplayName for the toolbar button is: " (vla-get-CommandDisplayName newButton1)))
)

Cách dùng

  1. Tải tệp 304019_vbatoolbarmenu.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh VBATOOLBARMENU để chạy lisp.

Mã nguồn 304019_vbatoolbarmenu.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/106822-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-g%E1%BB%8Di-tool-bar-trong-cui-v%E1%BB%ABa-add/
(defun C:VBATOOLBARMENU (/ fn acadobj thisdoc menus flag currMenuGroup
newToolbar newToolbarButton openMacro
SmallBitmapName LargeBitmapName)
 
(vl-load-com)
 
;;; CreateToolbar is called if the Toolbar in question doesn't exist
  (defun createToolbar ()
    (setq newToolbar (vla-add (vla-get-toolbars currMenuGroup) "VBA Menu"))
    ;;------------------------------------------------------------------
    ;; create the first Toolbar Button, VbaLoad
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaload" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Load" "VBA Load" openMacro
           )
    )
    (setq SmallBitmapName "VbaLoad.bmp")
    (setq LargeBitmapName "VbaLoad.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Load a VBA Application")
    ;;------------------------------------------------------------------
    ;; create the second Toolbar Button, Vbaide
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaide" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Editor" "VBA Editor" openMacro
           )
    )
    (setq SmallBitmapName "Vbaide.bmp")
    (setq LargeBitmapName "Vbaide.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Switch to the VBA Editor")
    ;;------------------------------------------------------------------
    ;; create the third Toolbar Button, Vbarun
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbarun" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Macro" "VBA Macro" openMacro
           )
    )
    (setq SmallBitmapName "Vbamacro.bmp")
    (setq LargeBitmapName "Vbamacro.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Run a VBA Macro")
    ;;------------------------------------------------------------------
    ;; create the fourth Toolbar Button, Vbaman
    (setq openMacro (strcat (chr 3) (chr 3) (chr 95) "vbaman" (chr 32)))
    (setq newToolbarButton (vla-addToolbarButton
             newToolbar
             (1+ (vla-get-count newToolbar))
             "VBA Manager" "VBA Manager" openMacro
           )
    )
    (setq SmallBitmapName "Vbaman.bmp")
    (setq LargeBitmapName "Vbaman.bmp")   
    (vla-setBitmaps newToolbarButton SmallBitmapName LargeBitmapName)
 
    (vla-put-helpString newToolbarButton "Display the VBA Manager")
    ;;------------------------------------------------------------------
 
    ;; re-compile the VBATOOLBARMENU menu - VBATOOLBARMENU.MNC
    (vla-save currMenuGroup acMenuFileCompiled)
    ;; save it as a MNS file
    (vla-save currMenuGroup acMenuFileSource)
  )
 
  (setq flag nil)
  (if (not (findfile "VbaToolbarMenu.mns"))
    (progn
      (setq fn (open "VbaToolbarMenu.mns" "w"))
      (close fn)
    )
  )
  ;; get hold of the application object
  ;; we'll use it to reference the menuGroups collection
  (setq acadobj (vlax-get-acad-object))
  ;; .. and get the active document
  (setq thisdoc (vla-get-activeDocument acadobj))
  ;; get all menu groups loaded into AutoCAD
  (setq menus (vla-get-menuGroups acadobj))
  (princ "\nLoaded menus: ")
  (vlax-for n menus
    (if (= (vla-get-name n) "VbaToolbarMenu")
      (setq flag T)
    )
    (terpri)
    (princ (vla-get-name n))
  )
  ;; if VbaToolbarMenu wasn't among the loaded menus then load it
  (if (null flag)
    (vla-load menus "VbaToolbarMenu.mns")
  )
  (setq currMenuGroup (vla-item menus "VbaToolbarMenu"))
  ;; if no Toolbars exist in VbaToolbarMenu then go create one
  ;; otherwise exit with grace
  (if (<= (vla-get-count (vla-get-menus currMenuGroup)) 0)
    (createToolbar)
    (princ "\nThe Vba Toolbar Menu is already loaded")
  )
  (princ)
)
 
(princ)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Nhờ viết LISP] Gọi Tool Bar trong CUI vừa Add. Xem đúng bài viết #304019 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 211)