In this tutorial we're going to create an application that extracts attributes from a block and displays these attributes within a dialog box. The dialog will be created "on-the-fly" and the number of attribute edit boxes will be determined by the number of attributes. |
|
We will be using a lot of Visual Lisp coding within this routine as I also want to demonstrate one method of extracting and updating attributes using Visual Lisp.
|
No further explanation is necessary as the coding is well commented :
|
;;;DCLATT.LSP
;This program is for demonstration and tutorial purposes only.
--------------------------------------------
;This program, using Visual Lisp will extract attributes from
;a block and display them in a dialog box.
;The dialog box will be created "on the fly" with the relevant
;number of edit boxes ;to suit the number of attributes within
;the block.
;The new attribute data will then be retrieved from the dialog
;and the block updated.
;Written by Kenny Ramage - May 2002
;afralisp@mweb.com.na
;http://www.afralisp.com
-------------------------------------------
;Usage :
;Load by typing (load "DCLATT") at the command prompt.
;Type DCLATT at the command prompt to run.
;Select any block containing attributes.
;Replace any of the values in the text boxes to updated
;the attributes.
---------------------------------------------
;Dependencies : None that I know of.
;Not much in the way of error checking I'm afraid.
---------------------------------------------
;Limitations : Will only edit a certain number of attributes.
;System dependent.
;I don't recommend more than 10.
;I've had up to 14 on my display.
----------------------------------------------
----------------------------------------------
(prompt "\nType DCLATT to run.....")
(defun c:dclatt ( / theblock thelist n taglist
txtlist lg fname fn nu dcl_id l relist)
;load the visual lisp extensions
(vl-load-com)
;get the entity and entity name
(setq theblock (car (entsel)))
;convert to vl object
(setq theblock (vlax-ename->vla-object theblock))
;check if it's a block
(if (= (vlax-get-property theblock 'ObjectName)
"AcDbBlockReference")
;if it is, do the following
(progn
;check if it has attributes
(if (= (vlax-get-property theblock
'HasAttributes) :vlax-true)
;if it has attributes, do the following
(progn
;get the attributes
(getatt theblock)
;create the dialog
(create_dialog)
;run the dialog
(run_the_dialog)
;update the attributes
(upatt)
);progn
;No attributes, inform the user
(alert "This Block has No Attributes!!
- Please try again.")
);if
);progn
;it's not a block, inform the user
(alert "This is not a Block!! - Please try again.")
);if
(princ)
);defun
------------------------
(defun getatt (enam)
;retrieve the attributes
(setq thelist (vlax-safearray->list
(variant-value
(vla-getattributes enam))))
;process each attribute
(foreach n thelist
;get the tag attribute data
(setq taglist (cons (vla-get-tagString n) taglist)
;get the text attribute data
txtlist (cons (vla-get-textString n) txtlist)
;how many attributes?
lg (length taglist)
);setq
);foreach
;reverse the lists
(setq taglist (reverse taglist)
txtlist (reverse txtlist))
);defun
-------------------------
(defun create_dialog ()
;create a temp DCL file
(setq fname (vl-filename-mktemp "dcl.dcl"))
;open it to write
(setq fn (open fname "w"))
;write the dialog header coding
(write-line "temp : dialog { label = \"Edit Attributes\";" fn)
;reset the incremental control number
(setq nu 0)
;start the loop to create the edit boxes
(repeat lg
;create the edit boxes
(write-line ": edit_box {" fn)
(setq l (strcat "\"" "eb" (itoa nu) "\"" ";"))
(write-line (strcat "key = " l) fn)
(setq l (nth nu taglist))
(write-line (strcat "label = " "\"" l "\"" ";") fn)
(setq l (nth nu txtlist))
(write-line (strcat "value = " "\"" l "\"" ";") fn)
(write-line "alignment = centered; edit_width = 20; }" fn)
;increment the counter
(setq nu (1+ nu))
);repeat
;ok and cancel button
(write-line "ok_only; }" fn)
;close the temp DCL file
(close fn)
);defun
------------------------------
(defun run_the_dialog ()
;load the dialog file and definition
(setq dcl_id (load_dialog fname))
(if (not (new_dialog "temp" dcl_id))
(exit )
);if
(mode_tile "eb0" 2)
;if the OK button is selected
(action_tile "accept" "(retatt)")
;start the dialog
(start_dialog)
;unload the dialog
(unload_dialog dcl_id)
;delete the temp DCL file
(vl-file-delete fname)
);defun
---------------------------
(defun retatt ()
;reset the increment counter
(setq nu 0)
start the loop
(repeat lg
;retrieve the tile value
(setq l (get_tile (strcat "eb" (itoa nu))))
;add it to the list
(setq relist (cons l relist))
;increment the counter
(setq nu (1+ nu))
);repeat
(setq relist (reverse relist))
;close the dialog
(done_dialog)
);defun
-----------------------------------------
(defun upatt ()
;reset the increment counter
(setq nu 0)
;start the loop
(repeat lg
;update the attribute
(vla-put-textstring (nth nu thelist) (nth nu relist))
;increment the counter
(setq nu (1+ nu))
);repeat
;update the block
(vla-update theblock)
);defun
---------------------------
;clean loading
(princ)
---------------------------
;End of ATTDCL.LSP
---------------------------
|
Now you need to create 2 or three blocks containing a varying number of attributes.
Here's what mine look like :
Load and run "DCLATT.LSP" and select "Attribute No 1".
You dialog should appear looking like this with three attribute edit boxes :
Now run the program again, but this time select "Attribute No 2".
The dialog will appear, but this time with four attribute edit boxes.
Clever hey?
A big thanks to Stig Madsen for the help with the coding in this project.
Just click here if you would like the source coding and sample drawing files for this tutorial.
|
posted on 2008-04-12 14:09
深藏记忆 阅读(229)
评论(0) 编辑 收藏 所属分类:
转载Vlisp