次に示す例は、Microsoft Word 2010 とともに動作する AutoLISP アプリケーションとマルチ テキストを含む AutoCAD 図面を作成します。
[ファイルを新規作成]をクリックします。
[別名で保存]をクリックします。
(defun c:copyMText2Word ( / ) (setq *AcadApp* (vlax-get-acad-object)) ; Get AutoCAD application (setq *ModelSpace* (vla-get-ModelSpace (vla-get-ActiveDocument *AcadApp*))) ; Get model space
次のコードの引数 :tlb-filename をシステム上の msword.olb ファイルを指すように変更します。
(if (equal nil mswc-wd140Words) ; check for a Word constant
(vlax-import-type-library
:tlb-filename " C:/Program Files/Microsoft Office/Office14/msword.olb"
:methods-prefix "mswm-"
:properties-prefix "mswp-"
:constants-prefix "mswc-"
)
)
このコードは、まず最初に既知の Microsoft Word 定数が値で定義されているかどうかを調べます。その定数が値を持っている場合は、Word のタイプ ライブラリが既に読み込まれていて、それ以上アクションを起こす必要はないと考えられます。変数が nil の場合は、vlax-import-type-library が呼び出されます。
(setq msw (vlax-get-object "Word.Application.14"))
(if (equal nil msw)
(progn
; Word is not running. Start it.
(setq msw (vlax-create-object "Word.Application.14"))
(vla-put-visible msw :vlax-true)
)
)
このコードは、vlax-get-object 関数を呼び出して、実行している Microsoft Word アプリケーションへの接続を確立します(この例では、バージョン 14 (Word 2010)を指定しています。14 を省略すると、任意の Word のインスタンスが受け入れられます)。Word のインスタンスが起動されていない場合は、vlax‑create‑object 関数を呼び出して起動します。
(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
[エディタ内のテキストをロード]をクリックします。