bailank 发表于 2018-4-3 22:10:02

VB6.0中可以运行的CAD画图语句,为什么在VB.net里面运行出错啊?

之前在VB6.0里面写了一个在CAD里面画图的程序,但是粘贴到VB.net里面的时候,就出状况了。
已经引用了AutoCAD 2010 Type Library。
后来又不放心,引用了acdbmgd.dll;acmgd.dll和Autodesk.AutoCAD.Interop.Common.dll。但是这个问题还是没有解决。
代码如下:


Imports Autodesk
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class Form1
    Const PI = 3.1415926

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   '----CAD相关参数---------
      Dim acadApp As Autodesk.AutoCAD.Interop.AcadApplication
      Dim acadDoc As AcadDocument

      Dim PickPt As Object '记录屏幕捕捉的点



      '----开aCAD-----
      acadApp = CreateObject("AutoCAD.Application")
      If Err.Number Then
            MsgBox("不能运行Autocad,请检查是否安装了autocad")
            Exit Sub
      End If
      acadDoc = acadApp.ActiveDocument
      acadApp.Visible = True
      '---------------------------------


      '先捕捉起点
      PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")


      '----CAD画线部分--------
      Dim objPoly1 As AcadLWPolyline
      Dim PolyLine(3) As Double
      Dim color As AcadAcCmColor

      Dim Text As AcadText
      Dim TextPt(2) As Double '文字所在点的坐标
      Dim TextBasePt(2) As Double
      Dim TextString As String

      PolyLine(0) = PickPt(0)
      PolyLine(1) = PickPt(1)
      PolyLine(2) = PickPt(0) + 100
      PolyLine(3) = PickPt(1) + 100

      objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)


      PolyLine(0) = PickPt(0) + 100
      PolyLine(1) = PickPt(0) + 100
      PolyLine(2) = PickPt(0) + 100 + 20
      PolyLine(3) = PickPt(1) + 100 + 20


      objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)

      Call objPoly1.SetWidth(0, 20, 20)

      color = acadApp.GetInterfaceObject("AutoCAD.AcCmColor.16") 'RGB(80, 100, 244)
      Call color.SetRGB(255, 255, 255)
      objPoly1.TrueColor = color


      '---写入文字------
      TextPt(0) = PickPt(0) + 25
      TextPt(1) = PickPt(1)
      TextPt(2) = 0

      TextString = "基准点"

      Text = acadDoc.ModelSpace.AddText(TextString, TextPt, 100)
      Call Text.Rotate(TextPt, -PI / 2)
      Text.TrueColor = color
    End Sub
End Class

运行之后,CAD能够打开,但是在
PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")
这行报错,报” 未处理 COMException”


详细的报错内容是:
“未处理 System.Runtime.InteropServices.COMException
ErrorCode=-2147352567
Message=发生意外。 (异常来自 HRESULT:0x80020009 (DISP_E_EXCEPTION))


如果将这行注释掉,将PickPt直接赋值:
Dim PickPt(2) As Double

      PickPt(0) = 0
      PickPt(1) = 0
      PickPt(2) = 0


代码变为:
Imports Autodesk
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class Form1
    Const PI = 3.1415926

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   '----CAD相关参数---------
      Dim acadApp As Autodesk.AutoCAD.Interop.AcadApplication
      Dim acadDoc As AcadDocument

      ' Dim PickPt As Object '记录屏幕捕捉的点

      Dim PickPt(2) As Double



      '----开aCAD-----
      acadApp = CreateObject("AutoCAD.Application")
      If Err.Number Then
            MsgBox("不能运行Autocad,请检查是否安装了autocad")
            Exit Sub
      End If
      acadDoc = acadApp.ActiveDocument
      acadApp.Visible = True
      '---------------------------------


      '先捕捉起点
      ' PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")


      PickPt(0) = 0
      PickPt(1) = 0
      PickPt(2) = 0



      '----CAD画线部分--------
      Dim objPoly1 As AcadLWPolyline
      Dim PolyLine(3) As Double
      Dim color As AcadAcCmColor

      Dim Text As AcadText
      Dim TextPt(2) As Double '文字所在点的坐标
      Dim TextBasePt(2) As Double
      Dim TextString As String

      PolyLine(0) = PickPt(0)
      PolyLine(1) = PickPt(1)
      PolyLine(2) = PickPt(0) + 100
      PolyLine(3) = PickPt(1) + 100

      objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)


      PolyLine(0) = PickPt(0) + 100
      PolyLine(1) = PickPt(0) + 100
      PolyLine(2) = PickPt(0) + 100 + 20
      PolyLine(3) = PickPt(1) + 100 + 20


      objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)

      Call objPoly1.SetWidth(0, 20, 20)

      color = acadApp.GetInterfaceObject("AutoCAD.AcCmColor.16") 'RGB(80, 100, 244)
      Call color.SetRGB(255, 255, 255)
      objPoly1.TrueColor = color


      '---写入文字------
      TextPt(0) = PickPt(0) + 25
      TextPt(1) = PickPt(1)
      TextPt(2) = 0

      TextString = "基准点"

      Text = acadDoc.ModelSpace.AddText(TextString, TextPt, 100)
      Call Text.Rotate(TextPt, -PI / 2)
      Text.TrueColor = color
    End Sub
End Class


则在
objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)
行报错,
报“未处理InvalidCastException”
“无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Autodesk.AutoCAD.Interop.Common.IAcadModelSpace”。此操作失败的原因是对 IID 为“{C60BC8AF-58DA-4866-859D-22A7F61411DE}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。”

VB.net版本是VS2010;AutoCAD版本是AutoCAD2010.

为什么VB6.0里面运行的好好的代码,在VB.net里面一个都运行不起来啊?各位大侠能否指点一下啊,谢谢啦!

bailank 发表于 2018-4-3 22:45:14

刚刚将项目属性里面,目标FrameWork由4.0改为.net FrameWork3.5,则:
objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)
画线没有问题了。

但是
PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")
错误依然存在。

深夜求助,望各位大侠指点一下啊!

bailank 发表于 2018-4-3 22:48:40

在color = acadApp.GetInterfaceObject("AutoCAD.AcCmColor.16")行也是报错,
与PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")行错误基本相同。

dong20030432 发表于 2018-5-8 12:15:12

.net不懂,但感觉你的程序可能是缺少命名空间
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Colors
页: [1]
查看完整版本: VB6.0中可以运行的CAD画图语句,为什么在VB.net里面运行出错啊?