本帖最后由 kucha007 于 2024-1-13 11:15 编辑
最近接触了DCL,每个参数要一个变量感觉太麻烦了
干脆记录到一个系统变量,之后更新或提取为全局变量,方便跨文件使用DCL
- ;获取或更新变量(全局+系统),将所有参数记录到一个系统变量。
- ;参数之间使用分号@@分隔,类型必须为字符串。Flag为是T时强制更新为默认值
- (defun K:SetGetUpdVar (Flag SysNam VarLst / XX GlbLst SysLst a b)
- ;(K:SetGetUpdVar SysNam (list (list "Key名" "变量名" "默认值")...))
- (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
- )
- )
- (setq SysNam (strcat "K\" SysNam));系统变量名
- (setq VarLst
- (mapcar
- '(lambda (XX) (list (nth 0 XX) (strcat "Global:" (nth 1 XX)) (nth 2 XX)))
- VarLst
- )
- );全局变量名补全
- (mapcar
- '(lambda (XX)
- (if (and (nth 0 XX)(get_tile (nth 0 XX)))
- (set (read (nth 1 XX)) (get_tile (nth 0 XX)))
- )
- )
- VarLst
- );获取DCL对应Key的值,更新对应的全局变量
-
- (if Flag ;强制更新变量值
- (progn
- (setenv SysNam (K:Lst->STR (mapcar 'caddr VarLst) "@@")) ;更新系统变量为默认值
- (mapcar '(lambda (XX) (set (read (nth 1 XX)) (nth 2 XX))) VarLst);更新全局变量为默认
- )
- (if
- (vl-every
- '(lambda (XX)
- (and
- (eval (read (nth 1 XX)))
- (eq (type (eval (read (nth 1 XX)))) 'STR)
- )
- )
- VarLst
- );全局变量都存在且都为STR
- (progn
- (mapcar '(lambda (xx) (setq GlbLst (cons (eval (read (nth 1 XX))) GlbLst ))) VarLst);收集全局变量
- (setenv SysNam (K:Lst->STR (reverse GlbLst) "@@")) ;更新系统变量
- )
- (if
- (and
- (getenv SysNam) ;系统变量存在
- (setq SysLst (K:STR->Lst (getenv SysNam) "@@"));尝试从系统变量中提取全局变量
- (eq (length SysLst) (length VarLst));数量相等
- )
- (progn
- (mapcar '(lambda (a b) (set (read (nth 1 a)) b)) VarLst SysLst);更新全局变量
- )
- (progn
- (setenv SysNam (K:Lst->STR (mapcar 'caddr VarLst) "@@")) ;更新系统变量为默认值
- (mapcar '(lambda (XX) (set (read (nth 1 XX)) (nth 2 XX))) VarLst);更新全局变量为默认
- )
- )
- )
- )
- (princ)
- )
详细用法:
- (setq Global:VarLst
- (list
- (list "Key1" "Nam1" "Var1");参数1
- (list "Key2" "Nam2" "Var2");参数2
- )
- );参数列表,自行扩写
- (K:SetGetUpdVar Nil "ZZMLSys" Global:VarLst);有全局变量时更新系统变量,无全局变量时获取系统变量(若无则设置为默认值)
- (K:SetGetUpdVar T "ZZMLSys" Global:VarLst);强制更新为默认
返回结果:
- (princ (getenv "K\\ZZMLSys"));系统变量
- (princ Global:Nam1);参数1全局变量
- (princ Global:Nam2);参数2全局变量
- ....
|