我正在尝试使用 vb.net OpenDoc6 命令打开绘图文档。这是我的代码片段,我尝试遍历所有文件并尝试打开图纸。
每当它尝试打开文件时,它都会给我一个 nullreferenceexception。我无法将其固定在一件特定的事情上。这可能是我在调用 OpenDoc 时试图传递的参数。
Imports EPDM.Interop.epdm
Imports SldWorks
Imports SolidWorks.Interop
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
Public Class TraverseFilesFolders
Dim swApp As SldWorks.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swModDoc As SldWorks.ModelDoc2
Dim longstatus As Integer = 0
Dim longwarning As Integer = 0
Dim vault As IEdmVault17
Public Sub TraverseFilesFolders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim Views() As EdmViewInfo = Nothing
Dim vault5 As IEdmVault5 = New EdmVault5()
vault = DirectCast(vault5, IEdmVault17)
vault.GetVaultViews(Views, False)
VaultsComboBox.Items.Clear()
For Each View As EdmViewInfo In Views
VaultsComboBox.Items.Add(View.mbsVaultName)
Next
If VaultsComboBox.Items.Count > 0 Then
VaultsComboBox.Text = VaultsComboBox.Items(0)
End If
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + vbCrLf + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub TraverseFoldersButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TraverseFoldersButton.Click
Try
'Log into selected vault as the current user
vault.LoginAuto(VaultsComboBox.Text, Me.Handle.ToInt32())
MessageBox.Show(vault.GetVaultType().ToString(), "Vault type")
Dim log As String = Nothing
vault.GetClientLog(log)
'TextBox1.Text = log
TraverseFolder(vault.RootFolder)
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + vbCrLf + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub TraverseFolder(ByVal CurFolder As IEdmFolder5)
Try
'Enumerate the files in the folder
Dim FilePos As IEdmPos5
FilePos = CurFolder.GetFirstFilePosition
Dim file As IEdmFile5
While Not FilePos.IsNull
file = CurFolder.GetNextFile(FilePos)
'Get its checked out status
If file.IsLocked Then
ListBox2.Items.Add(file.LockPath)
ElseIf Not file.IsLocked And file.GetLocalPath(CurFolder.ID).ToString.Contains("SLDDRW") Then
MessageBox.Show(file.GetLocalPath(CurFolder.ID).ToString)
Try
swModDoc = swApp.OpenDoc6(file.GetLocalPath(CurFolder.ID).ToString, 3, 0, "", longstatus, longwarning)
Catch ex As NullReferenceException
MessageBox.Show(ex.ToString)
End Try
End If
End While
'Enumerate the subfolders in the folder
Dim FolderPos As IEdmPos5
FolderPos = CurFolder.GetFirstSubFolderPosition
While Not FolderPos.IsNull
Dim SubFolder As IEdmFolder5
SubFolder = CurFolder.GetNextSubFolder(FolderPos)
TraverseFolder(SubFolder)
End While
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + vbCrLf + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
最佳答案
swApp 为空。根据您是要附加到现有 SOLIDWORKS session 还是创建新 session ,有多种初始化方法。
要创建新 session :
swApp = New SldWorks()
swApp.Visible = True
要附加到现有 session :
swApp = CType(Marshal.GetActiveObject("SldWorks.Application"), ISldWorks)
关于vb.net - 使用 OpenDoc6 打开工程图文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012879/