明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1604|回复: 2

跪求同TWF文件导入TIF到CAD的源代码

[复制链接]
发表于 2015-7-16 16:29 | 显示全部楼层 |阅读模式
前面看有个前辈写了一个这个程序,但用后发现图片例大了一倍,而且是封装过的,哪位大神有这个程序的源码能不能共享下啊

本帖子中包含更多资源

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

x
发表于 2015-10-21 22:23 | 显示全部楼层
GeoRefImg,这个是插入带座标图片的檔案,可以到下面的连结看一下!
不是批次的!
http://www.cadstudio.cz/georefimg

下面的代码如果worldfile的x旋转量及y旋转量不为0的话会跟GeoRefImg套出来的结果不同。
  1. ;;; AUTHOR
  2. ;;; Copyrightc 2011 Ron Perez (ronperez AT gmail DOT com)
  3. ;;;

  4. ;;;Reads world tiff file (.tfw) to scale and place image correctly in autocad.
  5. ;;;First insert all tiff images into drawing at whatever scale and insertion point.
  6. ;;;If the TFW exists in same directory and is named the same as the image selected,
  7. ;;;it will automatically be found and the image will be scaled and placed. If it is
  8. ;;;not in the same directory it can be browsed to.
  9. ;;;03.23.2011 Added support to create TFW files as well as support rotated images :)
  10. ;;;10.11.2011 *BUG FIX* Basepoint was off 1/2 the pixel resolution DOH!
  11. ;;;Needs a file with 6 lines (ScaleX Rotation -Rotation -ScaleY TopLeftXCoord TopLeftYCoord)
  12. (defun c:tfw (/              ss->lst foo     _readfile              _writetofile    bpt
  13.               data    ext     hgt     hs      imgpath imhgt   imwdth  l1
  14.               mvpt    name    opt     pre     rot     ss      tfw     wdth
  15.              )
  16.   (vl-load-com)
  17.   (defun ss->lst (ss / e n out)
  18.     (setq n -1)
  19.     (while (setq e (ssname ss (setq n (1+ n))))
  20.       (setq out (cons (vlax-ename->vla-object e) out))
  21.     )
  22.   )
  23.   (defun _writetofile (filename lst / file result)
  24.     (cond ((and (eq 'str (type filename)) (setq file (open filename "w")))
  25.            (foreach x lst
  26.              (write-line
  27.                (cond ((= (type x) 'str) x)
  28.                      ((= (type x) 'int) (itoa x))
  29.                      ((= (type x) 'real) (rtos x 2 16))
  30.                      ((vl-prin1-to-string x))
  31.                )
  32.                file
  33.              )
  34.            )
  35.            (close file)
  36.            filename
  37.           )
  38.     )
  39.   )
  40.   (defun _readfile (filename / file line result)
  41.     (cond
  42.       ((and (eq 'str (type filename)) (setq file (open filename "r")))
  43.        (while (setq line (read-line file))
  44.          (setq result (cons (vl-string-trim " " line) result))
  45.        )
  46.        (close file)
  47.        (reverse result)
  48.       )
  49.     )
  50.   )
  51.   (defun foo (file / ext)
  52.     (if        (setq ext (strcase (vl-filename-extension file) t))
  53.       (cond ((wcmatch ext ".tif*") ".tfw")
  54.             ((wcmatch ext ".jp*g") ".jgw")
  55.             ((eq ext ".png") ".pgw")
  56.       )
  57.     )
  58.   )
  59.   (setq opt "ReadIt")
  60.   (initget 0 "ReadIt WriteIt")
  61.   (setq        opt
  62.          (cond
  63.            ((getkword (strcat "\nImage World File [ReadIt/WriteIt] <" opt ">: "))
  64.            )
  65.            (opt)
  66.          )
  67.   )
  68.   (princ "\nSelect image(s): ")
  69.   (setq pre (getvar 'dwgprefix))
  70.   (if (and (setq ss (ssget ":L" '((0 . "image")))) (setq ss (ss->lst ss)))
  71.     (foreach image ss
  72.       (setq name    (vlax-get image 'name)
  73.             hgt            (vlax-get image 'height)
  74.             wdth    (vlax-get image 'width)
  75.             imhgt   (vlax-get image 'imageheight)
  76.             imwdth  (vlax-get image 'imagewidth)
  77.             rot            (vlax-get image 'rotation)
  78.             bpt            (vlax-get image 'origin)
  79.             imgpath (vlax-get image 'imagefile)
  80.             ext            (foo imgpath)
  81.             imgpath (vl-filename-directory imgpath)
  82.             tfw            (strcat imgpath "\" name ext)
  83.       )
  84.       (if (= opt "ReadIt")
  85.         (progn
  86.           (if
  87.             (and
  88.               (or
  89.                 (findfile tfw)
  90.                 (setq tfw (findfile (strcat pre name ext)))
  91.                 (setq tfw (getfiled (strcat "***Select <<" name ext ">>***")
  92.                                     pre
  93.                                     (substr ext 2)
  94.                                     16
  95.                           )
  96.                 )
  97.               )
  98.               (setq data (mapcar 'atof (_readfile tfw)))
  99.               (> (length data) 5)
  100.               (setq l1 (abs (car data)))
  101.               (setq hs (* l1 0.5))
  102.               (setq mvpt (list (nth 4 data) (nth 5 data) 0.0))
  103.             )
  104.              (progn (vla-put-imageheight image (* hgt l1))
  105.                     (vla-put-imagewidth image (* wdth l1))
  106.                     (vla-put-rotation image (cadr data))
  107.                     (setq rot (vlax-get image 'rotation))
  108.                     (setq bpt (polar bpt (+ (/ pi 2.) rot) (* hgt l1)))
  109.                     ;;Basepoint offset since it's the CENTER of the upper left pixel
  110.                     (setq bpt (list (+ (car bpt) hs) (- (cadr bpt) hs) 0.0))
  111.                     (vlax-invoke image 'move bpt mvpt)
  112.                     (princ (strcat "\nTFW File Read - " tfw))
  113.              )
  114.              (princ "\nTFW file NOT found or not correctly formatted!")
  115.           )
  116.         )
  117.         (progn (setq bpt (polar bpt (+ (/ pi 2.) rot) imhgt))
  118.                (setq hs (* (abs (/ imhgt hgt)) 0.5))
  119.                ;;Basepoint offset since it's the CENTER of the upper left pixel
  120.                (setq bpt (list (+ (car bpt) hs) (- (cadr bpt) hs) 0.0))
  121.                (if (setq tfw (_writetofile
  122.                                (if (findfile (strcat imgpath "\" name ext))
  123.                                  (strcat imgpath "\" name ext)
  124.                                  (strcat pre name ext)
  125.                                )
  126.                                (list (/ imhgt hgt)
  127.                                      rot
  128.                                      (- (abs rot))
  129.                                      (- (abs (/ imwdth wdth)))
  130.                                      (car bpt)
  131.                                      (cadr bpt)
  132.                                )
  133.                              )
  134.                    )
  135.                  (alert (princ tfw))
  136.                  (alert (strcat "\n!!Error writing file... " tfw))
  137.                )
  138.         )
  139.       )
  140.     )
  141.   )
  142.   (princ)
  143. )
发表于 2015-11-22 16:02 | 显示全部楼层
谢谢楼上,先收藏了.
用AutoDesk Raster Design可在cad中方便的插入对应tfw坐标的图片.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-20 19:51 , Processed in 6.043415 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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