- 积分
- 802
- 明经币
- 个
- 注册时间
- 2006-6-22
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
我先问的是这个意思,就是我用 vb.net编制了一个连接CAD画图的程序,编译后生成DLL。然后我重新用VB.NET做一窗体程序引用他,但为什么就不成功呢?
下面是画线代码:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Public Class Class1
Public Sub AddLine()
ConnectToAcad()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim sPoint As Point3d = New Point3d(0, 1, 1)
Dim ePoint As Point3d = New Point3d(4, 1, 1)
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开块表 Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' 以写方式打开模型空间块表记录 Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一条起点为(5,5,0),终点为(12,3,0)的直线 Create a line that starts at 5,5 and ends at 12,3
'Dim acLine As Line = New Line(New Point3d(5, 5, 0), New Point3d(12, 3, 0))
Dim acLine As Line = New Line(sPoint, ePoint)
acLine.SetDatabaseDefaults()
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine)
acTrans.AddNewlyCreatedDBObject(acLine, True)
'' 保存新对象到数据库中 Save the new object to the database
acTrans.Commit()
End Using
End Sub
Public Sub ConnectToAcad()
Dim acAppComObj As AcadApplication
Dim strProgId As String = "AutoCAD.Application.18"
On Error Resume Next
'' 获得一个正在运行的 AutoCAD 的实例 Get a running instance of AutoCAD
acAppComObj = GetObject(, strProgId)
'' 如果没有正在运行的实例将出现一个错误 An error occurs if no instance is running
If Err.Number > 0 Then
Err.Clear()
'' 创建一个新的AutoCAD的实例 Create a new instance of AutoCAD
acAppComObj = CreateObject("AutoCAD.Application.18")
'' 检查 AutoCAD 的实例是否被创建 Check to see if an instance of AutoCAD was created
If Err.Number > 0 Then
Err.Clear()
''如果 AutoCAD 的实例没有创建,那么发出一个信息并退出 If an instance of AutoCAD is not created then message and exit
MsgBox("Instance of 'AutoCAD.Application' could not be created.")
Exit Sub
End If
End If
''显示应用程序并返回名字和版本号 Display the application and return the name and version
acAppComObj.Visible = True
acAppComObj.WindowState = AcWindowState.acMax
'MsgBox("Now running " & acAppComObj.Name & " version " & acAppComObj.Version)
''获得活动的文档 Get the active document
Dim acDocComObj As AcadDocument
acDocComObj = acAppComObj.ActiveDocument
''可选的,加载你的程序集并启动你的命令,如果你的程序集已经被加载,那就仅仅只启动命令
'' Optionally, load your assembly and start your command or if your assembly
'' is demandloaded, simply start the command of your in-process assembly.
'acDocComObj.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
' Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ")
'acDocComObj.SendCommand("MyCommand ")
End Sub
End Class
译后生成了 drline.dll文件
调用:
Imports drLine.Class1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aa As New drLine.Class1
aa.AddLine() ‘出错’出错’
End Sub
End Class
|
|