ST7 - Adding a material to the library

Could someone please provide an example of AddMaterialToLibrary in dotnet VB please.

What's the API expecting varPropList to contain?


Public Sub AddMaterialToLibrary( _
   ByVal bstrMatName As String, _
   ByVal bstrLibrary As String, _
   ByVal bstrMaterialPath As String, _
   ByVal lNumProps As Long, _
   ByVal varPropList As Variant, _
   ByVal bstrFaceStyle As String, _
   ByVal bstrFillStyle As String _

Re: ST7 - Adding a material to the library

I can't help you further, as I don't have solution.

I was also testing it trying to just pass an array of double values, an array of property indices and even a two-dimensional array with the property index and its value but all without success.

Similar to the Get method, this API should be enhanced to pass two arrays of same length, one with the property index and the other with the corresponding value. Unfortunately, who designed this API should have written some documentation on that but hasn't.


Please file an incident report to get the documentation or the API updated!!

Re: ST7 - Adding a material to the library

Hi Martin,


I really appreciate you spending some looking into this. I hope I can return the favour sometime!


I'll get an IR logged as you suggest and be sure to update the forum with any updates or information that I get back from Siemens.


Many thanks,


Re: ST7 - Adding a material to the library

I got some response from the developers and they told me that it is important to have an active document set and you need to pass exactly 9 property values in a specific order.

From this info I can add successfully a new material to the library, but ST 7 closes unexpectedly when releasing the COM object for the MatTable.

It also seems that the corresponding C++ code causes less problems.

The VB code below normally works fine, if you start SE without opening any document and then immediatly run the macro. Once a document was opened to check the material table, I get the explained problem of SE closing unexpectedly.

    ' APP: Add a new material to the material table
    ' IMPORTANT: We can only add material to a library, if an active document is set!!
    ' The active document can be on any type, as we will manipulate an external material document.
    Dim objDoc As SolidEdgeDocument = objApp.Documents.Add("SolidEdge.PartDocument")
    Dim objMatTable As MatTable = objApp.GetMaterialTable()
    Dim matLibList As Object = Nothing
    Dim nLibs As Integer = 0
    ' Set the active document in the material table
    ' Get the name of the first available material library (without path and extension)
    ' ST8 has a GetMaterialLibraryList() method to return the library names directly (without path and extension)
    ' For ST7, we need to query the fullnames and remove the path and extension
    objMatTable.GetMaterialLibraryFileList(matLibList, nLibs)
    Dim libInfo As New IO.FileInfo(matLibList(0))
    Dim strMatLibName As String = libInfo.Name.Substring(0, libInfo.Name.Length - libInfo.Extension.Length)
    ' Define exactly 9 material properties in this specific order (pay attention to the units!!):
    ' 1) Density [kg/m3]
    ' 2) Coef. of Thermal Exp. [1/ºC]
    ' 3) Thermal Conductivity [W/mºC]
    ' 4) Specific Heat [J/kgºC]
    ' 5) Modulus of Elasticity [Pa]
    ' 6) Poisson's Ratio [unitless]
    ' 7) Yield Stress [Pa]
    ' 8) Ultimate Stress [Pa]
    ' 9) Elongation % [unitless]
    Dim propList() As Object = {7888.0, 0.0, 51.0, 500.0, 69985996255.0, 711.0, 250000000.0, 745000000.0, 0.0}
    ' Now, add the new material in the subfolder "Others" of the found library
    Dim strMaterialName As String = "MyMaterial"
    Dim strMaterialPath As String = "Others"
    Dim strFaceStyle As String = "Copper"
    Dim strFillStyle As String = "ANSI33(Brass)"
    objMatTable.AddMaterialToLibrary(strMaterialName, strMatLibName, strMaterialPath, 9, propList, strFaceStyle, strFillStyle)
    Marshal.ReleaseComObject(objMatTable) ' causes ST7 to crash !!
    objMatTable = Nothing

Re: ST7 - Adding a material to the library

Just a follow up information:

The code from my previous post is working fine under ST8. If there is any document already open, you can use that one and don't need to temporarily create a new one.

Re: ST7 - Adding a material to the library

Thanks for your help with this Martin.

Re: ST7 - Adding a material to the library

If you need this functionality in ST7 please file an IR requesting the problem being fixed in a MP!!

The developers will only backport the fix, if someone is requesting for it.

Re: ST7 - Adding a material to the library

Yup, I understand.


For now, I'm continuing to do what I've been doing since ST6, which is exporting the material table to xml, adding the material to the xml, then reading it back in.


With the changes made to material handling in ST7, I was expecting the materials api to have also seen an overhaul. Whilst this does appear to be the case (some new functions to accomodate multiple libraries) AddMaterial is still proving to be far more complex (stubborn!) than it needs to be (IMO).


I think I'll live with the xml route for the time being.