明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: highflybir

[【高飞鸟】] 【越飞越高讲堂1】ActiveX 和脚本技术在CAD的运用

    [复制链接]
发表于 2011-7-27 21:09 | 显示全部楼层
本帖最后由 highflybird 于 2011-7-27 21:12 编辑

ADODB.Stream
展示如何打包二进制文件和读写二进制文件。

  1. (defun c:test ()
  2.   ;;Read a Binary  file
  3.   (defun ReadBinary (FileName / stream arr)
  4.     (setq stream (vlax-create-object "ADODB.Stream"))
  5.     (vlax-put stream 'type 1)       ;adTypeBinary
  6.     (vlax-invoke stream 'open)      ;adModeRead  =1 adModeWrite  =2 adModeReadWrite =3
  7.     (vlax-invoke stream 'LoadFromFile filename)
  8.     (setq Arr (vlax-invoke-method stream 'read (vlax-get stream 'SIZE)));read stream
  9.     (vlax-invoke stream 'close)
  10.     (vlax-release-object stream)
  11.     (vlax-safearray->list (vlax-variant-value arr))   ;if a large size file ,it will take a long time in this step
  12.   )
  13.   ;;Write to a Binary  file from a text stream
  14.   (defun WriteBinary (FileName Array / stream)
  15.     (setq stream (vlax-create-object "ADODB.Stream"))
  16.     (vlax-put stream 'type 1)       ;adTypeBinary
  17.     (vlax-invoke stream 'open)      ;adModeRead  =1 adModeWrite  =2 adModeReadWrite =3
  18.     (vlax-invoke-method stream 'Write array)    ;write stream
  19.     (vlax-invoke stream 'saveToFile fileName 2)    ;save
  20.     (vlax-invoke stream 'close)
  21.     (vlax-release-object stream)
  22.   )

  23.   (setq path (getfiled "Please select a binary file:" "c:/" "" 8 ))     ;get file path
  24.   (setq f (open "C:\\test.txt" "W"))
  25.   (setq data (readBinary path))
  26.   (princ data f)
  27.   (close F)
  28.   ;;(setq stream (vl-get-resource "test"))                              ;we can wrap this text file into .vlx file
  29.   (setq f (open "C:\\test.txt" "R"))                                    ;open for read
  30.   (setq l "")
  31.   (while (setq s (read-line f))
  32.     (setq l (strcat l s))
  33.   )
  34.   (setq array (read l))
  35.   (close f)
  36.   
  37.   (setq dat (vlax-make-safearray 17 (cons 0 (1- (length array)))))      ;17 for unsigned char
  38.   (vlax-safearray-fill dat array)
  39.   (setq bin (vlax-make-variant dat))
  40.   (writeBinary "C:\\test.jpg" bin)     ;write binary file.
  41. )


回复 支持 2 反对 0

使用道具 举报

发表于 2011-7-27 21:47 | 显示全部楼层
                     
发表于 2011-8-4 14:35 | 显示全部楼层
楼主厉害啊!原来我还以为LISP只能画图呢~
发表于 2011-8-12 21:44 | 显示全部楼层
太震撼了。收藏以备学习。
发表于 2011-8-13 02:43 | 显示全部楼层
难道是学计算机的出身?佩服呀
发表于 2011-8-20 20:45 | 显示全部楼层
thank a lot thank a lot....
发表于 2011-8-21 11:13 | 显示全部楼层
强烈顶一下
发表于 2011-10-2 22:30 | 显示全部楼层
高人,值得学习和赞扬
发表于 2011-10-5 15:24 | 显示全部楼层
我想先闭门进修一段时间再来看,不然头很晕
发表于 2011-11-17 20:59 | 显示全部楼层
错误: ADODB.Stream: 文件无法被打开。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-2 10:32 , Processed in 0.528261 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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