乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 122|回复: 19

[编程交流] 导出文本

[复制链接]

8

主题

87

帖子

87

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 15:49:56 | 显示全部楼层 |阅读模式
这里是另一个从旧论坛上弹出的晦涩中拯救出来的宝石。
 
您希望从图形中输出文本。用这一点
AutoLISP应用程序。该应用程序将帮助您
达到理想的结果
 
  1. [color=blue](defun c:txtex (/ et)[/color]
  2. [color=blue](setq fl (open "dtext.txt" "w")[/color]
  3. [color=blue]et (entnext)[/color]
  4. [color=blue])[/color]
  5. [color=blue](while et[/color]
  6. [color=blue](setq el (entget et)[/color]
  7. [color=blue]tp (cdr (assoc 0 el))[/color]
  8. [color=blue])[/color]
  9. [color=blue](if (or (= tp "TEXT") (= tp "MTEXT"))[/color]
  10. [color=blue](write-line (cdr (assoc 1 el)) fl)[/color]
  11. [color=blue])[/color]
  12. [color=blue](setq et (entnext et))[/color]
  13. [color=blue])[/color]
  14. [color=blue](close fl)[/color]
  15. [color=blue])[/color]

 
将程序复制到记事本中打开的文件中。然后
将其保存为TXTEX。LSP。下一次加载
AutoCAD并运行它。
 
要运行程序,请在命令处键入TXTEX
促使这就是全部。创建的文本文件包含
在图纸中找到的所有文本。
 
您可以将文本文件插入Microsoft Excel或
Microsoft Word。我相信你知道怎么做。
否则请回到我身边。
 
等一下。你说的是100幅画
文本你想一次完成吗?如果是这样,我们必须
对程序进行一些修改。
 
您还需要一个脚本文件。脚本文件运行并
启动程序。每次创建文本文件时。
让我知道你想要什么。
 
Jos van Doorn。AutoCAD专家和AutoLISP
程序员同时出版ACAD时事通讯。免费。到
订阅发送空白电子邮件至:
acadnewsletter公司-subscribe@topica.com
回复

使用道具 举报

18

主题

434

帖子

422

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
94
发表于 2022-7-5 15:55:51 | 显示全部楼层
不错的节目。文本按创建顺序放置在文本文件中,忽略屏幕位置。如果您希望控制订单,或者当您不�如果不需要从DWG中导出所有文本,可以尝试以下程序。您可能需要使用其他应用程序编辑文本文件。
重要提示:您选择的(M)文本将被删除,因此请操作DWG文件的副本!
 
 
  1. (defun C:TEX()
  2. ;move selected text to file
  3. (alert "I hope you have a copy of your DWG!")
  4. (setq userfile (open "test13.txt" "w"));You
  5.              ;may change the name of the text file
  6. (setq txt (entsel "select (m)text"))
  7. (while txt
  8.    (setq e (entget (car txt)))
  9.    (setq x nil line "")
  10.    (setq x (member (assoc 3 e) e))
  11.    (while x
  12.      (setq line (cdr (assoc 3 x)))
  13.      (write-line line userfile)
  14.      (setq x (cdr x))
  15.      (setq x (member (assoc 3 x) x)))
  16.    (setq line (cdr (assoc 1 e)))
  17.    (write-line line userfile)
  18.    (command "erase" txt "")
  19.    (setq txt(entsel "\nnext (m)text (Enter for terminate)")))
  20. (close userfile))
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-5 15:56:53 | 显示全部楼层
  1. (defun c:txtex (/ et)
  2. (setq fl (open "dtext.txt" "w")
  3. et (entnext)
  4. )
  5. (while et
  6. (setq el (entget et)
  7. tp (cdr (assoc 0 el))
  8. )
  9. (if (or (= tp "TEXT") (= tp "MTEXT"))
  10. (write-line (cdr (assoc 1 el)) fl)
  11. )
  12. (setq et (entnext et))
  13. )
  14. (close fl)
  15. )

 
我用这个lisp,它过去很管用。
但现在,当我运行它时,当我打开dtext时。txt文件它是空的。
什么会导致这种情况发生?
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-5 16:00:48 | 显示全部楼层
 
 
可爱的小节目,干杯!
有没有办法指定程序将在其上操作的层或使其仅在当前层上操作?
回复

使用道具 举报

9

主题

49

帖子

