使用OpenDoc6打开工程图文档

时间:2019-07-12 19:15:17

标签: vb.net api solidworks

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

swApp为空。有两种初始化方法,具体取决于您是要附加到现有SOLIDWORKS会话还是要创建新会话。

要创建新会话,请执行以下操作:

private static GlyphLayout glyphLayout = new GlyphLayout();
public static void drawCentered(BitmapFont font, SpriteBatch spriteBatch,
                                    String text, Rectangle bounds) {
        glyphLayout.setText(font, text, Color.BLACK, bounds.width, Align.center, true);
        font.draw(
                spriteBatch,
                text,
                bounds.x,
                bounds.y + bounds.height / 2f + glyphLayout.height / 2f,
                bounds.width,
                Align.center,
                true);
    }

要附加到现有会话,请执行以下操作:

swApp = New SldWorks()
swApp.Visible = True