GetCurrentMaterialName - but in which library file

Phenom
Phenom

Hi Edgers,



when trying to find out which material is currently assigned to my part I can use the API call 

 

objMaterialTable.GetCurrentMaterialName ( pDocument, strMaterialName)

 

So far no problem.

But, what if there are several material libraries in use, how can I find out which lib is it, where this material is saved?

When interactiveley clicking in the material table, SE shows the long pathname/material name, but the API call only returns the short end name of the material.

 

20170911_1008.PNG

 

 

Why do I want to get that info.

Knowing which lib is in use, I can try to access the gage information of it for further selelction and usage.

 

Does anybody have any idea how to find out the material lib of the current material?

 

Thanks in advance

 



regards
Wolfgang
4 REPLIES

Re: GetCurrentMaterialName - but in which library file

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Wolfgang,

you can simply search for the materials in all libraries like below:

'----------------------------------------------------------------------
' PSM: Find the library of a material for a sheetmetal document
'----------------------------------------------------------------------
Dim objDoc As SheetMetalDocument = objApp.ActiveDocument
Dim objMatTable As MatTable = objApp.GetMaterialTable()
Dim strMaterial As String = ""
objMatTable.GetCurrentMaterialName(objDoc, strMaterial)
Dim strLibrary = GetLibraryName(objMatTable, strMaterial)
Console.WriteLine("Material [" & strMaterial & "] can be found in library [" & strLibrary & "]")

Function GetLibraryName(objMatTable As MatTable, strMaterial As String) As String
  Try
      Dim nLibraries As Integer = 0
      Dim libraries() As Object = Nothing
      objMatTable.GetMaterialLibraryList(libraries, nLibraries)
      For Each libName As String In libraries
        Dim nMaterials As Integer = 0
        Dim materials() As Object = Nothing
        objMatTable.GetMaterialListFromLibrary(libName, nMaterials, materials)
        If Array.IndexOf(materials, strMaterial) >= 0 Then
          Return libName
        End If
      Next
  Catch ex As Exception
      Console.WriteLine("EXC: " & ex.Message)
  End Try
  Return Nothing
End Function

Re: GetCurrentMaterialName - but in which library file

Phenom
Phenom

Hi @MartinBernhard

 

 

thanks for Your answer.

And it seems as it is exactly I supposed it to be, You have to run through all the libraries and compare every value with the Current Material 

 

I hoped that there will be a hidden call for get it faster.

 

 



regards
Wolfgang

Re: GetCurrentMaterialName - but in which library file

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

The call is quite fast, you don't need to worry about speed unless you will call it several thousand times.

But then, you might cache the libraries and their materials in memory ;-)

Re: GetCurrentMaterialName - but in which library file

Phenom
Phenom

Hi @MartinBernhard

 

 

 

thanks, Yes code (similar code) works perfectly and fast enbough.

 

There are not that much libraries in use, and any of them doesn't have so much materials defined to run through.

 

Again,  thanks to bring me into the right direction

 

 



regards
Wolfgang