找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 701|回复: 0

[每日一码] 分享几个辞典操作的函数

[复制链接]

已领礼包: 19个

财富等级: 恭喜发财

发表于 2017-5-18 17:20:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; dictionary functions
  3. ;; POSSIBLE IMPROVEMENTS: support trees of dictionaries
  4. ;;      now: all custom dictionaries are under the Root.
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  6. ;; Get Dictionary Object Name From Root Dictionary
  7. (defun dict-name (name / d0 d)
  8.   ;; name As string
  9.   (if (Setq d0 (namedobjdict)
  10.             d  (dictsearch d0 name)
  11.       )
  12.     (cdr (assoc -1 d))
  13.   )
  14. )

  15. ;; Remove Dictionary <Name> From Root Dictionary
  16. (defun dict-remove (name / d0)
  17.   ;; name As string
  18.   (if (dictsearch (Setq d0 (namedobjdict)) name)
  19.     (dictremove d0 name)
  20.   )
  21. )

  22. ;; Make A New Empty Dictionary; Reset Existing.
  23. (defun dict-new        (name / d0)
  24.   ;; name As string
  25.   (if (dictsearch (Setq d0 (namedobjdict)) name)
  26.     (dictremove d0 name)
  27.   )
  28.   (dictadd d0
  29.            name
  30.            (entmakex '((0 . "DICTIONARY") (100 . "AcDbDictionary")))
  31.   )
  32. )

  33. ;; List A Dictionary As Pairs {Name . Object Name}
  34. (defun dict-list (dname / d nl)
  35.   ;; dname As object name or string else ROOT
  36.   (cond
  37.     ((and (Setq        d (cond
  38.                     ((= 'ENAME (type dname))
  39.                      dname
  40.                     )
  41.                     ((= 'STR (type dname))
  42.                      (dict-name dname)
  43.                     )
  44.                     (T (namedobjdict))
  45.                   )
  46.           )
  47.           (Setq d (entget d))
  48.      )
  49.      (while (setq d (member (assoc 3 d) d))
  50.        (if (= 350 (caadr d))
  51.          (setq nl (cons (cons (cdar d) (cdadr d)) nl))
  52.        )
  53.        (setq d (cdr d))
  54.      )
  55.      (reverse nl)
  56.     )
  57.   )
  58. )

  59. ;; Get Raw <Name> Dictionary Data for <Key>
  60. (defun dict-getrawdata (name key / d)
  61.   ;; name and key As strings
  62.   (if (setq d (dict-name name))
  63.     (dictsearch d key)
  64.   )
  65. )

  66. ;; Clear The <Key> From Dictionary <Name>
  67. (defun dict-clear (name key / d)
  68.   ;; name and key As strings
  69.   (if (setq d (dict-name name))
  70.     (dictremove d key)
  71.   )
  72. )

  73. ;;;;;;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~;;;;;;;
  74. ;;;;;;; The two working functions: ;;;;;;;
  75. ;;;;;;;____________________________;;;;;;;

  76. ;; Get A <Key> Value From Dictionary <Name>
  77. (defun dict-get        (name key)
  78.   ;; name and key As strings
  79.   (x-relist ;; decode the data!
  80.             (cdr (member '(100 . "AcDbXrecord")
  81.                          (dict-getrawdata name key)
  82.                  )
  83.             )
  84.   )
  85. )

  86. ;; Put A <Val>ue Into <Name> Dictionary Under <Key>
  87. ;; Dictionary is created automatically if was not existing
  88. (defun dict-put        (name key val)
  89.   ;; three arguments As strings
  90.   (dict-clear name key)
  91.   ;;  clear the old value under this Key
  92.   (dictadd
  93.     (cond ((dict-name name))
  94.           ((dict-new name))
  95.     )
  96.     key
  97.     (entmakex
  98.       (cons '(0 . "XRECORD")
  99.             (cons '(100 . "AcDbXrecord")
  100.                   (x-enlist val)
  101.             )
  102.       )
  103.     )
  104.   )
  105. )
  106. ;; encode the data!
  107. ;;
  108. ;; POSSIBLE IMPROVEMENTS:
  109. ;;    implement dict-append ( APPEND PREPEND )
  110. ;;;;;;;;;;;;;;;;;;;;;;;
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-24 04:14 , Processed in 0.398231 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表