zdm860114 发表于 2009-3-14 12:03:00

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

代码如下,请各位大哥们看看,最后一句总有问题
初学这个,不太懂,请各位指教指教!在此谢过!
Dim Acsdapp As AcadApplication
Private Sub Form_Load()
On Error Resume Next
Set AcadApp = GetObject(, ".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

Saging 发表于 2009-3-14 12:52:00

ZoomAll?????
是不是漏了什么???

zdm860114 发表于 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这个问题很容易解决。

Saging 发表于 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中调试通过.

zzyong00 发表于 2009-3-24 16:36:00


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

pmq 发表于 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

AirCD 发表于 2009-4-24 17:10:00

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

votasee 发表于 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汗...

syk070205 发表于 2010-8-3 11:42:00

对了,Acadapp = GetObject(, "autocad.application"),这一句应该为:set Acadapp = GetObject(, "autocad.application"),否则你创建的应用程序对象无效!
页: [1]
查看完整版本: [求助]请高手看看,关于VB调用AutoCAD的,总有错语