MicroDraw 控件读取AutoLisp设置的扩展属性

如何读写Acad元素的扩展数据??  扩展数据和MicroDraw属性链表是如何转化的??
1.  AutoCad的每个元素(包括图层和可视元素点、线、园等)都可以设置扩展属性(XDATA); 在AutoCAD环境内可以用lisp(VisualLisp)语言进行简单的设置;
2. 当用MicroDraw控件打开DWG文件时, 文件内的元素如何包含扩展属性,则属性项自动转换到元素(MB_Drawable)的属性链表内;可以用控件相关接口进行获取(GetLongProp   GetRealProp 等);参见:http://www.microdraw.org/dz/viewthread.php?tid=375&highlight=%E5%B1%9E%E6%80%A7
3. 用MicroDraw控件保存DWG时, 元素属性链表将自动保存至acad元素的扩展数据;在acad环境内可以用lisp语言进行读取;
以下是例程lisp代码,文件名为op_xdata.lsp, 关于lisp语言的基本语法参见AutoCAD二次开发相关资料:
;;; 在Acad内对扩展数据(XDATA)进行操作
;;;
;;;
;;;  功能: 1.创建扩展数据  2. 修改扩展数据   3.删除扩展数据
;;;
;;;
;;;---------------------------------------------------------------------------------------------------
(defun c:writexdata(/)
  (setq the_ent_list(entget (car (entsel "\n 请选择元素: "))))
  ;;先判断是否已经有了日期
  (setq cur_date_list(assoc -3 the_ent_list))
  (if (null cur_date_list)
      (progn
         (setq cur_date(getstring "请输入日期(8位 如:20011201):"))
         ;以下注册扩展数名,数据名"MDRAW_DATA"相当于MicroDraw控件内的属性名称
         (regapp "MDRAW_DATA")
         (setq tem_list (cons '1000 cur_date))
         (setq tem_list (list "MDRAW_DATA" tem_list '(1070 . 1)))
         (setq tem_list (list -3 tem_list))
         (setq exdata (list tem_list))
        ;(setq exdata '((-3
        ;                   ("MDRAW_DATA" (1000 . "20010101") (1070 . 1))
        ;              ))
        ;)
         (setq newent
               (append the_ent_list exdata))
          (entmod newent)
       )
       ;;如果已经有了日期
       (progn
          (setq date_list1 (assoc -3 the_ent_list))
          (setq date_list2 (car (cdr date_list1)))
          (setq date_list (nth 1 date_list2))
          (setq cur_date (cdr date_list))
          (setq the_prompt(strcat "\n当前日期:"  cur_date "修改为:"))
          (setq new_date(getstring the_prompt))
          (setq date_list20(subst new_date cur_date date_list2))
          (setq date_list1(subst date_list20 date_list2 date_list1))
          (setq date_list1 (cons -3 date_list1))
          (setq the_ent_list (subst date_list1 (assoc -3 the_ent_list) the_ent_list))
          (entmod the_ent_list)
       )
   )
)
;;从元素内读出扩展数据
(defun c:readxdata(/)
 (setq sel_ent_list (entget (car (entsel " \n 请选择元素:")) (list "*")))
 (setq ent_name(cdr (assoc 5 sel_ent_list)))
 (setq ent_type (cdr (assoc 0 sel_ent_list)))
 (princ)
 (princ "元素类型是:")
 (princ ent_type)
 ;(if (= ent_type "TRACE")
 ; (setq trace_name ent_name)
 ;  (progn
 ;    (princ "选择错误!")
 ;    (princ)
 ;    (exit)
 ;  )
 ;)
 (setq trace_list (entget (handent ent_name) (list "*")))
  ;(if(= trace_list nil)
  ;  (progn
  ;      (princ "没有扩展数据")
  ;       (princ)
  ;       (exit)
  ;   )
  ;)
 (setq cur_date "")
 (setq date_list (assoc -3 trace_list))
 (if (not (null date_list))
     (progn
        (setq date_list (car (cdr date_list)))
        (setq date_list (nth 1 date_list))
        (setq cur_date (cdr date_list))
        (setq cur_date (strcat "[日]" cur_date))
      )
     ;else
     (progn
         (princ "没有扩展数据")
         (princ)
         (exit)
     )
 )
 (setq open_price (nth 2 (assoc 10 trace_list))) ;Get the open price
 (setq close_price(nth 2 (assoc 12 trace_list))) ;Get the close price
 (setq line_list (entget (entnext (handent trace_name)) (list "*")))
 (setq high_price (nth 2 (assoc 10 line_list))) ;Get the low price
 (setq low_price (nth 2 (assoc 11 line_list))) ;Get the high pice one day
 (setq open_price (* open_price value_scale)
close_price(* close_price value_scale)
high_price (* high_price value_scale)
low_price(* low_price value_scale))
 (setq str1 (strcat "[开]"  (rtos (+ open_price 1) 2 2)))
 (setq str2 (strcat "[高]"  (rtos high_price 2 2)))
 (setq str3 (strcat "[低]"  (rtos low_price 2 2)))
 (setq str4 (strcat "[收]"  (rtos (- close_price 1) 2 2)))
 (setq str_all (strcat "<<>>" cur_date str1 str2 str3 str4))
 (setvar "modemacro"  str_all)
)
复制代码
虽然MicroDraw和Acad是两个完全不同的CAD平台,但提供了兼容Acad内扩展数据的方法;
在 acad内命令行键入 (load "c:\\op_xdata.lsp")  加载lsp程序 【注:lsp文件在c盘上,文件名称是 op_xdata.lsp】
写入扩展数据
command:writedata [回车]
读取扩展数据
command:readdata [回车]
以下我们要讨论的是如何在MicroDra控件环境内读取这些扩展属性:
"(-3 "  表示扩展数据???
((-1 . <图元名: 7efc6f58>) (0 . "TRACE") (330 . <图元名: 7efb9cd0>) (5 .
"2DDB") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "KLRED") (100 .
"AcDbTrace") (10 22556.0 9250.0 0.0) (11 22564.0 9250.0 0.0) (12 22556.0 9319.0
0.0) (13 22564.0 9319.0 0.0) (39 . 0.0) (210 0.0 0.0 1.0) (-3 ("MDRAW_DATA"
(1000 . "20011201") (1070 . 1))))