主题:关于实现DWG文件的读取和储存vb实例中源代码的疑问!求解答
部分窗体源代码如下(所有源代码已打包上传):
Option Explicit
Private Const OdMouseAction_None = 0
Private Const OdMouseAction_PanHor = 1
Private Const OdMouseAction_RotateX = 2
Private Const OdMouseAction_RotateY = 3
Private Const OdMouseAction_RotateZ = 4
Private Const OdMouseAction_ZoomIn = 5
Private Const OdMouseAction_ZoomOut = 6
Private Const OdMouseAction_ZoomAll = 7
Private Const OdMouseAction_ZoomExt = 8
Private Const OdMouseAction_PanVert = 9
Dim oApp As odwApplication
Dim odaDoc As DWGdirectXCtl.AcadDocument
Dim oDevice As OdaDevice
Dim nCurrentMouseAction As Long
Dim RendererPath
Dim RecentDWG
Private Sub BtnOpen_Click()
Dim sFile As String
Dim nViewCount, nI As Long
Dim bkColor(0 To 2) As Long
On Error GoTo errhandle
oApp.FilePreferences.SupportPath = "D:\Autodesk\MDT\support;C:\WINNT\Fonts;"
Me.MousePointer = vbHourglass
With CommonDialog
.DialogTitle = "Open"
.CancelError = True
.Filter = "Autocad Drawing (*.dwg)|*.dwg|Autocad Exchange (*.dxf)|*.dxf"
.FileName = RecentDWG
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
RecentDWG = sFile
End With
DoEvents
Set odaDoc = oApp.Documents.Open(sFile)
DoEvents
StatusBar.Panels(1).Text = sFile & " was opened"
Me.MousePointer = vbDefault
BtnOpen.Enabled = False
BtnClose.Enabled = True
On Error GoTo viewerr
Frame1.Enabled = False
Set oDevice = OdaViewer.DeviceManager.NewDevice(GetRendererPath())
oDevice.SetupActiveLayoutViews odaDoc.Database
bkColor(0) = 0: bkColor(1) = 0: bkColor(2) = 0
oDevice.BackgroundColor = bkColor
nViewCount = oDevice.ViewCount
If nViewCount > 0 Then
Dim tmpView As OdaView
bkColor(0) = 250: bkColor(1) = 0: bkColor(2) = 0
For nI = 1 To nViewCount
Set tmpView = oDevice.Item(nI - 1)
tmpView.ViewportBorderVisible = True
tmpView.ViewportBorderColor = bkColor
Next nI
End If
oDevice.Update
Toolbar1.Enabled = True
nCurrentMouseAction = OdMouseAction_None
Exit Sub
errhandle:
Me.StatusBar.Panels(1).Text = "Failed to load file."
Me.MousePointer = vbDefault
Exit Sub
viewerr:
MsgBox Err.Description
Me.StatusBar.Panels(1).Text = "Failed to create view device."
End Sub
Private Sub Form_Load()
Set oApp = New odwApplication
oApp.SetControl StatusBar
BtnOpen.Enabled = True
BtnClose.Enabled = False
Frame1.Enabled = True
Toolbar1.Enabled = False
nCurrentMouseAction = OdMouseAction_None
' set the module level callback pointer
lpFormObj = ObjPtr(Me)
SetProp Me.hwnd, "PrevWndProc", SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
If GetSystemMetrics(SM_MOUSEWHEELPRESENT) Then
' MsgBox "A simple call to GetSystemMetrics tells you whether or not the mouse has a wheel. The mouse connected to this computer does have a wheel.", vbInformation + vbOKOnly, App.Title
Debug.Print "Yes Wheel"
Else
' MsgBox "A simple call to GetSystemMetrics tells you whether or not the mouse has a wheel. The mouse connected to this computer doesn't have a wheel.", vbInformation + vbOKOnly, App.Title
Debug.Print "No Wheel"
End If
End Sub
Private Sub Form_Resize()
If Me.Height > Me.StatusBar.Height + Me.Frame1.Height + 45 Then
OdaViewer.Width = Me.Width - 340 - Me.Toolbar1.Width
OdaViewer.Height = Me.Height - Me.StatusBar.Height - Me.Frame1.Height - 500
End If
End Sub
Private Function GetRendererPath()
If RendererPath = "" Then
BroseRenderer
End If
GetRendererPath = RendererPath
End Function
Private Sub BroseRenderer()
With CommonDialog
.DialogTitle = "Select Renderer Module"
.CancelError = True
.Filter = "DWGdirect Renderer (*.*)|*.*"
.FileName = RendererPath
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
RendererPath = .FileName
SelectRenderer.ToolTipText = RendererPath
End With
End Sub
Private Sub SelectRenderer_Click()
On Error GoTo errhandle
BroseRenderer
errhandle:
End Sub
程序运行过程中单击Open按钮并选择dwg文件后,会继续跳出select renderer module对话框,此处是因为该语句中Set oDevice = OdaViewer.DeviceManager.NewDevice(GetRendererPath()) 存在GetRendererPath()函数,不知此处到底选择怎样的文件,才不会出现“自动化错误”这样的问题而导致创建view device失败。此问题已困扰我很久,望高手指点,谢谢!
Option Explicit
Private Const OdMouseAction_None = 0
Private Const OdMouseAction_PanHor = 1
Private Const OdMouseAction_RotateX = 2
Private Const OdMouseAction_RotateY = 3
Private Const OdMouseAction_RotateZ = 4
Private Const OdMouseAction_ZoomIn = 5
Private Const OdMouseAction_ZoomOut = 6
Private Const OdMouseAction_ZoomAll = 7
Private Const OdMouseAction_ZoomExt = 8
Private Const OdMouseAction_PanVert = 9
Dim oApp As odwApplication
Dim odaDoc As DWGdirectXCtl.AcadDocument
Dim oDevice As OdaDevice
Dim nCurrentMouseAction As Long
Dim RendererPath
Dim RecentDWG
Private Sub BtnOpen_Click()
Dim sFile As String
Dim nViewCount, nI As Long
Dim bkColor(0 To 2) As Long
On Error GoTo errhandle
oApp.FilePreferences.SupportPath = "D:\Autodesk\MDT\support;C:\WINNT\Fonts;"
Me.MousePointer = vbHourglass
With CommonDialog
.DialogTitle = "Open"
.CancelError = True
.Filter = "Autocad Drawing (*.dwg)|*.dwg|Autocad Exchange (*.dxf)|*.dxf"
.FileName = RecentDWG
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
RecentDWG = sFile
End With
DoEvents
Set odaDoc = oApp.Documents.Open(sFile)
DoEvents
StatusBar.Panels(1).Text = sFile & " was opened"
Me.MousePointer = vbDefault
BtnOpen.Enabled = False
BtnClose.Enabled = True
On Error GoTo viewerr
Frame1.Enabled = False
Set oDevice = OdaViewer.DeviceManager.NewDevice(GetRendererPath())
oDevice.SetupActiveLayoutViews odaDoc.Database
bkColor(0) = 0: bkColor(1) = 0: bkColor(2) = 0
oDevice.BackgroundColor = bkColor
nViewCount = oDevice.ViewCount
If nViewCount > 0 Then
Dim tmpView As OdaView
bkColor(0) = 250: bkColor(1) = 0: bkColor(2) = 0
For nI = 1 To nViewCount
Set tmpView = oDevice.Item(nI - 1)
tmpView.ViewportBorderVisible = True
tmpView.ViewportBorderColor = bkColor
Next nI
End If
oDevice.Update
Toolbar1.Enabled = True
nCurrentMouseAction = OdMouseAction_None
Exit Sub
errhandle:
Me.StatusBar.Panels(1).Text = "Failed to load file."
Me.MousePointer = vbDefault
Exit Sub
viewerr:
MsgBox Err.Description
Me.StatusBar.Panels(1).Text = "Failed to create view device."
End Sub
Private Sub Form_Load()
Set oApp = New odwApplication
oApp.SetControl StatusBar
BtnOpen.Enabled = True
BtnClose.Enabled = False
Frame1.Enabled = True
Toolbar1.Enabled = False
nCurrentMouseAction = OdMouseAction_None
' set the module level callback pointer
lpFormObj = ObjPtr(Me)
SetProp Me.hwnd, "PrevWndProc", SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
If GetSystemMetrics(SM_MOUSEWHEELPRESENT) Then
' MsgBox "A simple call to GetSystemMetrics tells you whether or not the mouse has a wheel. The mouse connected to this computer does have a wheel.", vbInformation + vbOKOnly, App.Title
Debug.Print "Yes Wheel"
Else
' MsgBox "A simple call to GetSystemMetrics tells you whether or not the mouse has a wheel. The mouse connected to this computer doesn't have a wheel.", vbInformation + vbOKOnly, App.Title
Debug.Print "No Wheel"
End If
End Sub
Private Sub Form_Resize()
If Me.Height > Me.StatusBar.Height + Me.Frame1.Height + 45 Then
OdaViewer.Width = Me.Width - 340 - Me.Toolbar1.Width
OdaViewer.Height = Me.Height - Me.StatusBar.Height - Me.Frame1.Height - 500
End If
End Sub
Private Function GetRendererPath()
If RendererPath = "" Then
BroseRenderer
End If
GetRendererPath = RendererPath
End Function
Private Sub BroseRenderer()
With CommonDialog
.DialogTitle = "Select Renderer Module"
.CancelError = True
.Filter = "DWGdirect Renderer (*.*)|*.*"
.FileName = RendererPath
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
RendererPath = .FileName
SelectRenderer.ToolTipText = RendererPath
End With
End Sub
Private Sub SelectRenderer_Click()
On Error GoTo errhandle
BroseRenderer
errhandle:
End Sub
程序运行过程中单击Open按钮并选择dwg文件后,会继续跳出select renderer module对话框,此处是因为该语句中Set oDevice = OdaViewer.DeviceManager.NewDevice(GetRendererPath()) 存在GetRendererPath()函数,不知此处到底选择怎样的文件,才不会出现“自动化错误”这样的问题而导致创建view device失败。此问题已困扰我很久,望高手指点,谢谢!