Reply
Solved! Go to solution

Import of materials from the Excel table

Hello colleagues!

 

In our company there is a task to periodically add new materials to the table of materials "SE". New materials are created in the "Teamcenter", then I upload them to the "Excel" table, add the uploaded materials to the standard "Excel" material table, and then I:

 

1. open the "SE"
2. Run the model file
3. I open the table of materials
4. choose my database of materials
5. Choose Import
6. I select the "Excel" table.

 

 In this regard, the question is, can someone already have code that does this automatically, and if not, maybe there is a desire to help me write it (this will be my first macro for "SE")?

 

In my case, I want to achieve the following, I download the file "Excel" from the "Teamcenter", I press the "Start" button in it, the code is written there, the table is processed, then it is saved to a new file, with a specific name (it already exists), then The code for importing the materials is executed.

 

I will be happy with any information!

 

P.S. I found a topic that describes how to add one material to the table, but it seems to me that it's a little different.

 

Thanks in advance!

5 REPLIES

Re: Import of materials from the Excel table

[ Edited ]

Found the answer to my question here.

 

In "Visual Studio" created a console application, added a link to the "SE" api, copy the code, commented out what I did not understand, ran and got an error.

 

Tell me please what can be the problem?

 Error.PNG 

 

 

 

What type of application do I need to create in order to achieve the desired result.

P.S. Please tell me how to transfer the path to the file "Excel" at the start of my application

Re: Import of materials from the Excel table

If you are requesting review of your code please provide siad code.

 

It's quite possible that one of the lines you commetned out was required or part of error handeling...

 

 

 

===================================================
Matt Johnson
Solid Edge Certified Professional (ST7 & ST8)
Solid Edge ST9 (SEEC) - Production & Solid Edge ST10 - Testing
Teamcenter 10.1.6
NX10

Re: Import of materials from the Excel table

[ Edited ]

I'm sorry, I forgot to put the code.
Here he is.

 

The words "Операция надоступна ( исключение из" mean -

"Operation not available (the exception of" 

 

 The error says that the problem occurred in line 17

objApp = Marshal.GetActiveObject("SolidEdge.Application")

 

Imports System.Runtime.InteropServices
Module Module1
    Sub Main()

        'Public Class Form1
        'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objApp As SolidEdgeFramework.Application = Nothing
        Dim objType As Type = Nothing
        Dim objMatTable As SolidEdgeFramework.MatTable = Nothing
        Dim strMaterialFolderPath As String = ""
        Dim strLibrary As String = "1"
        Dim varPropList As Object = Nothing

        Try
            ' Get SE handle
            objApp = Marshal.GetActiveObject("SolidEdge.Application")

            If objApp Is Nothing Then
                ' Get the type from the Solid Edge ProgID
                objType = Type.GetTypeFromProgID("SolidEdge.Application")
                ' Start Solid Edge
                objApp = Activator.CreateInstance(objType)
                ' Make Solid Edge visible
                objApp.Visible = True
            End If
            ' Get material table object handle
            objMatTable = objApp.GetMaterialTable()

            Dim strXMLLibraryPath As String = "\\fs2\SE_NAUKA\Nastrojjki_SE\Material\ST8\Excel\2017_05_17_Materials_FOR_IMPORT.xlsx"

            objMatTable.ImportMaterialDataFromFile(strXMLLibraryPath, strLibrary)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'End Class
        'End Sub
    End Sub

End Module

 

 

 

Solution
Solution
Accepted by topic author NaukaSE
yesterday

Re: Import of materials from the Excel table

[ Edited ]

do you have SE open when you try to run this?

 

GetActiveObject("SolidEdge.Application")  implies that you are trying to connect to a running instance of Solid Edge

 

i assume you have no SE running and thus the error.  you need some error handeling between line 17 and 19...

 

line 17 trys to connect. 

line 19 - ?  if statment will open SE... 

 

but there is no error catch to pass from 17 - 19...

 

Note that in the example this whole portion of code was enclosed in teh try-catch-finally

 

Add that back and you should be good

 

HTH

===================================================
Matt Johnson
Solid Edge Certified Professional (ST7 & ST8)
Solid Edge ST9 (SEEC) - Production & Solid Edge ST10 - Testing
Teamcenter 10.1.6
NX10
Highlighted

Re: Import of materials from the Excel table

Did so, works, thanks.

Tell me please also how to make it so that when you start the application (from VBA), you can pass it to the input value for the variable strXMLLibraryPath 

 

 

Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1
    <STAThread()>
    Sub Main()

        'Public Class Form1
        'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objApp As SolidEdgeFramework.Application = Nothing
        Dim objType As Type = Nothing
        Dim objMatTable As SolidEdgeFramework.MatTable = Nothing
        Dim strMaterialFolderPath As String = ""
        Dim strLibrary As String = "1"
        Dim varPropList As Object = Nothing

        Try
            Try
                OleMessageFilter.Register()

                ' Connect to a running instance of Solid Edge
                objApp = Marshal.GetActiveObject("SolidEdge.Application")

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            Finally
                OleMessageFilter.Revoke()
            End Try

            If objApp Is Nothing Then
                ' Get the type from the Solid Edge ProgID
                objType = Type.GetTypeFromProgID("SolidEdge.Application")
                ' Start Solid Edge
                objApp = Activator.CreateInstance(objType)
                ' Make Solid Edge visible
                objApp.Visible = True
            End If
            ' Get material table object handle
            objMatTable = objApp.GetMaterialTable()

            Dim strXMLLibraryPath As String = "\\fs2\SE_NAUKA\Nastrojjki_SE\Material\ST8\Excel\2017_05_17_Materials_FOR_IMPORT.xlsx"

            objMatTable.ImportMaterialDataFromFile(strXMLLibraryPath, strLibrary)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'End Class
        'End Sub
    End Sub

End Module