Coloring By Material #3356
-
Hi everyone, I have a mesh comprised of multiple volume bodies with different materials. To be able to clearly identify the materials in the structure I'm hoping to plot the mesh with the elements color coded to their respective material. Currently when using the eplot() method, I am able to control the background color, but not the elements themselves. Attached below is my current attempt at this: from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl()
mapdl.prep7()
# ~~~ Basic Material ~~~ #
mapdl.mp("CTEX", 1, 2e-06)
mapdl.mp("EX", 1, 120000)
mapdl.mp("NUXY", 1, 0.28)
mapdl.mp("CTEX", 2, 4e-06)
mapdl.mp("EX", 2, 60000)
mapdl.mp("NUXY", 2, 0.28)
# ~~~ Parameters ~~~ #
x = 1
y = 1
z = 1
mDivs = 4
# ~~~ Element Types ~~~ #
mapdl.et(1, "PLANE42")
mapdl.et(2, "SOLID185")
# ~~~ Geometry Creation ~~~ #
mapdl.blc4(0, 0, x, y)
mapdl.lsla("S")
mapdl.lesize("ALL", ndiv=mDivs)
mapdl.amesh("ALL")
mapdl.type(2)
mapdl.extopt("ESIZE", mDivs / 2, 0)
mapdl.extopt("ACLEAR", 0)
mapdl.extopt("ATTR", 0, 0, 0)
mapdl.mat(1)
mapdl.vext("ALL", dz=z / 2)
mapdl.asel("S", "LOC", "Z", z / 2)
mapdl.mat(2)
mapdl.vext("ALL", dz=z / 2)
# ~~~ Assign Colors ~~~ #
mapdl.vsel("S", "MAT", '', 1)
mapdl.eslv()
mapdl.color(lab='ELEM', clab=3)
mapdl.vsel("S", "MAT", '', 2)
mapdl.eslv()
mapdl.color(lab='ELEM', clab=6)
mapdl.allsel()
mapdl.eplot(vtk=True)` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @AidanMcConneheyOVT import pyvista as pv Then after the mapdl.allsel() command we can create an instance of the PyVista plotter and add the mesh. The mesh.grid automatically includes the material ID number for each element. So we just need to use the right field name for the scalars value. plot = pv.Plotter()
plot.add_mesh(mapdl.mesh.grid, scalars='ansys_material_type')
plot.show() The tricky part is setting your own color mapping. There are a lot of pre defined color maps. Or you can create your own list. But this will use the MAPDL PyVista Theme, which I think the color map is 'jet', so it looks more like a MAPDL result plot than a element plot with colors (cyan, red, etc). Anyway, if you want a custom color order we can work on that. |
Beta Was this translation helpful? Give feedback.
Hi @AidanMcConneheyOVT
The 'color' command is a APDL command that does not affect the eplot command when using VTK to plot. It's only affects the native MAPDL plots. To plot the elements with materials color coded we can use PyVista. Well we need to import it first, so after importing launch_mapdl you can use:
Then after the mapdl.allsel() command we can create an instance of the PyVista plotter and add the mesh. The mesh.grid automatically includes the material ID number for each element. So we just need to use the right field name for the scalars value.
The tricky part is s…