CyberChaos(さいばかおす)

プログラミング言語、トランスパイラ、RPA、ChatGPT、データマイニング、リバースエンジニアリングのための忘備録

AutoCADでVB.NETを使い球を自動で描かせることに成功!

2024-02-03 17:32:51 | Autocad

またまたChatGPTがやってくれた!すげーよ!マジ神!

指定した場所に半径300の球を一瞬で描いた。

最初は失敗したかな?と思ったが3D表示に変えると、金属光沢のある球ができていた。

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

Public Class Class1

    <CommandMethod("SPHERE")>
    Public Sub DrawSphere()
        ' ダイアログを表示して中心点を取得
        Dim dialogResult As PromptPointResult = GetPoint("Specify center point: ")
        If dialogResult.Status <> PromptStatus.OK Then
            Exit Sub
        End If
        Dim centerPoint As Point3d = dialogResult.Value

        ' 半径300の球を描画
        DrawSphere(centerPoint, 300)
    End Sub

    Private Function GetPoint(message As String) As PromptPointResult
        Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim promptOptions As PromptPointOptions = New PromptPointOptions(message)
        Dim pointResult As PromptPointResult = editor.GetPoint(promptOptions)
        Return pointResult
    End Function

    Private Sub DrawSphere(centerPoint As Point3d, radius As Double)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                ' ブロックテーブルとブロックテーブルレコードを開く
                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim btr As BlockTableRecord = CType(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

                ' 半径300の球を作成
                Dim sphere As Solid3d = New Solid3d()
                sphere.CreateSphere(radius)
                sphere.TransformBy(Matrix3d.Displacement(centerPoint.GetAsVector()))
                btr.AppendEntity(sphere)
                trans.AddNewlyCreatedDBObject(sphere, True)

                trans.Commit()
            Catch ex As Exception
                Application.ShowAlertDialog("Error: " & ex.Message)
                trans.Abort()
            End Try
        End Using
    End Sub

End Class


AutoCADでVB.NETを使い自動で五角形を描かせることに成功!

2024-02-03 17:11:17 | Autocad

一辺が100の五角形を描くVB.netプログラム。任意の中心を指定するだけ。

VBのソースコードの部分だけ以下のようにChatGPTに質問して書いてもらった。

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

Public Class Class1

    <CommandMethod("PENTAGON")>
    Public Sub DrawPentagon()
        ' ダイアログを表示して中心点を取得
        Dim dialogResult As PromptPointResult = GetPoint("Specify center point: ")
        If dialogResult.Status <> PromptStatus.OK Then
            Exit Sub
        End If
        Dim centerPoint As Point3d = dialogResult.Value

        ' 一辺が100の五角形を描画
        DrawRegularPentagon(centerPoint, 100)
    End Sub

    Private Function GetPoint(message As String) As PromptPointResult
        Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim promptOptions As PromptPointOptions = New PromptPointOptions(message)
        Dim pointResult As PromptPointResult = editor.GetPoint(promptOptions)
        Return pointResult
    End Function

    Private Sub DrawRegularPentagon(centerPoint As Point3d, sideLength As Double)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                ' ブロックテーブルとブロックテーブルレコードを開く
                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim btr As BlockTableRecord = CType(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

                ' 一辺が100の五角形を作成
                Dim pentagonPoints As Point2dCollection = GetRegularPentagonPoints(centerPoint, sideLength)
                Dim poly As Polyline = New Polyline()
                For Each point As Point2d In pentagonPoints
                    poly.AddVertexAt(poly.NumberOfVertices, point, 0, 0, 0)
                Next

                poly.Closed = True
                btr.AppendEntity(poly)
                trans.AddNewlyCreatedDBObject(poly, True)

                trans.Commit()
            Catch ex As Exception
                Application.ShowAlertDialog("Error: " & ex.Message)
                trans.Abort()
            End Try
        End Using
    End Sub

    Private Function GetRegularPentagonPoints(centerPoint As Point3d, sideLength As Double) As Point2dCollection
        Dim points As Point2dCollection = New Point2dCollection()

        For i As Integer = 0 To 4
            Dim angle As Double = i * (2 * Math.PI) / 5
            Dim x As Double = centerPoint.X + sideLength * Math.Cos(angle)
            Dim y As Double = centerPoint.Y + sideLength * Math.Sin(angle)
            points.Add(New Point2d(x, y))
        Next

        Return points
    End Function

End Class

AutoCADでコマンド「PENTAGON」と入力し、中心を指定すると・・・

このように一瞬で一辺100の五角形が出現した。マウスでカチャカチャやったら何秒かかるだろうか?

小さなプログラムのブロックをたくさん作って組み合わせていけば、作図スピードが何十倍にも上がるのは言うまでもない。


AutoCAD2023でVB.NETを使いHelloWorldの表示に成功!

2024-02-03 12:46:37 | Autocad

技術評論社の2012年出版の古い本だが、「AutoCAD VB.NETマクロサンプル大全集」を見ながら頑張ってみた。

まずはVisualStudioで以下のVB.NETコードを書く。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInout
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput

Public Class Class1

    <CommandMethod("HLWL")>
    Public Sub MyHelloWorld()

        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        ed.WriteMessage(vbCrLf + "Hello World!")
    End Sub

End Class

その後の表現が我々初心者プログラミング厨房にとって難解極まりない。

参照するだのパスを通すだの、詳しく説明せずに煙に巻くのはやめんかい!!!(この本のことではなく、他の本またはサイト)

マジぶっ●すぞ!!!(この本にはそこそこ分かりやすく書かれてあったからこそ成功したのだ。)

参照とは、一番上の帯でプロジェクトというところを選び、このプログラムのプロジェクト名のプロパティの部分を押すと、本で指定されたdllファイルを選んでチェックを入れ、Trueに設定変更するように指示が出ているので、その通りに従えばよい。(accoremgd.dll,acdbmgd.dll,acmgd.dllの3つ)

そして、どのサイトを見ても書かれていなかったのが「パスを通す」という表現の作業であった。

具体的に何をするのかというと、いったんこのプログラムを書き終えて参照設定まで終えたらビルドボタンを押すと、幾種類かのファイルが生成されるので、その中のプロジェクト名.vbprojというファイルをメモ帳で開いて中身を編集していくことになるのだ。その過程でacad.exeというファイルのディレクトリをフルパスで「C:¥Program Files¥Autodesk¥AutoCAD 2023¥acad.exe(この部分は個々のPCの設定によって変わるので、acad.exeがどこにあるか探してディレクトリパスをコピっておくべき)」(なぜか半角¥はブログに書けない)追記する場面があるから、それをパスを通すと呼んでいるらしい。自称凄腕プログラマー&エンジニアはそういうわけわからん事ばかりほざいてドヤ顔で悦に浸り、後進の教育をおろそかにするから日本は世界で最低レベルとバカにされるんだよ。教えるのがめんどくさいとか教えてる時間がないとか嘘の言い訳をし、教える能力がないのがバレるのを恐れているだけだ。

愚痴はここまでにして・・・本には <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

のすぐ下に <StartAction>Program</StartAction>
    <StartProgram>C:¥Program Files¥Autodesk¥AutoCAD 2023¥acad.exe</StartProgram>

と追記して上書き保存し、ビルドしたプログラムをデバッグするとAutoCADが立ち上がり、

作図画面上でコマンドと表示されている状態で、NETLOADと入力すると.NETアセンブリを選択するというウィンドウが立ち上がるので、Hello_World.dllを選んで開き、HLWLとコマンドを打つと無事HelloWorld!と表示されて成功に終わった。

本文は以上。以下は画面のキャプチャー画像。

1.VB.NETのソースコード打ち込みの様子。

2.いわゆるパスを通している所wwwメモ帳で追記しているだけだがwww

3.小さすぎて見えないが中央下部にHelloWorldが表示されている所。

山場、難所は超えたから次はもっと難しいこと、さらにC#やF#でソースコードを書き替えて実践してみたい。

古い本なのに、AutoCAD2023とVisual Studio2022で本に書かれてある内容そのままでバッチリ動いて成功できたのは非常に大きな一歩だ!!!

おい、ボンクラ自称プログラマー、エンジニア共よ、首を洗って待って居ろ。いずれフリーランスとなってつぶしにいってやるからな、覚悟せい!!!