Dim CadAPP As AcadApplication
Dim CadDoc As AcadDocument
Set CadAPP = GetObject(, "Autocad.Application.16") '获取已经在运行的CAD程序 .16表示cad2004或者2006版本
If Err Then
Err.Clear
Set CadAPP = CreateObject("Autocad.Application.16") '如果获取失败,新建一个启动
End If
Set CadDoc = cadapp.Documents.Open(省略)
先在 工程-引用 里面增加 aucocad类型库然后写代码如下:Private Sub Command1_Click()
Dim p1(2) As Double, p2(2) As Double, p3(2) As Double
Dim acad As AcadApplication
Dim adoc As AcadDocument
Dim aline As acadline
Dim dima As AcadDimAligned
Set acad = CreateObject("autocad.application.16")
acad.Visible = True
Set adoc = acad.Documents.Add
p1(0) = 100: p1(1) = 100: p1(2) = 0
p2(0) = 1000: p2(1) = 1000: p2(2) = 0
p3(0) = 500: p3(1) = 520: p3(2) = 0
Set aline = adoc.ModelSpace.addline(p1, p2)
aline.Color = acblue
Set dima = adoc.ModelSpace.AddDimAligned(p1, p2, p3)
dima.TextHeight = 15
dima.TextColor = acGreen
dima.ArrowheadSize = 10
End Sub
最基本的首先你要在vb中加载autocad库,当然你要装了autocad,可以用vb控制建立cad文件,然后向cad文件中写数据。对于在CAD中建立直角坐标系,建议你看一下wcs对应的cad中的对象是哪一个,然后用vb调用建立wcs。下面是一个vb中创建CAD文件并向模型空间画直线的代码。前提一定要在vb中加载CAD,记得CAD的帮助文件中有vba的帮助,建议你下载cad vba二次开发的书籍,百度文库有。希望能够采纳我的回答!!!!!
Sub AddLineVB()
On Error Resume Next
' 连接至 AutoCAD 应用程序
Dim acadApp As AcadApplication
Set acadApp = GetObject _
(, "AutoCAD.Application.17")
If Err Then
Err.Clear
Set acadApp = CreateObject _
("AutoCAD.Application.17")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
' 连接至 AutoCAD 图形
Dim acadDoc As AcadDocument
Set acadDoc = acadApp.ActiveDocument
' 创建直线的端点
Dim lineObj As AcadLine
Dim startPoint(0 To 2) As Double
Dim endPoint(0 To 2) As Double
startPoint(0) = 1
startPoint(1) = 1
startPoint(2) = 0
endPoint(0) = 5
endPoint(1) = 5
endPoint(2) = 0
' 在模型空间中创建 Line 对象
Set lineObj = acadDoc.ModelSpace.AddLine _
(startPoint, endPoint)
ZoomAll
acadApp.visible = True
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)