LoginSignup
2
4

More than 5 years have passed since last update.

AutoCADのVBAマクロで、モデル空間とレイアウト空間の全テキストをCSV型式で出力する

Last updated at Posted at 2018-06-03

作成したきっかけ

図面内のテキスト情報をExcelで比較したかった為

動作環境

  • windows10
  • AutoCAD2017

AutoCAD画面(赤文字を出力したい)

image.png

VBA本文

Option Explicit

Sub テキスト情報をCSVに出力()

   Dim acDoc As AcadDocument
   Dim acEnt As AcadEntity
   Dim lines_count As Integer

    Dim p_xyz() As Double
    Dim str_data As String
    Dim i, num As Integer

    str_data = "空間名称,レイアウト名,,テキストの内容,文字高さ,X座標,Y座標,Z座標" & vbCrLf

   For Each acDoc In Documents
      For Each acEnt In acDoc.ModelSpace
         If (TypeOf acEnt Is AcadText) Or (TypeOf acEnt Is AcadMText) Then
            str_data = str_data & "ModelSpace,,"
            p_xyz = acEnt.InsertionPoint

              str_data = str_data & "モデル空間,""" & acEnt.TextString & """" & "," & acEnt.Height & "," & p_xyz(0) & "," & p_xyz(1) & "," & p_xyz(2) & vbCrLf

         End If
      Next acEnt
   Next acDoc

  num = ThisDrawing.Layouts.Count
'
  For i = 0 To num - 1
  ThisDrawing.ActiveLayout = ThisDrawing.Layouts.Item(i)
  Debug.Print ThisDrawing.Layouts.Item(i).Name

    For Each acDoc In Documents
      For Each acEnt In acDoc.PaperSpace
         If (TypeOf acEnt Is AcadText) Or (TypeOf acEnt Is AcadMText) Then
            str_data = str_data & "PaperSpace," & acDoc.PaperSpace.Layout.Name & ","
            p_xyz = acEnt.InsertionPoint

              str_data = str_data & "レイアウト空間,""" & acEnt.TextString & """" & "," & acEnt.Height & "," & p_xyz(0) & "," & p_xyz(1) & "," & p_xyz(2) & vbCrLf

         End If
      Next acEnt
   Next acDoc
  Next i

  MsgBox "テキストのピックアップ終わり"

  Dim file_name_txt, file_name_csv As String

  file_name_csv = Environ("HOMEDRIVE") & Environ("hOMEPATH") & "\Documents\" & ThisDrawing.Name & "のテキスト抽出.csv"

  Open file_name_csv For Output As #1
  Print #1, str_data
  Close #1

  MsgBox "テキスト抽出しました。"

  Shell "C:\Windows\Explorer.exe " & Environ("HOMEDRIVE") & Environ("hOMEPATH") & "\Documents\", vbNormalFocus

End Sub

出力結果のサンプル(Shift-JISで出力)

空間名称,レイアウト名,,テキストの内容,文字高さ,X座標,Y座標,Z座標
ModelSpace,,モデル空間,"{\fMS PGothic|b0|i0|c128|p50;わんわん}",2.5,36.2851367064068,-6.52786883406679,0
ModelSpace,,モデル空間,"{\fMS PGothic|b0|i0|c128|p50;abcde}",2.5,51.5677627384648,-6.73566893855514,0
ModelSpace,,モデル空間,"{\fMS PGothic|b0|i0|c128|p50;わんわん2}",2.5,18.9870629190264,74.9428743475937,0

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4