与Lee一样,变量USERI1-USERI5的简单使用可以用于检索存储值,但需要注意的是,其他程序也可能会写入USERI1。更好的方法是使用字典变量
- (setvar "USERI1" 1)
- (setq layerstd (getvar "USERI1")
本例存储了两个变量wallsize和thickness
- (defun get-or-create-Dict (/ adict)
- ;;test if "LAYSTD" is already present in the main dictionary
- (if (not (setq adict (dictsearch (namedobjdict) "LAYSTD")))
- ;;if not present then create a new one and set the main
- ;; dictionary as owner
- (progn
- (setq adict (entmakex '((0 . "DICTIONARY")(100 . "AcDbDictionary"))))
- ;;if succesfully created, add it to the main dictionary
- (if adict (setq adict (dictadd (namedobjdict) "LAYSTD" adict)))
- )
- ;;if present then just return its entity name
- (setq adict (cdr (assoc -1 adict)))
- )
- )
- (defun get-or-make-Xrecord (/ adict anXrec)
- ; change to allow w1 etc and thickness
- (cond
- ;;first get our dictionary. Notice that "LAYSTD" will be
- ;;created here in case it doesn't exist
- ((setq adict (get-or-create-Dict))
- (cond
- ;;if "LAYSTD" is now valid then look for "LAYSTDVARS" Xrecord
- ((not (setq anXrec (dictsearch adict wallvar)))
- ;the variable LAYSTDvars is name of xrecord so need to be a variable to add lots
- ;;if "LAYSTDVARS" was not found then create it
- ;(setq anXrec (entmakex '((0 . "XRECORD")
- (setq anXrec (entmakex (list (cons 0 "XRECORD")
- (cons 100 "AcDbXrecord")
- ;(1 . wallvar)
- (cons 1 wallvar)
- (cons 40 wallsize)
- ;(40 . wallsize)
- )
- )
- )
- ;;if creation succeeded then add it to our dictionary
- (if anXrec (setq anXrec (dictadd adict wallvar anXrec)))
- )
- ;;if it's already present then just return its entity name
- (setq anXrec
- (cdr (assoc -1 (dictsearch adict wallvar)))
- )
- )
- )
- )
- )
- (defun getLAYSTDvars (/ vars varlist)
- ;;retrieve XRecord "wallvar" from dictionary "LAYSTD"
- ;;which in turn calls both functions above
- (setq vars (get-or-make-Xrecord))
- ;;if our Xrecord is found, then get values in group code
- (cond (vars
- (setq varlist (entget vars))
- (setq WALLname (cdr (assoc 1 varlist)))
- (setq WALLTHICK (cdr (assoc 40 varlist)))
- )
- ;;otherwise return nil
- (T nil)
- )
- )
|