4

I am trying to open drawing documents using vb.net OpenDoc6 command. This is a snippet of my code where i try to traverse through all the files and try to open drawings.

Whenever it tries to open the file, it gives me a nullreferenceexception. I am not able to pin this down on one particular thing. It could be the parameters that i am trying to pass when calling 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
RobertBaron
  • 2,817
  • 1
  • 12
  • 19
Rahul
  • 903
  • 1
  • 10
  • 27
  • Obvious first step, is swApp null? How are you initializing it? It might not be attaching to the solidworks app properly – AndrewK Jul 13 '19 at 01:58
  • You would need to show more code. In particular, the initialization of `CurFolder` and `swApp`. – RobertBaron Jul 13 '19 at 11:12
  • @RobertBaron I put in the entire code. This is my first time doing Solidworks API so please let me know if i can improve my code . – Rahul Jul 15 '19 at 14:21
  • 1
    I am not familiar with SolidWorks, but I think you need to create the SolidWorks object, something like `Set swApp = CreateObject("SldWorks.Application")` or `Set swApp = Application.SldWorks`. – RobertBaron Jul 15 '19 at 14:41
  • @AndrewK I put in the entire code. Can you take a look as well – Rahul Jul 15 '19 at 19:14

1 Answers1

3

swApp is null. There are a couple ways to initialize depending if you want to attach to an existing SOLIDWORKS session or create a new one.

To create a new session:

swApp = New SldWorks()
swApp.Visible = True

To attach to an existing session:

swApp = CType(Marshal.GetActiveObject("SldWorks.Application"), ISldWorks)
AndrewK
  • 1,223
  • 2
  • 16
  • 25
  • Can you check this question out: https://stackoverflow.com/questions/57062906/changing-custom-properties-of-the-revision-tables-in-drawings – Rahul Jul 16 '19 at 20:17