Code:
Public Class MySldWorksCls
Dim oSwAppCls As SldWorks.SldWorks Friend ReadOnly Property mysldworks() _
As SldWorks.SldWorks
Get
Return oSwAppCls
End Get
End Property
''' <summary>
''' Initialisiert as Solidworks Objekt durhc die Methode GetObject
''' und fragt in einer MsgBox nach wenn das Objek nicht greifbar iast.
''' </summary>
'''<param name="bUserAbbruch">
''' Optionaler Rückgabewert, welcher mitteilt ob der User
''' die Aktion abgebrochen hat oder ein Fehler aufgetreten ist.
''' </param>
''' <returns>Instanz der aktuellen SolidWorks Sitzung</returns>
''' <remarks></remarks>
Public Function SolidWorksInstanz(Optional ByRef bUserAbbruch As Boolean = False) As SldWorks.SldWorks
Try
Dim lDialogStatus As Microsoft.VisualBasic.MsgBoxResult
Try
oSwAppCls = CType(GetObject(, "SLDWORKS.Application"), SldWorks.SldWorks)
Catch ex As Exception
End Try
If oSwAppCls Is Nothing Then
lDialogStatus = MsgBox("Fehler beim Aufbau einer Schnittstelle zu Solidworks." & vbNewLine & "Bitte öffnen Sie SolidWorks und klicken SIe auf 'OK'." & vbNewLine & "Sollte Solidworks bereits geöffnet sein, " & vbNewLine & "Starten sie es neu." & vbNewLine & "Wenn Sie den Startvorgang abbrechen möchten klicken Sie auf 'Abbrechen'.", MsgBoxStyle.OkCancel, "Fehler")
Select Case lDialogStatus
Case MsgBoxResult.Ok
Return SolidWorksInstanz(bUserAbbruch)
Case MsgBoxResult.Cancel
bUserAbbruch = True
Return Nothing
End Select
End If
Return oSwAppCls
Catch ex As Exception
Debug.Assert(False)
MsgBox("Fehler: Wo: " & ex.StackTrace & " Was: " & ex.Message)
Return Nothing
End Try
End Function
Friend Function dokumentoeffnen(ByVal sDateipfad As String, Optional ByVal eOpenDocoptions As SwConst.swOpenDocOptions_e = SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent, Optional ByRef eOpenwarning As SwConst.swFileLoadWarning_e = 0) As SldWorks.ModelDoc2
Try
Dim oSwModel As SldWorks.ModelDoc2
Dim eError As SwConst.swDocumentTypes_e
Dim sExtension As String
Dim edoctyp As SwConst.swDocumentTypes_e
If IO.File.Exists(sDateipfad) = False Then
Throw New Exception("Kein SolidWorks Application " & " Objekt vorhanden")
End If
sExtension = IO.Path.GetExtension(sDateipfad)
Select Case sExtension
Case ".SLDPRT"
edoctyp = SwConst.swDocumentTypes_e.swDocPART
Case ".SLDASM"
edoctyp = SwConst.swDocumentTypes_e.swDocASSEMBLY
Case ".SLDDRW"
edoctyp = SwConst.swDocumentTypes_e.swDocDRAWING
Case Else
Throw New Exception("Ubültiges Format")
End Select
oSwModel = oSwAppCls.OpenDoc6(sDateipfad, edoctyp, eOpenDocoptions, "", CType(eError, SwConst.swFileLoadError_e), CType(eOpenwarning, SwConst.swFileLoadWarning_e))
If oSwModel Is Nothing Then
Throw New Exception("Das Dokument: " & sDateipfad & " konntenicht JJJJ werden. " & eError.ToString)
End If
Return oSwModel
Catch ex As Exception
Debug.Assert(False)
MsgBox("Fehler: Wo: " & ex.StackTrace & " Was: " & ex.Message)
Throw New Exception("Fehler beim Öffnen der Dokuments: " & sDateipfad & ".")
Return Nothing
End Try
End Function
Friend Function SolidWorksInstanzGet() As SldWorks.SldWorks
Try
oSwAppCls = CType(Microsoft.VisualBasic.GetObject([Class]:="Sldworks.application"), SldWorks.SldWorks)
Return oSwAppCls
Catch ex As Exception
Debug.Assert(False)
MsgBox("Fehler: Wo: " & ex.StackTrace & " Was: " & ex.Message)
Return Nothing
End Try
End Function
Friend Sub versionanzeigen()
Try
oSwAppCls.Visible = True
MsgBox("SolidWorks Version: " & oSwAppCls.RevisionNumber)
Catch ex As Exception
Debug.Assert(False)
MsgBox("Fehler: Wo: " & ex.StackTrace & " Was: " & ex.Message)
End Try
End Sub
Friend Function SolidWorksInstanzCreate() _
As SldWorks.SldWorks
Try
oSwAppCls = Microsoft.VisualBasic.CreateObject(ProgId:="SldWorks.Application")
Return oSwAppCls
Catch ex As Exception
Debug.Assert(False)
MsgBox("Fehler: Wo: " & ex.StackTrace & " Was: " & ex.Message)
Return Nothing
End Try
End Function
End Class