stl file contains multiple parts / individual material parameters needed

Hello guys,

I have a stl file which contains 4 parts (see “test.stl” in the attachments).

As a first step I import this file and create a ngsolve mesh. Now I can print the following information:

print("materials = ", mesh.GetMaterials()):
materials = (‘default’,)

print("boundaries = ", mesh.GetBoundaries()):
boundaries = (‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’, ‘default’)

As you can see I get all 24 boundaries but only one material. How to define different material parameters for the individual parts?

Thank you for your help!


Attachment: test_2021-03-11.stl

You could do this in a postprocessing step: Iterate over the elements and decide by some criterion to change the element index. Then name the materials. Something like this should work:

for el in mesh.Elements3D():
  if mesh[el.points[0]] < 0:
    el.index = 2
mesh.SetMaterial(2, "new_mat_name")

Thank you very much for your helpful answer.

Here is my solution:

th = -700 # threshold

for el in mesh.Elements3D():
  if mesh[el.points[0]][0] < th: # I compare the x-coordinate with the threshold
    el.index = 2
mesh.SetMaterial(2, "mat2")