Output Mesh Data to ASCII format
-
Hello all,
I would like to output a Salome generated mesh using a TUI to an ASCII file. I have already been able to export the nodes using a snippet of code I saw somewhere here. Could someone please provide the snippet for writing the elements too please?
Thanks
JMB-
JMB wrote:
Hello all,
I would like to output a Salome generated mesh using a TUI to an ASCII file. I have already been able to export the nodes using a snippet of code I saw somewhere here. Could someone please provide the snippet for writing the elements too please?
Thanks
JMB
Hello,
I suggest you a small example how to write mesh data in ASCII file. Sure, you can rearrange data as well as to add data for polygons, pyramids etc.
Best Regards,
Grigory
import salome
salome.salome_init()
import geompy
import smesh
import SMESH
import StdMeshers
import os
import sys
dir=os.getenv("HOME"
File = dir + "/mesh_info.txt"
if os.access(File, os.F_OK):
os.remove(File)
sys.stdout = open (File, "w"
Box_1 = geompy.MakeBoxDXDYDZ(200, 300, 100)
geompy.addToStudy( Box_1, "Box_1" )
Mesh_1 = smesh.Mesh(Box_1)
Wire_discretisation = Mesh_1.Segment()
Average_length = Wire_discretisation.LocalLength(50)
Triangle_Mefisto = Mesh_1.Triangle()
Length_From_Edges_2D_Hyp_for_Triangulator = Triangle_Mefisto.LengthFromEdges()
Tetrahedron_Netgen = Mesh_1.Tetrahedron(algo=smesh.NETGEN)
Max_Element_Volume = Tetrahedron_Netgen.MaxElementVolume(10000)
isDone = Mesh_1.Compute()
if not isDone: print 'Mesh', Mesh_1.GetMesh(), ': computation failed'; error=error+1
#^Get mesh entities and write info into asci file^
error=0
M_nodes = Mesh_1.NbNodes()
if M_nodes == 0:
print "ERROR!!! The number of NODES is equal 0!!!"; error=error+1
else:
nodes = Mesh_1.GetNodesId()
print "Node, X, Y, Z"
for ind in nodes:
coord=Mesh_1.GetNodeXYZ( ind )
print ind , coord[0], coord[1], coord[2]
M_edges= Mesh_1.NbEdges()
if M_edges != 0:
edges = Mesh_1.GetElementsByType(SMESH.EDGE)
print "Edge, N1, N2"
for ind in edges:
nnode1=Mesh_1.GetElemNode( ind ,0)
nnode2=Mesh_1.GetElemNode( ind ,1)
print ind ,nnode1,nnode2
M_faces= Mesh_1.NbTriangles()+Mesh_1.NbQuadrangles()
if M_faces != 0:
fac = Mesh_1.GetElementsByType(SMESH.FACE)
print "Faces, N1, N2, N3, N4"
for ind in fac:
nb_nodes=Mesh_1.GetElemNbNodes(ind)
nnode1=Mesh_1.GetElemNode(ind,0)
nnode2=Mesh_1.GetElemNode(ind,1)
nnode3=Mesh_1.GetElemNode(ind,2)
if nb_nodes ==3:
print ind ,nnode1,nnode2,nnode3
else:
nnode4=Mesh_1.GetElemNode( ind ,3)
print ind ,nnode1,nnode2,nnode3,nnode4
M_volum = Mesh_1.NbTetras()+ Mesh_1.NbPrisms()
if M_volum != 0:
vol = Mesh_1.GetElementsByType(SMESH.VOLUME)
print "Volumes, N1, N2, N3, N4, N5, N6, N7, N8"
for ind in vol:
nb_nodes=Mesh_1.GetElemNbNodes(ind)
nnode1=Mesh_1.GetElemNode(ind,0)
nnode2=Mesh_1.GetElemNode(ind,1)
nnode3=Mesh_1.GetElemNode(ind,2)
nnode4=Mesh_1.GetElemNode( ind ,3)
if nb_nodes ==4:
print ind ,nnode1,nnode2,nnode3,nnode4
else:
nnode5 =Mesh_1.GetElemNode( ind ,4)
nnode6=Mesh_1.GetElemNode( ind ,5)
nnode7=Mesh_1.GetElemNode( ind ,6)
nnode8=Mesh_1.GetElemNode( ind ,7)
print ind,nnode1,nnode2,nnode3,nnode4,nnode5,nnode6,nnode7,nnode8-
Grigory Zhivotovsky wrote:
JMB wrote:
Hello all,
I would like to output a Salome generated mesh using a TUI to an ASCII file. I have already been able to export the nodes using a snippet of code I saw somewhere here. Could someone please provide the snippet for writing the elements too please?
Thanks
JMB
Hello,
I suggest you a small example how to write mesh data in ASCII file. Sure, you can rearrange data as well as to add data for polygons, pyramids etc.
Best Regards,
Grigory
Grigory,
Thank you very much for your help. I will try it!
JMB -
Grigory Zhivotovsky wrote:
JMB wrote:
Hello all,
I would like to output a Salome generated mesh using a TUI to an ASCII file. I have already been able to export the nodes using a snippet of code I saw somewhere here. Could someone please provide the snippet for writing the elements too please?
Thanks
JMB
Hello,
I suggest you a small example how to write mesh data in ASCII file. Sure, you can rearrange data as well as to add data for polygons, pyramids etc.
Best Regards,
Grigory
Hello Grigory,
Thank you for your assistance and the script that you emailed. I tried your script, but it would not work. I got an error:
>>> execfile("/home/.../write_mesh.py"
Traceback (most recent call last):
File "", line 1, in ?
File "/home/.../write_mesh.py", line 27, in ?
M_nodes = Mesh_1.NbNodes()
AttributeError: Mesh instance has no attribute 'NbNodes'
>>>
I found the error and fixed it. The script needs something like :
mesh=Mesh_1.GetMesh()
and all references to Mesh_1.* changed now to mesh.* (such as "nodes = mesh.GetNodesId()" ). Then your script works fine. Does this seem logical to you?
Next I would like to use this script after I have created a geometric model of my own manually; meshed it like I want (manually); added groups, etc. ( I want to be able to write the groups as well. So later, I would like some help with that too please ). However when I tried it I get an error:
>>> execfile("/home/../write_mesh-1.py"
Traceback (most recent call last):
File "", line 1, in ?
File "/home/.../write_mesh-1.py", line 18, in ?
mesh=Mesh_1.GetMesh()
NameError: name 'Mesh_1' is not defined
The script "write_mesh-1.py" is a slightly modified version with all the geometry creation & meshing lines removed as shown below. What do I need to do to make "write_mesh-1.py" a generic type of script that can be used after ANY model has been meshed?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import salome
salome.salome_init()
import geompy
import smesh
import SMESH
import StdMeshers
import os
import sys
dir=os.getenv("HOME"
File = dir + "/mesh_info.txt"
if os.access(File, os.F_OK):
os.remove(File)
sys.stdout = open (File, "w"
# Get mesh entities and write info into ascii file
error=0
mesh=Mesh_1.GetMesh()
M_nodes = mesh.NbNodes()
if M_nodes == 0:
print "ERROR!!! The number of NODES is equal 0!!!"; error=error+1
else:
nodes = mesh.GetNodesId()
print "Node, X, Y, Z"
for ind in nodes:
coord=mesh.GetNodeXYZ(ind)
print ind, coord[0], coord[1], coord[2]
M_edges= mesh.NbEdges()
if M_edges != 0:
edges = mesh.GetElementsByType(SMESH.EDGE)
print "Edge, N1, N2"
for ind in edges:
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
print ind,nnode1,nnode2
M_faces= mesh.NbTriangles()+mesh.NbQuadrangles()
if M_faces != 0:
fac = mesh.GetElementsByType(SMESH.FACE)
print "Faces, N1, N2, N3, N4"
for ind in fac:
nb_nodes=mesh.GetElemNbNodes(ind)
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
nnode3=mesh.GetElemNode(ind,2)
if nb_nodes ==3:
print ind,nnode1,nnode2,nnode3
else:
nnode4=mesh.GetElemNode(ind,3)
print ind,nnode1,nnode2,nnode3,nnode4
M_volum = mesh.NbTetras()+ mesh.NbPrisms()
if M_volum != 0:
vol = mesh.GetElementsByType(SMESH.VOLUME)
print "Volumes, N1, N2, N3, N4, N5, N6, N7, N8"
for ind in vol:
nb_nodes=mesh.GetElemNbNodes(ind)
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
nnode3=mesh.GetElemNode(ind,2)
nnode4=mesh.GetElemNode(ind,3)
if nb_nodes ==4:
print ind,nnode1,nnode2,nnode3,nnode4
else:
nnode5=mesh.GetElemNode(ind,4)
nnode6=mesh.GetElemNode(ind,5)
nnode7=mesh.GetElemNode(ind,6)
nnode8=mesh.GetElemNode(ind,7)
print ind,nnode1,nnode2,nnode3,nnode4,nnode5,nnode6,nnode7,nnode8
-
JMB wrote:
Grigory Zhivotovsky wrote:
Hello Grigory,
Hello,
I suggest you a small example how to write mesh data in ASCII file. Sure, you can rearrange data as well as to add data for polygons, pyramids etc.
Best Regards,
Grigory
Next I would like to use this script after I have created a geometric model of my own manually; meshed it like I want (manually); added groups, etc. ( I want to be able to write the groups as well. So later, I would like some help with that too please ). However when I tried it I get an error:
>>> execfile("/home/../write_mesh-1.py"
Traceback (most recent call last):
File "", line 1, in ?
File "/home/.../write_mesh-1.py", line 18, in ?
mesh=Mesh_1.GetMesh()
NameError: name 'Mesh_1' is not defined
The script "write_mesh-1.py" is a slightly modified version with all the geometry creation & meshing lines removed as shown below. What do I need to do to make "write_mesh-1.py" a generic type of script that can be used after ANY model has been meshed?
Hello Grigory (or anybody),
Could you assist me with this? See my previous message please...
Regards
JMB-
Hello Grigory (or anybody),
Could you assist me with this? See my previous message please...
Regards
JMB
Hello,
It is already a generic type of script – simply create def like this and add remaining data for groups, polygons, pyramids etc.
Regards,
Grigory
import salome
salome.salome_init()
import geompy
import smesh
import SMESH
import StdMeshers
import os
import sys
def asci_write(mesh):
#^Get mesh entities and write info into asci file^
dir=os.getenv("HOME"
File = dir + "/mesh_info.txt"
if os.access(File, os.F_OK):
os.remove(File)
sys.stdout = open (File, "w"
M_nodes = mesh.NbNodes()
if M_nodes == 0:
raise RuntimeError, "ERROR!!! The number of NODES is equal 0!!!"
else:
nodes = mesh.GetNodesId()
print "Node, X, Y, Z"
for ind in nodes:
coord=mesh.GetNodeXYZ(ind)
print ind, coord[0], coord[1], coord[2]
M_edges= mesh.NbEdges()
if M_edges != 0:
edges = mesh.GetElementsByType(SMESH.EDGE)
print "Edge, N1, N2"
for ind in edges:
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
print ind,nnode1,nnode2
M_faces= mesh.NbTriangles()+mesh.NbQuadrangles()
if M_faces != 0:
fac = mesh.GetElementsByType(SMESH.FACE)
print "Faces, N1, N2, N3, N4"
for ind in fac:
nb_nodes=mesh.GetElemNbNodes(ind)
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
nnode3=mesh.GetElemNode(ind,2)
if nb_nodes ==3:
print ind,nnode1,nnode2,nnode3
else:
nnode4=mesh.GetElemNode(ind,3)
print ind,nnode1,nnode2,nnode3,nnode4
M_volum = mesh.NbTetras()+ mesh.NbPrisms()
if M_volum != 0:
vol = mesh.GetElementsByType(SMESH.VOLUME)
print "Volumes, N1, N2, N3, N4, N5, N6, N7, N8"
for ind in vol:
nb_nodes=mesh.GetElemNbNodes(ind)
nnode1=mesh.GetElemNode(ind,0)
nnode2=mesh.GetElemNode(ind,1)
nnode3=mesh.GetElemNode(ind,2)
nnode4=mesh.GetElemNode(ind,3)
if nb_nodes ==4:
print ind,nnode1,nnode2,nnode3,nnode4
else:
nnode5 =mesh.GetElemNode(ind,4)
nnode6=mesh.GetElemNode(ind,5)
nnode7=mesh.GetElemNode(ind,6)
nnode8=mesh.GetElemNode(ind,7)
print ind,nnode1,nnode2,nnode3,nnode4,nnode5,nnode6,nnode7,nnode8
sys.stdout.close()
returnBox_1 = geompy.MakeBoxDXDYDZ(200, 300, 100)
geompy.addToStudy( Box_1, "Box_1" )
Mesh_1 = smesh.Mesh(Box_1)
Wire_discretisation = Mesh_1.Segment()
Average_length = Wire_discretisation.LocalLength(50)
Triangle_Mefisto = Mesh_1.Triangle()
Length_From_Edges_2D_Hyp_for_Triangulator = Triangle_Mefisto.LengthFromEdges()
Tetrahedron_Netgen = Mesh_1.Tetrahedron(algo=smesh.NETGEN)
Max_Element_Volume = Tetrahedron_Netgen.MaxElementVolume(10000)
isDone = Mesh_1.Compute()
if not isDone:
raise RuntimeError, "ERROR!!! Mesh computation failed"
mesh=Mesh_1.GetMesh()
asci_write(mesh)-
Grigory Zhivotovsky wrote:
Hello,
It is already a generic type of script – simply create def like this and add remaining data for groups, polygons, pyramids etc.
Regards,
Grigory
Hello Grigory,
Thank you for your reply. I was not clear in my previous message about what I want the "write_mesh.py" script to do. Let me try to explain better, maybe in a different way.
Suppose I have imported a model using STEP import (for example: the "Piston" model from the CAELinux website) and meshed it, defined groups, etc. using Salome's GUI methods. Then I want to call a script that will take this piston mesh and groups and export it to an ascii file. That is the script I am seeking help for.
In other words, it is similar to the "Export to UNV file" available from Salome, except I would be calling it using "File" -> "Load script". Does this give you a clearer picture of what I am seeking? Essentially I am trying to come up with a general translator script for exporting any preexisting mesh (without "write_mesh.py" script actually building the geometry and the mesh).
When I try my modified version of "write_mesh.py" script in this manner, it complains that it does not know what "Mesh_1" is. My problem is how to pass info about "Mesh_1" to this script that is being run using "File" -> "Load script" since it has not created the mesh (grid) to begin with. I hope I have made my question clearer now.
Will you help please? Thank you.
JMB-
Hello,
I suggest you to act in the following way:
1. create arbitrary mesh that you want in GUI
2. export mesh in med or unv file
3. use "write_mesh.py" script by load script or execfile(“…\write_mesh.py”
in console
As a result you will have your arbitrary mesh in asccii file.
The only thing you have to do is to define your mesh something like this
([CUBE_EN_HEXA8], status) = smesh.smesh.CreateMeshesFromMED('…/SALOME/series3x/SAMPLES/SAMPLES_SRC/MedFiles/cube_hexa8.med')
asci_write(CUBE_EN_HEXA8)
To have valid result for this file I also change
M_volum = mesh.NbTetras()+ mesh.NbPrisms
by
M_volum = mesh.NbTetras()+ mesh.NbPrisms()+mesh.NbHexas()+mesh.NbPyramids()
Regards,Grigory
-
Grigory Zhivotovsky wrote:
Hello,
I suggest you to act in the following way:1. create arbitrary mesh that you want in GUI
2. export mesh in med or unv file
3. use "write_mesh.py" script by load script or execfile(“…\write_mesh.py”
in console
As a result you will have your arbitrary mesh in asccii file.
To have valid result for this file I also changeM_volum = mesh.NbTetras()+ mesh.NbPrisms
by
M_volum = mesh.NbTetras()+ mesh.NbPrisms()+mesh.NbHexas()+mesh.NbPyramids()
Regards,
Grigory
Hello Grigory,
Thank you for your help! I works now! I have two questions:
1. I am just curious why the export step is necessary? If it is too hard to explain, then don't.
2. How do I extend the script to write the groups as well?
Regards
JMB-
JMB wrote:
Grigory Zhivotovsky wrote:
Hello,
I suggest you to act in the following way:1. create arbitrary mesh that you want in GUI
2. export mesh in med or unv file
3. use "write_mesh.py" script by load script or execfile(“…\write_mesh.py”
in console
As a result you will have your arbitrary mesh in asccii file.
To have valid result for this file I also changeM_volum = mesh.NbTetras()+ mesh.NbPrisms
by
M_volum = mesh.NbTetras()+ mesh.NbPrisms()+mesh.NbHexas()+mesh.NbPyramids()
Regards,
Grigory
Hello Grigory,
Thank you for your help! It works now! I have two questions:
1. I am just curious why the export step is necessary? If it is too hard to explain, then don't.
2. How do I extend the script to write the groups as well?
Regards
JMB
Hello Grigory (or anybody),
Can somebody drop a few hints, suggestions or snippets of code please?
Thanks
JMB-
Hello,
Unfortunately I’ m currently busy with other urgent task.
But I try to answer you in short.
1. I am just curious why the export step is necessary? If it is too hard to explain, then don't.You have to define the mesh you want to write.
The other way to perform your task is as follows. You can create 1 script where you create mesh (using dump python) and then call "write_mesh.py" in this script without export.
Select mesh in GUI and create GUI command is more difficult task requiring more efforts.
2. How do I extend the script to write the groups as well?
In smesh.py take
## Get the list of groups existing in the mesh
def GetGroups(self):
return self.mesh.GetGroups()
## Get the list of names of groups existing in the meshdef GetGroupNames(self): ,
In SMESH_Group.idl you will find all other methods necessary to write group info.
Regards,
Grigory
-
-
-
-
-
-
-
-
-
JMB wrote:
Hello all,
I would like to output a Salome generated mesh using a TUI to an ASCII file. I have already been able to export the nodes using a snippet of code I saw somewhere here. Could someone please provide the snippet for writing the elements too please?
Thanks
JMB
Hello,
I forgot to mention I am running Salome 3.2.2. and using File -> Load Script to run the python script.
Thanks
JMB
