你去查查書吧,書上挺詳細(xì)的,在這說不好說,你先在項(xiàng)目里引用。然后 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("不能運(yùn)行AutoCAD,請(qǐng)檢查是否安裝了AutoCAD") Exit Sub End If End If AcadApp.Visible = True '界面可視
成都創(chuàng)新互聯(lián)公司專業(yè)成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,集網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文平臺(tái)等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計(jì),讓網(wǎng)站在運(yùn)行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計(jì)制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。
vb2010(vb.net)貌似已經(jīng)沒有OLE控件
下面的代碼是用PictureBox控件顯示CAD的DWG文件
Private?Structure?BITMAPFILEHEADER
Dim?bfType?As?Short
Dim?bfSize?As?Integer
Dim?bfReserved1?As?Short
Dim?bfReserved2?As?Short
Dim?bfOffBits?As?Integer
End?Structure
Public?Function?GetDwgImage(ByVal?FileName?As?String)?As?Image
If?Not?File.Exists(FileName)?Then?Exit?Function
Dim?DwgF?As?FileStream????'文件流
Dim?PosSentinel?As?Integer??'文件描述塊的位置
Dim?br?As?BinaryReader??'讀取二進(jìn)制文件
Dim?TypePreview?As?Integer?'縮略圖格式
Dim?PosBMP?As?Integer?'縮略圖位置
Dim?LenBMP?As?Integer?'縮略圖大小
Dim?biBitCount?As?Short?'縮略圖比特深度
Dim?biH?As?BITMAPFILEHEADER?'BMP文件頭,DWG文件中不包含位圖文件頭,要自行加上去
Dim?BMPInfo()?As?Byte??'包含在DWG文件中的BMP文件體
Dim?BMPF?As?New?MemoryStream??'保存位圖的內(nèi)存文件流
Dim?bmpr?As?New?BinaryWriter(BMPF)?'寫二進(jìn)制文件類
Dim?myImg?As?Image
Try
DwgF?=?New?FileStream(FileName,?FileMode.Open,?FileAccess.Read)????'文件流
br?=?New?BinaryReader(DwgF)
DwgF.Seek(13,?SeekOrigin.Begin)?'從第十三字節(jié)開始讀取
PosSentinel?=?br.ReadInt32?'第13到17字節(jié)指示縮略圖描述塊的位置
DwgF.Seek(PosSentinel?+?30,?SeekOrigin.Begin)?'將指針移到縮略圖描述塊的第31字節(jié)
TypePreview?=?br.ReadByte?'第31字節(jié)為縮略圖格式信息,2?為BMP格式,3為WMF格式
Select?Case?TypePreview
Case?1
Case?2,?3
PosBMP?=?br.ReadInt32?'DWG文件保存的位圖所在位置
LenBMP?=?br.ReadInt32?'位圖的大小
DwgF.Seek(PosBMP?+?14,?SeekOrigin.Begin)?'移動(dòng)指針到位圖塊
biBitCount?=?br.ReadInt16?'讀取比特深度
DwgF.Seek(PosBMP,?SeekOrigin.Begin)?'從位圖塊開始處讀取全部位圖內(nèi)容備用
BMPInfo?=?br.ReadBytes(LenBMP)??'不包含文件頭的位圖信息
br.Close()
DwgF.Close()
With?biH??'建立位圖文件頭
.bfType?=?H4D42
If?biBitCount??9?Then?.bfSize?=?54?+?4?*?(2?^?biBitCount)?+?LenBMP?Else?.bfSize?=?54?+?LenBMP
.bfReserved1?=?0?'保留字節(jié)
.bfReserved2?=?0?'保留字節(jié)
.bfOffBits?=?14?+?H28?+?1024?'圖像數(shù)據(jù)偏移
End?With
'以下開始寫入位圖文件頭
bmpr.Write(biH.bfType)?'文件類型
bmpr.Write(biH.bfSize)?'文件大小
bmpr.Write(biH.bfReserved1)?'0
bmpr.Write(biH.bfReserved2)?'0
bmpr.Write(biH.bfOffBits)?'圖像數(shù)據(jù)偏移
bmpr.Write(BMPInfo)?'寫入位圖
BMPF.Seek(0,?SeekOrigin.Begin)?'指針移到文件開始處
myImg?=?Image.FromStream(BMPF)?'創(chuàng)建位圖文件對(duì)象
Return?myImg
bmpr.Close()
BMPF.Close()
End?Select
Catch?ex?As?Exception
Return?Nothing
End?Try
End?Function
如果可以的話請(qǐng)把分給我
以下是cad2007版的,引用autocad 2007 type library 和autocad/objectdbx common 17如果是04或者版本更低的只要引用autocad 2007 type library,代碼的話大同小異,思路是一樣的
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim acadapp As Autodesk.AutoCAD.Interop.AcadApplication
acadapp = GetObject(vbNullString, "autoCAD.application")
Dim acaddoc As Autodesk.AutoCAD.Interop.AcadDocument
acaddoc = acadapp.ActiveDocument
Dim Ms As Autodesk.AutoCAD.Interop.Common.AcadModelSpace
Ms = acaddoc.ModelSpace
Dim acadObjectI As Autodesk.AutoCAD.Interop.Common.AcadObject
Dim Linei As Autodesk.AutoCAD.Interop.Common.AcadLine
Dim Circlei As Autodesk.AutoCAD.Interop.Common.AcadCircle
Dim Polylinei As Autodesk.AutoCAD.Interop.Common.AcadPolyline
Dim pt As Autodesk.AutoCAD.Interop.Common.AcadPoint
For Each acadObjectI In Ms
Debug.Print(acadObjectI.ObjectName)
Select Case acadObjectI.ObjectName
Case "AcDbLine"
Linei = acadObjectI
Debug.Print("X =" Linei.StartPoint(0).ToString)
Debug.Print("Y =" Linei.StartPoint(1).ToString)
Case ""
Case ""
End Select
Next
End Sub
這個(gè)是vb.net教材里面的吧,我也是前兩天剛下的。遇到類似的問題,下面是我的解決辦法
首先要參考引用Autodesk.AutoCAD.Interop.dll還有Autodesk.AutoCAD.Interop.Common.dll
然后代碼有兩處也需要相應(yīng)修改,我用的是.NET 的 vb2008
'Dim AcadApp As AutoCAD.AcadApplication
Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call 連接AutoCAD()
End Sub
Sub 連接AutoCAD()
On Error Resume Next
AcadApp = GetObject(, "AutoCAD.Application")
If Err.Number Then
Err.Clear()
AcadApp = CreateObject("AutoCAD.Application")
If Err.Number Then
MsgBox("不能運(yùn)行AutoCAD,請(qǐng)檢查是否安裝了AutoCAD")
Exit Sub
End If
End If
AcadApp.Visible = True '界面可視
'AcadApp.WindowState = AutoCAD.AcWindowState.acMax '界面最大化
AcadApp.WindowState = Autodesk.AutoCAD.Interop.Common.AcWindowState.acMax
AppActivate(AcadApp.Caption) '顯示AutoCAD界面
End Sub