AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Visual Lisp and VBA - Page II

In this next exercise, we're going to design a drawing tile block application. The program will be written in Visual Lisp, but we will call a VBA program and dialog to display and return our title block data.

OK, open up your VBA Editor and start a new Project. Insert a UserForm and add 5 TextBoxes, 5 Labels and two command buttons. Retain their default names. Your dialog should look like this :

Now enter the following coding :

Option Explicit
'-----------------------------------------------------
Private Sub CommandButton1_Click()

'set the user system variable
ThisDrawing.SetVariable "UserI1", 1

'retrieve the text box values and store
'them in the user system variables
ThisDrawing.SetVariable "UserS1", TextBox1.Text
ThisDrawing.SetVariable "UserS2", TextBox2.Text
ThisDrawing.SetVariable "UserS3", TextBox3.Text
ThisDrawing.SetVariable "UserS4", TextBox4.Text
ThisDrawing.SetVariable "UserS5", TextBox5.Text
End

End Sub

'----------------------------------------------------------
Private Sub CommandButton2_Click()

'set the user system variable
ThisDrawing.SetVariable "UserI1", 0
End

End Sub

'------------------------------------------------------------
Private Sub UserForm_Initialize()

'retrieve user system variables and populate
'the text boxes
TextBox1.Text = ThisDrawing.GetVariable("UserS1")
TextBox2.Text = ThisDrawing.GetVariable("UserS2")
TextBox3.Text = ThisDrawing.GetVariable("UserS3")
TextBox4.Text = ThisDrawing.GetVariable("UserS4")
TextBox5.Text = ThisDrawing.GetVariable("UserS5")

'set the focus to the first text box
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(UserForm1.TextBox1.Text)

End Sub

Save this Project as "AL-VBA.DVB".

Now copy and paste this into Notepad and save it as "VL-VBA.LSP" :

;CODING STARTS HERE
;
;All Tutorials and Code are provided "as-is" for purposes of instruction and
;utility and may be used by anyone for any purpose entirely at their own risk.
;Please respect the intellectual rights of others.
;All material provided here is unsupported and without warranty of any kind.
;No responsibility will be taken for any direct or indirect consequences
;resulting from or associated with the use of these Tutorials or Code.
;*******************************************************************************
;			AfraLisp
;		http://www.afralisp.com
;	 	 afralisp@afralisp.com
;	   	 afralisp@mweb.com.na
;*******************************************************************************
;This application will extract attributes from a block and display them in a
;VBA dialog box. The attributes will then be updated.
;
;Dependencies : VL-VBA.DVB and VL-VBA.DWG are
;required and must be within the AutoCAD search path.
;
;Usage : Open VL-VBA.DWG then load and run VL-VBA.LSP.
;*******************************************************************************

(prompt "\nVL-VBA Loaded....Type VL-VBA to run.....")

(defun C:VL-VBA ( / thisdrawing applic ssets newsset filter_code filter_value
                             item theatts attlist theattribute1 theattribute2 theattribute3
                             theattribute4 theattribute5)

;load visual lisp extensions
(vl-load-com)

;retrieve reference to the active document
(setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))

;retrieve reference to the selection set collection
(setq ssets (vla-get-selectionsets thisdrawing))

;check if the selection set exists - $Set
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list ssets "$Set")))

  ;if it doesn't create a new one
  (setq newsset (vla-add ssets "$Set"))
  
     ;if it does exist
     (progn

	;delete it       
    	(vla-delete (vla-item ssets "$Set"))
       
	;then create a new one
  	(setq newsset (vla-add ssets "$Set"))
       
     );progn

);if

;create a single element array - integer
(setq filter_code (vlax-make-safearray vlax-vbinteger '(0 . 0)))

;create a single element array - variant
(setq filter_value (vlax-make-safearray vlax-vbvariant '(0 . 0)))

;filter for name - code 2
(vlax-safearray-fill filter_code '(2))

;filter for block name - attab-info
(vlax-safearray-fill filter_value '("attab-info"))

;filter the drawing for the block
(vla-select newsset acSelectionSetAll nil nil filter_code filter_value)

   ;if the block is found
   (if (>= (vla-get-count newsset) 1)

    ;display the dialog
    (ddisplay)

    ;if the block is not found
    (alert
      "\nIncorrect Drawing Sheet
      \n    Use Manual Edit"
    )

  );if

 ;release all objects
 (vlax-release-object thisdrawing)
 (vlax-release-object applic)

;finish clean	
(princ)

);defun

;;;**********************************************************

(defun ddisplay (/)

;retrieve the block reference
(setq item (vla-item newsset 0))

;retrieve the attributes
(setq theatts (vla-getattributes item))

;convert to a list
(setq attlist (vlax-safearray->list (variant-value theatts)))

;extract the attributes
(mapcar 'set '(theattribute1 theattribute2 theattribute3
                                 theattribute4 theattribute5) attlist)

  ;extract the text strings from the attributes
  ;then put the strings into system variables
  (vla-SetVariable thisdrawing "USERS1" (vla-get-textstring theattribute1))
  (vla-SetVariable thisdrawing "USERS2" (vla-get-textstring theattribute2))
  (vla-SetVariable thisdrawing "USERS3" (vla-get-textstring theattribute3))
  (vla-SetVariable thisdrawing "USERS4" (vla-get-textstring theattribute4))
  (vla-SetVariable thisdrawing "USERS5" (vla-get-textstring theattribute5))

;find the VBA file
(if (findfile "vl-vba.dvb")

;if it's found, do the following
(progn

;load the VBA file
(vl-vbaload "vl-vba.dvb")

;get a reference to the application object
(setq applic (vlax-get-acad-object))

;display the VBA dialog
(vla-eval applic "userform1.show")

  ;if OK was selected
  (if (= (getvar "USERI1") 1)

   ;do the following
   (progn

     ;update the attribute textstrings
     (vla-put-textstring theattribute1 (vla-GetVariable thisdrawing "USERS1"))
     (vla-put-textstring theattribute2 (vla-GetVariable thisdrawing "USERS2"))
     (vla-put-textstring theattribute3 (vla-GetVariable thisdrawing "USERS3"))
     (vla-put-textstring theattribute4 (vla-GetVariable thisdrawing "USERS4"))
     (vla-put-textstring theattribute5 (vla-GetVariable thisdrawing "USERS5"))

     ;update the block
     (vla-update newsset)

     ;regen the drawing
     (vl-cmdf "REGEN")

    );progn

   );if

);progn

;couldn't find the VBA file
(alert "\nCannot find VBA file.")

);if

);defun

;;;***********************************************************

;load clean
(princ)

;;;**********************************************************
;
;CODING ENDS HERE

Now, open the the drawing "AL-VBA.DWG".
A title block should appear :

Load and run "VL-VBA.LSP". Your dialog should appear in all it's glory containing the attribute data from the title block.

Change a few of the title block values and then press "OK". Your title block drawing should be updated to reflect your new values. Good one hey?


To download the source Visual Lisp, VBA coding and sample drawing for this tutorial, just click here.

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website