zhangtaosp 发表于 2012-2-23 15:18:59

vb.net调用AUTOCAD画线生成的DLL,不能调用

我先问的是这个意思,就是我用 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

zhuirvine 发表于 2012-2-23 18:53:42

。。。你这个没法看啊,你在你的aa.addline里加个断点,然后用调试模式启动CAD再加载DLL运行,运行到断点的地方会停住,然后你单步,就能看出来哪过不去了。。这个整个函数都出错,不好看啊

kdst 发表于 2012-5-24 23:44:40

看你的代码是用COM来写的,但你的代码既有.NET托管的代码又有COM方式的代码,不知道是不是这个原因.
页: [1]
查看完整版本: vb.net调用AUTOCAD画线生成的DLL,不能调用