41

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-5 16:05:09 | 显示全部楼层
试试这个:
  1. (defun c:txtex (/ file,en,entity,current_layer,entity_layer)
  2. (setq    file (open "dtext.txt" "w")
  3.    en   (entnext)
  4. )
  5. (setq current_layer (getvar 'clayer))
  6. (while en
  7.    (setq entity       (entget en)
  8.      text           (cdr (assoc 0 entity))
  9.      entity_layer (cdr (assoc 8 entity))
  10.    )
  11.    (if    (and (or (= text "TEXT")
  12.         (= text "MTEXT")
  13.         )
  14.         (= entity_layer current_layer)
  15.    )
  16.      (write-line (cdr (assoc 1 entity)) file)
  17.    )
  18.    (setq en (entnext en))
  19. )
  20. (close file)
  21. )
应适用于当前层
普尔泽莫
回复

使用道具 举报

10

主题

253

帖子

75

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
223
发表于 2022-7-5 16:07:30 | 显示全部楼层
如果您使用的是Autocad 2002及更高版本,并且希望获得完整的多行文字字符串(不带格式),则可以尝试使用以下内容:
代码擦除'cos包含一些错误(thnx到ASMI)此代码工作正常(我希望):
  1. (defun c:text-exp (/                      *error*
  2.                   file                   selset
  3.                   file_handle            kpblc-string-mtext-unformat
  4.                   _kpblc-string-replace  _kpblc-string-cut-between
  5.                   bylayer
  6.                   )
  7. (defun _kpblc-string-cut-between (str s1 s2 reg / tmp substring)
  8.    (setq tmp       (if s1
  9.                      (kpblc-string-find-substr-pass str s1 reg 0)
  10.                      1
  11.                      ) ;_ end of if
  12.          substring (kpblc-string-find-substr-pass str s2 reg tmp)
  13.          ) ;_ end of setq
  14.    (if (and (or s1 s2) tmp substring)
  15.      (substr
  16.        str
  17.        tmp
  18.        (if (and s2 tmp)
  19.          (1+ (- (kpblc-string-find-substr-pass str s2 reg tmp)
  20.                 tmp
  21.                 ) ;_ end of -
  22.              ) ;_ end of 1+
  23.          ) ;_ end of if
  24.        ) ;_ end of substr
  25.      ""
  26.      ) ;_ end of if
  27.    ) ;_ end of defun
  28. (defun *error* (msg)
  29.    (vl-catch-all-apply '(lambda () (close file_handle)))
  30.    (princ msg)
  31.    (princ)
  32.    ) ;_ end of defun
  33. (defun _kpblc-string-replace (string old_substr new_substr / pos)
  34.    (while (setq pos (vl-string-search old_substr string))
  35.      (setq string
  36.             (strcat
  37.               (substr string 1 pos)
  38.               new_substr
  39.               (_kpblc-string-replace
  40.                 (substr string (+ (strlen old_substr) pos 1))
  41.                 old_substr
  42.                 new_substr
  43.                 ) ;_ end of _kpblc-string-replace
  44.               ) ;_ end of strcat
  45.            ) ;_ end of setq
  46.      ) ;_ end of while
  47.    string
  48.    ) ;_ end of defun
  49. (defun kpblc-string-mtext-unformat (ent
  50.                                      /
  51.                                      _tmp
  52.                                      _substr
  53.                                      _mtext-str-extractor-clr
  54.                                      _mtext-str-extractor-srch
  55.                                      )
  56.    (defun _mtext-str-extractor-clr (str / _pos)
  57.      (if (setq _pos (_mtext-str-extractor-srch
  58.                       str
  59.                       '("{\" "\\f" "\\F")
  60.                       ) ;_ end of _mtext-str-extractor-srch
  61.                ) ;_ end of setq
  62.        (strcat
  63.          (if (> _pos 0)
  64.            (substr str 1 _pos)
  65.            ""
  66.            ) ;_ end of if
  67.          (_mtext-str-extractor-clr
  68.            (substr
  69.              str
  70.              (+ 2 (vl-string-search ";" str (1+ _pos)))
  71.              ) ;_ end of substr
  72.            ) ;_ end of _mtext-str-extractor-clr
  73.          ) ;_ end of strcat
  74.        str
  75.        ) ;_ end of if
  76.      ) ;_ end of defun
  77.    (defun _mtext-str-extractor-srch (str lst / _tmp)
  78.      (car (vl-sort
  79.             (vl-remove-if
  80.               'not
  81.               (mapcar (function (lambda (_x _y)
  82.                                   (vl-string-search _y _x)
  83.                                   ) ;_ end of lambda
  84.                                 ) ;_ end of function
  85.                       (repeat (length lst)
  86.                         (setq _tmp (cons str _tmp))
  87.                         ) ;_ end of repeat
  88.                       lst
  89.                       ) ;_ end of mapcar
  90.               ) ;_ end of vl-remove-if
  91.             '<
  92.             ) ;_ end of vl-sort
  93.           ) ;_ end of car
  94.      ) ;_ end of defun
  95.    (setq
  96.      _tmp (vl-string-subst
  97.             ""
  98.             "}"
  99.             (_mtext-str-extractor-clr
  100.               (_kpblc-string-replace
  101.                 (_kpblc-string-replace
  102.                   (_kpblc-string-replace
  103.                     (_kpblc-string-replace
  104.                       (_kpblc-string-replace
  105.                         (_kpblc-string-replace
  106.                           ent
  107.                           "\\\"
  108.                           ""
  109.                           ) ;_ end of _kpblc-string-replace
  110.                         "\\{"
  111.                         (chr 1)
  112.                         ) ;_ end of _kpblc-string-replace
  113.                       "\\}"
  114.                       (chr 2)
  115.                       ) ;_ end of _kpblc-string-replace
  116.                     "\\P"
  117.                     "\n"
  118.                     ) ;_ end of _kpblc-string-replace
  119.                   "\\L"
  120.                   ""
  121.                   ) ;_ end of _kpblc-string-replace
  122.                 "\\l"
  123.                 ""
  124.                 ) ;_ end of _kpblc-string-replace
  125.               ) ;_ end of _mtext-str-extractor-clr
  126.             ) ;_ end of vl-string-subst
  127.      ) ;_ end of setq
  128.    (while
  129.      (and (setq _substr (_kpblc-string-cut-between _tmp "\" ";" nil))
  130.           (/= _substr "")
  131.           ) ;_ end of and
  132.       (setq _tmp (vl-string-subst "" _substr _tmp))
  133.       ) ;_ end of while
  134.    (vl-string-subst "}" (chr 2) (vl-string-subst "{" (chr 1) _tmp))
  135.    _tmp
  136.    ) ;_ end of defun
  137. (vl-load-com)
  138. (if (and (setq file (getfiled "Enter a new export file name" "" "txt" 1))
  139.           (setq selset (ssget
  140.                          (if (= (setq bylayer
  141.                                        ((lambda ()
  142.                                           (initget "Yes No _ Y N")
  143.                                           (getkword
  144.                                             "\nSelect by current layer [Yes/No] <No> : "
  145.                                             ) ;_ end of getkword
  146.                                           ) ;_ end of lambda
  147.                                         )
  148.                                       ) ;_ end of setq
  149.                                 "Y"
  150.                                 ) ;_ end of =
  151.                            (list (cons 0 "*TEXT") (cons 8 (getvar "clayer")))
  152.                            (list (cons 0 "*TEXT"))
  153.                            ) ;_ end of if
  154.                          ) ;_ end of ssget
  155.                 ) ;_ end of setq
  156.           ) ;_ end of and
  157.    (progn
  158.      (setq file_handle (open file "w"))
  159.      (foreach item
  160.               (mapcar 'vlax-ename->vla-object
  161.                       (vl-remove-if 'listp (mapcar 'cadr (ssnamex selset)))
  162.                       ) ;_ end of mapcar
  163.        (write-line
  164.          (vl-string-translate
  165.            "\n"
  166.            " "
  167.            (vl-string-translate "\\P" " " (vla-get-textstring item))
  168.            ) ;_ end of VL-STRING-TRANSLATE
  169.          file_handle
  170.          ) ;_ end of write-line
  171.        ) ;_ end of foreach
  172.      (close file_handle)
  173.      ) ;_ end of progn
  174.    ) ;_ end of if
  175. ) ;_ end of defun
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-5 16:11:29 | 显示全部楼层
这是一个有一些选项的快捷方式。
  1. ;;  TextOut.lsp by CAB
  2. ;;  Version 1  01/26/07
  3. (defun c:TextOut()
  4. (TextOutSub (+ 1 2 16) nil) ; get text & mText & Strip
  5. (princ)
  6. )
  7. ;;  Dump text strings in drawing to a text file
  8. ;;  Output File name -> <DWG filename> + "-OUT.TXT"
  9. ;;  Flags to filter object Type
  10. ;;  Layer Name   nil = any layer
  11. (defun TextOutSub(flag lname / fl ent)
  12. (vl-load-com)
  13. ;;  Flags
  14. ;;  1  Text
  15. ;;  2  MText
  16. ;;  4  Attributes
  17. ;;  8  Attribute Definition
  18. ;;  16 Strip Text Format characters
  19. ;;  32
  20. ;;  lname   if nil use any layer
  21. ;;++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22. ;;  test ename, return objtype if correct type else nil
  23. ;;++++++++++++++++++++++++++++++++++++++++++++++++++++++
  24. (defun is_text (ename / obj typ)
  25.    (if
  26.      (setq typ
  27.        (assoc
  28.          (vla-get-objectname (setq obj (vlax-ename->vla-object ename)))
  29.          '(("AcDbText" . 1) ("AcDbMText" . 2) ("AcDbAttribute" . 4) ("AcDbAttributeDefinition" . )
  30.        )
  31.      )
  32.       (cons obj (cdr typ))
  33.    )
  34. )
  35. (setq fname (strcat (getvar "dwgprefix")
  36.                      (vl-filename-base (getvar "dwgname"))
  37.                      "-OUT.TXT"
  38.       ))
  39. (setq fl (open fname "w"))
  40. (if lname
  41.    (progn
  42.      (write-line (strcat "***  Filtered by Layer " lname " ***") fl)
  43.      (setq lname (strcase lname))
  44.    )
  45. )
  46. (while (setq ent (if ent (entnext ent)(entnext)))
  47.    (if (and (setq source (is_text ent))
  48.             (> (logand (cdr source) flag) 0)
  49.             (or (null lname)
  50.                 (= (strcase (vla-get-layer (car source))) lname)
  51.             ))
  52.      (progn
  53.        (setq TextSource (vla-get-textstring (car source)))
  54.        (and (> (logand 16 flag) 0) (setq TextSource (strip_text TextSource)))
  55.        (write-line (strcat "\n<---->  " (substr (vla-get-objectname (car source)) 5)
  56.                            "\n" TextSource) fl)
  57.      )
  58.    )
  59. )
  60. (close fl)
  61. (princ)
  62. )
  63. ;;;=======================[ Strip_Text.lsp ]=============================
  64. ;;; Author:  Charles Alan Butler Copyright© 2005
  65. ;;; Version: 2.2  Oct. 19, 2005
  66. ;;; Purpose: Strip format characters from text or mtext
  67. ;;; Returns: A string  
  68. ;;; Sub_Routines: -None
  69. ;;; Arguments: A string variable
  70. ;;;======================================================================
  71. (defun strip_text (str / skipcnt ndx newlst char fmtcode lst_len
  72.                   IS_MTEXT LST  NEXTCHR PT TMP)
  73. (setq ndx 0
  74.        ;; "fmtcode" is a list of code flags that will end with ;
  75.        fmtcode
  76.         (vl-string->list "CcFfHhTtQqWwAa") ;("\C" "\F" "\H" "\T" "\Q" "\W" "\A")
  77. )
  78. (if (/= str "") ; skip if empty text ""
  79.    (progn
  80.      (setq lst      (vl-string->list str)
  81.            lst_len  (length lst)
  82.            newlst   '()
  83.            is_mtext nil ; true if mtext
  84.      )
  85.      (while (< ndx lst_len)
  86.        ;; step through text and find FORMAT CHARACTERS
  87.        (setq char    (nth ndx lst) ; Get next character
  88.              nextchr (nth (1+ ndx) lst)
  89.              skipcnt 0
  90.        )
  91.        (cond
  92.          ((and (= char 123) (= nextchr 92)) ; "{" mtext code
  93.           (setq is_mtext t
  94.                 skipcnt 1
  95.           )
  96.          )
  97.          ((and (= char 125) is_mtext) ; "}"
  98.           (setq skipcnt 1)
  99.          )
  100.          ((= char 37) ; code start with "%"
  101.           (if (null nextchr) ; true if % is last char in text
  102.             (setq skipcnt 1)
  103.             ;;  Dtext codes
  104.             (if (= nextchr 37) ; %% code found
  105.               (if (< 47 (nth (+ ndx 2) lst) 58) ; is a number
  106.                 ;;number found so fmtcode %%nnn
  107.                 (setq skipcnt 5)
  108.                 ;; else letter code, so fmtcode %%p, %%d, %%c
  109.                 ;;  CAB note - this code does not always exist in the string
  110.                 ;;  it is used to create the character but the actual ascii code
  111.                 ;;  is used in the string, not the case for %%c
  112.                 (setq skipcnt 3)
  113.               ) ; endif
  114.             ) ; endif
  115.           ) ; endif
  116.          ) ; end cond (= char "%"))
  117.          ((= char 92) ; code start with ""
  118.           ;;  This section processes mtext codes
  119.           (cond
  120.             ;; Process Coded information
  121.             ((null nextchr) ; true if \ is last char in text
  122.              (setq skipcnt 1)
  123.             ) ; end cond 1
  124.             ((member nextchr fmtcode) ; this code will end with ";"
  125.              ;; fmtcode -> ("\C" "\F" "\H" "\T" "\Q" "\W" "\A"))
  126.              (while (/= (setq char (nth (+ skipcnt ndx) lst)) 59)
  127.                (setq skipcnt (1+ skipcnt))
  128.              )
  129.              (setq skipcnt (1+ skipcnt))
  130.             ) ; end cond
  131.             ;; found \U then get 7 character group
  132.             ((= nextchr 85) (setq skipcnt (+ skipcnt 7)))
  133.             ;; found \M then get 8 character group
  134.             ((= nextchr 77) (setq skipcnt (+ skipcnt ))
  135.             ;; found \P then replace with CR LF 13 10
  136.             ;;  debug do not add CR LF, just remobe \P
  137.             ((= nextchr 80) ; "\P"
  138.              (setq newlst  (append newlst '(32))
  139.                    ;ndx     (+ ndx 1)
  140.                    skipcnt 2
  141.              )
  142.             ) ; end cond
  143.             ((= nextchr 123) ; "\{" normal brace
  144.              (setq ndx (+ ndx 1))
  145.             ) ; end cond
  146.             ((= nextchr 125) ; "\}" normal brace
  147.              (setq ndx (+ ndx 1))
  148.             ) ; end cond
  149.             ((= nextchr 126) ; "\~" non breaking space
  150.              (setq newlst (append newlst '(32))) ; " "
  151.              (setq skipcnt 2) ; end cond 9
  152.             )
  153.             ;; 2 character group \L \l \O \o
  154.            ((member nextchr '(76 108 79 111))
  155.              (setq skipcnt 2)
  156.             ) ; end cond
  157.             ;;  Stacked text format as "[ top_txt / bot_txt ]"
  158.             ((= nextchr 83) ; "\S"
  159.              (setq pt  (1+ ndx)
  160.                    tmp '()
  161.              )
  162.              (while
  163.                (not
  164.                  (member
  165.                    (setq tmp (nth (setq pt (1+ pt)) lst))
  166.                    '(94 47 35) ; "^" "/" "#" seperator
  167.                  )
  168.                )
  169.                 (setq newlst (append newlst (list tmp)))
  170.              )
  171.              (setq newlst (append newlst '(47))) ; "/"
  172.              (while (/= (setq tmp (nth (setq pt (1+ pt)) lst)) 59) ; ";"
  173.                (setq newlst (append newlst (list tmp)))
  174.              )
  175.              (setq ndx     pt
  176.                    skipcnt (1+ skipcnt)
  177.              )
  178.             ) ; end cond
  179.           ) ; end cond stmt  Process Coded information
  180.          ) ; end cond  (or (= char "\")
  181.        ) ; end cond stmt
  182.        ;;  Skip format code characters
  183.        (if (zerop skipcnt) ; add char to string
  184.          (setq newlst (append newlst (list char))
  185.                ndx    (+ ndx 1)
  186.          )
  187.          ;;  else skip some charactersPLOTTABS
  188.          (setq ndx (+ ndx skipcnt))
  189.        )
  190.      ) ; end while Loop
  191.    ) ; end progn
  192. ) ; endif
  193. (vl-list->string newlst) ; return the stripped string
  194. ) ; end defun
  195. ;;;======================================================================
回复

使用道具 举报

4

主题

10

帖子

6

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-5 16:15:08 | 显示全部楼层
真 的。。。。。。感谢您的所有选择/支持。我会尝试一下,让大家都知道。
 
再次感谢,
3月5日
回复

使用道具 举报

8

主题

52

帖子

44

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-5 16:17:02 | 显示全部楼层
CAB-这也适用于属性吗?我试过了,但它似乎没有从图形中提取任何属性。我喜欢李的属性提取程序。我想知道是否有任何程序将两者结合起来。这将非常有用。谢谢
 
回复

使用道具 举报

4

主题

10

帖子

6

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-5 16:20:32 | 显示全部楼层
似乎有两种不同的Lisp程序。这是正确的吗?还有,李是谁?我看了看下面,我没有看到李Mac。很抱歉
 
马克
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-14 13:08 , Processed in 1.886989 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表