CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoLISP 开发指南

示例:将文本字符串从 MText 对象复制到 Microsoft Word 文档 (AutoLISP/ActiveX)

2023-1-6 00:55| 发布者: admin| 查看: 513| 评论: 0|来自: AutoCAD

摘要: 下面的示例创建一个适用于 Microsoft Word 2016 的 AutoLISP 应用程序和一个包含 MText 的 AutoCAD 绘图。

下面的示例创建一个适用于 Microsoft Word 2016 的 AutoLISP 应用程序和一个包含 MText 的 AutoCAD 绘图。

注意:AutoLISP 中的 ActiveX 支持仅限于 Windows。

创建一个新的 AutoLISP 程序并加载 ActiveX 支持函数

  1. 在 Visual LISP 中,单击“文件新建文件”。
  2. 单击文件另存为。
  3. 在“另存为”对话框中,
    • 指定新的 AutoLISP 源文件的位置。
    • 在“文件名”框中,输入copyMText2Word
    • 在“保存类型”下拉列表中,选择“Lisp 源文件”。
    • 单击保存。
  4. 在新的 Visual LISP 文本编辑器窗口中,输入 (vl-load-com)。

获取 AutoCAD 应用程序和当前模型空间对象并存储其指针

  • 在 Visual LISP 文本编辑器窗口中,输入
    (defun c:copyMText2Word ( / )
      (setq *AcadApp* (vlax-get-acad-object)) ; Get AutoCAD application
      (setq *ModelSpace* (vla-get-ModelSpace
      (vla-get-ActiveDocument *AcadApp*))) ; Get model space

导入 Microsoft Word 类型库

更改以下代码中的 :tlb-filename 参数以指向系统上的msword.olb文件。

  • 在 Visual LISP 文本编辑器窗口中,输入
      (if (equal nil mswc-wd160Words) ; check for a Word constant
        (vlax-import-type-library
          :tlb-filename "C:/Program Files (x86)/Microsoft Office/root/Office16/msword.olb"
          :methods-prefix "mswm-"
          :properties-prefix "mswp-"
          :constants-prefix "mswc-"
        )
      )

    此代码首先检查是否使用值定义了已知的 Microsoft Word 常量。如果常量具有值,则假定 Word 类型库已导入,无需进一步操作。如果变量为 ,则被调用。nilvlax-import-type-library

建立与微软 Word 应用程序的连接

  • 在 Visual LISP 文本编辑器窗口中,输入
      (setq msw (vlax-get-object "Word.Application.16"))
      (if (equal nil msw)
        (progn
          ; Word is not running. Start it.
          (setq msw (vlax-create-object "Word.Application.16"))
          (vla-put-visible msw :vlax-true)
        )
      )

    代码问题建立与正在运行的 Microsoft Word 应用程序的连接。(在此示例中,指定了版本 16(Word 2016);如果省略这 16,则将接受 Word 的任何实例。如果没有正在运行的 Word 实例,则发出以启动一个实例。vlax-get-objectvlax-create-object

在模型空间中处理 MText 对象

  • 在 Visual LISP 文本编辑器窗口中,输入
      (if (/= nil msw)
        (progn
          ;; Get the document collection object.
          (setq docs (vla-get-documents msw))
    
          ;; Add a new document
          (setq doc (mswm-add docs))
    
          ;; Get the paragraphs of the document (to do some formatting)
          (setq paragraphs (mswp-get-paragraphs doc))
    
          ;; Now iterate through the model space and export any mtext
          ;; every Mtext entity to Word.
          (vlax-for ent *ModelSpace*
            (if (equal (vla-get-ObjectName ent) "AcDbMText")
              (progn
                ;; Get the following information from the Mtext entity:
                ;; o the text string
                ;; o the location of a corner of the text boundary
                (setq text (vla-get-TextString ent)
                      textpos (vla-get-InsertionPoint ent)
                      arrayTextpos (vlax-variant-value textpos)
                      textinfo
                        (strcat
                           (rtos (vlax-safearray-get-element arrayTextpos 0) 2 2)
                           ", "
                           (rtos (vlax-safearray-get-element arrayTextpos 1) 2 2)
                           ", "
                           (rtos (vlax-safearray-get-element arrayTextpos 2) 2 2)
                         )
                ) ;_ end of setq
    
                ; Print some info (with formatting)
                ; Get the last paragraph in the document
                (setq pg (mswp-get-last paragraphs))
      
                ; Obtain the range of the paragraph
                (setq range (mswp-get-range pg))
    
                ; Do some formatting
                (mswp-put-bold range 1) ;bold
                (mswp-put-underline range mswc-wdUnderlineSingle) ;underline
    
                ; Insert info about the text at the end of the paragraph
                (mswm-InsertAfter range
                (strcat "AcDbMText at position " textinfo "\n"))
    
                ; Now show the text string (from the ACAD text entity)
                (setq pg (mswp-get-last paragraphs))
                (setq range (mswp-get-range pg))
                (mswp-put-bold range 0)
                (mswp-put-underline range mswc-wdUnderlineNone)
                (mswm-InsertAfter range (strcat text "\n\n"))
              ) ;_ end of progn
            ) ;_ end of if AcDbMText
          ) ;_ end of vlax-for
         ) ;_ end of progn
        (princ "\nNo Microsoft Word application found.\n")
      )
    ) ;_ end of defun

加载并运行代码

  1. 在 Visual LISP 中,单击“工具在编辑器中加载文本”。
  2. 在AutoCAD命令提示下,输入copymtext2word

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 12:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部