明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: wudechao

[源码] 把参照绝对路径改为相对路径

    [复制链接]
 楼主| 发表于 2015-11-13 12:47 | 显示全部楼层
spp_wall 发表于 2014-7-23 11:35
怎么08也不能用!!!!!

不好意思,ffg-getfolder函数复制少了一段代码.现在可以用了.
 楼主| 发表于 2015-11-13 12:58 | 显示全部楼层
clinber 发表于 2015-2-10 15:11
命令: (LOAD "C:/Users/Administrator/Desktop/xl/参照.lsp") ; 错误: 输入的列表有缺陷

重要事情说三遍,不好意思,ffg-getfolder函数复制少了一段代码.现在可以用了.
发表于 2015-11-16 18:34 | 显示全部楼层
wudechao 发表于 2015-11-13 12:58
重要事情说三遍,不好意思,ffg-getfolder函数复制少了一段代码.现在可以用了.

谢谢大神啊
发表于 2015-11-16 18:42 | 显示全部楼层
wudechao 发表于 2015-11-13 12:58
重要事情说三遍,不好意思,ffg-getfolder函数复制少了一段代码.现在可以用了.

谢谢楼主  我最近也搞了一个 不过貌似没你这个好
  1. ;;;****************************************
  2. ;;; No.1  外部参照绝对路径改为相对路径
  3. ;;;****************************************

  4. ;;-------------=={ Full Path to Relative Path }==-------------;;
  5. ;;                                                            ;;
  6. ;;  Converts a Full XRef path to a Relative Path.             ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  9. ;;------------------------------------------------------------;;
  10. ;;  Arguments:                                                ;;
  11. ;;  dir  - Directory of the Drawing in which the Xref resides ;;
  12. ;;  path - Full Xref Path                                     ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Returns:  Relative XRef Path                              ;;
  15. ;;------------------------------------------------------------;;
  16. (defun LM:XRef:Full->Relative ( dir path / p q )
  17.         (setq dir (vl-string-right-trim "\" dir))
  18.         (cond
  19.                 (   (and
  20.                                         (setq p (vl-string-position 58  dir))
  21.                                         (setq q (vl-string-position 58 path))
  22.                                         (not (eq (strcase (substr dir 1 p)) (strcase (substr path 1 q))))
  23.                                 )
  24.                         path
  25.                 )
  26.                 (   (and
  27.                                         (setq p (vl-string-position 92  dir))
  28.                                         (setq q (vl-string-position 92 path))
  29.                                         (eq (strcase (substr dir 1 p)) (strcase (substr path 1 q)))
  30.                                 )
  31.                         (LM:Xref:Full->Relative (substr dir (+ 2 p)) (substr path (+ 2 q)))
  32.                 )
  33.                 (   (and
  34.                                         (setq q (vl-string-position 92 path))
  35.                                         (eq (strcase dir) (strcase (substr path 1 q)))
  36.                                 )
  37.                         (strcat ".\" (substr path (+ 2 q)))
  38.                 )
  39.                 (   (eq "" dir)
  40.                         path
  41.                 )
  42.                 (   (setq p (vl-string-position 92 dir))
  43.                         (LM:Xref:Full->Relative (substr dir (+ 2 p)) (strcat "..\" path))
  44.                 )
  45.                 (   (LM:Xref:Full->Relative "" (strcat "..\" path)))
  46.         )
  47. )

  48. ;;-------------=={ Relative Path to Full Path }==-------------;;
  49. ;;                                                            ;;
  50. ;;  Converts a Relative XRef path to a Full Path.             ;;
  51. ;;------------------------------------------------------------;;
  52. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  53. ;;------------------------------------------------------------;;
  54. ;;  Arguments:                                                ;;
  55. ;;  dir  - Directory of the Drawing in which the Xref resides ;;
  56. ;;  path - Relative Xref Path                                 ;;
  57. ;;------------------------------------------------------------;;
  58. ;;  Returns:  Full XRef Path                                  ;;
  59. ;;------------------------------------------------------------;;

  60. (defun LM:XRef:Relative->Full ( dir path )
  61.         (setq dir (vl-string-right-trim "\" dir))
  62.         (cond
  63.                 (   (eq ".." (substr path 1 2))
  64.                         (LM:XRef:Relative->Full
  65.                                 (substr dir 1 (vl-string-position 92 dir nil t))
  66.                                 (substr path 4)
  67.                         )
  68.                 )
  69.                 (   (eq "." (substr path 1 1))
  70.                         (strcat dir (substr path 2))
  71.                 )
  72.                 (   (strcat dir "\" path))
  73.         )
  74. )

  75. (defun C:ChXrefPath (/ refblkent refblkfile refblkfilenew refblkfileold refblkname refblknametemp refblkpath refblkpathtemp reffilepath refnamenew)
  76.   (vl-load-com)
  77.   (setvar "cmdecho" 0)
  78.   (setq refFilePath (getvar "DWGPREFIX"))     ; 读取当前图纸路径
  79.   (while (setq refBlkEnt (tblnext "Block" (not refBlkEnt)))
  80.     (setq refBlkName (cdr (assoc 2 refBlkEnt)))
  81.                 (setq refBlkFile (cdr (assoc 1 refBlkEnt)))
  82.           (if        (and refBlkFile (/= refBlkFile ""))
  83.                         (progn
  84.                                 (setq refBlkNameTemp (vl-filename-base refBLKFile))
  85.                                 (setq refBlkPath (vl-filename-directory refBLKFile))
  86.                                 (if (/= refBlkNameTemp refBlkName)                               
  87.                                         (if (= 46 (ascii refBlkPath))
  88.                                                 (progn
  89.                                                         (setq refBlkPathTemp (LM:XRef:Relative->Full refFilePath refBlkPath))
  90.                                                         (setq refBlkFileOld (strcat refBlkPathTemp "\" refBlkNameTemp ".dwg"))
  91.                                                         (setq refBlkFileNew (strcat refBlkPathTemp "\" refBlkName ".dwg"))
  92.                                                         (vl-file-rename refBlkFileOld refBlkFileNew)
  93.                                                         (setq refNameNew (LM:XRef:Full->Relative refFilePath refBlkFileNew))
  94.                                                         (print refNameNew)
  95.                                                         (if (findfile refNameNew)
  96.                                                                 (progn
  97.                                                                         (vl-cmdf "-xRef" "P" refBlkName refNameNew)
  98.                                                                         (princ (strcat "\n " refBlkName " 修改了"))
  99.                                                                         (princ)
  100.                                                                 )
  101.                                                         );end_if
  102.                                                 )               
  103.                                                 (progn
  104.                                                         (setq refBlkFileNew (strcat refBlkPath "\" refBlkName ".dwg"))
  105.                                                         (vl-file-rename refBlkFile refBlkFileNew)
  106.                                                         (setq refNameNew (LM:XRef:Full->Relative refFilePath refBlkFileNew))
  107.                                                         (print refNameNew)
  108.                                                         (if (findfile refNameNew)
  109.                                                                 (progn
  110.                                                                         (vl-cmdf "-xRef" "P" refBlkName refNameNew)
  111.                                                                         (princ (strcat "\n " refBlkName " 修改了"))
  112.                                                                         (princ)
  113.                                                                 )
  114.                                                         );end_if
  115.                                                 );end_progn       
  116.                                         )
  117.                                         (if (/= 46 (ascii refBlkPath))
  118.                                                 (progn
  119.                                                         (setq refNameNew (LM:XRef:Full->Relative refFilePath refBlkFile))
  120.                                                         (print refNameNew)
  121.                                                         (if (findfile refNameNew)
  122.                                                                 (progn
  123.                                                                         (vl-cmdf "-xRef" "P" refBlkName refNameNew)
  124.                                                                         (princ (strcat "\n " refBlkName " 修改了"))
  125.                                                                         (princ)
  126.                                                                 )
  127.                                                         );end_if
  128.                                                 );end_progn
  129.                                         )
  130.                                 )
  131.                         )
  132.                 );end_if
  133.         );end_while
  134.         (princ)
  135. )
发表于 2016-4-3 09:30 | 显示全部楼层
最好能提供对话框输入,对话框默认输入为:.\\或者..\\
发表于 2016-4-3 09:31 | 显示全部楼层
这个功能不错,得顶起来。
发表于 2016-4-24 01:54 | 显示全部楼层
这个功能不错,得顶起来。
 楼主| 发表于 2016-6-21 22:57 | 显示全部楼层
本帖最后由 wudechao 于 2016-6-21 23:57 编辑

;20160621修改版本
(defun c:xfx (/ *error* ss ffg k path files path2 path3 m m2 n n2 total tst temp xpath i obj)
  (vl-load-com)
  (defun ffg-getfolder (msg startpath / winshell shfolder ffg path1 catchit)
    (setq winshell (vlax-create-object "Shell.Application"))
    (setq shfolder (vlax-invoke-method winshell 'browseforfolder 0 msg 1 startpath))
    (setq catchit (vl-catch-all-apply '(lambda ()
                                         (setq shfolder (vlax-get-property shfolder 'self))
                                         (setq path1 (vlax-get-property shfolder 'path))
                                       )
                  )
    )
    (if (vl-catch-all-error-p catchit)
      nil
      path1
    )
  )
  (setvar "cmdecho" 0);主程序
  (defun *error* (msg)
    (if (> total 0)
      (prompt (strcat "\n总共有 \" " (itoa total) " \"个参照物体路径改为相对路径."))
      (prompt (strcat "\n未找到合适的参照物体路径!"))
    );_ 结束if
    (setvar "cmdecho" 1)
    (princ)
  );_结束error
  (setq tst nil
        m 0
        ss (ssget "x" '((0 . "insert")))
  )
  (setq ffg (getvar "dwgprefix"));获取当前文件路径,
  (setq k (vl-string-position 92 ffg 3 nil));查找第二个"\",减少选择路径对话框展开层数(删除硬盘符号和第一级目录)
  (setq path (ffg-getfolder "请选择参照文件所在的目录:" (substr ffg 1 k)));获取参照目录
  (setq files (vl-directory-files path "*.dwg" 1));列出目录下所有文件
  (setq m (vl-string-mismatch ffg path 0 0));前面有多少个相同的字符
  (if (>= m (strlen ffg))
    (progn
      (setq tst t)
    );_ 结束prgon
    (progn;参照文件路径位于该文件父目录之上(就是参照路径比文件所在路径还短)
      (setq n 0
            n2 0
            path3 ""
      );参照路径前面相同部分
      (setq path2 (substr ffg (1+ m)));删除参照路径前面相同部分
      (setq m2 (strlen path2))
      (repeat m2
        (setq temp (ascii (substr path2 (- m2 n) 1)))
        (if (= temp 92)
          (setq n2 (+ 1 n2))
        );_ 结束if
        (setq n (+ 1 n))
      );_ 结束repeat
      (repeat n2
        (setq path3 (strcat ".." (chr 92) path3))
      );_ 结束repeat
    );_ 结束progn
  );_ 结束if
  (setq xpath (substr path (1+ m)));删除参照前面的绝对路径,
  (setq i 0
        total 0
  )
  (repeat (sslength ss)
    (setq obj (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) (cdr (assoc 2 (entget
                                                                                                               (ssname ss i)
                                                                                                       )
                                                                                              )
                                                                                         )
              )
    )
    (if (and
          (= (vla-get-isxref obj) :vlax-true)
          (member (strcat (vla-get-name obj) ".dwg") files);参照图块是否在文件列表
        )
      (progn
        (if (= xpath "")
          (progn
            (vla-put-path obj (strcat "." (chr 92) xpath (vla-get-name obj) ".dwg"))
          );_ 结束progn
          (progn
            (if tst
              (vla-put-path obj (strcat "." (chr 92) xpath (chr 92) (vla-get-name obj) ".dwg"));目录前面添加".\",目录后面添加"\"
              (vla-put-path obj (strcat path3 xpath (chr 92) (vla-get-name obj) ".dwg"));
            );_ 结束if
          );_ 结束progn
        );_ 结束if
        (setq total (+ 1 total))

      );_ 结束progn
    );_ 结束if
    (setq i (1+ i))
  );_ 结束repeat
  (setvar "cmdecho" 0)
  (command "-xref" "r" "*")
  (if (> total 0)
    (prompt (strcat "\n总共有 \" " (itoa total) " \"个参照物体路径改为相对路径."))
    (prompt (strcat "\n未找到合适的参照物体路径!"))
  );_ 结束if
  (setvar "cmdecho" 1)
  (princ)
);_ 结束defun

评分

参与人数 1金钱 +6 收起 理由
utopio + 6

查看全部评分

发表于 2016-11-4 13:45 | 显示全部楼层
wudechao 发表于 2016-6-21 22:57
;20160621修改版本
(defun c:xfx (/ *error* ss ffg k path files path2 path3 m m2 n n2 total tst temp  ...

这个版本测试了,完全OK。上一版改了之后重新打开就不能找到外部参照了。
2个版本各有用处,都不错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-17 13:08 , Processed in 0.163248 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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