2012-07-17 63 views
0

非常類似於 Solidworks API and Winforms C# swApp (Standalone) 但是,我似乎無法得到我的應用程序工作的解決方案。我已經創建了一個可以創建一個新的繪圖的宏,但是你會得到一個帶有兩個按鈕的Winform,一個按鈕可以創建另一個宏,而另一個則可以創建另一個宏。我似乎無法得到按鈕的工作。Solidworks API和Winforms VB.NET swApp

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., 0.028482, 0), SketchSegment) 
    skSegment = CType(swDoc.SketchManager.CreateLine(0., 0.028482, 0, 0.09885, -0.060241, 0), SketchSegment) 
    swDoc.ClearSelection2(True) 

End Sub 
Public swApp As SldWorks 

我一直就行swDoc得到一個空引用異常= CTYPE(swApp等

在此先感謝

回答

0
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