【AutoCAD】.NETで開発を行おう – パート5 – 10分 –

AutoCAD

概要

以下のように画層名を入力して、画層を作成するダイアログを作成します。

VisualStudio 2019でプロジェクトを作成

パート1と同様に VisualStudio 2019のプロジェクトを作成します。

  • vbの空のクラスライブラリの新規プロジェクトを作成します。
  • 同様に”C:\Program Files\Autodesk\AutoCAD 2022”のacmgd.dll、acdbmgd.dll、accoremgd.dllの3つを参照に指定します。

フォームの作成

ソース

フォーム

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices


Public Class Form1
    Private Sub Button_CANCEL_Click(sender As Object, e As EventArgs) Handles Button_CANCEL.Click
        Me.Hide()
    End Sub

    Private Sub Button_OK_Click(sender As Object, e As EventArgs) Handles Button_OK.Click
        Dim oDb As Database = HostApplicationServices.WorkingDatabase
        Dim oTr As Transaction = oDb.TransactionManager.StartTransaction
        Try
            Dim oLt As LayerTable = oTr.GetObject(oDb.LayerTableId, OpenMode.ForWrite)
            Dim oLtr As LayerTableRecord
            If Not oLt.Has(TextBox1.Text) Then
                oLtr = New LayerTableRecord
                oLtr.Name = TextBox1.Text
                oLt.Add(oLtr)
                oTr.AddNewlyCreatedDBObject(oLtr, True)
            Else
                MsgBox("既に画層が存在しています")
            End If
            oTr.Commit
        Catch oEx As Exception
            MsgBox(oEx.ToString())
        Finally
            oTr.Dispose()
        End Try
        Me.Hide()
    End Sub
End Class

コマンド関数

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Public Class Class1
    <CommandMethod("ShowDialog")>
    Public Shared Sub ShowDialog()
        Dim oForm As Form1 = New Form1
        Application.ShowModalDialog(oForm)
        oForm.Dispose()
    End Sub
End Class

コンパイル

パート1を参考にコンパイルします。

NETLOADコマンドでdllの読み込み

  • AutoCADを立ち上げて、コマンドでNETLOADコマンドを打ちます。
  • 作成したdllを指定します。
  • ShowDialogを打ち込みます。

以下のようなダイアログが出力されまますので、画素名に”あああ”を入力してOKボタンをクリックします。

画層名があああと表示されます。

スポンサーリンク
AutoCADその他
Engineerをフォローする
レンサバ