本帖最后由 kucha007 于 2024-5-12 15:45 编辑
用于简单的变量跨文件,期待其他人的解决方案~
- ;获取或更新变量(系统+全局+黑板),不存在时更新为默认值(系统变量记录的全局变量值使用@@分隔)
- (defun K:SetGetData (RecDATA SysNam VarLst / K:STR->Lst K:Lst->STR XX SysLst a b)
- (progn ;基础函数
- ;根据分隔符拆分字符串@LeeMac
- (defun K:STR->Lst (STR Del / len Lst pos)
- (setq len (1+ (strlen Del)))
- (while (setq pos (vl-string-search Del STR))
- (setq Lst (cons (substr STR 1 pos) Lst)
- STR (substr STR (+ pos len))
- )
- )
- (reverse (cons STR Lst))
- )
- ;使用分隔符串联字符串@LeeMac
- (defun K:Lst->STR (Lst Del / STR)
- (setq STR (car Lst))
- (foreach itm (cdr Lst) (setq STR (strcat STR Del itm)))
- STR
- )
- )
- (if
- (vl-every
- '(lambda (XX)
- (and
- (eval (read (car XX)));全局变量存在
- (vl-bb-ref (read (car XX)));黑板空间存在同名变量
- (eq (type (eval (read (car XX)))) 'STR);全局变量的值都为STR
- (eq (type (vl-bb-ref (read (car XX)))) 'STR);全局变量的值都为STR
- )
- )
- VarLst
- );变量存在且类型都为STR
- (if RecDATA
- (progn
- (setq SysLst (mapcar '(lambda (XX) (eval (read (car XX)))) VarLst));取全局
- (setenv SysNam (K:Lst->STR SysLst "@@")) ;更新系统
- (mapcar '(lambda (a b) (vl-bb-set (read (car a)) b)) VarLst SysLst) ;更新黑板
- );用全局更新黑板+系统
- (progn
- (setq SysLst (mapcar '(lambda (XX) (vl-bb-ref (read (car XX)))) VarLst));取黑板
- (setenv SysNam (K:Lst->STR SysLst "@@")) ;更新系统
- (mapcar '(lambda (a b) (set (read (car a)) b)) VarLst SysLst) ;更新全局
- );用黑板更新全局+系统
- )
- (if
- (and
- (getenv SysNam) ;系统变量存在
- (setq SysLst (K:STR->Lst (getenv SysNam) "@@"));尝试从系统变量中提取全局变量的值
- (eq (length SysLst) (length VarLst));值的数量相等
- )
- (mapcar '(lambda (a b)
- (set (read (car a)) b);更新全局变量
- (vl-bb-set (read (car a)) b);更新黑板空间的变量值
- )
- VarLst
- SysLst
- )
- (progn
- (setenv SysNam (K:Lst->STR (mapcar 'cadr VarLst) "@@")) ;更新系统变量为默认值
- (mapcar '(lambda (XX) (set (read (car XX)) (cadr XX))) VarLst);更新全局变量为默认
- (mapcar '(lambda (XX) (vl-bb-set (read (car XX)) (eval (read (car XX))))) VarLst);更新黑板空间变量为默认
- )
- )
- )(princ)
- )
用法:
- (setq *MkVarLst*
- (list
- (list "Global:RvLay" "1");1默认层,2当前层
- (list "Global:RvMde" "1");1修改,2批注,3检查
- )
- );参数列表,自行扩写
- (K:SetGetData Nil "K\\RRSysNam" *MkVarLst*);用黑板更新系统和全局(缺省取默认)
- ;你的程序.........
- (K:SetGetData T "K\\RRSysNam" *MkVarLst*);用全局更新系统和黑板
|