重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
操作步骤:
成都创新互联主营东宝网站建设的网络公司,主营网站建设方案,成都app开发,东宝h5小程序开发搭建,东宝网站营销推广欢迎东宝等地区企业咨询
一、创建一个ACTIVEX DLL cadPro工程
二、添加一个模块命名为ModCad.代码如下
Public acadApp As Object
Public acadDoc As Object
(这里建立一个新块主要是为了以后在多个窗体、类或者多个工程中可以调用)
二、添加一个类ClsTest代码如下:
Public Function MenuMain(MenuIndex As Integer)
'下面的判断在VB中测试的时候可以用到,在生成DLL后VBA调用时可注释
If ModCad.acadApp Is Nothing Or ModCad.acadDoc Is Nothing Then
ConnectToAcad
setApp ModCad.acadApp
setDoc ModCad.acadDoc
End If
Select Case MenuIndex
Case 1
AutoCADTest
Case 2
End Select
End Function
Public Function ConnectToAcad()
On Error Resume Next
Set ModCad.acadApp = GetObject(, "AutoCAD.Application.16.2") '16.2是CAD的版本
If Err Then
Err.Clear
Set ModCad.acadApp = CreateObject("AutoCAD.Application.16.2")
End If
If Err Then
Err.Clear
Set ModCad.acadApp = GetObject(, "AutoCAD.Application")
If Err Then
Err.Clear
Set ModCad.acadApp = CreateObject("AutoCAD.Application")
End If
If Err Then
Unload Me
End If
End If
ModCad.acadApp.Visible = True
Set ModCad.acadDoc = ModCad.acadApp.ActiveDocument
End Function
Public Function setApps(acadObj As AcadApplication)
Set ModCad.acadApp = acadObj
Set ModCad.acadDoc = acadApp.ActiveDocument
End Function
'VB中要在CAD中实现的功能
Private Function AutoCADTest()
Dim cadLine As AcadLine
Dim cadPoint As Variant
cadPoint = ModCad.acadDoc.Utility.GetPoint(, "请选取一个插入点:")
ModCad.acadDoc.ModelSpace.AddCircle cadPoint, 5
End Function
三、如果要在VB中调试,在添加一个EXE工程,引用ACTIVEX工程,添加一个窗体,在窗体上添加一个按钮,
Private Sub Command1_Click()
Dim Rec As New cadPro.MenuMain
Rec.MenuMain 1
End Sub
四、生成DLL文件在VBA中调用。
在工具----设定引用项目中加入生成的DLL。代码如下:
Option Explicit
Private clsGre As New cadPro.ClsTest
Public Sub cadtest()
On Error Resume Next
clsGre.setApp ThisDrawing.Application
clsGre.MenuMain 1
End Sub
按照以上四个步骤就可实现VB与AutoCAD的连接。
以上程序在本机测试通过。
OS:winxp sp2 cht
AuotCAD Version:AutoCAD 2006
应用程序启动了没
dim acadApp As AcadApplication
acadApp = CType(CreateObject("AutoCAD.Application.18"), AcadApplication)
acadApp.Visible = True
启动了CAD才能添加文件,其中18对应的是cad2010,版本不同不一样。
Dim ppr As PromptPointResult = ed.GetPoint("请选择插入点:")
Dim pt As Point3d = ppr.Value
utility.WriteToEditor(pt.ToString())
Dim pidBlock As New PIDBlock()
'自己定义的图块类,保存图块的路径和名称
pidBlock.Name = "sample"
pidBlock.Path = blockPath "b_sample.dwg"
Using blkDb As New Database(False, True)
'read drawing
blkDb.ReadDwgFile(pidBlock.Path, System.IO.FileShare.Read, True, Nothing)
blkDb.CloseInput(True)
Using docLock As DocumentLock = doc.LockDocument()
'多文档要先这样,否则报至命错误
Using t As Transaction = doc.TransactionManager.StartTransaction()
'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert(pidBlock.Name, blkDb, False)
'create a ref to the block
Dim bt As BlockTable = DirectCast(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = DirectCast(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Using bref As New BlockReference(pt, idBTR)
btr.AppendEntity(bref)
t.AddNewlyCreatedDBObject(bref, True)
End Using
t.Commit()
End Using
End Using
End Using
你去查查书吧,书上挺详细的,在这说不好说,你先在项目里引用。然后 Dim acadapp As AcadApplication Dim acaddoc As AcadDocument On Error Resume Next AcadApp = GetObject(, "AutoCAD.Application") If Err.Number Then Err.Clear() AcadApp = CreateObject("AutoCAD.Application") If Err.Number Then MsgBox("不能运行AutoCAD,请检查是否安装了AutoCAD") Exit Sub End If End If AcadApp.Visible = True '界面可视