明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1031|回复: 3

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

[复制链接]
发表于 2018-4-3 22:10:02 | 显示全部楼层 |阅读模式
之前在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里面一个都运行不起来啊?各位大侠能否指点一下啊,谢谢啦!
 楼主| 发表于 2018-4-3 22:45:14 | 显示全部楼层
刚刚将项目属性里面,目标FrameWork由4.0改为.net FrameWork3.5,则:
objPoly1 = acadDoc.ModelSpace.AddLightWeightPolyline(PolyLine)
画线没有问题了。

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

深夜求助,望各位大侠指点一下啊!
 楼主| 发表于 2018-4-3 22:48:40 | 显示全部楼层
在color = acadApp.GetInterfaceObject("AutoCAD.AcCmColor.16")行也是报错,
与PickPt = acadDoc.Utility.GetPoint(, vbCrLf & "请输入起点:")行错误基本相同。
发表于 2018-5-8 12:15:12 | 显示全部楼层
.net不懂,但感觉你的程序可能是缺少命名空间
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Colors
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 06:28 , Processed in 0.151512 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表