找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 650|回复: 1

[精彩文萃] xData using vla-get and vla-set

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2016-12-13 17:47:30 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 newer 于 2016-12-13 17:48 编辑


You can use the (vla-GetXData) and (vla-SetXData) functions. These functions are not documented in the Visual LISP online help.
To get more information about these functions, look at the function description of GetXData and SetXData in the ACADAUTO.CHM help file (the AutoCAD ActiveX and VBA Reference).
Here is a short description of the functions:
(vla-GetXData entity appName 'codes 'datas)
entity:
This is the entity name of an AutoCAD entity.
appName:
This is a string with the application name. The functions returns all XDatas belonging to the specified application. If appName is "", the functions returns all XDatas attached to 'entity'.
codes:
This list receives the XData group codes.
datas:
This list receives the data of the group codes specified in 'codes'
(vla-SetXData entity codes datas)
entity:
This is the entity name of an AutoCAD entity.
codes:
This list defines the XData group codes.
datas:
This list contains the data for the specified group codes
The following is a sample code which attaches some XData to every entity in model space.After this, it extracts the XData of every entity from every application:


(defun c:setandgetxdatas () 
    (vl-load-com) 

  ;; Get objects 
  (setq acadApp (vlax-get-acad-object) 
        acadDoc (vla-get-ActiveDocument acadApp) 
        mspace (vla-get-ModelSpace acadDoc) 
  ) 

  ;; Register an application name 
  (regapp "MYAPP") 

  ;; Attach some xdatas: 
  ;; 1001: application name  ;; 1000: string ;; 1010: 3D point ;; 1040: real  
  
  ;; 1070: 16bit integer 
  (setq codes  '(1001 1000 1010 1040 1070) 
        values '("MYAPP" "XDATA String" (1.0 1.0 0.0) 5.0 4) 
  ) 


  ;; Create the Safe and Variant Arrays needed for vla-SetXData 
  (setq ListLength (1- (length codes))) 
  (setq ArrayTypes 
     (vlax-make-safearray 
        vlax-vbInteger 
        (cons 0 ListLength) 
     ) 
  ) 
  (setq ArrayValues 
     (vlax-make-safearray 
        vlax-vbVariant 
        (cons 0 ListLength) 
     ) 
  ) 
  (vlax-safearray-fill ArrayTypes codes) 
 ; simple list works 
 ; A more complex list needs to be constructed one element at a time: 
  (setq i 0) 
  (while (< i ListLength) 
    (if (equal (type (setq lstElem (nth i values))) 'LIST) 
      (vlax-safearray-put-element 
         ArrayValues 
         i 
         (vlax-3d-point lstElem) 
      ) 
      (vlax-safearray-put-element ArrayValues i (nth i values)) 
    ) 
    (setq i (1+ i)) 
  ) 
   
  (setq VarTypes (vlax-make-variant ArrayTypes)) 
  (setq VarValues (vlax-make-variant ArrayValues)) 
  (vlax-for ent mspace (vla-SetXData ent VarTypes VarValues)) 
  (princ "\nFinished storing XData on All Drawing entities.\n") 

  (setq EntIndx 0) 
  ;; Get all xdatas from entities 
  (princ "\nReading XData from drawing entities.\n") 
  (vlax-for ent mspace 
    (princ (strcat "\n\nEntity(" (itoa EntIndx) "): ")) 
    (princ (vla-get-ObjectName ent)) 
    (princ "\nXData: ") 

    ;; Deconstruct the Variant Array return of 
    ;; vla-GetXData to SafeArrays,  

    ;;and thento a list 
    (setq VarDataTypes nil 
          VarDataValues nil 
    ) 
    (vla-GetXData ent "" 'VarDataTypes 'VarDataValues) 
    (if (/= VarDataTypes nil) 
      (progn 
         (setq LispList nil) 
         ;; Get the dimension of the safearray 
         (setq lBound (vlax-safearray-get-l-bound VarDataTypes 1)) 
         (setq uBound (vlax-safearray-get-u-bound VarDataTypes 1)) 
         (setq aCounter lBound) 
         (while (<= aCounter uBound) 
            (setq dataCode (vlax-safearray-get-element 
                               VarDataTypes 
                               aCounter 
                           ) 
            ) 
            (setq VarValue (vlax-safearray-get-element 
                               VarDataValues 
                               aCounter 
                           ) 
            ) 
            ;; VarValue contains the variant, but we need the Lisp value of it 
            (if (and (> dataCode 1009) (< dataCode 1040)) 
               ;; Test to see if it's a point Variant 
               (progn (setq saValue (vlax-variant-value VarValue)) 
                      (setq Value (vlax-safearray->list saValue)) 
               ) 
               (setq Value (vlax-variant-value VarValue)) 
            ) 
            ;; Create the list 
            (setq LispList (append LispList (list (cons dataCode Value)))) 
            (setq aCounter (1+ aCounter)) 
         ) ;_ end of while 
      ) ;_ end of progn 
      (setq LispList nil) 
    ) ;_ end of if 
    (if LispList 
      (progn (princ "\n\tCodes: ") 
             (foreach LstItem LispList 
                (princ (car LstItem)) 
                (princ "  ") 
             ) 
             (princ "\n\tData: ") 
             (foreach LstItem LispList 
                (princ (cdr LstItem)) 
                (princ "  ") 
             ) 
      ) 
    ) 

    (setq EntIndx (1+ EntIndx)) 
  ) ;vlax-for 
  (princ) 
) 

(princ "\nc:setandgetxdatas loaded, type setandgetxdatas to run.") 
(princ)



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 3191个

财富等级: 富可敌国

发表于 2016-12-14 08:15:38 | 显示全部楼层
好好学习天天向上
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-9-27 14:20 , Processed in 0.266621 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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