明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: sctw

[提问] 求“不存在的字体”批量替换程序lsp

  [复制链接]
发表于 2020-8-11 17:27 | 显示全部楼层
本帖最后由 gaics 于 2020-8-12 08:36 编辑

  1. (defun c:tt (/ name D en zt3 zt4 a b c d e f g err)
  2.   (setvar "cmdecho" 0)
  3.   (while (setq D (tblnext "style" (null D)))
  4.     (setq name (cdr (assoc 2 D)))
  5.     ;;;;(alert name)
  6.     (setq zt3 (cdr (assoc 3 D)))
  7.     (setq zt4 (cdr (assoc 4 D)))
  8.     (if        (/= zt3 "")
  9.       (cond
  10.         ((or (= (vl-filename-extension zt3) ".shx")
  11.              (= (vl-filename-extension zt3) ".SHX")
  12.          )
  13.          (if (not (findfile zt3))
  14.            (setq zt3 "hzasc.shx"
  15.                  a   t
  16.            )
  17.          )
  18.         )
  19.         ((null (vl-filename-extension zt3))
  20.          (if
  21.            (findfile (strcat zt3 ".shx"))
  22.             (setq zt3 (strcat zt3 ".shx")
  23.                   a   t
  24.             )
  25.             (setq zt3 "hzasc.shx"
  26.                   a   t
  27.             )
  28.          )
  29.         )
  30.         ((if
  31.            (not (findfile (strcat (getenv "windir") "\\fonts\\" zt3)))
  32.             (setq zt3 "simkai.ttf"
  33.                   b   t
  34.             )
  35.          )
  36.         )
  37.       )
  38.       (progn
  39.         (setq en (vlax-ename->vla-object (tblobjname "style" name)))
  40.         (vla-getfont en 'c 'd 'e 'f 'g)
  41.         (setq
  42.           err (vl-catch-all-apply 'vla-setfont (list en c d e f g))
  43.         )
  44.         (if (vl-catch-all-error-p err)
  45.                  (vla-setfont en "楷体" d e f g)
  46.         )
  47.       )
  48.     )
  49.     (if        (/= zt4 "")
  50.       (if (vl-filename-extension zt4)
  51.         (if (not (findfile zt4))
  52.           (setq        zt4 "hztxt.shx"
  53.                 a   t
  54.           )
  55.         )
  56.         (if (findfile (strcat zt4 ".shx"))
  57.           (setq        zt4 (strcat zt4 ".shx")
  58.                 a   t
  59.           )
  60.           (setq        zt4 "hztxt.shx"
  61.                 a   t
  62.           )
  63.         )
  64.       )
  65.     )
  66.     (if        a
  67.       (command "-style" name (strcat zt3 "," zt4) "" "" "" "" "" "")
  68.     )
  69.     (if        b
  70.       (command "-style" name zt3 "" "" "" "" "")
  71.     )
  72.     (setq a nil b nil)
  73.   )
  74.   (princ)
  75. )

重新写了一下,感觉这版问题少一些。但是对外部参照缺失的字体会出错。
回复

使用道具 举报

发表于 2020-8-11 17:28 | 显示全部楼层
sctw 发表于 2020-8-11 15:19
这跟用wcmatch有什么区别呢

没有后缀名的情况(vl-filename-extension zt3)返回“nil”,对于wcmatch是无效参数。
回复

使用道具 举报

 楼主| 发表于 2020-8-11 23:48 | 显示全部楼层
gaics 发表于 2020-8-11 17:27
重新写了一下,感觉这版问题少一些。但是对外部参照缺失的字体会出错。

这个版本不能运行成功,每个字体样式都会弹出提示框,命令行也对应提示:字体文件不存在

本帖子中包含更多资源

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

x
回复

使用道具 举报

 楼主| 发表于 2020-8-11 23:59 | 显示全部楼层
我另外想了一个思路,用style命令里能看到的中文字体名来判断是否为windows字体,否则为CAD字体。
         (setvar "textstyle" name)                   ;把字体样式设置为当前样式
         (setq acadObj (vlax-get-acad-object))
         (setq doc (vla-get-ActiveDocument acadObj))
         (vla-GetFont (vla-get-ActiveTextStyle doc) 'typeFace 'Bold 'Italic 'charSet 'PitchandFamily)
            (if (= "" typeFace)                      ;中文字体为空,则属于CAD字体,这样更方便些。

对于有的样式名含有非法字符的,如:* : ; \ / ? "等再用wcmatch来判断
      (if (or (= "" name) ;如果字体样式名为空
              (wcmatch name "*
  • *,*[\\]*,*[/]*,*[?]*,*[`]*,*[<]*,*[>]*,*[:]*,*[;]*,*[|]*,*[,]*,*[=]*,*[\"]*") ;或者字体样式名为非法字符
               ) ;判断字体样式名里含有非法字符
           (princ  (strcat "\n 发现文字样式名<" name ">中含有非法字符,不能处理该样式字体!!!"))

  • 回复

    使用道具 举报

    发表于 2020-8-12 08:13 | 显示全部楼层
    本帖最后由 gaics 于 2020-8-12 08:23 编辑
    sctw 发表于 2020-8-11 23:59
    我另外想了一个思路,用style命令里能看到的中文字体名来判断是否为windows字体,否则为CAD字体。
            ...

    你可以按你的思路尝试写一下
    前面那个提示框可以把(alert name)这句删除。字体不存在就把“楷体”换一个你的系统里有的字体。

    回复

    使用道具 举报

    发表于 2020-8-12 15:15 | 显示全部楼层
    本帖最后由 gaics 于 2020-8-12 19:20 编辑
    1. (defun c:tt (/ a b c d e err)
    2.   (vl-load-com)
    3.   (setvar "cmdecho" 0)
    4.   (command "undo" "be")
    5.   (vlax-for x (vla-get-textstyles
    6.   (vla-get-activedocument (vlax-get-acad-object))
    7.        )
    8.     (vla-getfont x 'a 'b 'c 'd 'e)
    9.     (if (= a "")
    10.       (progn
    11. (if (and (not (findfile (vla-get-fontfile x)))
    12.    (not (findfile (strcat (vla-get-fontfile x) ".shx")))
    13.      )
    14.    (vla-put-fontfile x "hzasc.shx")
    15. )
    16. (if
    17.    (and (/= (vla-get-bigfontfile x) "")
    18.         (not (findfile (vla-get-bigfontfile x)))
    19.         (not (findfile (strcat (vla-get-bigfontfile x) ".shx")))
    20.    )
    21.     (vla-put-bigfontfile x "hztxt.shx")
    22. )
    23.       )
    24.       (progn (setq
    25.         err (vl-catch-all-apply 'vla-setfont (list x a b c d e))
    26.       )
    27.       (if (vl-catch-all-error-p err)
    28.         (vla-setfont x "仿宋" b c d e)
    29.       )
    30.       )
    31.     )
    32.   )
    33.   (command "undo" "e")
    34.   (princ)
    35. )

    我现在测试比较完美了,外部参照也没有问题。判断非法字符感觉没有必要,外部参照的字体样式名称是包含“|”的,代码可以运行。

    评分

    参与人数 2明经币 +2 金钱 +10 收起 理由
    sctw + 1 + 10 赞一个!
    magicheno + 1 很给力!

    查看全部评分

    回复

    使用道具 举报

    发表于 2020-8-12 17:36 | 显示全部楼层
    本帖最后由 magicheno 于 2020-8-12 17:44 编辑
    gaics 发表于 2020-8-12 15:15
    我现在测试比较完美了,外部参照也没有问题。判断非法字符感觉没有必要,外部参照的字体样式名称是包含“ ...

    大爱,非常好用,一直想找这样的功能,非常喜欢,感谢大侠
    回复

    使用道具 举报

     楼主| 发表于 2020-8-12 22:38 | 显示全部楼层
    gaics 发表于 2020-8-12 15:15
    我现在测试比较完美了,外部参照也没有问题。判断非法字符感觉没有必要,外部参照的字体样式名称是包含“ ...

    测试了,非常好,还有一个小疑问:
            (vla-setfont x "仿宋" b c d e);这一句中的“仿宋”我改成其它(比如楷体等)没有作用,都是用FangSong_GB2312.ttf来替换,不知为什么?
    回复

    使用道具 举报

    发表于 2020-8-12 22:40 | 显示全部楼层
    楷体可以啊,我最早就是用楷体测试的。什么系统?多少位?
    回复

    使用道具 举报

     楼主| 发表于 2020-8-12 22:51 | 显示全部楼层
    sctw 发表于 2020-8-12 22:38
    测试了,非常好,还有一个小疑问:
            (vla-setfont x "仿宋" b c d e);这一句中的“仿宋”我改成 ...

    是我自已搞错了,是可以变化的
    回复

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2024-4-28 14:25 , Processed in 0.479028 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

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