重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
vb2010(vb.net)貌似已经没有OLE控件
创新互联公司是一家专注于做网站、成都做网站与策划设计,中原网站建设哪家好?创新互联公司做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:中原等地区。中原做网站价格咨询:028-86922220
下面的代码是用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 '读取二进制文件
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 '保存位图的内存文件流
Dim bmpr As New BinaryWriter(BMPF) '写二进制文件类
Dim myImg As Image
Try
DwgF = New FileStream(FileName, FileMode.Open, FileAccess.Read) '文件流
br = New BinaryReader(DwgF)
DwgF.Seek(13, SeekOrigin.Begin) '从第十三字节开始读取
PosSentinel = br.ReadInt32 '第13到17字节指示缩略图描述块的位置
DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin) '将指针移到缩略图描述块的第31字节
TypePreview = br.ReadByte '第31字节为缩略图格式信息,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) '移动指针到位图块
biBitCount = br.ReadInt16 '读取比特深度
DwgF.Seek(PosBMP, SeekOrigin.Begin) '从位图块开始处读取全部位图内容备用
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 '保留字节
.bfReserved2 = 0 '保留字节
.bfOffBits = 14 + H28 + 1024 '图像数据偏移
End With
'以下开始写入位图文件头
bmpr.Write(biH.bfType) '文件类型
bmpr.Write(biH.bfSize) '文件大小
bmpr.Write(biH.bfReserved1) '0
bmpr.Write(biH.bfReserved2) '0
bmpr.Write(biH.bfOffBits) '图像数据偏移
bmpr.Write(BMPInfo) '写入位图
BMPF.Seek(0, SeekOrigin.Begin) '指针移到文件开始处
myImg = Image.FromStream(BMPF) '创建位图文件对象
Return myImg
bmpr.Close()
BMPF.Close()
End Select
Catch ex As Exception
Return Nothing
End Try
End Function
' 首先在工程里引用 AutoCAD 类型库,例如 CAD2004的 “AutoCAD 2004 Type Library”
' 下面的代码在CAD里画一个圆,圆心在(100,100,0)处,半径为 50
Dim Acadapp As AcadApplication
On Error Resume Next
Set Acadapp = GetObject(, "AutoCAD.Application") ' 连接 CAD
If Err Then
Err.Clear
Set Acadapp = CreateObject("AutoCAD.Application") ' 如果CAD没有打开,则打开一个新的CAD
If Err Then
MsgBox Err.Description ' 如果打开CAD失败显示错误信息
Exit Sub
End If
End If
Dim circleObj As AcadCircle
Dim point1(0 To 2) As Double, dR As Double
point1(0) = 100#: point1(1) = 100#: point1(2) = 0# ' 定义圆心坐标
dR = 50# ' 定义圆半径
Set circleObj = Acadapp.ActiveDocument.ModelSpace.AddCircle(point1, dR) '按定义圆心和半径画出一个圆
想要更详细的学习,建议看CAD帮助里的 VBA 部分。
CAD里的结点是什么信息了,要输出世界坐标还是用户坐标,最好把CAD文件发上来,不清楚怎么有人能帮你??
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