vb.netsolidworksapi

Solidworks API and Winforms VB.NET swApp


Very similar to Solidworks API and Winforms C# swApp (Standalone) however I cant seem to get the Solution on my application working. I have made a Macro that creates a new drawing which runs, however then you get a winform that has two buttons, one button does another macro while the other does a different one. I can't seem to get the buttons working.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim swDoc As ModelDoc2 = Nothing
    Dim swPart As PartDoc = Nothing
    Dim swDrawing As DrawingDoc = Nothing
    Dim swAssembly As AssemblyDoc = Nothing
    Dim boolstatus As Boolean = False
    Dim longstatus As Integer = 0
    Dim longwarnings As Integer = 0
    swDoc = CType(swApp.ActiveDoc, ModelDoc2)
    swDoc.ClearSelection2(True)
    Dim skSegment As SketchSegment
    skSegment = CType(swDoc.SketchManager.CreateLine(-0.053838, 0.028482, 0, -0.003395, -0.060241, 0), SketchSegment)
    skSegment = CType(swDoc.SketchManager.CreateLine(-0.003395, -0.060241, 0, 0.012312, 0.028482, 0), SketchSegment)
    skSegment = CType(swDoc.SketchManager.CreateLine(0.012312, 0.028482, 0, 0.09885, -0.060241, 0), SketchSegment)
    swDoc.ClearSelection2(True)

End Sub
Public swApp As SldWorks

I keep getting a null reference exception on the line swDoc= CType(swApp etc

Thanks in advance


Solution

  •    Dim swApp As SldWorks
        swApp = Nothing
        Try
            swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks)
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try