Code:
Public Shared Function GetCATIAInterfaceLanguage3() As Integer
Dim workdoc As Object = GetWorkDocument() Dim paras As Parameters = workdoc.Parameters
Dim pp As Parameter
Try
pp = paras.GetItem("Part Number")
If pp IsNot Nothing Then
Return 714 'English
End If
Catch x As Exception
End Try
Try
pp = paras.GetItem("Teilenummer")
If pp IsNot Nothing Then
Return 602 'German
End If
Catch x As Exception
End Try
Try
pp = paras.GetItem("Référence")
If pp IsNot Nothing Then
Return 598 'Francais
End If
Catch x As Exception
End Try
Try
pp = paras.GetItem("Numero parte")
If pp IsNot Nothing Then
Return 706 'Italiano
End If
Catch x As Exception
End Try
'well well well... now there are some languages like Japanese and Chinese, whose Strings CANNOT be entered into Visual Studio
'this is really a pity.
Throw New Exception("Language not detectable by programmatic means. Please contact your Dassault Software Experience Senior Deployment Developer Consultant Specialist Expert.")
End Function
''' <summary>
''' Returns the root Part or an Activated Top Level Product
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetWorkDocument() As Object
Dim ReturnObject As Object = Nothing
ReturnObject = getPartFromCATIA()
If ReturnObject Is Nothing Then
ReturnObject = getProductFromCATIA()
ReturnObject = ActivateTopLevelProduct(ReturnObject)
End If
Return ReturnObject
End Function
''' <summary>
''' Retrieves the part
''' </summary>
''' <remarks></remarks>
Public Shared Function getPartFromCATIA() As MECMOD.Part
Dim oPart = Nothing
Dim CATIAapp As INFITF.Application = getCATIA()
If CATIAapp Is Nothing Then
Return Nothing
End If
Try
If TypeName(CATIAapp.ActiveDocument) = "PartDocument" Then
oPart = CATIAapp.ActiveDocument.Part
End If
Catch COMex As System.Runtime.InteropServices.COMException
'TODO? error handling here?
End Try
Return oPart
End Function
''' <summary>
''' Retrieves the product
''' </summary>
''' <remarks></remarks>
Public Shared Function getProductFromCATIA() As ProductStructureTypeLib.Product
Dim oProduct = Nothing
Dim CATIAapp As INFITF.Application = getCATIA()
If CATIAapp Is Nothing Then
Return Nothing
End If
Try
If TypeName(CATIAapp.ActiveDocument) = "ProductDocument" Then
Dim oSelection As INFITF.Selection = GetSelection()
Try
oSelection.Clear()
oSelection.Search("CATProductSearch.Product,in")
'TODO: what happens if oSelection.Count was 0?!?
If oSelection.Count > 0 Then
Dim oCheckProd
oCheckProd = oSelection.Item(1).Value
oProduct = oCheckProd.ReferenceProduct
End If
oSelection.Clear()
'if the Product structure has not been loaded yet, we force that:
oProduct.ApplyWorkMode(DESIGN_MODE)
Catch ex As Exception
Throw New Exception("Error retrieving product: " & ex.Message)
End Try
End If
Catch COMex As System.Runtime.InteropServices.COMException
Throw New Exception("Error retrieving product: " & COMex.Message)
End Try
Return oProduct
End Function
''' <summary>
''' Retrieves a handle to CATIA/DELMIA via InteropServices
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function getCATIA() As INFITF.Application
Try
If Process.GetProcessesByName("cnext").Length = 0 Then
MessageBox.Show("No CATIA Application launched. Exiting.", "CATIA not found.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
ElseIf Process.GetProcessesByName("cnext").Length > 1 Then
MessageBox.Show("Multiple CATIA instances detected. Please ensure that only one CATIA instance is running before launching FFA Next Generation", "Multiple CATIA instances found.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End If
Dim appl As Object = System.Runtime.InteropServices.Marshal.GetActiveObject("CATIA.Application")
Return CType(appl, INFITF.Application)
Catch ex As System.Runtime.InteropServices.COMException
Try
Dim appl As Object = System.Runtime.InteropServices.Marshal.GetActiveObject("DELMIA.Application")
Return CType(appl, INFITF.Application)
Catch ex2 As System.Runtime.InteropServices.COMException
MessageBox.Show("CATIA.Application (CNEXT.exe) not found! Start V5 application first." & System.Environment.NewLine & System.Environment.NewLine & ex2.Message, "CATIA not found.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
MessageBox.Show("DELMIA.Application (DELMIA.exe) not found! Start V5 application first." & System.Environment.NewLine & System.Environment.NewLine & ex.Message, "DELMIA not found.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
Shared Function GetSelection() As INFITF.Selection
Return getCATIA().ActiveDocument.Selection
End Function
''' <summary>
''' Activates and returns the top level product
''' </summary>
''' <param name="checkProd"></param>
''' <remarks></remarks>
Public Shared Function ActivateTopLevelProduct(checkProd As Product) As Product
If checkProd Is Nothing Then
Throw New ArgumentNullException("Cannot activate top level product when product to be checked is NULL.")
End If
Dim ActivatedTopLevelProduct As Product
Dim oProd As INFITF.Document = getCATIA.ActiveDocument
If oProd.Product.Name <> checkProd.Name Then
oProd.Product.ReferenceProduct.ApplyWorkMode(DESIGN_MODE)
ActivatedTopLevelProduct = oProd.Product.ReferenceProduct
ActivatedTopLevelProduct.Parent.Activate()
Else
ActivatedTopLevelProduct = oProd.Product.ReferenceProduct
End If
Return ActivatedTopLevelProduct
End Function