找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 493|回复: 0

[转贴]:搜到的一些 vblibrary 函数

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2004-5-27 20:31:46 | 显示全部楼层 |阅读模式

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

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

×

  1. ;;; This file contains VB data conversion functions that are necessary for
  2. ;;; the function library functions to work properly.
  3. ;;; Available functions are:
  4. ;;;
  5. ;;; (object? x)
  6. ;;; (collection? x)
  7. ;;; (safearray? x)
  8. ;;; (variant? x)
  9. ;;; (VariantArray? x)
  10. ;;; (ToObject x)
  11. ;;; (pks>obj x)
  12. ;;; (ToVB x)
  13. ;;; (ToLISP x)
  14. ;;; (PtList=>VB ptlst dim)
  15. ;;; (TableNames tblobj)
  16. ;;; (GetProperty obj prop)
  17. ;;; (PutProperty obj prop nval)
  18. ;;; (MS-TypeLibraryLoad appname)

  19. (vl-load-com)

  20. ;;; This function returns T if x is a VBA object
  21. ;;测试是否是 VBA  Object
  22. (defun object? (x) (eq 'VLA-OBJECT (type x)))

  23. ;;; This function returns T if x is a variant.
  24. ;;测试是否是 Variant
  25. (defun variant? (x) (= 'variant (type x)))

  26. ;;; This function returns T if x is a variant array.
  27. ;;测试是否为 variant array
  28. (defun VariantArray? (x)
  29.   (and (variant? x) (>= (vlax-variant-type x) 8192))
  30. )

  31. ;;; This function returns T if x is a VBA collection.
  32. ;;测试是否为 VBA collection
  33. (defun collection? (x)
  34.   (and (object? x) (vlax-property-available-p x 'Count))
  35. )

  36. ;;; This function returns T if x is a VBA safearray
  37. ;;VBA Saferarray 测试
  38. (defun safearray? (x) (eq 'SAFEARRAY (type x)))


  39. ;;; The function attempts to convert it's input into a VBA object.
  40. ;;; - the ename of an entget list is returned as an object
  41. ;;; - the first ename of a pickset is returned as an object
  42. ;;; - an ename is returned as an object
  43. ;;; - an object is returned as itself
  44. ;;; - the first object in a collection is returned as an object
  45. ;;;
  46. ;; 转换为 VBA Object
  47. ;; 实体名(entsel返回格式)转换为 Object
  48. ;; 实体名 转换为Object
  49. ;; SelectionSet 返回 第一个 Object
  50. ;; Object 返回 自身
  51. ;; 选择集 返回 第一个实体
  52. (defun ToObject        (x)
  53.   (cond
  54.     ((and (listp x)
  55.           (= (length x) 2)
  56.           (nam? (car x))
  57.           (Point? (cadr x))
  58.      )
  59.      (ToObject (car x))
  60.     )
  61.     ((nam? x) (vlax-ename->vla-object x))
  62.     ((collection? x) (vla-Item x 1))
  63.     ((object? x) x)
  64.     ((pks? x) (ToObject (ssname x 0)))
  65.     (T nil)
  66.   )
  67. )


  68. ;;....Convert a PICKSET to a list of OBJECTs
  69. ;;选择集转换为 Object 集合
  70. (defun pks>obj (x / cnt lst)
  71.   (mapcar 'vlax-ename->vla-object (pks>nam x))
  72. )


  73. ;;....convert Lisp data to VB
  74. ;;转换Lisp数据为 VB 数据类型,支持 点、表(字串、整数、实数、Object、空表)
  75. ;;                             t & nil
  76. ;;                             整数、实数、字符、实体
  77. (defun ToVB (x / len typ lst)
  78.   (cond
  79.     ((Point? x)
  80.      (vlax-3D-point (2D>3Dpt (mapcar 'float x)))
  81.     )
  82.     ((and (listp x) (apply 'eq (mapcar 'type x)))
  83.      (setq len (length x)
  84.            typ (type (car x))
  85.            lst (vlax-make-safearray
  86.                  (cond
  87.                    ((= typ 'STR) vlax-vbString)
  88.                    ((= typ 'VLA-OBJECT) vlax-vbObject)
  89.                    ((= typ 'INT) vlax-vbInteger)
  90.                    ((= typ 'REAL) vlax-vbDouble)
  91.                    (T nil)
  92.                  )
  93.                  '(0 . (1- len))
  94.                )
  95.      )
  96.      (vlax-safearray-fill lst 'x)
  97.     )
  98.     ((or (= x nil) (= x T) (= x '()))
  99.      (vlax-make-variant x vlax-vbBoolean)
  100.     )
  101.     ((= 'INT (type x)) (vlax-make-variant x vbInteger))
  102.     ((= 'REAL (type x)) (vlax-make-variant x vbDouble))
  103.     ((= 'STR (type x)) (vlax-make-variant x vbString))
  104.     ((= 'ENAME (type x)) (vlax-ename->vla-object x))
  105.     (T x)
  106.   )
  107. )

  108. ;;..................Convert VB data to LISP data types
  109. ;; VB 数据转换为 Lisp 数据类型
  110. (defun ToLISP (x)
  111.   (cond
  112.     ((safearray? x) (vlax-safearray->list x))
  113.     ((VariantArray? x)
  114.      (vlax-safearray->list (vlax-variant-value x))
  115.     )
  116.     ((variant? x) (vlax-variant-value x))
  117.     ((= x :vlax-true) T)
  118.     ((= x :vlax-false) nil)
  119.     (T x)
  120.   )
  121. )



  122. ;;; Take a list of points and convert them to a variant array of doubles suitable
  123. ;;; for VB lwpolyline generation. Set the DIM argument to 2 or 3 depending on
  124. ;;; whether you need a list for 2D or 3D lwpolylines.
  125. ;; 点表转换为 VB variant array 支持 2D 、3D 点
  126. (defun PTLIST=>VB (ptlst dim / vbarr)
  127.   (setq        ptlst (mapcar '(lambda (x)
  128.                          (if (= 2 dim)
  129.                            (xyof X)
  130.                            (2D>3Dpt X)
  131.                          )
  132.                        )
  133.                       ptlst
  134.               )
  135.         ptlst (apply 'append ptlst)
  136.         vbarr (vlax-make-safearray
  137.                 vlax-vbDouble
  138.                 (cons 0 (1- (length ptlst)))
  139.               )                                ;dimension the array
  140.   )
  141.   (vlax-safearray-fill vbarr ptlst)        ;fill the array with doubles
  142.   (vlax-make-variant vbarr vbDouble)        ;return variant array with doubles
  143. )


  144. ;;;Return all the name entries in a given table.
  145. ;;返回 表内实体
  146. (defun TableNames (tableobj / lst cnt)
  147.   (setq cnt 0)
  148.   (repeat (vla-get-Count tableobj)
  149.     (setq lst (cons (vla-get-Name (vla-Item tableobj cnt)) lst)
  150.           cnt (1+ cnt)
  151.     )
  152.   )
  153.   (acad_strlsort lst)
  154. )


  155. ;;........Retrieve information from an object
  156. ;;获取实体特性
  157. (defun GetProperty (obj prop)
  158.   (setq obj (ToObject obj))
  159.   (if (vlax-property-available-p obj prop)
  160.     (ToLISP (vlax-get-property obj prop))
  161.     nil
  162.   )
  163. )

  164. ;;........Modify an object's properties
  165. ;;修改 Object 特性 (Object 特性 值)
  166. (defun PutProperty (obj prop nval)
  167.   (setq        obj  (ToObject obj)
  168.         nval (ToVB nval)
  169.   )
  170.   (if (vlax-property-available-p obj prop)
  171.     (vlax-put-property obj prop nval)
  172.     nil
  173.   )
  174. )


  175. ;;........Load the type library for the specified Microsoft Application
  176. ;;        "WORD" "EXCEL" "ACCESS" are allowable inputs, more could be added
  177. (defun MS-TypeLibraryLoad (app / mspath)
  178.   (setq        app    (strcase app)
  179.         mspath "C:/Program Files/Microsoft Office/Office/"
  180.   )
  181.   (cond
  182.     ((= app "WORD")
  183.      (if (equal nil docc-wd100Words)
  184.        (vlax-import-type-library
  185.          :tlb-filename
  186.          (strcat mspath "msword8.olb")
  187.          :methods-prefix
  188.          "docm-"
  189.          :properties-prefix
  190.          "docp-"
  191.          :constants-prefix
  192.          "docc-"
  193.        )
  194.      )
  195.     )
  196.     ((= app "ACCESS")
  197.      (vlax-import-type-library
  198.        :tlb-filename
  199.        (strcat mspath "msacc8.olb")
  200.        :methods-prefix
  201.        "mdbm-"
  202.        :properties-prefix
  203.        "mdbp-"
  204.        :constants-prefix
  205.        "mdbc-"
  206.      )
  207.     )
  208.     ((= app "EXCEL")
  209.      (vlax-import-type-library
  210.        :tlb-filename
  211.        (strcat mspath "excel8.olb")
  212.        :methods-prefix
  213.        "xlsm-"
  214.        :properties-prefix
  215.        "xlsp-"
  216.        :constants-prefix
  217.        "xlsc-"
  218.      )
  219.     )
  220.     (T
  221.      (prompt
  222.        (strcat "\n " app " is an invalid app name to load.")
  223.      )
  224.     )
  225.   )
  226. )



  227. ;;******************************************************************
  228. ;;****          SUPPORT FUNCTIONS
  229. ;;******************************************************************
  230. ;;是否选择集
  231. (defun pks? (x) (eq 'PICKSET (type x)))
  232. ;;测试实体
  233. (defun nam? (x) (eq 'ENAME (type x)))
  234. ;;表测试
  235. (defun lst? (x) (eq 'LIST (type x)))
  236. ;;实数表
  237. (defun nbr*? (x)
  238.   (and (lst? x)
  239.        (apply 'and (mapcar 'numberp x))
  240.   )
  241. )
  242. ;;点测试
  243. (defun Point? (x)
  244.   (and (listp x)
  245.        (<= 2 (length x) 3)
  246.        (apply 'and (mapcar 'numberp x))
  247.   )
  248. )
  249. ;;3D -> 2D 点
  250. (defun XYof (x) (list (car x) (cadr x)))
  251. ;;2D -> 3D 点
  252. (defun 2D>3Dpt (x)
  253.   (if (= 2 (length x))
  254.     (append x (list 0.0))
  255.     x
  256.   )
  257. )

  258. ;;....Convert a PICKSET to a list of ENAMES
  259. ;; 选择集转换为实体表
  260. (defun pks>nam (x / cnt lst)
  261.   (setq        cnt 0
  262.         lst '()
  263.   )
  264.   (repeat (pkslength x)
  265.     (setq lst (cons (ssname x cnt) lst)
  266.           cnt (1+ cnt)
  267.     )
  268.   )
  269.   (reverse lst)
  270. )

  271. ;;...get the true length of a PICKSET x
  272. ;; 选择集长
  273. (defun pkslength (x)
  274.   (if x
  275.     (sslength x)
  276.     0
  277.   )
  278. )
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-18 23:01 , Processed in 0.400724 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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