明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2188|回复: 9

[求助]请高手看看,关于VB调用AutoCAD的,总有错语

[复制链接]
发表于 2009-3-14 12:03:00 | 显示全部楼层 |阅读模式

代码如下,请各位大哥们看看,最后一句总有问题

初学这个,不太懂,请各位指教指教!在此谢过!

Dim Acsdapp As AcadApplication
Private Sub Form_Load()
On Error Resume Next
Set AcadApp = GetObject(, "AutoCAD.application")
If Err Then
   Err.Clear
   Set AcadApp = CreateObject("autocad.application")
If Err Then
   MsgBox ("不能运行AutoCAD")
   Exit Sub
   End If
End If
AcadApp.Visible = Ture
End Sub

Private Sub Command1_Click()
Dim a, b As Single
Dim lineobj As AcadLWPolyline
Dim points(0 To 9) As Double
a = Val(Text1.Text)
b = Val(Text2.Text)
points(0) = 0: points(1) = 0
points(2) = points(0) + a: points(3) = points(1)
points(4) = points(2): points(5) = points(3) + b
points(6) = points(0): points(7) = points(5)
points(8) = points(0): points(9) = points(1)
Set lineobj = AcadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)
ZoomAll
End Sub

发表于 2009-3-14 12:52:00 | 显示全部楼层

ZoomAll?????

是不是漏了什么???

 楼主| 发表于 2009-3-14 14:45:00 | 显示全部楼层

我也不太明白,反正到

Set lineobj = AcadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)
这一句就出问题了

大家帮我看看啊,到底是那里出问题了

不胜感激!

发表于 2009-3-14 21:29:00 | 显示全部楼层

这是VB模块调用的概念问题,首先要学点VB知识。你执行这个语句,

用F9将这条语句中断,

Set lineobj = AcadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)

在本地窗口中看AcadApp是不是为Nothing,学点VB这个问题很容易解决。


发表于 2009-3-24 15:57:00 | 显示全部楼层

    Private Sub Command1_Click()
        Dim a, b As Single
        Dim lineobj As AutoCAD.AcadLWPolyline
        Dim points(9) As Double

        Acadapp = GetObject(, "autocad.application")
        If Err.Number Then
            Err.Clear()
            Acadapp = CreateObject("autocad.application")
            If Err.Number Then
                MsgBox("不能运行AutoCAD")
                Exit Sub
            End If
        End If

        Acadapp.Visible = True
        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        points(0) = 0 : points(1) = 0
        points(2) = points(0) + a : points(3) = points(1)
        points(4) = points(2) : points(5) = points(3) + b
        points(6) = points(0) : points(7) = points(5)
        points(8) = points(0) : points(9) = points(1)
        lineobj = Acadapp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)
        Acadapp.ZoomAll()
    End Sub

在VB.NET 2003中调试通过.

发表于 2009-3-24 16:36:00 | 显示全部楼层
zdm860114发表于2009-3-14 14:45:00我也不太明白,反正到Set lineobj = AcadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)这一句就出问题了大家帮我看看啊,到底是那里出问题了不胜感激!

 问问题,要问的明白,才能回答的明白,“出问题了”,出什么问题了?错误提示是什么?你没给出来,在我看来,哪代码没什么问题

发表于 2009-3-25 10:59:00 | 显示全部楼层

Dim Acsdapp As AcadApplication   应为Acadapp
Private Sub Form_Load()
On Error Resume Next
Set AcadApp = GetObject(, "autocad.application")
If Err Then
   Err.Clear
   Set AcadApp = CreateObject("autocad.application")
If Err Then
   MsgBox ("不能运行AutoCAD")
   Exit Sub
   End If
End If
AcadApp.Visible = Ture
End Sub

发表于 2009-4-24 17:10:00 | 显示全部楼层

呵呵,居然是代码敲错了,楼主该好好检查啊。犯这种错误不大应该。

发表于 2009-7-22 14:35:00 | 显示全部楼层

还是这里好啊...

我最近也开始学习VB开发CAD.结果却一懂不懂!

下面这段代码引自帮助文件上的示例代码..

Sub Example_AddCircle()
    ' 该示例在模型空间中创建圆。
  
    Dim circleObj As AcadCircle
    Dim centerPoint(0 To 2) As Double
    Dim radius As Double
   
    ' 定义圆
    centerPoint(0) = 0#: centerPoint(1) = 0#: centerPoint(2) = 0#
    radius = 5#
   
    ' 在模型空间中创建圆对象
    Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPoint, radius)
    ZoomAll
   
End Sub
结果却弄死也不对.楼主的程序我拿来一用.却对了.哈哈...
原来这个THISdrawing..要改为Acadapp.Activedocumnet汗...
发表于 2010-8-3 11:42:00 | 显示全部楼层
对了,Acadapp = GetObject(, "autocad.application"),这一句应该为:set Acadapp = GetObject(, "autocad.application"),否则你创建的应用程序对象无效!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 22:45 , Processed in 0.198549 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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