nanoCAD 5.0 VB.NET / COM(ActiveX) 2024/12/06, 07
やっていることは、「VB.NET から COM (ActiveX) を遅延バインディングで使う」です。
純粋な VB.NET ではありません。
以下のいずれのコードでも nanoCAD 5 ではエラーになります。
Dim app0 As InanoCADApplication = _AcAp.Application.HostApplication
Dim app1 As InanoCADApplication = TryCast(_AcAp.Application.HostApplication,
InanoCADApplication)
Dim app2 As InanoCADApplication = GetObject("", "nanoCAD.Application.5.0")
Dim app3 As InanoCADApplication = TryCast(GetObject("", "nanoCAD.Application.5.0"),
InanoCADApplication)
で、遅延バインディングを使っています。
普段 Delphi から COM (ActiveX) を使っているので、とっつきやすいというだけの理由です。
ダイアログを使って、文字列を編集するサンプルです。nanoCAD の EDIT コマンドのほうが優秀です。
nanoCAD と OdaX、2 つの COM (タイプライブラリ)を参照していますが、
コードの確認のために使っているだけなので、実際には不要です。
.comapp = Nothing ... は不要かも知れませんが、念のために使っています。(Delphi では不要です)
※こちらの環境(高DPI環境)では、フォームを使ったツールで nanoCAD 自体と自前のフォームが小さくなる現象がありました。
nanoCAD のアイコン(または、nCad.exe) を右クリック。「プロパティ」をクリック。
「互換性」タブをクリック。[高DPIの設定] をクリック。
「高いDPIスケールの動作を上書きします。拡大縮小の実行元:」のチェックをオン。
その下のリストから「システム」を選択。[OK] をクリック。
で、こちらの環境では、正常に表示されるようになりました。
Imports Teigha.Runtime Imports HostMgd.ApplicationServices Imports Teigha.DatabaseServices Imports Teigha.Geometry Imports HostMgd.EditorInput ''COM(未使用) Imports nanoCAD Imports OdaX ' alias Imports _AcRx = Teigha.Runtime Imports _AcAp = HostMgd.ApplicationServices Imports _AcDb = Teigha.DatabaseServices Imports _AcGe = Teigha.Geometry Imports _AcEd = HostMgd.EditorInput Imports _AcGi = Teigha.GraphicsInterface Imports _AcClr = Teigha.Colors Imports _AcWnd = HostMgd.Windows Public Class Class1 <CommandMethod("ComDlg", CommandFlags.Modal)> Public Shared Sub ComDlg() Try '' フォーム生成 Dim edForm As Form1 = New Form1 '' nanoCAD COM を取得(遅延バインディング) With edForm .comapp = GetObject("", "nanoCAD.Application.5.0") .comdoc = edForm.comapp.ActiveDocument Dim obj As Object = Nothing Dim vpt As Object = Nothing .comdoc.Utility.GetEntity(obj, vpt, "編集する TEXT を選択 : ") If obj IsNot Nothing Then If obj.ObjectName = "AcDbText" Then .comtxt = obj .TextBox1.Text = obj.TextString _AcAp.Application.ShowModalDialog(edForm) End If End If .comtxt = Nothing .comdoc = Nothing .comapp = Nothing .Dispose() End With Catch ex As System.Exception ''コメントアウト ' _AcAp.Application.ShowAlertDialog(ex.Message) End Try End Sub End Class
Imports Teigha.Runtime Imports HostMgd.ApplicationServices Imports Teigha.DatabaseServices Imports Teigha.Geometry Imports HostMgd.EditorInput Public Class Form1 Public comapp As Object Public comdoc As Object Public comtxt As Object '' OK ボタン Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Db As Database = HostApplicationServices.WorkingDatabase Using Tr As Transaction = Db.TransactionManager.StartTransaction Try If comtxt IsNot Nothing Then comtxt.TextString = TextBox1.Text comtxt.UpDate End If '' 変更を反映 Tr.Commit() ''←重要 Catch Ex As Exception MsgBox(Ex.ToString()) End Try End Using Me.Hide() End Sub '' キャンセルボタン Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Me.Hide() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'フォームのLoadイベントハンドラ Me.AutoSize = True Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink 'フォームの大きさを画面の半分にする 'Me.Width = System.Windows.Forms.Screen.GetBounds(Me).Width \ 2 'Me.Height = System.Windows.Forms.Screen.GetBounds(Me).Height \ 2 End Sub Private Sub TextBox1_KeyPress(sender As Object, e As Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = vbCr Then Button1_Click(Button1, EventArgs.Empty) ElseIf e.KeyChar = Chr(27) Then Button2_Click(Button2, EventArgs.Empty) End If End Sub Private Sub Form1_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing End Sub End Class
よく分からないながら、同じことを、COM を使わずにやってみた↓ 2024/12/07
とりあえず、動く。
Imports Teigha.Runtime Imports HostMgd.ApplicationServices Imports Teigha.DatabaseServices Imports Teigha.Geometry Imports HostMgd.EditorInput ''COM(未使用) 'Imports nanoCAD 'Imports OdaX ' alias Imports _AcRx = Teigha.Runtime Imports _AcAp = HostMgd.ApplicationServices Imports _AcDb = Teigha.DatabaseServices Imports _AcGe = Teigha.Geometry Imports _AcEd = HostMgd.EditorInput Imports _AcGi = Teigha.GraphicsInterface Imports _AcClr = Teigha.Colors Imports _AcWnd = HostMgd.Windows Public Class Class1 <CommandMethod("ComDlg", CommandFlags.Modal)> Public Shared Sub ComDlg() Dim doc As _AcAp.Document = _AcAp.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Using tr As Transaction = db.TransactionManager.StartTransaction Try Dim res As PromptEntityResult = ed.GetEntity(vbLf + "文字列を編集する TEXT を指示 : ") If (res.Status = PromptStatus.OK) Then Dim ent As Entity = tr.GetObject(res.ObjectId, OpenMode.ForWrite) If TypeOf ent Is DBText Then Dim txtEnt As DBText = TryCast(ent, DBText) Dim edForm As Form1 = New Form1() With edForm .tr = tr .txtEnt = txtEnt .TextBox1.Text = txtEnt.TextString End With _AcAp.Application.ShowModalDialog(edForm) edForm.Dispose() End If End If Catch ex As System.Exception _AcAp.Application.ShowAlertDialog(ex.Message) End Try End Using End Sub End Class
Imports Teigha.Runtime Imports HostMgd.ApplicationServices Imports Teigha.DatabaseServices Imports Teigha.Geometry Imports HostMgd.EditorInput Public Class Form1 Public tr As Transaction Public txtEnt As DBText Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try txtEnt.TextString = TextBox1.Text '' 変更を反映 tr.Commit() ''←重要 Catch Ex As Exception MsgBox(Ex.ToString()) End Try Me.Hide() ''不要? tr.Dispose() txtEnt.Dispose() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Me.Hide() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'フォームのLoadイベントハンドラ Me.AutoSize = True Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink 'フォームの大きさを画面の半分にする 'Me.Width = System.Windows.Forms.Screen.GetBounds(Me).Width \ 2 'Me.Height = System.Windows.Forms.Screen.GetBounds(Me).Height \ 2 End Sub Private Sub TextBox1_KeyPress(sender As Object, e As Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = vbCr Then Button1_Click(Button1, EventArgs.Empty) ElseIf e.KeyChar = Chr(27) Then Button2_Click(Button2, EventArgs.Empty) End If End Sub Private Sub Form1_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing End Sub End Class